10000 BUG: core: fix NPY_TITLE_KEY macro on pypy by pv · Pull Request #10860 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

BUG: core: fix NPY_TITLE_KEY macro on pypy #10860

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

Merged
merged 1 commit into from
Apr 9, 2018
Merged
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 fil 8000 es.
Loading
Diff view
Diff view
31 changes: 29 additions & 2 deletions numpy/core/include/numpy/ndarrayobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -232,9 +232,36 @@ PyArray_DiscardWritebackIfCopy(PyArrayObject *arr)
dict.
*/

#define NPY_TITLE_KEY(key, value) ((PyTuple_GET_SIZE((value))==3) && \
(PyTuple_GET_ITEM((value), 2) == (key)))
static NPY_INLINE int
NPY_TITLE_KEY_check(PyObject *key, PyObject *value)
{
PyObject *title;
if (PyTuple_GET_SIZE(value) != 3) {
return 0;
}
title = PyTuple_GET_ITEM(value, 2);
if (key == title) {
return 1;
}
#ifdef PYPY_VERSION
/*
* On PyPy, dictionary keys do not always preserve object identity.
* Fall back to comparison by value.
*/
if (PyUnicode_Check(title) && PyUnicode_Check(key)) {
return PyUnicode_Compare(title, key) == 0 ? 1 : 0;
}
#if PY_VERSION_HEX < 0x03000000
if (PyString_Check(title) && PyString_Check(key)) {
return PyObject_Compare(title, key) == 0 ? 1 : 0;
}
#endif
#endif
return 0;
}

/* Macro, for backward compat with "if NPY_TITLE_KEY(key, value) { ..." */
#define NPY_TITLE_KEY(key, value) (NPY_TITLE_KEY_check((key), (value)))

#define DEPRECATE(msg) PyErr_WarnEx(PyExc_DeprecationWarning,msg,1)
#define DEPRECATE_FUTUREWARNING(msg) PyErr_WarnEx(PyExc_FutureWarning,msg,1)
Expand Down
10 changes: 10 additions & 0 deletions numpy/core/tests/test_regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -2332,3 +2332,13 @@ def test_void_item_memview(self):
#va[0] = b'\xff\xff\xff\xff'
#del va
#assert_equal(x, b'\x00\x00\x00\x00')

def test_structarray_title(self):
# The following used to segfault on pypy, due to NPY_TITLE_KEY
# not working properly and resulting to double-decref of the
# structured array field items:
# See: https://bitbucket.org/pypy/pypy/issues/2789
for j in range(5):
structure = np.array([1], dtype=[(('x', 'X'), np.object_)])
structure[0]['x'] = np.array([2])
gc.collect()
0