Commits

Antonio Cuni committed e6c43c1

implement PyArray_Type

Comments (0)

Files changed (4)

 
 def pytest_configure(config):
     exe = sys.executable
+    ROOT.chdir()
     ret = os.system('"%s" "%s" build' % (exe, SETUP_PY))
     if ret != 0:
         print "build failed, quitting py.test"

include/numpypy_c.h

         return -1;                              \
     }
 
+static PyObject* _PyArray_Type;
+#define PyArray_Type (*_PyArray_Type)
+
 static PyObject* (*PyArray_SimpleNewFromData)(int nd, npy_intp* dims,  int typenum, void* data);
-
 static int (*PyArray_NDIM)(PyObject* array);
 static npy_intp* (*PyArray_DIMS)(PyObject* array);
 static PyObject* (*PyArray_Return)(PyArrayObject* array);
 
 static int 
 import_array(void) {
+    PyObject* np = PyImport_ImportModule("numpypy");
+    if (!np)
+        return NULL;
+    
+    _PyArray_Type = PyObject_GetAttrString(np, "ndarray");
+    if (!_PyArray_Type)
+        return NULL;
+
     PyObject* impl = PyImport_ImportModule("numpypy_c.impl");
     if (!impl)
         return -1;

numpypy_c/testing/c_test.c

     Py_RETURN_NONE;
 }
 
+static PyObject*
+check_array(PyObject* self, PyObject* args) {
+    PyObject* obj;
+    if (!PyArg_ParseTuple(args, "O!", &PyArray_Type, &obj))
+        return NULL;
+    Py_INCREF(obj);
+    return obj;
+}
+
 
 static PyMethodDef c_test_methods[] = {
     {"_frombuffer_2_2", _frombuffer_2_2, METH_VARARGS, "..."},
     {"_test_Return", _test_Return, METH_NOARGS, "..."},
     {"_test_DATA", _test_DATA, METH_NOARGS, "..."},
     {"_test_STRIDES", _test_STRIDES, METH_NOARGS, "..."},
+    {"check_array", check_array, METH_VARARGS, "..."},
     {NULL}  /* Sentinel */
 };
 

numpypy_c/testing/test_numpypy_c.py

     array[0, 0] = 42
     assert buf[0] == 42
 
+def test_check_array():
+    a = np.array([1, 2, 3, 4])
+    assert c_test.check_array(a) is a
+    py.test.raises(TypeError, "c_test.check_array(42)")