Debian Codegraph @

rpython/flowspace/specialcase.py

raw symbols SCIP
1 import os
2 from rpython.flowspace.model import Constant
3
4 SPECIAL_CASES = {}
5
6 def register_flow_sc(func):
7 """Decorator triggering special-case handling of ``func``.
8
9 When the flow graph builder sees ``func``, it calls the decorated function
10 with ``decorated_func(ctx, *args_w)``, where ``args_w`` is a sequence of
11 flow objects (Constants or Variables).
12 """
13 def decorate(sc_func):
14 SPECIAL_CASES[func] = sc_func
15 return decorate
16
17 def redirect_function(srcfunc, dstfuncname):
18 @register_flow_sc(srcfunc)
19 def sc_redirected_function(ctx, *args_w):
20 components = dstfuncname.split('.')
21 obj = __import__('.'.join(components[:-1]))
22 for name in components[1:]:
23 obj = getattr(obj, name)
24 return ctx.appcall(obj, *args_w)
25
26
27 @register_flow_sc(__import__)
28 def sc_import(ctx, *args_w):
29 assert all(isinstance(arg, Constant) for arg in args_w)
30 args = [arg.value for arg in args_w]
31 return ctx.import_name(*args)
32
33 @register_flow_sc(locals)
34 def sc_locals(_, *args):
35 raise Exception(
36 "A function calling locals() is not RPython. "
37 "Note that if you're translating code outside the PyPy "
38 "repository, a likely cause is that py.test's --assert=rewrite "
39 "mode is getting in the way. You should copy the file "
40 "pytest.ini from the root of the PyPy repository into your "
41 "own project.")
42
43 @register_flow_sc(getattr)
44 def sc_getattr(ctx, w_obj, w_index, w_default=None):
45 if w_default is not None:
46 return ctx.appcall(getattr, w_obj, w_index, w_default)
47 else:
48 from rpython.flowspace.operation import op
49 return op.getattr(w_obj, w_index).eval(ctx)
50
51 # _________________________________________________________________________
52
53 redirect_function(open, 'rpython.rlib.rfile.create_file')
54 redirect_function(os.fdopen, 'rpython.rlib.rfile.create_fdopen_rfile')
55 redirect_function(os.tmpfile, 'rpython.rlib.rfile.create_temp_rfile')
56
57 # on top of PyPy only: 'os.remove != os.unlink'
58 # (on CPython they are '==', but not identical either)
59 redirect_function(os.remove, 'os.unlink')
60
61 redirect_function(os.path.isdir, 'rpython.rlib.rpath.risdir')
62 redirect_function(os.path.isabs, 'rpython.rlib.rpath.risabs')
63 redirect_function(os.path.normpath,'rpython.rlib.rpath.rnormpath')
64 redirect_function(os.path.abspath, 'rpython.rlib.rpath.rabspath')
65 redirect_function(os.path.join, 'rpython.rlib.rpath.rjoin')
66 if hasattr(os.path, 'splitdrive'):
67 redirect_function(os.path.splitdrive, 'rpython.rlib.rpath.rsplitdrive')
68
69 # _________________________________________________________________________
70 # a simplified version of the basic printing routines, for RPython programs
71 class StdOutBuffer:
72 linebuf = []
73 final = True
74 stdoutbuffer = StdOutBuffer()
75
76 def rpython_print_item(s):
77 buf = stdoutbuffer.linebuf
78 if not stdoutbuffer.final:
79 buf.append(' ')
80 for c in s:
81 buf.append(c)
82 stdoutbuffer.final = False
83 rpython_print_item._annenforceargs_ = (str,)
84
85 def rpython_print_end(s):
86 buf = stdoutbuffer.linebuf
87 stdoutbuffer.final = True
88 for c in s:
89 buf.append(c)
90 if c == '\n':
91 w = ''.join(buf)
92 del buf[:]
93 os.write(1, w)
94
95 def rpython_print_newline():
96 rpython_print_end('\n')