Skip to content

BUG: Clear error before and after PyErr_WarnEx for 3118 buffers #9348

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
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions numpy/core/src/multiarray/ctors.c
Original file line number Diff line number Diff line change
Expand Up @@ -1296,6 +1296,7 @@ _array_from_buffer_3118(PyObject *obj, PyObject **out)
PyObject *msg;
msg = PyBytes_FromFormat("Invalid PEP 3118 format string: '%s'",
view->format);
PyErr_Clear();
PyErr_WarnEx(PyExc_RuntimeWarning, PyBytes_AS_STRING(msg), 0);
Py_DECREF(msg);
goto fail;
Expand Down Expand Up @@ -1485,14 +1486,16 @@ PyArray_GetArrayParamsFromObject(PyObject *op,
}

/* If op supports the PEP 3118 buffer interface */
if (!PyBytes_Check(op) && !PyUnicode_Check(op) &&
_array_from_buffer_3118(op, (PyObject **)out_arr) == 0) {
if (writeable
&& PyArray_FailUnlessWriteable(*out_arr, "PEP 3118 buffer") < 0) {
Py_DECREF(*out_arr);
return -1;
if (!PyBytes_Check(op) && !PyUnicode_Check(op)) {
if (_array_from_buffer_3118(op, (PyObject **)out_arr) == 0) {
if (writeable &&
PyArray_FailUnlessWriteable(*out_arr, "PEP 3118 buffer") < 0) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't look safe if out_arr == NULL...

Py_XDECREF(*out_arr);
return -1;
}
return (*out_arr) == NULL ? -1 : 0;
}
return (*out_arr) == NULL ? -1 : 0;
PyErr_Clear();
}

/* If op supports the __array_struct__ or __array_interface__ interface */
Expand Down
26 changes: 24 additions & 2 deletions numpy/tests/test_ctypeslib.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
import sys

import numpy as np
from numpy.ctypeslib import ndpointer, load_library
from numpy.ctypeslib import ndpointer, load_library, as_array
from numpy.distutils.misc_util import get_shared_lib_extension
from numpy.testing import TestCase, run_module_suite, dec
from numpy.testing import TestCase, assert_array_equal, run_module_suite, dec

try:
cdll = None
Expand Down Expand Up @@ -113,6 +113,28 @@ def test_cache(self):
a2 = ndpointer(dtype=np.float64)
self.assertEqual(a1, a2)

class TestAsArray(TestCase):
@dec.skipif(not _HAS_CTYPE,
"ctypes not available on this python installation")
def test_array(self):
from ctypes import c_int
at = c_int * 2
a = as_array(at(1, 2))
self.assertEqual(a.shape, (2,))
assert_array_equal(a, np.array([1, 2]))
a = as_array((at * 3)(at(1, 2), at(3, 4), at(5, 6)))
self.assertEqual(a.shape, (3, 2))
assert_array_equal(a, np.array([[1, 2], [3, 4], [5, 6]]))

@dec.skipif(not _HAS_CTYPE,
"ctypes not available on this python installation")
def test_pointer(self):
from ctypes import c_int, cast, POINTER
p = cast((c_int * 10)(*range(10)), POINTER(c_int))
a = as_array(p, (10,))
self.assertEqual(a.shape, (10,))
assert_array_equal(a, np.array(range(10)))


if __name__ == "__main__":
run_module_suite()