-
-
Notifications
You must be signed in to change notification settings - Fork 10.9k
BUG: np.dtype(ctypes.Structure) does not respect _pack_ field #10532
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
Comments
Incidentally, I've been thinking a lot about using |
I don't fully understand what you're suggesting - but right now using ctypes as an exchange format is a pretty poor path for anything, because the objects are hard to inspect:
|
I basically mean doing what you just did above, but extending it to comprehensively define a useful custom dtype that inherits from |
To be clear - by custom dtype, do you mean structured dtype, or are you envisaging applying it to things like fixed-pointer numbers / auto-differentiating scalars / etc? |
More the auto-differentiating scalars and other custom dtypes of that nature, like those supporting units or missing integer support. So: class AmazingDtype(ctypes.Structure):
# this is an amazing third-party NumPy dtype
# that implements automatic forward differentiation
# without touching a single C file by using the
# Python standard library ctypes.Structure to fill
# in all the requisite structure members of the type class
# needed to do useful things as a NumPy dtype
pass |
I would not like to encourage deeper use of ctypes. The code behind them has many open issues. The package defines yet-another-dsl for describing c-like interfaces. It has so many layers it will always be slow and buggy. Why not directly inherit from a new |
I'm with @mattip here. We don't want to built on top of ctypes - only provide compatibility with it. |
As of #12254, this should be fairly straightforward to fix. |
I submitted a pull request with a fix for this bug. I am new to git and I am struggling a bit to understand how I am supposed to do this exactly. This is the request: I assume a code review would be next? To fix the issue I saw in the dtypes documentation how to use a dictionary to provide the offset values. In my fix in case the class has a pack attribute I will provide the offset values from it, otherwise use the previously existing code. |
A neat but rather pointless trick @np.dtype
class Foo(ctypes.Structure):
_fields_ = [
('x', ctypes.c_uint32),
('y', ctypes.c_uint32)
] |
See numpy#10532 Made several changes to offset and size calulation. First pull request was far from correct.
See numpy#10532 Cosmetic changes + added an additional test with a more complex structure.
Fix should be fairly straightforward by changing:
numpy/numpy/core/_dtype_ctypes.py
Lines 35 to 46 in 44810f0
The text was updated successfully, but these errors were encountered: