[pypy-commit] cffi default: Fix: the code incorrectly accepted e.g. 'ffi.new(ffi.new("int*"))',
arigo
noreply at buildbot.pypy.org
Fri Jul 27 18:55:25 CEST 2012
Author: Armin Rigo <arigo at tunes.org>
Branch:
Changeset: r730:93b68a44b94d
Date: 2012-07-27 18:54 +0200
http://bitbucket.org/cffi/cffi/changeset/93b68a44b94d/
Log: Fix: the code incorrectly accepted e.g. 'ffi.new(ffi.new("int*"))',
by taking the type of the inner cdata object.
diff --git a/cffi/api.py b/cffi/api.py
--- a/cffi/api.py
+++ b/cffi/api.py
@@ -94,23 +94,27 @@
self._function_caches.append(function_cache)
return lib
- def typeof(self, cdecl, consider_function_as_funcptr=False):
+ def _typeof(self, cdecl, consider_function_as_funcptr=False):
+ # string -> ctype object
+ try:
+ btype, cfaf = self._parsed_types[cdecl]
+ if consider_function_as_funcptr and not cfaf:
+ raise KeyError
+ except KeyError:
+ cfaf = consider_function_as_funcptr
+ type = self._parser.parse_type(cdecl,
+ consider_function_as_funcptr=cfaf)
+ btype = self._get_cached_btype(type)
+ self._parsed_types[cdecl] = btype, cfaf
+ return btype
+
+ def typeof(self, cdecl):
"""Parse the C type given as a string and return the
corresponding Python type: <class 'ffi.CData<...>'>.
It can also be used on 'cdata' instance to get its C type.
"""
if isinstance(cdecl, basestring):
- try:
- btype, cfaf = self._parsed_types[cdecl]
- if consider_function_as_funcptr and not cfaf:
- raise KeyError
- except KeyError:
- cfaf = consider_function_as_funcptr
- type = self._parser.parse_type(cdecl,
- consider_function_as_funcptr=cfaf)
- btype = self._get_cached_btype(type)
- self._parsed_types[cdecl] = btype, cfaf
- return btype
+ return self._typeof(cdecl)
else:
return self._backend.typeof(cdecl)
@@ -119,7 +123,7 @@
string naming a C type, or a 'cdata' instance.
"""
if isinstance(cdecl, basestring):
- BType = self.typeof(cdecl)
+ BType = self._typeof(cdecl)
return self._backend.sizeof(BType)
else:
return self._backend.sizeof(cdecl)
@@ -129,7 +133,7 @@
given as a string.
"""
if isinstance(cdecl, basestring):
- cdecl = self.typeof(cdecl)
+ cdecl = self._typeof(cdecl)
return self._backend.alignof(cdecl)
def offsetof(self, cdecl, fieldname):
@@ -137,7 +141,7 @@
structure, which must be given as a C type name.
"""
if isinstance(cdecl, basestring):
- cdecl = self.typeof(cdecl)
+ cdecl = self._typeof(cdecl)
return self._backend.offsetof(cdecl, fieldname)
def new(self, cdecl, init=None):
@@ -163,16 +167,18 @@
about that when copying the pointer to the memory somewhere
else, e.g. into another structure.
"""
- BType = self.typeof(cdecl)
- return self._backend.newp(BType, init)
+ if isinstance(cdecl, basestring):
+ cdecl = self._typeof(cdecl)
+ return self._backend.newp(cdecl, init)
def cast(self, cdecl, source):
"""Similar to a C cast: returns an instance of the named C
type initialized with the given 'source'. The source is
casted between integers or pointers of any type.
"""
- BType = self.typeof(cdecl)
- return self._backend.cast(BType, source)
+ if isinstance(cdecl, basestring):
+ cdecl = self._typeof(cdecl)
+ return self._backend.cast(cdecl, source)
def buffer(self, cdata, size=-1):
"""Return a read-write buffer object that references the raw C data
@@ -190,8 +196,9 @@
"""
if not callable(python_callable):
raise TypeError("the 'python_callable' argument is not callable")
- BFunc = self.typeof(cdecl, consider_function_as_funcptr=True)
- return self._backend.callback(BFunc, python_callable, error)
+ if isinstance(cdecl, basestring):
+ cdecl = self._typeof(cdecl, consider_function_as_funcptr=True)
+ return self._backend.callback(cdecl, python_callable, error)
def getctype(self, cdecl, replace_with=''):
"""Return a string giving the C type 'cdecl', which may be itself
@@ -200,7 +207,7 @@
a variable name, or '*' to get actually the C type 'pointer-to-cdecl'.
"""
if isinstance(cdecl, basestring):
- cdecl = self.typeof(cdecl)
+ cdecl = self._typeof(cdecl)
replace_with = replace_with.strip()
if (replace_with.startswith('*')
and '&[' in self._backend.getcname(cdecl, '&')):
diff --git a/cffi/backend_ctypes.py b/cffi/backend_ctypes.py
--- a/cffi/backend_ctypes.py
+++ b/cffi/backend_ctypes.py
@@ -904,6 +904,8 @@
return BType._offsetof(fieldname)
def newp(self, BType, source):
+ if not issubclass(BType, CTypesData):
+ raise TypeError
return BType._newp(source)
def cast(self, BType, source):
diff --git a/testing/backend_tests.py b/testing/backend_tests.py
--- a/testing/backend_tests.py
+++ b/testing/backend_tests.py
@@ -1187,3 +1187,10 @@
assert ffi.getctype("e*") == 'enum $1 *'
assert ffi.getctype("pe") == 'enum $1 *'
assert ffi.getctype("e1*") == 'enum $2 *'
+
+ def test_new_ctype(self):
+ ffi = FFI(backend=self.Backend())
+ p = ffi.new("int *")
+ py.test.raises(TypeError, ffi.new, p)
+ p = ffi.new(ffi.typeof("int *"), 42)
+ assert p[0] == 42
More information about the pypy-commit
mailing list