code/testing/test_source.py - line 196
| 192 |
| 193 |
| 194 |
| 195 |
| 196 |
| 197 |
| 198 |
| 199 |
| 200 | |
def test_compile_and_getsource(self): |
co = self.source.compile() |
exec co |
f(7) |
-> excinfo = py.test.raises(AssertionError, "f(6)") |
frame = excinfo.traceback[-1].frame |
stmt = frame.code.fullsource.getstatement(frame.lineno) |
|
assert str(stmt).strip().startswith('assert') | |
test/raises.py - line 20
| 5 |
| 6 |
| 7 |
| 8 |
| 9 |
| 10 |
| 11 |
| 12 |
| 13 |
| 14 |
| 15 |
| 16 |
| 17 |
| 18 |
| 19 |
| 20 |
| 21 |
| 22 |
| 23 |
| 24 |
| 25 |
| 26 |
| 27 |
| 28 |
| 29 |
| 30 |
| 31 |
| 32 |
| 33 |
| 34 |
| 35 |
| 36 |
| 37 |
| 38 |
| 39 | |
def raises(ExpectedException, *args, **kwargs): |
""" raise AssertionError, if target code does not raise the expected |
exception. |
""" |
assert args |
__tracebackhide__ = True |
if isinstance(args[0], str): |
expr, = args |
assert isinstance(expr, str) |
frame = sys._getframe(1) |
loc = frame.f_locals.copy() |
loc.update(kwargs) |
|
source = py.code.Source(expr) |
try: |
-> exec source.compile() in frame.f_globals, loc |
|
|
|
except ExpectedException: |
return py.code.ExceptionInfo() |
else: |
func = args[0] |
assert callable |
try: |
func(*args[1:], **kwargs) |
|
except ExpectedException: |
return py.code.ExceptionInfo() |
k = ", ".join(["%s=%r" % x for x in kwargs.items()]) |
if k: |
k = ', ' + k |
expr = '%s(%r%s)' %(func.__name__, args, k) |
raise ExceptionFailure(msg="DID NOT RAISE", |
expr=args, expected=ExpectedException) | |
None</home/hpk/py/release/0.9.x/py/test/raises.py:20> - line 1
None</home/hpk/py/release/0.9.x/py/code/testing/test_source.py:193> - line 4
|
|
def f(x): |
assert (x == |
3 + |
-> 4) | |
magic/assertion.py - line 15
| 8 |
| 9 |
| 10 |
| 11 |
| 12 |
| 13 |
| 14 |
| 15 |
| 16 |
| 17 |
| 18 |
| 19 |
| 20 |
| 21 |
| 22 |
| 23 |
| 24 |
| 25 |
| 26 | |
def __init__(self, *args): |
BuiltinAssertionError.__init__(self, *args) |
if args: |
self.msg = str(args[0]) |
else: |
f = sys._getframe(1) |
try: |
-> source = py.code.Frame(f).statement |
source = str(source.deindent()).strip() |
except py.error.ENOENT: |
source = None |
|
|
if source: |
self.msg = exprinfo.interpret(source, f, should_fail=True) |
if not self.args: |
self.args = (self.msg,) |
else: |
self.msg = None | |
code/frame.py - line 16
|
|
def statement(self): |
-> return self.code.fullsource.getstatement(self.lineno) | |
code/source.py - line 95
|
|
def getstatement(self, lineno): |
""" return Source statement which contains the |
given linenumber (counted from 0). |
""" |
-> start, end = self.getstatementrange(lineno) |
return self[start:end] | |
code/source.py - line 125
| 98 |
| 99 |
| 100 |
| 101 |
| 102 |
| 103 |
| 104 |
| 105 |
| 106 |
| 107 |
| 108 |
| 109 |
| 110 |
| 111 |
| 112 |
| 113 |
| 114 |
| 115 |
| 116 |
| 117 |
| 118 |
| 119 |
| 120 |
| 121 |
| 122 |
| 123 |
| 124 |
| 125 |
| 126 |
| 127 |
| 128 | |
def getstatementrange(self, lineno): |
""" return (start, end) tuple which spans the minimal |
statement region which containing the given lineno. |
""" |
|
|
if not (0 <= lineno < len(self)): |
raise IndexError("lineno out of range") |
|
|
from codeop import compile_command |
for start in range(lineno, -1, -1): |
trylines = self.lines[start:lineno+1] |
|
trylines.insert(0, 'def xxx():') |
trysource = '\n '.join(trylines) |
|
try: |
compile_command(trysource) |
except (SyntaxError, OverflowError, ValueError): |
pass |
else: |
break |
|
|
for end in range(lineno+1, len(self)+1): |
trysource = self[start:end] |
-> if trysource.isparseable(): |
break |
|
return start, end | |