[pypy-commit] cffi default: Haaaack! Have 'lib.__class__' return &PyModule_Type. It makes

arigo pypy.commits at gmail.com
Wed May 25 12:01:39 EDT 2016


Author: Armin Rigo <arigo at tunes.org>
Branch: 
Changeset: r2699:f6d4b0a1e70b
Date: 2016-05-25 18:02 +0200
http://bitbucket.org/cffi/cffi/changeset/f6d4b0a1e70b/

Log:	Haaaack! Have 'lib.__class__' return &PyModule_Type. It makes
	help(lib) behave and display a nice module-like view of your
	compiled lib.

	Note that this is seriously unexpected, but I hope nobody should get
	hit by it by looking at 'lib.__class__' directly... Might be
	reverted if I hear about someone :-)

diff --git a/c/lib_obj.c b/c/lib_obj.c
--- a/c/lib_obj.c
+++ b/c/lib_obj.c
@@ -516,14 +516,18 @@
     }
     if (strcmp(p, "__class__") == 0) {
         PyErr_Clear();
-        x = (PyObject *)Py_TYPE(lib);
+        x = (PyObject *)&PyModule_Type;
+        /* ^^^ used to be Py_TYPE(lib).  But HAAAAAACK!  That makes
+           help() behave correctly.  I couldn't find a more reasonable
+           way.  Urgh. */
         Py_INCREF(x);
         return x;
     }
-    /* this hack is for Python 3.5 */
+    /* this hack is for Python 3.5, and also to give a more 
+       module-like behavior */
     if (strcmp(p, "__name__") == 0) {
         PyErr_Clear();
-        return lib_repr(lib);
+        return PyText_FromFormat("%s.lib", PyText_AS_UTF8(lib->l_libname));
     }
     return NULL;
 }
diff --git a/testing/cffi1/test_recompiler.py b/testing/cffi1/test_recompiler.py
--- a/testing/cffi1/test_recompiler.py
+++ b/testing/cffi1/test_recompiler.py
@@ -1167,8 +1167,8 @@
     assert MYFOO == 42
     assert hasattr(lib, '__dict__')
     assert lib.__all__ == ['MYFOO', 'mybar']   # but not 'myvar'
-    assert lib.__name__ == repr(lib)
-    assert lib.__class__ is type(lib)
+    assert lib.__name__ == '_CFFI_test_import_from_lib.lib'
+    assert lib.__class__ is type(sys)   # !! hack for help()
 
 def test_macro_var_callback():
     ffi = FFI()


More information about the pypy-commit mailing list