summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--numpy/random/mtrand/Python.pxi14
-rw-r--r--numpy/random/mtrand/mt_compat.h68
-rw-r--r--numpy/random/mtrand/mtrand.pyx21
-rw-r--r--numpy/random/mtrand/numpy.pxd6
4 files changed, 11 insertions, 98 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: