summaryrefslogtreecommitdiff
path: root/numpy/random
diff options
context:
space:
mode:
authorPauli Virtanen <pav@iki.fi>2009-12-06 12:08:54 +0000
committerPauli Virtanen <pav@iki.fi>2009-12-06 12:08:54 +0000
commit108960148ba8dd319a0b656158b8c7897fcf3307 (patch)
tree953d080951975fd9e50ba0f67449691e73f375c7 /numpy/random
parent65b64fdf528ffae27054621b43ff78aa48b991b9 (diff)
downloadnumpy-108960148ba8dd319a0b656158b8c7897fcf3307.tar.gz
3K: random: make mtrand to import
Diffstat (limited to 'numpy/random')
-rw-r--r--numpy/random/mtrand/mtrand.pyx6
-rw-r--r--numpy/random/mtrand/mtrand_py_helper.h23
2 files changed, 27 insertions, 2 deletions
diff --git a/numpy/random/mtrand/mtrand.pyx b/numpy/random/mtrand/mtrand.pyx
index e98414ae6..0340bef62 100644
--- a/numpy/random/mtrand/mtrand.pyx
+++ b/numpy/random/mtrand/mtrand.pyx
@@ -31,6 +31,9 @@ cdef extern from "math.h":
double sin(double x)
double cos(double x)
+cdef extern from "mtrand_py_helper.h":
+ object empty_py_bytes(unsigned long length, void **bytes)
+
cdef extern from "randomkit.h":
ctypedef struct rk_state:
@@ -883,8 +886,7 @@ cdef class RandomState:
"""
cdef void *bytes
- bytestring = PyString_FromStringAndSize(NULL, length)
- bytes = PyString_AS_STRING(bytestring)
+ bytestring = empty_py_bytes(length, &bytes)
rk_fill(bytes, length, self.internal_state)
return bytestring
diff --git a/numpy/random/mtrand/mtrand_py_helper.h b/numpy/random/mtrand/mtrand_py_helper.h
new file mode 100644
index 000000000..2e7e4b7dc
--- /dev/null
+++ b/numpy/random/mtrand/mtrand_py_helper.h
@@ -0,0 +1,23 @@
+#ifndef _MTRAND_PY_HELPER_H_
+#define _MTRAND_PY_HELPER_H_
+
+#include <Python.h>
+
+static PyObject *empty_py_bytes(unsigned long length, void **bytes)
+{
+ PyObject *b;
+#if PY_MAJOR_VERSION >= 3
+ b = PyBytes_FromStringAndSize(NULL, length);
+ if (b) {
+ *bytes = PyBytes_AS_STRING(b);
+ }
+#else
+ b = PyString_FromStringAndSize(NULL, length);
+ if (b) {
+ *bytes = PyString_AS_STRING(b);
+ }
+#endif
+ return b;
+}
+
+#endif /* _MTRAND_PY_HELPER_H_ */