Commits

Armin Rigo committed 184ddf1

Fix for issue 77: give cdatas __module__, __name__ and __doc__ attributes,
for convenience with functools.wraps().

  • Participants
  • Parent commits 2a392bc

Comments (0)

Files changed (4)

c/_cffi_backend.c

     if (PyType_Ready(&MiniBuffer_Type) < 0)
         INITERROR;
 
+    v = PyString_FromString("_cffi_backend");
+    if (v == NULL || PyDict_SetItemString(CData_Type.tp_dict,
+                                          "__module__", v) < 0)
+        INITERROR;
+    v = PyString_FromString("<cdata>");
+    if (v == NULL || PyDict_SetItemString(CData_Type.tp_dict,
+                                          "__name__", v) < 0)
+        INITERROR;
+
     v = PyCapsule_New((void *)cffi_exports, "cffi", NULL);
     if (v == NULL || PyModule_AddObject(m, "_C_API", v) < 0)
         INITERROR;
     c[1:3] = d
     assert list(c) == [0, 40, 50, 30, 0]
 
+def test_cdata_name_module_doc():
+    p = new_primitive_type("signed char")
+    x = cast(p, 17)
+    assert x.__module__ == '_cffi_backend'
+    assert x.__name__ == '<cdata>'
+    assert hasattr(x, '__doc__')
+
 def test_version():
     # this test is here mostly for PyPy
     assert __version__ == "0.6"

cffi/backend_ctypes.py

 class CTypesData(object):
     __metaclass__ = CTypesType
     __slots__ = ['__weakref__']
+    __name__ = '<cdata>'
 
     def __init__(self, *args):
         raise TypeError("cannot instantiate %r" % (self.__class__,))

testing/test_function.py

         """)
         m = ffi.dlopen("m")
         assert not hasattr(m, 'nonexistent')
+
+    def test_wraps_from_stdlib(self):
+        import functools
+        ffi = FFI(backend=self.Backend())
+        ffi.cdef("""
+            double sin(double x);
+        """)
+        def my_decorator(f):
+            @functools.wraps(f)
+            def wrapper(*args):
+                return f(*args) + 100
+            return wrapper
+        m = ffi.dlopen("m")
+        sin100 = my_decorator(m.sin)
+        x = sin100(1.23)
+        assert x == math.sin(1.23) + 100