Debian Codegraph @

pypy/tool/pytest/objspace.py

raw symbols SCIP
1 import py
2 import sys
3 from rpython.config.config import ConflictConfigError
4 from pypy.tool.option import make_config, make_objspace
5 from pypy.tool.pytest import appsupport
6 from pypy.conftest import option
7
8 _SPACECACHE={}
9 def gettestobjspace(**kwds):
10 """ helper for instantiating and caching spaces for testing.
11 """
12 try:
13 config = make_config(option, **kwds)
14 except ConflictConfigError as e:
15 # this exception is typically only raised if a module is not available.
16 # in this case the test should be skipped
17 py.test.skip(str(e))
18 if getattr(option, 'runappdirect', None):
19 return TinyObjSpace()
20 key = config.getkey()
21 try:
22 return _SPACECACHE[key]
23 except KeyError:
24 space = maketestobjspace(config)
25 _SPACECACHE[key] = space
26 return space
27
28 def maketestobjspace(config=None):
29 if config is None:
30 config = make_config(option)
31 if config.objspace.usemodules.thread:
32 config.translation.thread = True
33 config.objspace.extmodules = 'pypy.tool.pytest.fake_pytest'
34 space = make_objspace(config)
35 space.startup() # Initialize all builtin modules
36 space.setitem(space.builtin.w_dict, space.wrap('raises'),
37 space.wrap(appsupport.app_raises))
38 space.setitem(space.builtin.w_dict, space.wrap('skip'),
39 space.wrap(appsupport.app_skip))
40 space.setitem(space.builtin.w_dict, space.wrap('py3k_skip'),
41 space.wrap(appsupport.app_py3k_skip))
42 space.raises_w = appsupport.raises_w.__get__(space)
43 return space
44
45 def skip_on_incompatible_interpreter(space_options):
46 info = getattr(sys, 'pypy_translation_info', None)
47 for key, value in space_options.iteritems():
48 if key == 'usemodules':
49 if info is not None:
50 for modname in value:
51 ok = info.get('objspace.usemodules.%s' % modname,
52 False)
53 if not ok:
54 py.test.skip("cannot runappdirect test: "
55 "module %r required" % (modname,))
56 else:
57 if '__pypy__' in value:
58 py.test.skip("no module __pypy__ on top of CPython")
59 continue
60 if info is None:
61 continue
62 if ('translation.' + key) in info:
63 key = 'translation.' + key
64 has = info.get(key, None)
65 if has != value:
66 #print sys.pypy_translation_info
67 py.test.skip("cannot runappdirect test: space needs %s = %s, "\
68 "while pypy-c was built with %s" % (key, value, has))
69
70
71 class TinyObjSpace(object):
72 """An object space that delegates everything to the hosting Python."""
73 def __init__(self):
74 for name in ('int', 'long', 'str', 'unicode', 'list', 'None', 'ValueError',
75 'OverflowError'):
76 setattr(self, 'w_' + name, eval(name))
77 self.w_bytes = bytes
78 import __builtin__ as __builtin__
79 self.builtin = __builtin__
80
81 def appexec(self, args, body):
82 body = body.lstrip()
83 assert body.startswith('(')
84 src = py.code.Source("def anonymous" + body)
85 return (src, args)
86
87 def wrap(self, obj):
88 if isinstance(obj, str):
89 return obj.decode('utf-8')
90 if isinstance(obj, dict):
91 return dict((self.wrap(k), self.wrap(v))
92 for k, v in obj.iteritems())
93 if isinstance(obj, tuple):
94 return tuple(self.wrap(item) for item in obj)
95 if isinstance(obj, list):
96 return list(self.wrap(item) for item in obj)
97 return obj
98
99 def newtext(self, obj):
100 assert isinstance(obj, str)
101 return obj.decode('utf-8')
102
103 def newbytes(self, obj):
104 assert isinstance(obj, str)
105 return obj
106
107 def unpackiterable(self, itr):
108 return list(itr)
109
110 def is_true(self, obj):
111 if isinstance(obj, tuple) and isinstance(obj[0], py.code.Source):
112 raise ValueError('bool(appexec object) unknown')
113 return bool(obj)
114
115 def is_none(self, obj):
116 return obj is None
117
118 def str_w(self, w_str):
119 return w_str
120
121 def utf8_w(self, w_utf8):
122 return w_utf8
123
124 def bytes_w(self, w_bytes):
125 return w_bytes
126
127 def newdict(self, module=None):
128 return {}
129
130 def newtuple(self, iterable):
131 return tuple(iterable)
132
133 def newlist(self, iterable):
134 return list(iterable)
135
136 def newbytes(self, obj):
137 return bytes(obj)
138
139 def newutf8(self, obj, lgth):
140 return obj
141
142 def newtext(self, obj):
143 assert isinstance(obj, str)
144 return obj.decode('utf-8')
145
146 def call_function(self, func, *args, **kwds):
147 return func(*args, **kwds)
148
149 def call_method(self, obj, name, *args, **kwds):
150 return getattr(obj, name)(*args, **kwds)
151
152 def getattr(self, obj, name):
153 return getattr(obj, name)
154
155 def setattr(self, obj, name, value):
156 setattr(obj, name, value)
157
158 def getbuiltinmodule(self, name):
159 return __import__(name)
160
161 def delslice(self, obj, *args):
162 obj.__delslice__(*args)
163
164 def is_w(self, obj1, obj2):
165 return obj1 is obj2
166
167 def setitem(self, obj, key, value):
168 obj[key] = value