Author: Armin Rigo <ar...@tunes.org>
Branch: cffi-1.0
Changeset: r77199:a530bbeca295
Date: 2015-05-08 10:35 +0200
http://bitbucket.org/pypy/pypy/changeset/a530bbeca295/

Log:    repr, dir on Lib

diff --git a/pypy/module/_cffi_backend/lib_obj.py 
b/pypy/module/_cffi_backend/lib_obj.py
--- a/pypy/module/_cffi_backend/lib_obj.py
+++ b/pypy/module/_cffi_backend/lib_obj.py
@@ -23,7 +23,7 @@
         self.includes = []        # list of W_LibObjects included here
 
     def descr_repr(self):
-        XXX
+        return self.space.wrap("<Lib object for '%s'>" % self.libname)
 
     @jit.elidable_promote()
     def _get_attr_elidable(self, attr):
@@ -117,6 +117,14 @@
         raise oefmt(self.space.w_AttributeError,
                     "C attribute cannot be deleted")
 
+    def descr_dir(self):
+        space = self.space
+        total = rffi.getintfield(self.ctx, 'c_num_globals')
+        g = self.ctx.c_globals
+        names_w = [space.wrap(rffi.charp2str(g[i].c_name))
+                   for i in range(total)]
+        return space.newlist(names_w)
+
 
 W_LibObject.typedef = TypeDef(
         'CompiledLib',
@@ -124,5 +132,6 @@
         __getattribute__ = interp2app(W_LibObject.descr_getattribute),
         __setattr__ = interp2app(W_LibObject.descr_setattr),
         __delattr__ = interp2app(W_LibObject.descr_delattr),
+        __dir__ = interp2app(W_LibObject.descr_dir),
         )
 W_LibObject.typedef.acceptable_as_base_class = False
diff --git a/pypy/module/_cffi_backend/test/test_recompiler.py 
b/pypy/module/_cffi_backend/test/test_recompiler.py
--- a/pypy/module/_cffi_backend/test/test_recompiler.py
+++ b/pypy/module/_cffi_backend/test/test_recompiler.py
@@ -16,6 +16,7 @@
     space.appexec([], """():
         import _cffi_backend     # force it to be initialized
     """)
+    assert module_name.startswith('test_')
     module_name = '_CFFI_' + module_name
     rdir = udir.ensure('recompiler', dir=1)
     rdir.join('Python.h').write(
@@ -72,6 +73,13 @@
             '#include <math.h>')
         assert lib.cos(1.43) == math.cos(1.43)
 
+    def test_repr_lib(self):
+        ffi, lib = self.prepare(
+            "",
+            'test_repr_lib',
+            "")
+        assert repr(lib) == "<Lib object for '_CFFI_test_repr_lib'>"
+
     def test_funcarg_ptr(self):
         ffi, lib = self.prepare(
             "int foo(int *);",
@@ -208,10 +216,10 @@
         assert lib.FOOBAR == ffi.NULL
         assert ffi.typeof(lib.FOOBAR) == ffi.typeof("double *")
 
-    def test_dir():
-        ffi = FFI()
-        ffi.cdef("int ff(int); int aa; static const int my_constant;")
-        lib = verify(ffi, 'test_dir', """
+    def test_dir(self):
+        ffi, lib = self.prepare(
+            "int ff(int); int aa; static const int my_constant;",
+            'test_dir', """
             #define my_constant  (-45)
             int aa;
             int ff(int x) { return x+aa; }
@@ -219,25 +227,27 @@
         lib.aa = 5
         assert dir(lib) == ['aa', 'ff', 'my_constant']
 
-    def test_verify_opaque_struct():
-        ffi = FFI()
-        ffi.cdef("struct foo_s;")
-        lib = verify(ffi, 'test_verify_opaque_struct', "struct foo_s;")
+    def test_verify_opaque_struct(self):
+        ffi, lib = self.prepare(
+            "struct foo_s;",
+            'test_verify_opaque_struct',
+            "struct foo_s;")
         assert ffi.typeof("struct foo_s").cname == "struct foo_s"
 
-    def test_verify_opaque_union():
-        ffi = FFI()
-        ffi.cdef("union foo_s;")
-        lib = verify(ffi, 'test_verify_opaque_union', "union foo_s;")
+    def test_verify_opaque_union(self):
+        ffi, lib = self.prepare(
+            "union foo_s;",
+            'test_verify_opaque_union',
+            "union foo_s;")
         assert ffi.typeof("union foo_s").cname == "union foo_s"
 
-    def test_verify_struct():
-        ffi = FFI()
-        ffi.cdef("""struct foo_s { int b; short a; ...; };
-                    struct bar_s { struct foo_s *f; };""")
-        lib = verify(ffi, 'test_verify_struct',
-                     """struct foo_s { short a; int b; };
-                        struct bar_s { struct foo_s *f; };""")
+    def test_verify_struct(self):
+        ffi, lib = self.prepare(
+            """struct foo_s { int b; short a; ...; };
+               struct bar_s { struct foo_s *f; };""",
+            'test_verify_struct',
+            """struct foo_s { short a; int b; };
+               struct bar_s { struct foo_s *f; };""")
         ffi.typeof("struct bar_s *")
         p = ffi.new("struct foo_s *", {'a': -32768, 'b': -2147483648})
         assert p.a == -32768
@@ -256,11 +266,11 @@
         assert ffi.typeof(ffi.addressof(p, "b")) is ffi.typeof("int *")
         assert ffi.addressof(p, "b")[0] == p.b
 
-    def test_verify_exact_field_offset():
-        ffi = FFI()
-        ffi.cdef("""struct foo_s { int b; short a; };""")
-        lib = verify(ffi, 'test_verify_exact_field_offset',
-                     """struct foo_s { short a; int b; };""")
+    def test_verify_exact_field_offset(self):
+        ffi, lib = self.prepare(
+            """struct foo_s { int b; short a; };""",
+            'test_verify_exact_field_offset',
+            """struct foo_s { short a; int b; };""")
         e = raises(ffi.error, ffi.new, "struct foo_s *", [])    # lazily
         assert str(e.value) == ("struct foo_s: wrong offset for field 'b' 
(cdef "
                            'says 0, but C compiler says 4). fix it or use 
"...;" '
_______________________________________________
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to