diff options
author | Nathaniel J. Smith <njs@pobox.com> | 2016-04-10 19:30:21 +0000 |
---|---|---|
committer | Nathaniel J. Smith <njs@pobox.com> | 2016-04-10 19:30:21 +0000 |
commit | f1b009112f07b57fde07b99129c1b118a6669f51 (patch) | |
tree | b661a0ff9bf7d793d0cca737f24556e2fce3e90f | |
parent | a03435ab57da3575b8c927465df11dbdd9ef093a (diff) | |
parent | a2c4c117478b34a0761a98a9e5c474ab5c4f5490 (diff) | |
download | numpy-f1b009112f07b57fde07b99129c1b118a6669f51.tar.gz |
Merge pull request #7536 from charris/random-use-pycapsule
MAINT: Always use PyCapsule instead of PyCObject in mtrand.pyx
-rw-r--r-- | numpy/random/mtrand/Python.pxi | 14 | ||||
-rw-r--r-- | numpy/random/mtrand/mt_compat.h | 68 | ||||
-rw-r--r-- | numpy/random/mtrand/mtrand.pyx | 21 | ||||
-rw-r--r-- | numpy/random/mtrand/numpy.pxd | 6 | ||||
-rw-r--r-- | numpy/random/tests/test_random.py | 22 |
5 files changed, 24 insertions, 107 deletions
diff --git a/numpy/random/mtrand/Python.pxi b/numpy/random/mtrand/Python.pxi index 01d47af50..f23a3bfe6 100644 --- a/numpy/random/mtrand/Python.pxi +++ b/numpy/random/mtrand/Python.pxi @@ -28,20 +28,6 @@ cdef extern from "Python.h": void Py_INCREF(object obj) void Py_XINCREF(object obj) - # CObject API -# If this is uncommented it needs to be fixed to use PyCapsule -# for Python >= 3.0 -# -# ctypedef void (*destructor1)(void* cobj) -# ctypedef void (*destructor2)(void* cobj, void* desc) -# int PyCObject_Check(object p) -# object PyCObject_FromVoidPtr(void* cobj, destructor1 destr) -# object PyCObject_FromVoidPtrAndDesc(void* cobj, void* desc, -# destructor2 destr) -# void* PyCObject_AsVoidPtr(object self) -# void* PyCObject_GetDesc(object self) -# int PyCObject_SetVoidPtr(object self, void* cobj) - # TypeCheck API int PyFloat_Check(object obj) int PyInt_Check(object obj) diff --git a/numpy/random/mtrand/mt_compat.h b/numpy/random/mtrand/mt_compat.h deleted file mode 100644 index ab56a553c..000000000 --- a/numpy/random/mtrand/mt_compat.h +++ /dev/null @@ -1,68 +0,0 @@ -/* - * This is a convenience header file providing compatibility utilities - * for supporting Python 2 and Python 3 in the same code base. - * - * It can be removed when Python 2.6 is dropped as PyCapsule is available - * in both Python 3.1+ and Python 2.7. - */ - -#ifndef _MT_COMPAT_H_ -#define _MT_COMPAT_H_ - -#include <Python.h> -#include <numpy/npy_common.h> - -#ifdef __cplusplus -extern "C" { -#endif - - -/* - * PyCObject functions adapted to PyCapsules. - * - * The main job here is to get rid of the improved error handling - * of PyCapsules. It's a shame... - */ -#if PY_VERSION_HEX >= 0x03000000 - -static NPY_INLINE PyObject * -NpyCapsule_FromVoidPtr(void *ptr, void (*dtor)(PyObject *)) -{ - PyObject *ret = PyCapsule_New(ptr, NULL, dtor); - if (ret == NULL) { - PyErr_Clear(); - } - return ret; -} - -static NPY_INLINE void * -NpyCapsule_AsVoidPtr(PyObject *obj) -{ - void *ret = PyCapsule_GetPointer(obj, NULL); - if (ret == NULL) { - PyErr_Clear(); - } - return ret; -} - -#else - -static NPY_INLINE PyObject * -NpyCapsule_FromVoidPtr(void *ptr, void (*dtor)(void *)) -{ - return PyCObject_FromVoidPtr(ptr, dtor); -} - -static NPY_INLINE void * -NpyCapsule_AsVoidPtr(PyObject *ptr) -{ - return PyCObject_AsVoidPtr(ptr); -} - -#endif - -#ifdef __cplusplus -} -#endif - -#endif /* _COMPAT_H_ */ diff --git a/numpy/random/mtrand/mtrand.pyx b/numpy/random/mtrand/mtrand.pyx index 3adeb1990..72dff7cfc 100644 --- a/numpy/random/mtrand/mtrand.pyx +++ b/numpy/random/mtrand/mtrand.pyx @@ -23,6 +23,7 @@ include "Python.pxi" include "numpy.pxd" +include "cpython/pycapsule.pxd" from libc cimport string @@ -594,7 +595,7 @@ def _rand_bool(low, high, size, rngstate): cdef npy_bool *out cdef ndarray array "arrayObject" cdef npy_intp cnt - cdef rk_state *state = <rk_state *>NpyCapsule_AsVoidPtr(rngstate) + cdef rk_state *state = <rk_state *>PyCapsule_GetPointer(rngstate, NULL) rng = <npy_bool>(high - low) off = <npy_bool>(low) @@ -621,7 +622,7 @@ def _rand_int8(low, high, size, rngstate): cdef npy_uint8 *out cdef ndarray array "arrayObject" cdef npy_intp cnt - cdef rk_state *state = <rk_state *>NpyCapsule_AsVoidPtr(rngstate) + cdef rk_state *state = <rk_state *>PyCapsule_GetPointer(rngstate, NULL) rng = <npy_uint8>(high - low) off = <npy_uint8>(<npy_int8>low) @@ -648,7 +649,7 @@ def _rand_int16(low, high, size, rngstate): cdef npy_uint16 *out cdef ndarray array "arrayObject" cdef npy_intp cnt - cdef rk_state *state = <rk_state *>NpyCapsule_AsVoidPtr(rngstate) + cdef rk_state *state = <rk_state *>PyCapsule_GetPointer(rngstate, NULL) rng = <npy_uint16>(high - low) off = <npy_uint16>(<npy_int16>low) @@ -699,7 +700,7 @@ def _rand_int32(low, high, size, rngstate): cdef npy_uint32 *out cdef ndarray array "arrayObject" cdef npy_intp cnt - cdef rk_state *state = <rk_state *>NpyCapsule_AsVoidPtr(rngstate) + cdef rk_state *state = <rk_state *>PyCapsule_GetPointer(rngstate, NULL) rng = <npy_uint32>(high - low) off = <npy_uint32>(<npy_int32>low) @@ -726,7 +727,7 @@ def _rand_int64(low, high, size, rngstate): cdef npy_uint64 *out cdef ndarray array "arrayObject" cdef npy_intp cnt - cdef rk_state *state = <rk_state *>NpyCapsule_AsVoidPtr(rngstate) + cdef rk_state *state = <rk_state *>PyCapsule_GetPointer(rngstate, NULL) rng = <npy_uint64>(high - low) off = <npy_uint64>(<npy_int64>low) @@ -752,7 +753,7 @@ def _rand_uint8(low, high, size, rngstate): cdef npy_uint8 *out cdef ndarray array "arrayObject" cdef npy_intp cnt - cdef rk_state *state = <rk_state *>NpyCapsule_AsVoidPtr(rngstate) + cdef rk_state *state = <rk_state *>PyCapsule_GetPointer(rngstate, NULL) rng = <npy_uint8>(high - low) off = <npy_uint8>(low) @@ -779,7 +780,7 @@ def _rand_uint16(low, high, size, rngstate): cdef npy_uint16 *out cdef ndarray array "arrayObject" cdef npy_intp cnt - cdef rk_state *state = <rk_state *>NpyCapsule_AsVoidPtr(rngstate) + cdef rk_state *state = <rk_state *>PyCapsule_GetPointer(rngstate, NULL) rng = <npy_uint16>(high - low) off = <npy_uint16>(low) @@ -806,7 +807,7 @@ def _rand_uint32(low, high, size, rngstate): cdef npy_uint32 *out cdef ndarray array "arrayObject" cdef npy_intp cnt - cdef rk_state *state = <rk_state *>NpyCapsule_AsVoidPtr(rngstate) + cdef rk_state *state = <rk_state *>PyCapsule_GetPointer(rngstate, NULL) rng = <npy_uint32>(high - low) off = <npy_uint32>(low) @@ -833,7 +834,7 @@ def _rand_uint64(low, high, size, rngstate): cdef npy_uint64 *out cdef ndarray array "arrayObject" cdef npy_intp cnt - cdef rk_state *state = <rk_state *>NpyCapsule_AsVoidPtr(rngstate) + cdef rk_state *state = <rk_state *>PyCapsule_GetPointer(rngstate, NULL) rng = <npy_uint64>(high - low) off = <npy_uint64>(low) @@ -914,7 +915,7 @@ cdef class RandomState: def __init__(self, seed=None): self.internal_state = <rk_state*>PyMem_Malloc(sizeof(rk_state)) - self.state_address = NpyCapsule_FromVoidPtr(self.internal_state, NULL) + self.state_address = PyCapsule_New(self.internal_state, NULL, NULL) self.lock = Lock() self.seed(seed) diff --git a/numpy/random/mtrand/numpy.pxd b/numpy/random/mtrand/numpy.pxd index 488278d6c..d5b0d74ca 100644 --- a/numpy/random/mtrand/numpy.pxd +++ b/numpy/random/mtrand/numpy.pxd @@ -2,12 +2,6 @@ cdef extern from "numpy/npy_no_deprecated_api.h": pass -cdef extern from "mt_compat.h": - - object NpyCapsule_FromVoidPtr(void *ptr, void (*dtor)(object o)) - void * NpyCapsule_AsVoidPtr(object o) - - cdef extern from "numpy/arrayobject.h": cdef enum NPY_TYPES: diff --git a/numpy/random/tests/test_random.py b/numpy/random/tests/test_random.py index a07fa52f5..013205835 100644 --- a/numpy/random/tests/test_random.py +++ b/numpy/random/tests/test_random.py @@ -260,11 +260,13 @@ class TestRandomDist(TestCase): def test_random_integers(self): np.random.seed(self.seed) - actual = np.random.random_integers(-99, 99, size=(3, 2)) - desired = np.array([[31, 3], - [-52, 41], - [-48, -66]]) - assert_array_equal(actual, desired) + with warnings.catch_warnings(): + warnings.simplefilter("ignore", DeprecationWarning) + actual = np.random.random_integers(-99, 99, size=(3, 2)) + desired = np.array([[31, 3], + [-52, 41], + [-48, -66]]) + assert_array_equal(actual, desired) def test_random_integers_max_int(self): # Tests whether random_integers can generate the @@ -272,10 +274,12 @@ class TestRandomDist(TestCase): # into a C long. Previous implementations of this # method have thrown an OverflowError when attempting # to generate this integer. - actual = np.random.random_integers(np.iinfo('l').max, - np.iinfo('l').max) - desired = np.iinfo('l').max - assert_equal(actual, desired) + with warnings.catch_warnings(): + warnings.simplefilter("ignore", DeprecationWarning) + actual = np.random.random_integers(np.iinfo('l').max, + np.iinfo('l').max) + desired = np.iinfo('l').max + assert_equal(actual, desired) def test_random_integers_deprecated(self): with warnings.catch_warnings(): |