Author: Matti Picus <[email protected]>
Branch: 
Changeset: r67338:a30928d7419e
Date: 2013-10-13 01:52 +0300
http://bitbucket.org/pypy/pypy/changeset/a30928d7419e/

Log:    support more numpy c-api

diff --git a/pypy/module/cpyext/include/numpy/npy_3kcompat.h 
b/pypy/module/cpyext/include/numpy/npy_3kcompat.h
--- a/pypy/module/cpyext/include/numpy/npy_3kcompat.h
+++ b/pypy/module/cpyext/include/numpy/npy_3kcompat.h
@@ -0,0 +1,39 @@
+/*
+ * In numpy this is a convenience header file providing compatibility utilities
+ * for supporting Python 2 and Python 3 in the same code base.
+ *
+ * PyPy uses it as a convenient place to add compatability declarations
+ */
+
+#ifndef _NPY_3KCOMPAT_H_
+#define _NPY_3KCOMPAT_H_
+
+#include <numpy/npy_common.h>
+
+#define npy_PyFile_Dup(file, mode) (NULL)
+#define npy_PyFile_DupClose(file, handle) (0)
+
+static NPY_INLINE PyObject*
+npy_PyFile_OpenFile(PyObject *filename, const char *mode)
+{
+    PyObject *open;
+    open = PyDict_GetItemString(PyEval_GetBuiltins(), "open");
+    if (open == NULL) {
+        return NULL;
+    }
+    return PyObject_CallFunction(open, "Os", filename, mode);
+}
+
+static NPY_INLINE int
+npy_PyFile_CloseFile(PyObject *file)
+{
+    PyObject *ret;
+
+    ret = PyObject_CallMethod(file, "close", NULL);
+    if (ret == NULL) {
+        return -1;
+    }
+    Py_DECREF(ret);
+    return 0;
+}
+
diff --git a/pypy/module/cpyext/ndarrayobject.py 
b/pypy/module/cpyext/ndarrayobject.py
--- a/pypy/module/cpyext/ndarrayobject.py
+++ b/pypy/module/cpyext/ndarrayobject.py
@@ -171,6 +171,15 @@
         w_array.implementation.shape = []
     return w_array
 
+@cpython_api([rffi.INT_real], PyObject)
+def _PyArray_DescrFromType(space, typenum):
+    try:
+        dtype = get_dtype_cache(space).dtypes_by_num[typenum]
+        return dtype
+    except KeyError:
+        raise OperationError(space.w_ValueError, space.wrap(
+            '_PyArray_DescrFromType called with invalid dtype %d' % typenum))
+
 @cpython_api([PyObject, Py_ssize_t, Py_ssize_t, Py_ssize_t], PyObject)
 def _PyArray_FromObject(space, w_obj, typenum, min_depth, max_depth):
     try:
diff --git a/pypy/module/cpyext/test/test_ndarrayobject.py 
b/pypy/module/cpyext/test/test_ndarrayobject.py
--- a/pypy/module/cpyext/test/test_ndarrayobject.py
+++ b/pypy/module/cpyext/test/test_ndarrayobject.py
@@ -265,6 +265,12 @@
                 return obj2;
                 '''
                 ),
+                ("test_DescrFromType", "METH_O",
+                """
+                    Signed typenum = PyInt_AsLong(args);
+                    return _PyArray_DescrFromType(typenum);
+                """
+                ),
                 ], prologue='#include <numpy/arrayobject.h>')
         arr = mod.test_simplenew()
         assert arr.shape == (2, 3)
@@ -278,3 +284,5 @@
         #Make sure these work without errors
         arr = mod.test_FromAny()
         arr = mod.test_FromObject()
+        dt = mod.test_DescrFromType(11)
+        assert dt.num == 11
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to