pyo3 icon indicating copy to clipboard operation
pyo3 copied to clipboard

Implement PEP 489 - Multi-phase extension module initialization

Open messense opened this issue 3 years ago • 6 comments
trafficstars

https://peps.python.org/pep-0489/

This is very much WIP, opening for discussion, previously: https://github.com/PyO3/pyo3/discussions/1982

messense avatar Mar 23 '22 09:03 messense

Looks like PyPy doesn't support PEP 489.

  = note: /usr/bin/ld: /home/runner/work/pyo3/pyo3/target/x86_64-unknown-linux-gnu/debug/deps/libpyo3-5c3820f48b335551.rlib(pyo3-5c3820f48b335551.pyo3.839e673f-cgu.2.rcgu.o): in function `pyo3_ffi::modsupport::PyModule_FromDefAndSpec':
          /home/runner/work/pyo3/pyo3/pyo3-ffi/src/modsupport.rs:146: undefined reference to `PyModule_FromDefAndSpec2'
          /usr/bin/ld: /home/runner/work/pyo3/pyo3/target/x86_64-unknown-linux-gnu/debug/deps/libpyo3-5c3820f48b335551.rlib(pyo3-5c3820f48b335551.pyo3.839e673f-cgu.5.rcgu.o): in function `pyo3::impl_::pymodule::ModuleDef::make_module':
          /home/runner/work/pyo3/pyo3/src/impl_/pymodule.rs:80: undefined reference to `PyModule_ExecDef'

messense avatar Mar 25 '22 09:03 messense

:(

I guess we have to make a choice between making a new API that's not supported on PyPy, or just using different behaviour on CPython and PyPy. I think a separate API is probably better?

davidhewitt avatar Mar 25 '22 09:03 davidhewitt

Maybe we should feature-gate it? See also https://github.com/cython/cython/issues/2343:

Require users to explicitly enable this feature with a compiler directive, as Cython cannot safely determine that a module supports this (there might be global state in external C libraries or C function callbacks), and there will be a visible performance impact.

messense avatar Mar 25 '22 10:03 messense

It would be interesting to explore. There's also still a possibility to have a #[pymodule(pep489)] or similar.

That Cython thread is really interesting in general.

davidhewitt avatar Mar 25 '22 10:03 davidhewitt

I guess we have to make a choice between making a new API that's not supported on PyPy, or just using different behaviour on CPython and PyPy. I think a separate API is probably better?

But isn't "same interface but slightly incompatible behaviour" (w.r.t. CPython) already something that PyPy users expect? I fear that having CPython-only API will only end up with extensions having feature-gated PyPy-compatible code paths any way, so I would prefer it if the thankless task of trying to paper this over was centralized in PyO3 (were we could also move the ecosystem over if/when PyPy adds support for this.)

adamreichold avatar Mar 25 '22 10:03 adamreichold

I think it might depend how different the PyPy behaviour is. I do agree if we can solve the problem for users transparently, that would be nicest.

davidhewitt avatar Mar 25 '22 10:03 davidhewitt