Author: Ronan Lamy <ronan.l...@gmail.com> Branch: less-stringly-ops Changeset: r66227:2e539dd83216 Date: 2013-08-08 21:14 +0100 http://bitbucket.org/pypy/pypy/changeset/2e539dd83216/
Log: Create a decorator to register flowspace special-cases diff --git a/rpython/flowspace/specialcase.py b/rpython/flowspace/specialcase.py --- a/rpython/flowspace/specialcase.py +++ b/rpython/flowspace/specialcase.py @@ -1,8 +1,22 @@ from rpython.flowspace.model import Constant -from rpython.flowspace.operation import func2op, op +from rpython.flowspace.operation import func2op from rpython.rlib.rarithmetic import r_uint from rpython.rlib.objectmodel import we_are_translated +SPECIAL_CASES = {} + +def register_flow_sc(func): + """Decorator triggering special-case handling of ``func``. + + When the flow graph builder sees ``func``, it calls the decorated function + with ``decorated_func(space, *args_w)``, where ``args_w`` is a sequence of + flow objects (Constants or Variables). + """ + def decorate(sc_func): + SPECIAL_CASES[func] = sc_func + return decorate + +@register_flow_sc(__import__) def sc_import(space, args_w): assert len(args_w) > 0 and len(args_w) <= 5, 'import needs 1 to 5 arguments' args = [space.unwrap(arg) for arg in args_w] @@ -33,6 +47,7 @@ # _________________________________________________________________________ +@register_flow_sc(r_uint) def sc_r_uint(space, args_w): # special case to constant-fold r_uint(32-bit-constant) # (normally, the 32-bit constant is a long, and is not allowed to @@ -42,9 +57,11 @@ return Constant(r_uint(w_value.value)) return space.frame.do_operation('simple_call', space.wrap(r_uint), w_value) +@register_flow_sc(we_are_translated) def sc_we_are_translated(space, args_w): return Constant(True) +@register_flow_sc(locals) def sc_locals(space, args): raise Exception( "A function calling locals() is not RPython. " @@ -54,8 +71,5 @@ "pytest.ini from the root of the PyPy repository into your " "own project.") -SPECIAL_CASES = {__import__: sc_import, r_uint: sc_r_uint, - we_are_translated: sc_we_are_translated, - locals: sc_locals} for fn, oper in func2op.items(): - SPECIAL_CASES[fn] = oper.make_sc() + register_flow_sc(fn)(oper.make_sc()) _______________________________________________ pypy-commit mailing list pypy-commit@python.org http://mail.python.org/mailman/listinfo/pypy-commit