Skip to content

Avoid using PyDictObject when compiling for RustPython#6057

Merged
ngoldbaum merged 3 commits into
PyO3:mainfrom
bschoenmaeckers:dict_object_rustpython
Jun 1, 2026
Merged

Avoid using PyDictObject when compiling for RustPython#6057
ngoldbaum merged 3 commits into
PyO3:mainfrom
bschoenmaeckers:dict_object_rustpython

Conversation

@bschoenmaeckers
Copy link
Copy Markdown
Member

@bschoenmaeckers bschoenmaeckers commented May 18, 2026

fixes #6047

Tested using:

~/repo/pyo3 ❱ RUSTFLAGS="--cfg RustPython" cargo clippy --features abi3-py314
   Compiling pyo3-ffi v0.28.3 (/Users/basschoenmaeckers/repo/pyo3/pyo3-ffi)
   Compiling pyo3 v0.28.3 (/Users/basschoenmaeckers/repo/pyo3)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 1.84s

@bschoenmaeckers bschoenmaeckers added the CI-skip-changelog Skip checking changelog entry label May 18, 2026
Copy link
Copy Markdown
Member

@davidhewitt davidhewitt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the slow review; I'm not sure this is enough:

  • See e.g. dict_len function which assumes the layout of PyDictObject; as long as the cast suceeds then this function is potentially wrong. Perhaps we should make PyDictObject opaque (or not exposed at all) in pyo3-ffi under RustPython?
  • I haven't checked extensively, but I have similar concerns for other types. e.g. PyTupleMethods::as_slice.

@bschoenmaeckers bschoenmaeckers force-pushed the dict_object_rustpython branch from 26f0cab to fef3a8c Compare May 26, 2026 10:14
@bschoenmaeckers
Copy link
Copy Markdown
Member Author

Sorry for the slow review; I'm not sure this is enough:

  • See e.g. dict_len function which assumes the layout of PyDictObject; as long as the cast suceeds then this function is potentially wrong. Perhaps we should make PyDictObject opaque (or not exposed at all) in pyo3-ffi under RustPython?
  • I haven't checked extensively, but I have similar concerns for other types. e.g. PyTupleMethods::as_slice.

RustPython will never support compiling without the abi3 feature enabled. So PyDictObject will never be available. I can look into enforcing the Py_LIMITED_API cfg when compiling for RustPython.

@bschoenmaeckers
Copy link
Copy Markdown
Member Author

In 55c2ea9, I force enable the Py_LIMITED_API flag so RustPython will never get compiled with incompatible api's.

@bschoenmaeckers bschoenmaeckers force-pushed the dict_object_rustpython branch from 55c2ea9 to adb884c Compare June 1, 2026 11:41
// If Py_GIL_DISABLED is set, do not build with limited API support.
if (self.abi3 && !self.is_free_threaded())
|| self.implementation == PythonImplementation::RustPython
{
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, this looks good. cc @ngoldbaum for interaction with #5786, I guess we won't need the is_free_threaded exclusion after that? Which PR should we merge first?

Comment thread pyo3-ffi/src/lib.rs
Comment on lines -603 to +606
#[cfg(not(Py_LIMITED_API))]
#[cfg(not(any(Py_LIMITED_API, RustPython)))]
mod cpython;

#[cfg(not(Py_LIMITED_API))]
#[cfg(not(any(Py_LIMITED_API, RustPython)))]
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are the changes here still needed given the change to pyo3-build-config?

Copy link
Copy Markdown
Member Author

@bschoenmaeckers bschoenmaeckers Jun 1, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not really but maybe a nice sanity check

Copy link
Copy Markdown
Contributor

@ngoldbaum ngoldbaum left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's merge this and I'll go ahead and update #5786. It's already huge and this is pretty small. That said, I think #5786 is pretty much ready.

@ngoldbaum ngoldbaum added this pull request to the merge queue Jun 1, 2026
Merged via the queue into PyO3:main with commit 30c8b24 Jun 1, 2026
46 of 47 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CI-skip-changelog Skip checking changelog entry

Projects

None yet

Development

Successfully merging this pull request may close these issues.

RustPython type imports via mutable modules can lead to type confusion

3 participants