pyo3 icon indicating copy to clipboard operation
pyo3 copied to clipboard

Add error messages when compiling for ABI3 with unsupported macro features

Open davidhewitt opened this issue 11 months ago • 5 comments

Building from https://github.com/PyO3/pyo3/pull/3835#issuecomment-1986917915 and https://pyo3.rs/v0.20.3/building_and_distribution#missing-features

We have some #[pyclass] options which do not work on older ABI3 versions:

  • #[pyo3(text_signature = "...")] does not work on classes until Python 3.10 or greater.
  • #[pyclass(dict)] and #[pyclass(weakref)] options on classes are not supported until Python 3.9 or greater.

At the moment I think these conditions fail silently (they compile but the runtime behaviour is missing). The proc macros should instead detect these cases and emit a helpful warning.

davidhewitt avatar Mar 10 '24 14:03 davidhewitt

Hi! I can do this! But I'll need some guidance! Where should I start?

cojmeister avatar Mar 21 '24 17:03 cojmeister

Thanks for volunteering! To focus on #[pyclass(weakref)] as an initial example, try looking in pyo3-macros-backend/src/pyclass.rs for the part of the proc-macro code which handles the weakref option. pyo3-macros-backend/src/utils.rs has a method for checking if it's an ABI3 build, so you can use that method (and other information from pyo3_build_config) to work out if weakref has been set for an unsupported build.

Once that's figured out, look for e.g. the bail_spanned! / ensure_spanned! macros which is how most of PyO3's macro code emits errors.

davidhewitt avatar Mar 22 '24 23:03 davidhewitt

Once the error has been implemented, it would be worth adding a test for this as part of test_compile_error.

davidhewitt avatar Mar 22 '24 23:03 davidhewitt

Hey @davidhewitt, I've opened PR #3993 Can you let me know if I'm going on the right direction? Thanks!

cojmeister avatar Mar 25 '24 17:03 cojmeister

I created a follow up PR #4194 that superceded #3993. This doesn't include any error message for text_signature which may no longer be necessary or can be a separate PR.

codeguru42 avatar May 21 '24 15:05 codeguru42

@davidhewitt I think this is closed now, right?

codeguru42 avatar Jun 16 '24 17:06 codeguru42

Yes, many thanks again!

davidhewitt avatar Jun 16 '24 22:06 davidhewitt