| 1 |
|
| 2 |
def test_invalid_positions_dont_crash(): |
| 3 |
def f(a, b): |
| 4 |
return a / b |
| 5 |
|
| 6 |
c = f.__code__.replace(co_linetable=b'\xff') |
| 7 |
list(c.co_lines()) |
| 8 |
list(c.co_positions()) |
| 9 |
c.co_lnotab |
| 10 |
|
| 11 |
|
| 12 |
class Qualname: |
| 13 |
f = lambda self: 1 |
| 14 |
|
| 15 |
double_lambda = lambda : (lambda : 1) |
| 16 |
|
| 17 |
def test_co_qualname(): |
| 18 |
def f(): |
| 19 |
pass |
| 20 |
assert f.__code__.co_qualname == "test_co_qualname.<locals>.f" |
| 21 |
assert Qualname.f.__code__.co_qualname == "Qualname.<lambda>" |
| 22 |
inner = double_lambda() |
| 23 |
assert inner.__code__.co_qualname == "<lambda>.<locals>.<lambda>" |
| 24 |
|
| 25 |
def test_replace_co_qualname(): |
| 26 |
co = compile("x = x + 1", 'baz', 'exec') |
| 27 |
assert co.co_qualname == "<module>" |
| 28 |
co2 = co.replace(co_qualname="abc") |
| 29 |
assert co2.co_qualname == "abc" |
| 30 |
|
| 31 |
def test_co_positions_no_debug_ranges(): |
| 32 |
import sys |
| 33 |
def f(): |
| 34 |
x = 1 |
| 35 |
return x |
| 36 |
saved = sys._xoptions.copy() |
| 37 |
try: |
| 38 |
sys._xoptions['no_debug_ranges'] = True |
| 39 |
for line, end_line, column, end_column in f.__code__.co_positions(): |
| 40 |
if line is None: |
| 41 |
continue |
| 42 |
assert line == end_line |
| 43 |
assert column is None |
| 44 |
assert end_column is None |
| 45 |
finally: |
| 46 |
sys._xoptions.clear() |
| 47 |
sys._xoptions.update(saved) |