| 1 |
from inspect import CO_VARARGS, CO_VARKEYWORDS |
| 2 |
|
| 3 |
import py |
| 4 |
from pypy.interpreter import gateway, pycode, typedef, baseobjspace |
| 5 |
from pypy.interpreter.error import OperationError, oefmt |
| 6 |
|
| 7 |
try: |
| 8 |
from _pytest.assertion.reinterpret import reinterpret as interpret |
| 9 |
except ImportError: |
| 10 |
from _pytest.assertion.newinterpret import interpret |
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
class AppCode(object): |
| 15 |
def __init__(self, space, pycode): |
| 16 |
self.code = pycode |
| 17 |
self.raw = pycode |
| 18 |
self.w_file = space.getattr(pycode, space.wrap('co_filename')) |
| 19 |
self.name = space.getattr(pycode, space.wrap('co_name')) |
| 20 |
self.firstlineno = space.unwrap( |
| 21 |
space.getattr(pycode, space.wrap('co_firstlineno'))) - 1 |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
self.path = py.path.local(space.utf8_w(self.w_file)) |
| 27 |
self.space = space |
| 28 |
|
| 29 |
def fullsource(self): |
| 30 |
filename = self.space.utf8_w(self.w_file) |
| 31 |
source = py.code.Source(py.std.linecache.getlines(filename)) |
| 32 |
if source.lines: |
| 33 |
return source |
| 34 |
try: |
| 35 |
return py.code.Source(self.path.read(mode="rU")) |
| 36 |
except py.error.Error: |
| 37 |
return None |
| 38 |
fullsource = property(fullsource, None, None, "Full source of AppCode") |
| 39 |
|
| 40 |
def getargs(self, var=False): |
| 41 |
raw = self.raw |
| 42 |
argcount = raw.co_argcount |
| 43 |
if var: |
| 44 |
argcount += raw.co_flags & CO_VARARGS |
| 45 |
argcount += raw.co_flags & CO_VARKEYWORDS |
| 46 |
return raw.co_varnames[:argcount] |
| 47 |
|
| 48 |
class AppFrame(py.code.Frame): |
| 49 |
|
| 50 |
def __init__(self, space, pyframe): |
| 51 |
self.code = AppCode(space, \ |
| 52 |
space.unwrap(space.getattr(pyframe, space.wrap('f_code')))) |
| 53 |
|
| 54 |
lineno = space.unwrap(space.getattr(pyframe, space.wrap('f_lineno'))) |
| 55 |
self.lineno = (lineno - 1) if lineno is not None else 0 |
| 56 |
|
| 57 |
self.space = space |
| 58 |
self.w_globals = space.getattr(pyframe, space.wrap('f_globals')) |
| 59 |
self.w_locals = space.getattr(pyframe, space.wrap('f_locals')) |
| 60 |
self.f_locals = self.w_locals |
| 61 |
|
| 62 |
def get_w_globals(self): |
| 63 |
return self.w_globals |
| 64 |
|
| 65 |
def eval(self, code, **vars): |
| 66 |
space = self.space |
| 67 |
for key, w_value in vars.items(): |
| 68 |
space.setitem(self.w_locals, space.wrap(key), w_value) |
| 69 |
if isinstance(code, str): |
| 70 |
return space.eval(code, self.get_w_globals(), self.w_locals) |
| 71 |
pyc = pycode.PyCode._from_code(space, code) |
| 72 |
return pyc.exec_host_bytecode(self.w_globals, self.w_locals) |
| 73 |
exec_ = eval |
| 74 |
|
| 75 |
def repr(self, w_value): |
| 76 |
try: |
| 77 |
return self.space.unwrap(self.space.repr(w_value)) |
| 78 |
except Exception as e: |
| 79 |
return "<Sorry, exception while trying to do repr, %r>"%e |
| 80 |
|
| 81 |
def is_true(self, w_value): |
| 82 |
return self.space.is_true(w_value) |
| 83 |
|
| 84 |
def getargs(self, var=False): |
| 85 |
space = self.space |
| 86 |
retval = [] |
| 87 |
for arg in self.code.getargs(var): |
| 88 |
w_val = space.finditem(self.w_locals, space.wrap(arg)) |
| 89 |
if w_val is None: |
| 90 |
w_val = space.wrap('<no value found>') |
| 91 |
retval.append((arg, w_val)) |
| 92 |
return retval |
| 93 |
|
| 94 |
|
| 95 |
class AppExceptionInfo(py.code.ExceptionInfo): |
| 96 |
"""An ExceptionInfo object representing an app-level exception.""" |
| 97 |
|
| 98 |
def __init__(self, space, operr): |
| 99 |
self.space = space |
| 100 |
self.operr = operr |
| 101 |
self.typename = operr.w_type.getname(space) |
| 102 |
self.traceback = AppTraceback(space, self.operr.get_traceback()) |
| 103 |
debug_excs = getattr(operr, 'debug_excs', []) |
| 104 |
if debug_excs: |
| 105 |
self._excinfo = debug_excs[0] |
| 106 |
self.value = self.operr.errorstr(self.space) |
| 107 |
|
| 108 |
def __repr__(self): |
| 109 |
return "<AppExceptionInfo %s>" % self.operr.errorstr(self.space) |
| 110 |
|
| 111 |
def exconly(self, tryshort=True): |
| 112 |
return '(application-level) ' + self.operr.errorstr(self.space) |
| 113 |
|
| 114 |
def errisinstance(self, exc): |
| 115 |
clsname = exc.__name__ |
| 116 |
|
| 117 |
|
| 118 |
if exc.__module__ != "exceptions": |
| 119 |
return False |
| 120 |
try: |
| 121 |
w_exc = getattr(self.space, 'w_' + clsname) |
| 122 |
except KeyboardInterrupt: |
| 123 |
raise |
| 124 |
except: |
| 125 |
pass |
| 126 |
else: |
| 127 |
return self.operr.match(self.space, w_exc) |
| 128 |
return False |
| 129 |
|
| 130 |
def __str__(self): |
| 131 |
return '(application-level) ' + self.operr.errorstr(self.space) |
| 132 |
|
| 133 |
class AppTracebackEntry(py.code.Traceback.Entry): |
| 134 |
exprinfo = None |
| 135 |
frame = None |
| 136 |
|
| 137 |
def __init__(self, space, tb): |
| 138 |
self.frame = AppFrame(space, space.getattr(tb, space.wrap('tb_frame'))) |
| 139 |
self.lineno = space.unwrap(space.getattr(tb, space.wrap('tb_lineno'))) - 1 |
| 140 |
|
| 141 |
def reinterpret(self): |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
return self.exprinfo |
| 150 |
|
| 151 |
def ishidden(self): |
| 152 |
return False |
| 153 |
|
| 154 |
class AppTraceback(py.code.Traceback): |
| 155 |
Entry = AppTracebackEntry |
| 156 |
|
| 157 |
def __init__(self, space, apptb): |
| 158 |
l = [] |
| 159 |
while apptb is not space.w_None and apptb is not None: |
| 160 |
l.append(self.Entry(space, apptb)) |
| 161 |
apptb = space.getattr(apptb, space.wrap('tb_next')) |
| 162 |
list.__init__(self, l) |
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
def build_pytest_assertion(space): |
| 167 |
def my_init(space, w_self, __args__): |
| 168 |
"Our new AssertionError.__init__()." |
| 169 |
w_parent_init = space.getattr(w_BuiltinAssertionError, |
| 170 |
space.wrap('__init__')) |
| 171 |
space.call_args(w_parent_init, __args__.prepend(w_self)) |
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
args_w, kwargs_w = __args__.unpack() |
| 178 |
if args_w: |
| 179 |
w_msg = args_w[0] |
| 180 |
else: |
| 181 |
frame = space.getexecutioncontext().gettopframe() |
| 182 |
runner = AppFrame(space, frame) |
| 183 |
try: |
| 184 |
source = runner.statement |
| 185 |
source = str(source).strip() |
| 186 |
except (py.error.ENOENT, SyntaxError): |
| 187 |
source = None |
| 188 |
from pypy import conftest |
| 189 |
if source and py.test.config._assertstate.mode != "off": |
| 190 |
msg = interpret(source, runner, should_fail=True) |
| 191 |
space.setattr(w_self, space.wrap('args'), |
| 192 |
space.newtuple([space.wrap(msg)])) |
| 193 |
w_msg = space.wrap(msg) |
| 194 |
else: |
| 195 |
w_msg = space.w_None |
| 196 |
space.setattr(w_self, space.wrap('msg'), w_msg) |
| 197 |
|
| 198 |
|
| 199 |
w_BuiltinAssertionError = space.getitem(space.builtin.w_dict, |
| 200 |
space.wrap('AssertionError')) |
| 201 |
w_metaclass = space.type(w_BuiltinAssertionError) |
| 202 |
w_init = space.wrap(gateway.interp2app(my_init)) |
| 203 |
w_dict = space.getattr(w_BuiltinAssertionError, space.wrap('__dict__')) |
| 204 |
w_dict = space.call_method(w_dict, 'copy') |
| 205 |
|
| 206 |
w_dict.setitem_str('__module__', space.getattr(w_BuiltinAssertionError, |
| 207 |
space.wrap('__module__'))) |
| 208 |
space.setitem(w_dict, space.wrap('__init__'), w_init) |
| 209 |
return space.call_function(w_metaclass, |
| 210 |
space.wrap('AssertionError'), |
| 211 |
space.newtuple([w_BuiltinAssertionError]), |
| 212 |
w_dict) |
| 213 |
|
| 214 |
def _exc_info(space, operror): |
| 215 |
"""sys.exc_info() isn't set until a app except block catches it, |
| 216 |
but we can directly copy the two lines of code from module/sys/vm.py.""" |
| 217 |
operror.normalize_exception(space) |
| 218 |
return space.appexec([operror.w_type, operror.get_w_value(space), |
| 219 |
space.wrap(operror.get_traceback())], """(t, v, tb): |
| 220 |
class _ExceptionInfo: |
| 221 |
pass |
| 222 |
e = _ExceptionInfo() |
| 223 |
e.type = t |
| 224 |
e.value = v |
| 225 |
e.traceback = tb |
| 226 |
return e |
| 227 |
""") |
| 228 |
|
| 229 |
|
| 230 |
class W_RaisesContextManager(baseobjspace.W_Root): |
| 231 |
|
| 232 |
def __init__(self, space, w_ExpectedException): |
| 233 |
self.space = space |
| 234 |
self.w_ExpectedException = w_ExpectedException |
| 235 |
|
| 236 |
def enter(self): |
| 237 |
return self |
| 238 |
|
| 239 |
def exit(self, w_exc_type, w_exc_value, w_traceback): |
| 240 |
space = self.space |
| 241 |
if space.is_none(w_exc_type): |
| 242 |
self.report_error("no exception") |
| 243 |
if not space.exception_match(w_exc_type, self.w_ExpectedException): |
| 244 |
self.report_error(space.text_w(space.repr(w_exc_type))) |
| 245 |
self.w_value = w_exc_value |
| 246 |
self.w_type = w_exc_type |
| 247 |
self.w_tb = w_traceback |
| 248 |
return space.w_True |
| 249 |
|
| 250 |
def report_error(self, got): |
| 251 |
space = self.space |
| 252 |
raise oefmt(space.w_AssertionError, |
| 253 |
"raises() expected %s, but got %s", |
| 254 |
space.text_w(space.repr(self.w_ExpectedException)), |
| 255 |
got) |
| 256 |
|
| 257 |
W_RaisesContextManager.typedef = typedef.TypeDef("RaisesContextManager", |
| 258 |
__enter__ = gateway.interp2app_temp(W_RaisesContextManager.enter), |
| 259 |
__exit__ = gateway.interp2app_temp(W_RaisesContextManager.exit), |
| 260 |
value=typedef.interp_attrproperty_w('w_value', cls=W_RaisesContextManager), |
| 261 |
type=typedef.interp_attrproperty_w('w_type', cls=W_RaisesContextManager), |
| 262 |
tb=typedef.interp_attrproperty_w('w_tb', cls=W_RaisesContextManager), |
| 263 |
) |
| 264 |
|
| 265 |
def pypyraises(space, w_ExpectedException, w_expr=None, __args__=None): |
| 266 |
"""A built-in function providing the equivalent of py.test.raises().""" |
| 267 |
if w_expr is None: |
| 268 |
return W_RaisesContextManager(space, w_ExpectedException) |
| 269 |
args_w, kwds_w = __args__.unpack() |
| 270 |
if space.isinstance_w(w_expr, space.w_text): |
| 271 |
if args_w: |
| 272 |
raise oefmt(space.w_TypeError, |
| 273 |
"raises() takes no argument after a string expression") |
| 274 |
expr = space.unwrap(w_expr) |
| 275 |
source = py.code.Source(expr) |
| 276 |
frame = space.getexecutioncontext().gettopframe() |
| 277 |
w_locals = frame.getdictscope() |
| 278 |
pycode = frame.pycode |
| 279 |
filename = "<%s:%s>" %(pycode.co_filename, |
| 280 |
space.int_w(frame.fget_f_lineno(space))) |
| 281 |
lines = [x + "\n" for x in expr.split("\n")] |
| 282 |
py.std.linecache.cache[filename] = (1, None, lines, filename) |
| 283 |
w_locals = space.call_method(w_locals, 'copy') |
| 284 |
for key, w_value in kwds_w.items(): |
| 285 |
space.setitem(w_locals, space.wrap(key), w_value) |
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
try: |
| 290 |
space.exec_(unicode(source).encode('utf-8'), frame.get_w_globals(), |
| 291 |
w_locals, filename=filename) |
| 292 |
except OperationError as e: |
| 293 |
if e.match(space, w_ExpectedException): |
| 294 |
return _exc_info(space, e) |
| 295 |
raise |
| 296 |
else: |
| 297 |
try: |
| 298 |
space.call_args(w_expr, __args__) |
| 299 |
except OperationError as e: |
| 300 |
if e.match(space, w_ExpectedException): |
| 301 |
return _exc_info(space, e) |
| 302 |
raise |
| 303 |
raise oefmt(space.w_AssertionError, "DID NOT RAISE") |
| 304 |
|
| 305 |
app_raises = gateway.interp2app(pypyraises) |
| 306 |
|
| 307 |
def pypyskip(space, w_message=None): |
| 308 |
"""skip a test at app-level. """ |
| 309 |
if w_message is None: |
| 310 |
msg = '' |
| 311 |
else: |
| 312 |
msg = space.unwrap(w_message) |
| 313 |
py.test.skip(msg) |
| 314 |
|
| 315 |
app_skip = gateway.interp2app(pypyskip) |
| 316 |
|
| 317 |
def py3k_pypyskip(space, w_message): |
| 318 |
"""skip a test at app-level. """ |
| 319 |
msg = space.unwrap(w_message) |
| 320 |
py.test.skip('[py3k] %s' % msg) |
| 321 |
|
| 322 |
app_py3k_skip = gateway.interp2app(py3k_pypyskip) |
| 323 |
|
| 324 |
def raises_w(space, w_ExpectedException, *args, **kwds): |
| 325 |
try: |
| 326 |
excinfo = py.test.raises(OperationError, *args, **kwds) |
| 327 |
type, value, tb = excinfo._excinfo |
| 328 |
if not value.match(space, w_ExpectedException): |
| 329 |
raise type, value, tb |
| 330 |
return excinfo |
| 331 |
except py.test.raises.Exception as e: |
| 332 |
e.tbindex = getattr(e, 'tbindex', -1) - 1 |
| 333 |
raise |