Skip to content

ctypeslib.as_array aborts #6741

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

Closed
PolarNick239 opened this issue Nov 27, 2015 · 7 comments · Fixed by #10882
Closed

ctypeslib.as_array aborts #6741

PolarNick239 opened this issue Nov 27, 2015 · 7 comments · Fixed by #10882

Comments

@PolarNick239
Copy link

This script fails:

import numpy as np
import ctypes

sh = (1, 1)
tmp = np.zeros(sh, np.uint8)
test = ctypes.cast(ctypes.c_void_p(tmp.__array_interface__['data'][0]), ctypes.POINTER(ctypes.c_ubyte))
np.ctypeslib.as_array(test, sh)

python 3.4.2-dbg
numpy==1.10.1
Ubuntu 14.10

  1. Under release python this code runs without crashes, because of disabled assertions
  2. Under debug release python asserts failed:
    python3.4: ../Objects/abstract.c:2077: PyObject_Call: Assertion '(result != ((void *)0) && !PyErr_Occurred()) || (result == ((void *)0) && PyErr_Occurred())' failed.

Can you please explain why?

P.S. may be it can help:
If sh = (1, 1) change to sh = 239:

  1. Release python will raise TypeError: shape must be a tuple
  2. Debug python will also aborts
@PolarNick239 PolarNick239 changed the title ctypeslib.as_array crashes ctypeslib.as_array aborts Nov 27, 2015
@charris
Copy link
Member

charris commented Nov 27, 2015

Works here on Python 2.7.10 debug.

@charris
Copy link
Member

charris commented Nov 27, 2015

Can reproduce on 3.4.

@charris
Copy link
Member

charris commented Nov 27, 2015

Also with numpy 1.9.3 and 3.4 debug, so not a regression.

@jakirkham
Copy link
Contributor

Not totally sure, but have a strong feeling this may be related to these types of problems ( #6990 ).

@jakirkham
Copy link
Contributor

Ran this with gdb and it appears it is raising a warning about having an "Invalid PEP 3118 format string".

@jakirkham
Copy link
Contributor

Can you please explain why?

@PolarNick239: In Python 3.x, it appears they have taken a stricter stance on Python exception handling in C. Namely, they encourage (by way of assertions) us to handle an exception before calling any function that might raise another one. In some cases, CPython has been found to be violating this and we have started finding errors of our own where we are violating this. I am not sure who is responsible in this case.

@PolarNick239
Copy link
Author

Thanks for explanation! :)

eric-wieser added a commit to eric-wieser/numpy that referenced this issue Apr 12, 2018
Fixes numpygh-6741

If an object claims to support the PEP3118 buffer protocol, then any mistakes in the implementation should be raised as errors, rather than falling back on other approaches.

This has some behavior changes:

* np.array(mem_view_33d) now gives a more useful error message than "memoryview: unsupported format <B"
* np.array(ctypes_pointer) now errors rather than creating an object array containing a single pointer

Also adds support for 32-dimensional buffers, where previously the max was 31-dimensional.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants