Author: Matti Picus <[email protected]>
Branch:
Changeset: r87891:18d68f2e6f63
Date: 2016-10-20 15:28 +0300
http://bitbucket.org/pypy/pypy/changeset/18d68f2e6f63/
Log: test, implement PyMemoryView_FromBuffer
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
@@ -1,6 +1,6 @@
from pypy.module.cpyext.api import (cpython_api, Py_buffer, CANNOT_FAIL,
Py_MAX_FMT, Py_MAX_NDIMS, build_type_checkers,
Py_ssize_tP)
-from pypy.module.cpyext.pyobject import PyObject, make_ref, incref
+from pypy.module.cpyext.pyobject import PyObject, make_ref, incref, from_ref
from rpython.rtyper.lltypesystem import lltype, rffi
from rpython.rlib.rarithmetic import widen
from pypy.objspace.std.memoryobject import W_MemoryView
@@ -132,11 +132,22 @@
def PyMemoryView_FromObject(space, w_obj):
return space.call_method(space.builtin, "memoryview", w_obj)
+@cpython_api([lltype.Ptr(Py_buffer)], PyObject)
+def PyMemoryView_FromBuffer(space, view):
+ """Create a memoryview object wrapping the given buffer-info structure
view.
+ The memoryview object then owns the buffer, which means you shouldn't
+ try to release it yourself: it will be released on deallocation of the
+ memoryview object."""
+ w_obj = from_ref(space, view.c_obj)
+ if isinstance(w_obj, W_MemoryView):
+ return w_obj
+ return space.call_method(space.builtin, "memoryview", w_obj)
+
@cpython_api([PyObject], PyObject)
def PyMemoryView_GET_BASE(space, w_obj):
# return the obj field of the Py_buffer created by PyMemoryView_GET_BUFFER
# XXX needed for numpy on py3k
- raise NotImplementedError('PyMemoryView_GET_BUFFER')
+ raise NotImplementedError('PyMemoryView_GET_BASE')
@cpython_api([PyObject], lltype.Ptr(Py_buffer), error=CANNOT_FAIL)
def PyMemoryView_GET_BUFFER(space, w_obj):
diff --git a/pypy/module/cpyext/stubs.py b/pypy/module/cpyext/stubs.py
--- a/pypy/module/cpyext/stubs.py
+++ b/pypy/module/cpyext/stubs.py
@@ -41,14 +41,6 @@
given shape with the given number of bytes per element."""
raise NotImplementedError
-@cpython_api([Py_buffer], PyObject)
-def PyMemoryView_FromBuffer(space, view):
- """Create a memoryview object wrapping the given buffer-info structure
view.
- The memoryview object then owns the buffer, which means you shouldn't
- try to release it yourself: it will be released on deallocation of the
- memoryview object."""
- raise NotImplementedError
-
@cpython_api([PyObject, rffi.INT_real, lltype.Char], PyObject)
def PyMemoryView_GetContiguous(space, obj, buffertype, order):
"""Create a memoryview object to a contiguous chunk of memory (in either
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
@@ -29,6 +29,12 @@
assert w_view.c_len == 5
o = rffi.charp2str(w_view.c_buf)
assert o == 'hello'
+ w_mv = api.PyMemoryView_FromBuffer(w_view)
+ for f in ('format', 'itemsize', 'ndim', 'readonly',
+ 'shape', 'strides', 'suboffsets'):
+ w_f = space.wrap(f)
+ assert space.eq_w(space.getattr(w_mv, w_f),
+ space.getattr(w_memoryview, w_f))
class AppTestBufferProtocol(AppTestCpythonExtensionBase):
def test_buffer_protocol(self):
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit