Author: Armin Rigo <[email protected]>
Branch:
Changeset: r1245:184ddf196151
Date: 2013-04-15 19:13 +0200
http://bitbucket.org/cffi/cffi/changeset/184ddf196151/
Log: Fix for issue 77: give cdatas __module__, __name__ and __doc__
attributes, for convenience with functools.wraps().
diff --git a/c/_cffi_backend.c b/c/_cffi_backend.c
--- a/c/_cffi_backend.c
+++ b/c/_cffi_backend.c
@@ -5096,6 +5096,15 @@
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;
diff --git a/c/test_c.py b/c/test_c.py
--- a/c/test_c.py
+++ b/c/test_c.py
@@ -2723,6 +2723,13 @@
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"
diff --git a/cffi/backend_ctypes.py b/cffi/backend_ctypes.py
--- a/cffi/backend_ctypes.py
+++ b/cffi/backend_ctypes.py
@@ -16,6 +16,7 @@
class CTypesData(object):
__metaclass__ = CTypesType
__slots__ = ['__weakref__']
+ __name__ = '<cdata>'
def __init__(self, *args):
raise TypeError("cannot instantiate %r" % (self.__class__,))
diff --git a/testing/test_function.py b/testing/test_function.py
--- a/testing/test_function.py
+++ b/testing/test_function.py
@@ -365,3 +365,19 @@
""")
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
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit