summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorArmin Rigo <arigo@tunes.org>2013-05-30 09:27:56 +0200
committerArmin Rigo <arigo@tunes.org>2013-05-30 09:27:56 +0200
commite8adc51142e255cdc8a45063fd8d6f2be803739e (patch)
tree75e883b4aa338b6fb7aefef5d90db377178bd063
parentc0bc61a7fdc45f04e8e427700f0f5b1495bf2442 (diff)
downloadcffi-e8adc51142e255cdc8a45063fd8d6f2be803739e.tar.gz
Issue #81 resolved: dir(ffi.verify(...))
-rw-r--r--cffi/vengine_cpy.py6
-rw-r--r--cffi/vengine_gen.py27
-rw-r--r--testing/test_verify.py15
3 files changed, 38 insertions, 10 deletions
diff --git a/cffi/vengine_cpy.py b/cffi/vengine_cpy.py
index d866c87..e2dd16b 100644
--- a/cffi/vengine_cpy.py
+++ b/cffi/vengine_cpy.py
@@ -156,6 +156,9 @@ class VCPythonEngine(object):
class FFILibrary(object):
_cffi_python_module = module
_cffi_ffi = self.ffi
+ _cffi_dir = []
+ def __dir__(self):
+ return FFILibrary._cffi_dir + list(self.__dict__)
library = FFILibrary()
module._cffi_setup(lst, ffiplatform.VerificationError, library)
#
@@ -701,7 +704,8 @@ class VCPythonEngine(object):
return ptr[0]
def setter(library, value):
ptr[0] = value
- setattr(library.__class__, name, property(getter, setter))
+ setattr(type(library), name, property(getter, setter))
+ type(library)._cffi_dir.append(name)
# ----------
diff --git a/cffi/vengine_gen.py b/cffi/vengine_gen.py
index cbcdc88..43ca807 100644
--- a/cffi/vengine_gen.py
+++ b/cffi/vengine_gen.py
@@ -74,6 +74,9 @@ class VGenericEngine(object):
class FFILibrary(types.ModuleType):
_cffi_generic_module = module
_cffi_ffi = self.ffi
+ _cffi_dir = []
+ def __dir__(self):
+ return FFILibrary._cffi_dir
library = FFILibrary("")
#
# finally, call the loaded_gen_xxx() functions. This will set
@@ -168,21 +171,22 @@ class VGenericEngine(object):
newfunction = self._load_constant(False, tp, name, module)
else:
indirections = []
- if any(isinstance(type, model.StructOrUnion) for type in tp.args):
+ if any(isinstance(typ, model.StructOrUnion) for typ in tp.args):
indirect_args = []
- for i, type in enumerate(tp.args):
- if isinstance(type, model.StructOrUnion):
- type = model.PointerType(type)
- indirections.append((i, type))
- indirect_args.append(type)
+ for i, typ in enumerate(tp.args):
+ if isinstance(typ, model.StructOrUnion):
+ typ = model.PointerType(typ)
+ indirections.append((i, typ))
+ indirect_args.append(typ)
tp = model.FunctionPtrType(tuple(indirect_args),
tp.result, tp.ellipsis)
BFunc = self.ffi._get_cached_btype(tp)
wrappername = '_cffi_f_%s' % name
newfunction = module.load_function(BFunc, wrappername)
- for i, type in indirections:
- newfunction = self._make_struct_wrapper(newfunction, i, type)
+ for i, typ in indirections:
+ newfunction = self._make_struct_wrapper(newfunction, i, typ)
setattr(library, name, newfunction)
+ type(library)._cffi_dir.append(name)
def _make_struct_wrapper(self, oldfunc, i, tp):
backend = self.ffi._backend
@@ -390,6 +394,7 @@ class VGenericEngine(object):
is_int = isinstance(tp, model.PrimitiveType) and tp.is_integer_type()
value = self._load_constant(is_int, tp, name, module)
setattr(library, name, value)
+ type(library)._cffi_dir.append(name)
# ----------
# enums
@@ -437,6 +442,7 @@ class VGenericEngine(object):
def _loaded_gen_enum(self, tp, name, module, library):
for enumerator, enumvalue in zip(tp.enumerators, tp.enumvalues):
setattr(library, enumerator, enumvalue)
+ type(library)._cffi_dir.append(enumerator)
# ----------
# macros: for now only for integers
@@ -450,6 +456,7 @@ class VGenericEngine(object):
def _loaded_gen_macro(self, tp, name, module, library):
value = self._load_constant(True, tp, name, module)
setattr(library, name, value)
+ type(library)._cffi_dir.append(name)
# ----------
# global variables
@@ -475,6 +482,7 @@ class VGenericEngine(object):
BArray = self.ffi._get_cached_btype(tp)
value = self.ffi.cast(BArray, value)
setattr(library, name, value)
+ type(library)._cffi_dir.append(name)
return
# remove ptr=<cdata 'int *'> from the library instance, and replace
# it by a property on the class, which reads/writes into ptr[0].
@@ -486,7 +494,8 @@ class VGenericEngine(object):
return ptr[0]
def setter(library, value):
ptr[0] = value
- setattr(library.__class__, name, property(getter, setter))
+ setattr(type(library), name, property(getter, setter))
+ type(library)._cffi_dir.append(name)
cffimod_header = r'''
#include <stdio.h>
diff --git a/testing/test_verify.py b/testing/test_verify.py
index 712744e..25bdd14 100644
--- a/testing/test_verify.py
+++ b/testing/test_verify.py
@@ -1619,3 +1619,18 @@ def test_call_with_voidstar_arg():
ffi.cdef("int f(void *);")
lib = ffi.verify("int f(void *x) { return ((char*)x)[0]; }")
assert lib.f(b"foobar") == ord(b"f")
+
+def test_dir():
+ ffi = FFI()
+ ffi.cdef("""void somefunc(void);
+ extern int somevar, somearray[2];
+ static char *const sv2;
+ enum my_e { AA, BB, ... };
+ #define FOO ...""")
+ lib = ffi.verify("""void somefunc(void) { }
+ int somevar, somearray[2];
+ #define sv2 "text"
+ enum my_e { AA, BB };
+ #define FOO 42""")
+ assert dir(lib) == ['AA', 'BB', 'FOO', 'somearray',
+ 'somefunc', 'somevar', 'sv2']