-
-
Notifications
You must be signed in to change notification settings - Fork 52
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
Correct handling of StgDict/StgInfo for Structure return types #446
Conversation
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.
Thank you for taking care of this!
src/rubicon/objc/ctypes_patch.py
Outdated
|
||
# The StgDictObject structure from "Modules/_ctypes/ctypes.h". This structure is | ||
# not officially stable across Python versions, but it didn't change between being | ||
# introduced in 2009, and being replaced in 2004/Python 3.13.0a6. |
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.
# introduced in 2009, and being replaced in 2004/Python 3.13.0a6. | |
# introduced in 2009, and being replaced in 2024/Python 3.13.0a6. |
src/rubicon/objc/ctypes_patch.py
Outdated
raise TypeError( | ||
f"Expected a type object, not {type(tp).__module__}.{type(tp).__qualname__}" | ||
) | ||
return StgInfo.from_address(id(tp) + py_type_type.tp_basicsize) |
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.
Can we use the proper PyObject_GetTypeData
function for this? It's declared in object.h and is part of the public C API, so unlike the ctypes-internal functions, we don't need to reimplement this one ourselves.
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.
After obtaining the StgInfo
, we should also check that its initialized
field is non-zero, like the real code in CPython does.
I'm not familiar with this area, so I'll leave the reviewing to others. |
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.
Thanks for the fix, and the very well documented code! Pending the requested changes by @dgelessus, this looks good to me.
@dgelessus Are you OK with this updated version? |
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.
LGTM as well. Thanks!
Fixes #444.
python/cpython#114314 replaced the StgDict structure with StgInfo; this has been released in Python 3.13.0a6.
The real fix for this is to address this upstream, rather than requiring us to work around ctypes; but:
Although the patch looks like a lot of code, most of the delta is actually a re-org so that the older code will be auto removed when Py3.13 is the oldest supported version.
As a test, I've also run the full Toga testbed on macOS with this code and it worked as expected.
PR Checklist: