Author: Armin Rigo <[email protected]>
Branch: cffi-new-allocator
Changeset: r78450:2094c5d180ef
Date: 2015-07-06 11:19 +0200
http://bitbucket.org/pypy/pypy/changeset/2094c5d180ef/
Log: Kill again newp_allocator()
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
@@ -25,7 +25,6 @@
'new_function_type': 'newtype.new_function_type',
'newp': 'func.newp',
- 'newp_allocator': 'func.newp_allocator',
'cast': 'func.cast',
'callback': 'func.callback',
'alignof': 'func.alignof',
diff --git a/pypy/module/_cffi_backend/allocator.py
b/pypy/module/_cffi_backend/allocator.py
--- a/pypy/module/_cffi_backend/allocator.py
+++ b/pypy/module/_cffi_backend/allocator.py
@@ -54,15 +54,9 @@
@unwrap_spec(w_init=WrappedDefault(None))
def descr_call(self, space, w_arg, w_init):
- from pypy.module._cffi_backend.ctypeobj import W_CType
- if isinstance(w_arg, W_CType):
- w_ctype = w_arg
- else:
- ffi = self.ffi
- if ffi is None:
- raise oefmt(space.w_TypeError,
- "expected a ctype object, got '%T'", w_arg)
- w_ctype = ffi.ffi_type(w_arg, ffi.ACCEPT_STRING)
+ ffi = self.ffi
+ assert ffi is not None
+ w_ctype = ffi.ffi_type(w_arg, ffi.ACCEPT_STRING | ffi.ACCEPT_CTYPE)
return w_ctype.newp(w_init, self)
@@ -73,7 +67,8 @@
W_Allocator.typedef.acceptable_as_base_class = False
-def new_allocator(space, ffi, w_alloc, w_free, should_clear_after_alloc):
+def new_allocator(ffi, w_alloc, w_free, should_clear_after_alloc):
+ space = ffi.space
if space.is_none(w_alloc):
w_alloc = None
if space.is_none(w_free):
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
@@ -437,11 +437,11 @@
If both 'alloc' and 'free' are None, the default is used.
If 'should_clear_after_alloc' is set to False, then the memory
-returned by 'alloc' is assumed to be already cleared; otherwise
-CFFI will clear it.
+returned by 'alloc' is assumed to be already cleared (or you are
+fine with garbage); otherwise CFFI will clear it.
"""
#
- return allocator.new_allocator(self.space, self, w_alloc, w_free,
+ return allocator.new_allocator(self, w_alloc, w_free,
should_clear_after_alloc)
diff --git a/pypy/module/_cffi_backend/func.py
b/pypy/module/_cffi_backend/func.py
--- a/pypy/module/_cffi_backend/func.py
+++ b/pypy/module/_cffi_backend/func.py
@@ -11,13 +11,6 @@
# ____________________________________________________________
-@unwrap_spec(should_clear_after_alloc=int)
-def newp_allocator(space, w_alloc, w_free, should_clear_after_alloc):
- return allocator.new_allocator(space, None, w_alloc, w_free,
- should_clear_after_alloc)
-
-# ____________________________________________________________
-
@unwrap_spec(w_ctype=ctypeobj.W_CType)
def cast(space, w_ctype, w_ob):
return w_ctype.cast(w_ob)
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
@@ -3402,37 +3402,6 @@
py.test.raises(RuntimeError, "p[42]")
py.test.raises(RuntimeError, "p[42] = -1")
-def test_newp_allocator():
- BChar = new_primitive_type("char")
- BCharPtr = new_pointer_type(BChar)
- BCharArray = new_array_type(BCharPtr, None)
- BInt = new_primitive_type("int")
- BIntPtr = new_pointer_type(BInt)
- seen = []
- def myalloc(size):
- seen.append(size)
- return newp(BCharArray, "#" * size)
- def myfree(raw):
- seen.append(raw)
- alloc1 = newp_allocator(None, None, True)
- alloc2 = newp_allocator(myalloc, myfree, False)
- p1 = alloc1(BIntPtr)
- p2 = alloc2(BIntPtr)
- assert typeof(p1) is BIntPtr
- assert typeof(p2) is BIntPtr
- assert p1[0] == 0
- assert p2[0] == ord('#') * 0x01010101
- assert seen == [sizeof(BInt)]
- raw2 = cast(BCharPtr, p2)
- del p1, p2
- retries = 0
- while len(seen) != 2:
- retries += 1
- assert retries <= 5
- import gc; gc.collect()
- assert seen == [sizeof(BInt), raw2]
- assert repr(seen[1]) == "<cdata 'char[]' owning 5 bytes>"
-
def test_version():
# this test is here mostly for PyPy
assert __version__ == "1.2.0"
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
@@ -302,7 +302,7 @@
seen.append(size)
return ffi.new("char[]", "X" * size)
def myfree(raw):
- seen.append('free')
+ seen.append(raw)
alloc1 = ffi.new_allocator(myalloc, myfree)
alloc2 = ffi.new_allocator(alloc=myalloc, free=myfree,
should_clear_after_alloc=False)
@@ -315,13 +315,17 @@
assert ffi.sizeof(p2) == 40
assert p1[5] == 0
assert p2[6] == ord('X') * 0x01010101
+ raw1 = ffi.cast("char *", p1)
+ raw2 = ffi.cast("char *", p2)
del p1, p2
retries = 0
while len(seen) != 4:
retries += 1
assert retries <= 5
import gc; gc.collect()
- assert seen == [40, 40, 'free', 'free']
+ assert seen == [40, 40, raw1, raw2]
+ assert repr(seen[2]) == "<cdata 'char[]' owning 41 bytes>"
+ assert repr(seen[3]) == "<cdata 'char[]' owning 41 bytes>"
def test_ffi_new_allocator_3(self):
import _cffi_backend as _cffi1_backend
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit