Author: Armin Rigo <ar...@tunes.org> Branch: Changeset: r1256:831a3f4af696 Date: 2013-05-30 09:27 +0200 http://bitbucket.org/cffi/cffi/changeset/831a3f4af696/
Log: Issue #81 resolved: dir(ffi.verify(...)) diff --git a/cffi/vengine_cpy.py b/cffi/vengine_cpy.py --- a/cffi/vengine_cpy.py +++ b/cffi/vengine_cpy.py @@ -156,6 +156,9 @@ 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 @@ 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 --- a/cffi/vengine_gen.py +++ b/cffi/vengine_gen.py @@ -74,6 +74,9 @@ 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 @@ 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 @@ 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 @@ 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 @@ 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 @@ 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 @@ 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 --- a/testing/test_verify.py +++ b/testing/test_verify.py @@ -1619,3 +1619,18 @@ 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'] _______________________________________________ pypy-commit mailing list pypy-commit@python.org http://mail.python.org/mailman/listinfo/pypy-commit