Author: mattip <matti.pi...@gmail.com>
Branch: cpyext-ext
Changeset: r83324:ed8ef8873c00
Date: 2016-03-24 21:29 +0200
http://bitbucket.org/pypy/pypy/changeset/ed8ef8873c00/

Log:    test, add half of PyObject_Realloc

diff --git a/pypy/module/cpyext/include/object.h 
b/pypy/module/cpyext/include/object.h
--- a/pypy/module/cpyext/include/object.h
+++ b/pypy/module/cpyext/include/object.h
@@ -570,7 +570,7 @@
 PyAPI_FUNC(int) PyObject_CheckReadBuffer(PyObject *);
 
 #define PyObject_MALLOC         PyObject_Malloc
-/* #define PyObject_REALLOC        PyObject_Realloc  NotImplemented */
+#define PyObject_REALLOC        PyObject_Realloc
 #define PyObject_FREE           PyObject_Free
 #define PyObject_Del            PyObject_Free
 #define PyObject_DEL            PyObject_Free
diff --git a/pypy/module/cpyext/object.py b/pypy/module/cpyext/object.py
--- a/pypy/module/cpyext/object.py
+++ b/pypy/module/cpyext/object.py
@@ -21,6 +21,15 @@
                          flavor='raw',
                          add_memory_pressure=True)
 
+@cpython_api([rffi.VOIDP, Py_ssize_t], rffi.VOIDP)
+def PyObject_Realloc(space, ptr, size):
+    if not lltype.cast_ptr_to_int(ptr):
+        return lltype.malloc(rffi.VOIDP.TO, size,
+                         flavor='raw',
+                         add_memory_pressure=True)
+    # XXX FIXME
+    return lltype.nullptr(rffi.CCHARP.TO)
+
 @cpython_api([rffi.VOIDP], lltype.Void)
 def PyObject_Free(space, ptr):
     lltype.free(ptr, flavor='raw')
diff --git a/pypy/module/cpyext/test/test_cpyext.py 
b/pypy/module/cpyext/test/test_cpyext.py
--- a/pypy/module/cpyext/test/test_cpyext.py
+++ b/pypy/module/cpyext/test/test_cpyext.py
@@ -106,6 +106,7 @@
     """
     if sys.platform == 'win32':
         kwds["compile_extra"] = ["/we4013"]
+        kwds["link_extra"] = ["/LIBPATH:" + os.path.join(sys.exec_prefix, 
'libs')]
     elif sys.platform == 'darwin':
         pass
     elif sys.platform.startswith('linux'):
diff --git a/pypy/module/cpyext/test/test_object.py 
b/pypy/module/cpyext/test/test_object.py
--- a/pypy/module/cpyext/test/test_object.py
+++ b/pypy/module/cpyext/test/test_object.py
@@ -231,6 +231,24 @@
         assert type(x) is int
         assert x == -424344
 
+    def test_object_realloc(self):
+        module = self.import_extension('foo', [
+            ("realloctest", "METH_NOARGS",
+             """
+                 PyObject * ret;
+                 char *copy, *orig = PyObject_MALLOC(12);
+                 memcpy(orig, "hello world", 12);
+                 copy = PyObject_REALLOC(orig, 15);
+                 if (copy == NULL)
+                     Py_RETURN_NONE;
+                 ret = PyString_FromString(copy, 12);
+                 PyObject_Free(orig);
+                 PyObject_Free(copy);
+                 return ret;  
+             """)])
+        x = module.realloctest()
+        assert x == 'hello world'
+
     def test_TypeCheck(self):
         module = self.import_extension('foo', [
             ("typecheck", "METH_VARARGS",
_______________________________________________
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to