question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

BUG: np.dtype(ctypes.Structure) does not respect _pack_ field

See original GitHub issue
class Foo(ctypes.Structure):
    _fields_ = [('one', ctypes.c_uint8), ('two', ctypes.c_uint32)]
    _pack_ = 2
>>> ctypes.sizeof(Foo())  # packed to alignment 2
6
>>> np.dtype(Foo).itemsize  # default alignment incorrectly used
8

Fix should be fairly straightforward by changing:

https://github.com/numpy/numpy/blob/44810f07ec6a281d96d6a69c105ce6ae59aec718/numpy/core/_dtype_ctypes.py#L35-L46

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:10 (10 by maintainers)

github_iconTop GitHub Comments

1reaction
mattipcommented, Aug 13, 2018

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 dtypes.baseclass that returns NotImplemented where needed instead?

0reactions
eric-wiesercommented, Nov 10, 2018

@tylerjereddy:

Incidentally, I’ve been thinking a lot about using ctypes to define custom dtypes lately,

A neat but rather pointless trick

@np.dtype
class Foo(ctypes.Structure):
    _fields_ = [
        ('x', ctypes.c_uint32),
        ('y', ctypes.c_uint32)
    ]
Read more comments on GitHub >

github_iconTop Results From Across the Web

Numpy error when converting array of ctypes types to void ...
At 1 st glance, it seems it's a ctypes bug as it doesn't respect the standard, but I wouldn't rush into claiming this...
Read more >
NumPy 1.17.0 Release Notes
Shape-1 fields in dtypes won't be collapsed to scalars in a future version#. Currently, a field specified as [(name, dtype, 1)] or "1type" ......
Read more >
schrodinger.application.matsci.elasticity.stress module ...
Starting in NumPy 1.9, astype method now returns an error if the string dtype to cast to is not long enough in 'safe'...
Read more >
Release Notes — NumPy v1.12 Manual - GitHub Pages
np.full now returns an array of the fill-value's dtype if no dtype is given, ... #7156: BUG: Reascertain that linspace respects ndarray subclasses...
Read more >
Issue 41883: ctypes pointee goes out of scope, then pointer in ...
msg377652 ‑ (view) Author: Ian M. Hoffman (NankerPhelge) Date: 2020‑09‑29 03:40 msg377657 ‑ (view) Author: Eryk Sun (eryksun) * Date: 2020‑09‑29 05:46 msg377674 ‑ (view)...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found