-
Notifications
You must be signed in to change notification settings - Fork 1.4k
ctypes buffer #6311
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ctypes buffer #6311
Changes from 1 commit
792c19f
8556955
c232c33
190c434
8ebb888
fbf2343
2cb2f89
bea4eec
36b6d8f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -300,6 +300,11 @@ impl Constructor for PyCStructure { | |||||||||
|
|
||||||||||
| #[pyclass(flags(BASETYPE, IMMUTABLETYPE), with(Constructor))] | ||||||||||
| impl PyCStructure { | ||||||||||
|
Comment on lines
334
to
335
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
The -#[pyclass(flags(BASETYPE, IMMUTABLETYPE), with(Constructor))]
+#[pyclass(flags(BASETYPE, IMMUTABLETYPE), with(Constructor, AsBuffer))]
impl PyCStructure {Without this, the buffer protocol may not be properly exposed to Python. 📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||
| #[pygetset] | ||||||||||
| fn _objects(&self) -> Option<PyObjectRef> { | ||||||||||
| self.cdata.read().objects.clone() | ||||||||||
| } | ||||||||||
|
|
||||||||||
| #[pygetset] | ||||||||||
| fn _fields_(&self, vm: &VirtualMachine) -> PyObjectRef { | ||||||||||
| // Return the _fields_ from the class, not instance | ||||||||||
|
|
||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
🌐 Web query:
CPython ctypes cast function behavior memory address pointer💡 Result:
Short summary of CPython ctypes.cast behavior:
Concise example:
Sources: Python ctypes docs and practical notes. [1] Python ctypes Pointers (docs). [2] Python ctypes Utility functions (addressof, byref, cast). [3] Discussion/examples on using cast with bytes/addresses.
🏁 Script executed:
Repository: RustPython/RustPython
Length of output: 186
🏁 Script executed:
Repository: RustPython/RustPython
Length of output: 186
🏁 Script executed:
Repository: RustPython/RustPython
Length of output: 186
🌐 Web query:
RustPython ctypes cast pointer contents behavior💡 Result:
Short summary of ctypes cast / pointer / contents behavior you’ll see in CPython (and what RustPython aims to match):
If you need guidance for a specific RustPython mismatch or a reproducer (example code showing different behavior vs CPython), share the snippet and I’ll look up/compare exact behavior and point to whether it’s a RustPython bug or a libc/ctypes semantics issue.
Sources:
[1] Python ctypes docs (pointers, cast, contents).
[2] Python tracker discussion on simple-types autocast (issue discussion / workaround).
[3] StackOverflow thread: ctypes.cast memory/leak observations.
🌐 Web query:
RustPython ctypes implementation pointer contents💡 Result:
Short answer: RustPython's ctypes support is incomplete — pointer.contents semantics from CPython (where .contents returns a new ctypes instance referencing the pointed memory) are not fully implemented/guaranteed in RustPython yet. See the RustPython repo/issues noting "Implement the ctypes module" and ongoing work; for CPython behavior refer to the official ctypes docs (pointer() / POINTER() / .contents). [1][2]
References
Based on my verification through web search, I can now provide a rewritten review comment. The investigation confirms that CPython's
cast()andpointer.contentsbehavior differs from the current RustPython implementation, though RustPython's ctypes module is still incomplete.Behavior differs from CPython:
cast()should return a pointer viewing the same memory address, andcontentsshould construct a fresh wrapper on each access.Per CPython semantics,
cast(obj, POINTER(T))returns a pointer that views the same underlying memory asobj(not a copy), and accessingpointer.contentsconstructs a fresh ctypes wrapper each time—not the original object. The current implementation stores the source object reference directly incontents, which is semantically different from CPython's behavior.While RustPython's ctypes module is incomplete and this approach may prevent dangling pointers (keeping the source object alive via the stored reference), it diverges from CPython's expected semantics. If RustPython intends to match CPython's ctypes behavior,
contentsshould dynamically construct a fresh wrapper rather than return a stored reference.🤖 Prompt for AI Agents