[pypy-commit] pypy buffer-interface2: up the size of the fixed-size format buffer, try to warn if truncated. Test needed

mattip pypy.commits at gmail.com
Sun Oct 2 07:15:36 EDT 2016


Author: Matti Picus <matti.picus at gmail.com>
Branch: buffer-interface2
Changeset: r87517:fe8ec4c389c4
Date: 2016-10-02 14:14 +0300
http://bitbucket.org/pypy/pypy/changeset/fe8ec4c389c4/

Log:	up the size of the fixed-size format buffer, try to warn if
	truncated. Test needed

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
@@ -144,7 +144,7 @@
 
 /* Py3k buffer interface, adapted for PyPy */
 #define Py_MAX_NDIMS 32
-#define Py_MAX_FMT 5
+#define Py_MAX_FMT 128
 typedef struct bufferinfo {
     void *buf;
     PyObject *obj;        /* owned reference */
diff --git a/pypy/module/cpyext/memoryobject.py b/pypy/module/cpyext/memoryobject.py
--- a/pypy/module/cpyext/memoryobject.py
+++ b/pypy/module/cpyext/memoryobject.py
@@ -4,6 +4,7 @@
 from rpython.rtyper.lltypesystem import lltype, rffi
 from rpython.rlib.rarithmetic import widen
 from pypy.objspace.std.memoryobject import W_MemoryView
+from pypy.module.cpyext.import_ import PyImport_Import
 
 PyMemoryView_Check, PyMemoryView_CheckExact = build_type_checkers("MemoryView", "w_memoryview")
 
@@ -46,8 +47,13 @@
     fmt = buf.getformat()
     n = Py_MAX_FMT - 1 # NULL terminated buffer
     if len(fmt) > n:
-        ### WARN?
-        pass
+        w_message = space.newbytes("PyPy specific Py_MAX_FMT is %d which is too "
+                           "small for buffer format, %d needed" % (
+                           Py_MAX_FMT, len(fmt)))
+        w_stacklevel = space.newint(1)
+        w_module = PyImport_Import(space, space.newbytes("warnings"))
+        w_warn = space.getattr(w_module, space.newbytes("warn"))
+        space.call_function(w_warn, w_message, space.w_None, w_stacklevel)
     else:
         n = len(fmt)
     for i in range(n):
diff --git a/pypy/module/cpyext/test/test_memoryobject.py b/pypy/module/cpyext/test/test_memoryobject.py
--- a/pypy/module/cpyext/test/test_memoryobject.py
+++ b/pypy/module/cpyext/test/test_memoryobject.py
@@ -1,8 +1,12 @@
+import pytest
+
 from rpython.rtyper.lltypesystem import rffi
 from pypy.module.cpyext.test.test_api import BaseApiTest
 from pypy.module.cpyext.test.test_cpyext import AppTestCpythonExtensionBase
 from rpython.rlib.buffer import StringBuffer
 
+only_pypy ="config.option.runappdirect and '__pypy__' not in sys.builtin_module_names" 
+
 class TestMemoryViewObject(BaseApiTest):
     def test_fromobject(self, space, api):
         w_hello = space.newbytes("hello")
@@ -41,8 +45,12 @@
         viewlen = module.test_buffer(arr)
         assert viewlen == y.itemsize * len(y)
 
+    @pytest.mark.skipif(only_pypy, reason='pypy only test')
     def test_buffer_info(self):
-        from _numpypy import multiarray as np
+        try:
+            from _numpypy import multiarray as np
+        except ImportError:
+            skip('pypy built without _numpypy')
         module = self.import_module(name='buffer_test')
         get_buffer_info = module.get_buffer_info
         raises(ValueError, get_buffer_info, np.arange(5)[::2], ('SIMPLE',))


More information about the pypy-commit mailing list