[pypy-commit] pypy default: update to cffi/3544b1031b51
arigo
pypy.commits at gmail.com
Sun Feb 5 02:29:11 EST 2017
Author: Armin Rigo <arigo at tunes.org>
Branch:
Changeset: r89933:8bf21def2904
Date: 2017-02-05 08:25 +0100
http://bitbucket.org/pypy/pypy/changeset/8bf21def2904/
Log: update to cffi/3544b1031b51
diff --git a/lib_pypy/cffi/api.py b/lib_pypy/cffi/api.py
--- a/lib_pypy/cffi/api.py
+++ b/lib_pypy/cffi/api.py
@@ -93,6 +93,7 @@
# ctypes backend: attach these constants to the instance
self.NULL = self.cast(self.BVoidP, 0)
self.CData, self.CType = backend._get_types()
+ self.buffer = backend.buffer
def cdef(self, csource, override=False, packed=False):
"""Parse the given C source. This registers all declared functions,
@@ -316,18 +317,18 @@
"""
return self._backend.unpack(cdata, length)
- def buffer(self, cdata, size=-1):
- """Return a read-write buffer object that references the raw C data
- pointed to by the given 'cdata'. The 'cdata' must be a pointer or
- an array. Can be passed to functions expecting a buffer, or directly
- manipulated with:
-
- buf[:] get a copy of it in a regular string, or
- buf[idx] as a single character
- buf[:] = ...
- buf[idx] = ... change the content
- """
- return self._backend.buffer(cdata, size)
+ #def buffer(self, cdata, size=-1):
+ # """Return a read-write buffer object that references the raw C data
+ # pointed to by the given 'cdata'. The 'cdata' must be a pointer or
+ # an array. Can be passed to functions expecting a buffer, or directly
+ # manipulated with:
+ #
+ # buf[:] get a copy of it in a regular string, or
+ # buf[idx] as a single character
+ # buf[:] = ...
+ # buf[idx] = ... change the content
+ # """
+ # note that 'buffer' is a type, set on this instance by __init__
def from_buffer(self, python_buffer):
"""Return a <cdata 'char[]'> that points to the data of the
@@ -593,11 +594,15 @@
ensure('extra_link_args', '/MANIFEST')
def set_source(self, module_name, source, source_extension='.c', **kwds):
+ import os
if hasattr(self, '_assigned_source'):
raise ValueError("set_source() cannot be called several times "
"per ffi object")
if not isinstance(module_name, basestring):
raise TypeError("'module_name' must be a string")
+ if os.sep in module_name or (os.altsep and os.altsep in module_name):
+ raise ValueError("'module_name' must not contain '/': use a dotted "
+ "name to make a 'package.module' location")
self._assigned_source = (str(module_name), source,
source_extension, kwds)
diff --git a/pypy/module/_cffi_backend/__init__.py b/pypy/module/_cffi_backend/__init__.py
--- a/pypy/module/_cffi_backend/__init__.py
+++ b/pypy/module/_cffi_backend/__init__.py
@@ -50,7 +50,7 @@
'string': 'func.string',
'unpack': 'func.unpack',
- 'buffer': 'cbuffer.buffer',
+ 'buffer': 'cbuffer.MiniBuffer',
'memmove': 'func.memmove',
'get_errno': 'cerrno.get_errno',
diff --git a/pypy/module/_cffi_backend/cbuffer.py b/pypy/module/_cffi_backend/cbuffer.py
--- a/pypy/module/_cffi_backend/cbuffer.py
+++ b/pypy/module/_cffi_backend/cbuffer.py
@@ -56,19 +56,8 @@
e.w_type = space.w_ValueError
raise
-MiniBuffer.typedef = TypeDef(
- "_cffi_backend.buffer",
- __len__ = interp2app(MiniBuffer.descr_len),
- __getitem__ = interp2app(MiniBuffer.descr_getitem),
- __setitem__ = interp2app(MiniBuffer.descr_setitem),
- __weakref__ = make_weakref_descr(MiniBuffer),
- __str__ = interp2app(MiniBuffer.descr_str),
- )
-MiniBuffer.typedef.acceptable_as_base_class = False
-
-
@unwrap_spec(w_cdata=cdataobj.W_CData, size=int)
-def buffer(space, w_cdata, size=-1):
+def MiniBuffer___new__(space, w_subtype, w_cdata, size=-1):
ctype = w_cdata.ctype
if isinstance(ctype, ctypeptr.W_CTypePointer):
if size < 0:
@@ -89,3 +78,25 @@
"don't know the size pointed to by '%s'", ctype.name)
ptr = w_cdata.unsafe_escaping_ptr() # w_cdata kept alive by MiniBuffer()
return space.wrap(MiniBuffer(LLBuffer(ptr, size), w_cdata))
+
+MiniBuffer.typedef = TypeDef(
+ "_cffi_backend.buffer",
+ __new__ = interp2app(MiniBuffer___new__),
+ __len__ = interp2app(MiniBuffer.descr_len),
+ __getitem__ = interp2app(MiniBuffer.descr_getitem),
+ __setitem__ = interp2app(MiniBuffer.descr_setitem),
+ __weakref__ = make_weakref_descr(MiniBuffer),
+ __str__ = interp2app(MiniBuffer.descr_str),
+ __doc__ = """ffi.buffer(cdata[, byte_size]):
+Return a read-write buffer object that references the raw C data
+pointed to by the given 'cdata'. The 'cdata' must be a pointer or an
+array. Can be passed to functions expecting a buffer, or directly
+manipulated with:
+
+ buf[:] get a copy of it in a regular string, or
+ buf[idx] as a single character
+ buf[:] = ...
+ buf[idx] = ... change the content
+""",
+ )
+MiniBuffer.typedef.acceptable_as_base_class = False
diff --git a/pypy/module/_cffi_backend/ffi_obj.py b/pypy/module/_cffi_backend/ffi_obj.py
--- a/pypy/module/_cffi_backend/ffi_obj.py
+++ b/pypy/module/_cffi_backend/ffi_obj.py
@@ -265,22 +265,6 @@
return self.space.wrap(align)
- @unwrap_spec(w_cdata=W_CData, size=int)
- def descr_buffer(self, w_cdata, size=-1):
- """\
-Return a read-write buffer object that references the raw C data
-ointed to by the given 'cdata'. The 'cdata' must be a pointer or an
-array. Can be passed to functions expecting a buffer, or directly
-manipulated with:
-
- buf[:] get a copy of it in a regular string, or
- buf[idx] as a single character
- buf[:] = ...
- buf[idx] = ... change the content"""
- #
- return cbuffer.buffer(self.space, w_cdata, size)
-
-
@unwrap_spec(w_name=WrappedDefault(None),
w_error=WrappedDefault(None),
w_onerror=WrappedDefault(None))
@@ -751,6 +735,9 @@
return space.appexec([], """():
return type('error', (Exception,), {'__module__': 'ffi'})""")
+def make_buffer(space):
+ return space.gettypefor(cbuffer.MiniBuffer)
+
_extras = get_dict_rtld_constants()
if sys.platform == 'win32':
_extras['getwinerror'] = interp2app(W_FFIObject.descr_getwinerror)
@@ -770,7 +757,7 @@
cls=W_FFIObject),
addressof = interp2app(W_FFIObject.descr_addressof),
alignof = interp2app(W_FFIObject.descr_alignof),
- buffer = interp2app(W_FFIObject.descr_buffer),
+ buffer = ClassAttr(make_buffer),
callback = interp2app(W_FFIObject.descr_callback),
cast = interp2app(W_FFIObject.descr_cast),
def_extern = interp2app(W_FFIObject.descr_def_extern),
diff --git a/pypy/module/_cffi_backend/test/_backend_test_c.py b/pypy/module/_cffi_backend/test/_backend_test_c.py
--- a/pypy/module/_cffi_backend/test/_backend_test_c.py
+++ b/pypy/module/_cffi_backend/test/_backend_test_c.py
@@ -2288,6 +2288,7 @@
buf = buffer(c)
assert repr(buf).startswith('<_cffi_backend.buffer object at 0x')
assert bytes(buf) == b"hi there\x00"
+ assert type(buf) is buffer
if sys.version_info < (3,):
assert str(buf) == "hi there\x00"
assert unicode(buf) == u+"hi there\x00"
diff --git a/pypy/module/_cffi_backend/test/test_ffi_obj.py b/pypy/module/_cffi_backend/test/test_ffi_obj.py
--- a/pypy/module/_cffi_backend/test/test_ffi_obj.py
+++ b/pypy/module/_cffi_backend/test/test_ffi_obj.py
@@ -256,6 +256,8 @@
ffi = _cffi1_backend.FFI()
a = ffi.new("signed char[]", [5, 6, 7])
assert ffi.buffer(a)[:] == '\x05\x06\x07'
+ assert ffi.buffer(cdata=a, size=2)[:] == b'\x05\x06'
+ assert type(ffi.buffer(a)) is ffi.buffer
def test_ffi_from_buffer(self):
import _cffi_backend as _cffi1_backend
diff --git a/pypy/module/test_lib_pypy/cffi_tests/cffi0/backend_tests.py b/pypy/module/test_lib_pypy/cffi_tests/cffi0/backend_tests.py
--- a/pypy/module/test_lib_pypy/cffi_tests/cffi0/backend_tests.py
+++ b/pypy/module/test_lib_pypy/cffi_tests/cffi0/backend_tests.py
@@ -1131,6 +1131,7 @@
b = ffi.buffer(a)
except NotImplementedError as e:
py.test.skip(str(e))
+ assert type(b) is ffi.buffer
content = b[:]
assert len(content) == len(b) == 2
if sys.byteorder == 'little':
diff --git a/pypy/module/test_lib_pypy/cffi_tests/cffi1/test_ffi_obj.py b/pypy/module/test_lib_pypy/cffi_tests/cffi1/test_ffi_obj.py
--- a/pypy/module/test_lib_pypy/cffi_tests/cffi1/test_ffi_obj.py
+++ b/pypy/module/test_lib_pypy/cffi_tests/cffi1/test_ffi_obj.py
@@ -234,6 +234,7 @@
a = ffi.new("signed char[]", [5, 6, 7])
assert ffi.buffer(a)[:] == b'\x05\x06\x07'
assert ffi.buffer(cdata=a, size=2)[:] == b'\x05\x06'
+ assert type(ffi.buffer(a)) is ffi.buffer
def test_ffi_from_buffer():
import array
diff --git a/pypy/module/test_lib_pypy/cffi_tests/cffi1/test_recompiler.py b/pypy/module/test_lib_pypy/cffi_tests/cffi1/test_recompiler.py
--- a/pypy/module/test_lib_pypy/cffi_tests/cffi1/test_recompiler.py
+++ b/pypy/module/test_lib_pypy/cffi_tests/cffi1/test_recompiler.py
@@ -37,6 +37,11 @@
['-Werror'])
return recompiler._verify(ffi, module_name, source, *args, **kwds)
+def test_set_source_no_slashes():
+ ffi = FFI()
+ py.test.raises(ValueError, ffi.set_source, "abc/def", None)
+ py.test.raises(ValueError, ffi.set_source, "abc/def", "C code")
+
def test_type_table_func():
check_type_table("double sin(double);",
More information about the pypy-commit
mailing list