Author: Amaury Forgeot d'Arc <[email protected]>
Branch: more-rposix
Changeset: r74843:9af5b8184a71
Date: 2014-12-05 19:52 +0100
http://bitbucket.org/pypy/pypy/changeset/9af5b8184a71/

Log:    Found a much better way to register a replacement function.

diff --git a/rpython/flowspace/operation.py b/rpython/flowspace/operation.py
--- a/rpython/flowspace/operation.py
+++ b/rpython/flowspace/operation.py
@@ -12,10 +12,11 @@
 from rpython.tool.sourcetools import compile2
 from rpython.flowspace.model import (Constant, WrapException, const, Variable,
                                      SpaceOperation)
-from rpython.flowspace.specialcase import register_flow_sc, get_specialcase
+from rpython.flowspace.specialcase import register_flow_sc
 from rpython.annotator.model import (
     SomeTuple, AnnotatorError, read_can_only_throw)
 from rpython.annotator.argument import ArgumentsForTranslation
+from rpython.flowspace.specialcase import SPECIAL_CASES
 
 
 NOT_REALLY_CONST = {
@@ -569,8 +570,11 @@
         w_callable, args_w = self.args[0], self.args[1:]
         if isinstance(w_callable, Constant):
             fn = w_callable.value
-            sc = get_specialcase(fn)
-            if sc:
+            try:
+                sc = SPECIAL_CASES[fn]   # TypeError if 'fn' not hashable
+            except (KeyError, TypeError):
+                pass
+            else:
                 return sc(ctx, *args_w)
         return ctx.do_op(self)
 
@@ -585,8 +589,11 @@
         w_callable = self.args[0]
         if isinstance(w_callable, Constant):
             fn = w_callable.value
-            sc = get_specialcase(fn)
-            if sc:
+            try:
+                sc = SPECIAL_CASES[fn]   # TypeError if 'fn' not hashable
+            except (KeyError, TypeError):
+                pass
+            else:
                 from rpython.flowspace.flowcontext import FlowingError
                 raise FlowingError(
                     "should not call %r with keyword arguments" % (fn,))
diff --git a/rpython/flowspace/specialcase.py b/rpython/flowspace/specialcase.py
--- a/rpython/flowspace/specialcase.py
+++ b/rpython/flowspace/specialcase.py
@@ -54,26 +54,6 @@
         from rpython.flowspace.operation import op
         return op.getattr(w_obj, w_index).eval(ctx)
 
-def get_specialcase(fn):
-    try:
-        return SPECIAL_CASES[fn]   # TypeError if 'fn' not hashable
-    except (KeyError, TypeError):
-        # Try to import modules containing special cases
-        for modname in SPECIAL_MODULES.get(getattr(fn, '__module__', None), 
[]):
-            __import__(modname)
-        try:
-            return SPECIAL_CASES[fn]
-        except (KeyError, TypeError):
-            pass
-    return None
-
-SPECIAL_MODULES = {
-    # Modules with functions registered with @register_flow_sc, and
-    # which cannot be imported when before the flow object space
-    # (because of import loops).
-    'posix': ['rpython.rlib.rposix'],
-}
-
 # _________________________________________________________________________
 
 redirect_function(open,       'rpython.rlib.rfile.create_file')
diff --git a/rpython/rlib/objectmodel.py b/rpython/rlib/objectmodel.py
--- a/rpython/rlib/objectmodel.py
+++ b/rpython/rlib/objectmodel.py
@@ -290,15 +290,14 @@
 def sc_we_are_translated(ctx):
     return Constant(True)
 
+def register_replacement_for(replaced_function, sandboxed_name=None):
+    def wrap(func):
+        from rpython.rtyper.extregistry import ExtRegistryEntry
+        class ExtRegistry(ExtRegistryEntry):
+            _about_ = replaced_function
+            def compute_annotation(self):
+                return self.bookkeeper.immutablevalue(func)
 
-def register_replacement_for(replaced_function, sandboxed_name=None):
-    """Decorator that causes RPython to replace the function passed as 
parameter
-    with the function being defined."""
-    def wrap(func):
-        if replaced_function is not None:
-            @register_flow_sc(replaced_function)
-            def sc_redirected_function(ctx, *args_w):
-                return ctx.appcall(func, *args_w)
         if sandboxed_name:
             func._sandbox_external_name = sandboxed_name
             # XXX THIS IS NOT CORRECT. Only do this when config.sandbox.
@@ -306,7 +305,6 @@
         return func
     return wrap
 
-
 def keepalive_until_here(*values):
     pass
 
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to