Author: Antonio Cuni <[email protected]>
Branch: 
Changeset: r98374:3bb3cd0a3643
Date: 2019-12-23 19:17 +0100
http://bitbucket.org/pypy/pypy/changeset/3bb3cd0a3643/

Log:    merge this branch to speedup cpyext tests, especially on py3.6

diff --git a/lib-python/2.7/pickle.py b/lib-python/2.7/pickle.py
--- a/lib-python/2.7/pickle.py
+++ b/lib-python/2.7/pickle.py
@@ -1444,5 +1444,14 @@
     import doctest
     return doctest.testmod()
 
+# ===== PyPy modification to support pickling cpyext methods =====
+try:
+    import cpyext
+except ImportError:
+    pass
+else:
+    Pickler.dispatch[cpyext.FunctionType] = Pickler.save_global
+# ================= end of PyPy modification ====================
+
 if __name__ == "__main__":
     _test()
diff --git a/pypy/module/cpyext/moduledef.py b/pypy/module/cpyext/moduledef.py
--- a/pypy/module/cpyext/moduledef.py
+++ b/pypy/module/cpyext/moduledef.py
@@ -6,6 +6,7 @@
     interpleveldefs = {
         'load_module': 'api.load_extension_module',
         'is_cpyext_function': 'interp_cpyext.is_cpyext_function',
+        'FunctionType': 'methodobject.W_PyCFunctionObject',
     }
 
     appleveldefs = {
@@ -15,15 +16,6 @@
 
     def startup(self, space):
         space.fromcache(State).startup(space)
-        method = pypy.module.cpyext.typeobject.get_new_method_def(space)
-        # the w_self argument here is a dummy, the only thing done with w_obj
-        # is call type() on it
-        w_obj = pypy.module.cpyext.methodobject.W_PyCFunctionObject(space,
-                                                           method, 
space.w_None)
-        space.appexec([w_obj], """(meth):
-            from pickle import Pickler
-            Pickler.dispatch[type(meth)] = Pickler.save_global
-        """)
 
     def register_atexit(self, function):
         if len(self.atexit_funcs) >= 32:
diff --git a/pypy/module/cpyext/test/test_cpyext.py 
b/pypy/module/cpyext/test/test_cpyext.py
--- a/pypy/module/cpyext/test/test_cpyext.py
+++ b/pypy/module/cpyext/test/test_cpyext.py
@@ -82,23 +82,6 @@
 def freeze_refcnts(self):
     rawrefcount._dont_free_any_more()
 
-def preload(space, name):
-    from pypy.module.cpyext.pyobject import make_ref
-    if '.' not in name:
-        w_obj = space.builtin.getdictvalue(space, name)
-    else:
-        module, localname = name.rsplit('.', 1)
-        code = "(): import {module}; return {module}.{localname}"
-        code = code.format(**locals())
-        w_obj = space.appexec([], code)
-    make_ref(space, w_obj)
-
-def preload_expr(space, expr):
-    from pypy.module.cpyext.pyobject import make_ref
-    code = "(): return {}".format(expr)
-    w_obj = space.appexec([], code)
-    make_ref(space, w_obj)
-
 def is_interned_string(space, w_obj):
     try:
         s = space.str_w(w_obj)
@@ -148,13 +131,37 @@
         Eagerly create pyobjs for various builtins so they don't look like
         leaks.
         """
-        for name in [
-                'buffer', 'mmap.mmap',
-                'types.FunctionType', 'types.CodeType',
-                'types.TracebackType', 'types.FrameType']:
-            preload(space, name)
-        for expr in ['type(str.join)']:
-            preload_expr(space, expr)
+        from pypy.module.cpyext.pyobject import make_ref
+        w_to_preload = space.appexec([], """():
+            import sys
+            import mmap
+            #
+            # copied&pasted to avoid importing the whole types.py, which is
+            # expensive on py3k
+            # <types.py>
+            def _f(): pass
+            FunctionType = type(_f)
+            CodeType = type(_f.__code__)
+            try:
+                raise TypeError
+            except TypeError:
+                tb = sys.exc_info()[2]
+                TracebackType = type(tb)
+                FrameType = type(tb.tb_frame)
+                del tb
+            # </types.py>
+            return [
+                buffer,
+                mmap.mmap,
+                FunctionType,
+                CodeType,
+                TracebackType,
+                FrameType,
+                type(str.join),
+            ]
+        """)
+        for w_obj in space.unpackiterable(w_to_preload):
+            make_ref(space, w_obj)
 
     def cleanup(self):
         self.space.getexecutioncontext().cleanup_cpyext_state()
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to