[Stdlib] Use PySequence_Contains for Python membership#6609
[Stdlib] Use PySequence_Contains for Python membership#6609puneetdixit200 wants to merge 5 commits into
Conversation
BEGIN_PUBLIC [Stdlib] Use PySequence_Contains for Python membership Bind CPython PySequence_Contains and use it for PythonObject membership checks so list, dict, custom __contains__, and iterator fallback behavior stays delegated to CPython. Add Python interop coverage for the raw binding, __contains__, iterator fallback, and non-iterable error propagation. END_PUBLIC Assisted-by: AI Signed-off-by: Puneet Dixit <[email protected]>
There was a problem hiding this comment.
Pull request overview
Note
Copilot was unable to run its full agentic suite in this review.
This PR updates Mojo’s Python interop membership testing (in) to use CPython’s PySequence_Contains, and expands test coverage to validate correct behavior across __contains__ and __iter__ paths.
Changes:
- Implement
PythonObject.__contains__viaPySequence_Containswith proper error propagation. - Add a CPython binding/wrapper for
PySequence_Contains. - Extend tests for membership semantics and adjust an overly-specific error substring assertion.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| mojo/stdlib/test/python/test_python_object.mojo | Adds new membership tests for __contains__ and __iter__; loosens brittle error message match. |
| mojo/stdlib/test/python/test_python_cpython.mojo | Adds assertions covering PySequence_Contains results before/after list mutation. |
| mojo/stdlib/std/python/python_object.mojo | Replaces manual membership emulation with PySequence_Contains and raises on -1. |
| mojo/stdlib/std/python/_cpython.mojo | Introduces dynamic binding + CPython wrapper method for PySequence_Contains. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Add coverage that a Python __contains__ exception propagates through Mojo membership. Assisted-by: AI Signed-off-by: Puneet Dixit <[email protected]>
Tighten the non-iterable membership assertion, broaden the test name, and keep the embedded Python source runtime-only. Assisted-by: AI Signed-off-by: Puneet Dixit <[email protected]>
|
I have read the CLA Document and I hereby sign the CLA You can retrigger this bot by commenting recheck in this Pull Request. Posted by the CLA Assistant Lite bot. |
|
Pushed Changes:
|
Apply the formatter's single-line style for the PySequence_Contains loader. Assisted-by: AI Signed-off-by: Puneet Dixit <[email protected]>
|
Follow-up after CI lint: pushed Current status: review threads are resolved, PR remains approved, |
Match the stable prefix of the non-container membership error so the test covers both current Python 3.14 wording and older Python wording. Assisted-by: AI Signed-off-by: Puneet Dixit <[email protected]>
|
Pushed Verification:
I could not run the Mojo/Bazel test locally because this environment has no |
Hi @puneetdixit200 - you'll need to sign our CLA before I can accept this contribution, please see the above. |
Linked issue
Fixes #3806
Type of change
Motivation
PythonObject.__contains__currently implements membership in Mojo by checking__contains__and otherwise iterating. The linked issue and maintainer guidance call for delegating membership to CPython throughPySequence_Contains, which uses CPython sequence slots and fallback behavior directly.What changed
PySequence_ContainsCPython binding, loader field, and wrapper.PythonObject.__contains__with a singlePySequence_Containscall and Python error propagation on-1.__contains__implementation, iterator fallback, and non-iterable error propagation.Testing
git diff --check HEAD~1..HEAD./bazelw test //mojo/stdlib/test/python:test_python_object.mojo.test //mojo/stdlib/test/python:test_python_cpython.mojo.testcould not run on this machine because Bazel exits before target analysis:error: a fully Xcode install must be selected globally, run sudo xcode-select -s /path/to/Xcode.app. This machine only has/Library/Developer/CommandLineToolsselected and no/Applications/Xcode*.app../bazelw run formatis blocked by the same Xcode preflight error.Checklist
./bazelw run formatto format my changesAssisted-by:trailer in my commit message or this PR descriptionAssisted-by: AI