| 1 |
# Minimal tests for dis module |
| 2 |
|
| 3 |
import contextlib |
| 4 |
import dis |
| 5 |
import functools |
| 6 |
import io |
| 7 |
import itertools |
| 8 |
import opcode |
| 9 |
import re |
| 10 |
import sys |
| 11 |
import tempfile |
| 12 |
import textwrap |
| 13 |
import types |
| 14 |
import unittest |
| 15 |
from test.support import (captured_stdout, requires_debug_ranges, |
| 16 |
requires_specialization, cpython_only, |
| 17 |
os_helper) |
| 18 |
from test.support.bytecode_helper import BytecodeTestCase |
| 19 |
|
| 20 |
|
| 21 |
CACHE = dis.opmap["CACHE"] |
| 22 |
|
| 23 |
def get_tb(): |
| 24 |
def _error(): |
| 25 |
try: |
| 26 |
1 / 0 |
| 27 |
except Exception as e: |
| 28 |
tb = e.__traceback__ |
| 29 |
return tb |
| 30 |
|
| 31 |
tb = _error() |
| 32 |
while tb.tb_next: |
| 33 |
tb = tb.tb_next |
| 34 |
return tb |
| 35 |
|
| 36 |
TRACEBACK_CODE = get_tb().tb_frame.f_code |
| 37 |
|
| 38 |
class _C: |
| 39 |
def __init__(self, x): |
| 40 |
self.x = x == 1 |
| 41 |
|
| 42 |
@staticmethod |
| 43 |
def sm(x): |
| 44 |
x = x == 1 |
| 45 |
|
| 46 |
@classmethod |
| 47 |
def cm(cls, x): |
| 48 |
cls.x = x == 1 |
| 49 |
|
| 50 |
dis_c_instance_method = """\ |
| 51 |
%3d RESUME 0 |
| 52 |
|
| 53 |
%3d LOAD_FAST 1 (x) |
| 54 |
LOAD_CONST 1 (1) |
| 55 |
COMPARE_OP 72 (==) |
| 56 |
LOAD_FAST 0 (self) |
| 57 |
STORE_ATTR 0 (x) |
| 58 |
RETURN_CONST 0 (None) |
| 59 |
""" % (_C.__init__.__code__.co_firstlineno, _C.__init__.__code__.co_firstlineno + 1,) |
| 60 |
|
| 61 |
dis_c_instance_method_bytes = """\ |
| 62 |
RESUME 0 |
| 63 |
LOAD_FAST 1 |
| 64 |
LOAD_CONST 1 |
| 65 |
COMPARE_OP 72 (==) |
| 66 |
LOAD_FAST 0 |
| 67 |
STORE_ATTR 0 |
| 68 |
RETURN_CONST 0 |
| 69 |
""" |
| 70 |
|
| 71 |
dis_c_class_method = """\ |
| 72 |
%3d RESUME 0 |
| 73 |
|
| 74 |
%3d LOAD_FAST 1 (x) |
| 75 |
LOAD_CONST 1 (1) |
| 76 |
COMPARE_OP 72 (==) |
| 77 |
LOAD_FAST 0 (cls) |
| 78 |
STORE_ATTR 0 (x) |
| 79 |
RETURN_CONST 0 (None) |
| 80 |
""" % (_C.cm.__code__.co_firstlineno, _C.cm.__code__.co_firstlineno + 2,) |
| 81 |
|
| 82 |
dis_c_static_method = """\ |
| 83 |
%3d RESUME 0 |
| 84 |
|
| 85 |
%3d LOAD_FAST 0 (x) |
| 86 |
LOAD_CONST 1 (1) |
| 87 |
COMPARE_OP 72 (==) |
| 88 |
STORE_FAST 0 (x) |
| 89 |
RETURN_CONST 0 (None) |
| 90 |
""" % (_C.sm.__code__.co_firstlineno, _C.sm.__code__.co_firstlineno + 2,) |
| 91 |
|
| 92 |
# Class disassembling info has an extra newline at end. |
| 93 |
dis_c = """\ |
| 94 |
Disassembly of %s: |
| 95 |
%s |
| 96 |
Disassembly of %s: |
| 97 |
%s |
| 98 |
Disassembly of %s: |
| 99 |
%s |
| 100 |
""" % (_C.__init__.__name__, dis_c_instance_method, |
| 101 |
_C.cm.__name__, dis_c_class_method, |
| 102 |
_C.sm.__name__, dis_c_static_method) |
| 103 |
|
| 104 |
def _f(a): |
| 105 |
print(a) |
| 106 |
return 1 |
| 107 |
|
| 108 |
dis_f = """\ |
| 109 |
%3d RESUME 0 |
| 110 |
|
| 111 |
%3d LOAD_GLOBAL 1 (print + NULL) |
| 112 |
LOAD_FAST 0 (a) |
| 113 |
CALL 1 |
| 114 |
POP_TOP |
| 115 |
|
| 116 |
%3d RETURN_CONST 1 (1) |
| 117 |
""" % (_f.__code__.co_firstlineno, |
| 118 |
_f.__code__.co_firstlineno + 1, |
| 119 |
_f.__code__.co_firstlineno + 2) |
| 120 |
|
| 121 |
dis_f_with_offsets = """\ |
| 122 |
%3d 0 RESUME 0 |
| 123 |
|
| 124 |
%3d 2 LOAD_GLOBAL 1 (print + NULL) |
| 125 |
12 LOAD_FAST 0 (a) |
| 126 |
14 CALL 1 |
| 127 |
22 POP_TOP |
| 128 |
|
| 129 |
%3d 24 RETURN_CONST 1 (1) |
| 130 |
""" % (_f.__code__.co_firstlineno, |
| 131 |
_f.__code__.co_firstlineno + 1, |
| 132 |
_f.__code__.co_firstlineno + 2) |
| 133 |
|
| 134 |
|
| 135 |
dis_f_co_code = """\ |
| 136 |
RESUME 0 |
| 137 |
LOAD_GLOBAL 1 |
| 138 |
LOAD_FAST 0 |
| 139 |
CALL 1 |
| 140 |
POP_TOP |
| 141 |
RETURN_CONST 1 |
| 142 |
""" |
| 143 |
|
| 144 |
def bug708901(): |
| 145 |
for res in range(1, |
| 146 |
10): |
| 147 |
pass |
| 148 |
|
| 149 |
dis_bug708901 = """\ |
| 150 |
%3d RESUME 0 |
| 151 |
|
| 152 |
%3d LOAD_GLOBAL 1 (range + NULL) |
| 153 |
LOAD_CONST 1 (1) |
| 154 |
|
| 155 |
%3d LOAD_CONST 2 (10) |
| 156 |
|
| 157 |
%3d CALL 2 |
| 158 |
GET_ITER |
| 159 |
L1: FOR_ITER 3 (to L2) |
| 160 |
STORE_FAST 0 (res) |
| 161 |
|
| 162 |
%3d JUMP_BACKWARD 5 (to L1) |
| 163 |
|
| 164 |
%3d L2: END_FOR |
| 165 |
POP_TOP |
| 166 |
RETURN_CONST 0 (None) |
| 167 |
""" % (bug708901.__code__.co_firstlineno, |
| 168 |
bug708901.__code__.co_firstlineno + 1, |
| 169 |
bug708901.__code__.co_firstlineno + 2, |
| 170 |
bug708901.__code__.co_firstlineno + 1, |
| 171 |
bug708901.__code__.co_firstlineno + 3, |
| 172 |
bug708901.__code__.co_firstlineno + 1) |
| 173 |
|
| 174 |
|
| 175 |
def bug1333982(x=[]): |
| 176 |
assert 0, ((s for s in x) + |
| 177 |
1) |
| 178 |
pass |
| 179 |
|
| 180 |
dis_bug1333982 = """\ |
| 181 |
%3d RESUME 0 |
| 182 |
|
| 183 |
%3d LOAD_ASSERTION_ERROR |
| 184 |
LOAD_CONST 1 (<code object <genexpr> at 0x..., file "%s", line %d>) |
| 185 |
MAKE_FUNCTION |
| 186 |
LOAD_FAST 0 (x) |
| 187 |
GET_ITER |
| 188 |
CALL 0 |
| 189 |
|
| 190 |
%3d LOAD_CONST 2 (1) |
| 191 |
|
| 192 |
%3d BINARY_OP 0 (+) |
| 193 |
CALL 0 |
| 194 |
RAISE_VARARGS 1 |
| 195 |
""" % (bug1333982.__code__.co_firstlineno, |
| 196 |
bug1333982.__code__.co_firstlineno + 1, |
| 197 |
__file__, |
| 198 |
bug1333982.__code__.co_firstlineno + 1, |
| 199 |
bug1333982.__code__.co_firstlineno + 2, |
| 200 |
bug1333982.__code__.co_firstlineno + 1) |
| 201 |
|
| 202 |
|
| 203 |
def bug42562(): |
| 204 |
pass |
| 205 |
|
| 206 |
|
| 207 |
# Set line number for 'pass' to None |
| 208 |
bug42562.__code__ = bug42562.__code__.replace(co_linetable=b'\xf8') |
| 209 |
|
| 210 |
|
| 211 |
dis_bug42562 = """\ |
| 212 |
RESUME 0 |
| 213 |
RETURN_CONST 0 (None) |
| 214 |
""" |
| 215 |
|
| 216 |
# Extended arg followed by NOP |
| 217 |
code_bug_45757 = bytes([ |
| 218 |
opcode.opmap['EXTENDED_ARG'], 0x01, # EXTENDED_ARG 0x01 |
| 219 |
opcode.opmap['NOP'], 0xFF, # NOP 0xFF |
| 220 |
opcode.opmap['EXTENDED_ARG'], 0x01, # EXTENDED_ARG 0x01 |
| 221 |
opcode.opmap['LOAD_CONST'], 0x29, # LOAD_CONST 0x29 |
| 222 |
opcode.opmap['RETURN_VALUE'], 0x00, # RETURN_VALUE 0x00 |
| 223 |
]) |
| 224 |
|
| 225 |
dis_bug_45757 = """\ |
| 226 |
EXTENDED_ARG 1 |
| 227 |
NOP |
| 228 |
EXTENDED_ARG 1 |
| 229 |
LOAD_CONST 297 |
| 230 |
RETURN_VALUE |
| 231 |
""" |
| 232 |
|
| 233 |
# [255, 255, 255, 252] is -4 in a 4 byte signed integer |
| 234 |
bug46724 = bytes([ |
| 235 |
opcode.EXTENDED_ARG, 255, |
| 236 |
opcode.EXTENDED_ARG, 255, |
| 237 |
opcode.EXTENDED_ARG, 255, |
| 238 |
opcode.opmap['JUMP_FORWARD'], 252, |
| 239 |
]) |
| 240 |
|
| 241 |
|
| 242 |
dis_bug46724 = """\ |
| 243 |
L1: EXTENDED_ARG 255 |
| 244 |
EXTENDED_ARG 65535 |
| 245 |
EXTENDED_ARG 16777215 |
| 246 |
JUMP_FORWARD -4 (to L1) |
| 247 |
""" |
| 248 |
|
| 249 |
def func_w_kwargs(a, b, **c): |
| 250 |
pass |
| 251 |
|
| 252 |
def wrap_func_w_kwargs(): |
| 253 |
func_w_kwargs(1, 2, c=5) |
| 254 |
|
| 255 |
dis_kw_names = """\ |
| 256 |
%3d RESUME 0 |
| 257 |
|
| 258 |
%3d LOAD_GLOBAL 1 (func_w_kwargs + NULL) |
| 259 |
LOAD_CONST 1 (1) |
| 260 |
LOAD_CONST 2 (2) |
| 261 |
LOAD_CONST 3 (5) |
| 262 |
LOAD_CONST 4 (('c',)) |
| 263 |
CALL_KW 3 |
| 264 |
POP_TOP |
| 265 |
RETURN_CONST 0 (None) |
| 266 |
""" % (wrap_func_w_kwargs.__code__.co_firstlineno, |
| 267 |
wrap_func_w_kwargs.__code__.co_firstlineno + 1) |
| 268 |
|
| 269 |
dis_intrinsic_1_2 = """\ |
| 270 |
0 RESUME 0 |
| 271 |
|
| 272 |
1 LOAD_CONST 0 (0) |
| 273 |
LOAD_CONST 1 (('*',)) |
| 274 |
IMPORT_NAME 0 (math) |
| 275 |
CALL_INTRINSIC_1 2 (INTRINSIC_IMPORT_STAR) |
| 276 |
POP_TOP |
| 277 |
RETURN_CONST 2 (None) |
| 278 |
""" |
| 279 |
|
| 280 |
dis_intrinsic_1_5 = """\ |
| 281 |
0 RESUME 0 |
| 282 |
|
| 283 |
1 LOAD_NAME 0 (a) |
| 284 |
CALL_INTRINSIC_1 5 (INTRINSIC_UNARY_POSITIVE) |
| 285 |
RETURN_VALUE |
| 286 |
""" |
| 287 |
|
| 288 |
dis_intrinsic_1_6 = """\ |
| 289 |
0 RESUME 0 |
| 290 |
|
| 291 |
1 BUILD_LIST 0 |
| 292 |
LOAD_NAME 0 (a) |
| 293 |
LIST_EXTEND 1 |
| 294 |
CALL_INTRINSIC_1 6 (INTRINSIC_LIST_TO_TUPLE) |
| 295 |
RETURN_VALUE |
| 296 |
""" |
| 297 |
|
| 298 |
_BIG_LINENO_FORMAT = """\ |
| 299 |
1 RESUME 0 |
| 300 |
|
| 301 |
%3d LOAD_GLOBAL 0 (spam) |
| 302 |
POP_TOP |
| 303 |
RETURN_CONST 0 (None) |
| 304 |
""" |
| 305 |
|
| 306 |
_BIG_LINENO_FORMAT2 = """\ |
| 307 |
1 RESUME 0 |
| 308 |
|
| 309 |
%4d LOAD_GLOBAL 0 (spam) |
| 310 |
POP_TOP |
| 311 |
RETURN_CONST 0 (None) |
| 312 |
""" |
| 313 |
|
| 314 |
dis_module_expected_results = """\ |
| 315 |
Disassembly of f: |
| 316 |
4 RESUME 0 |
| 317 |
RETURN_CONST 0 (None) |
| 318 |
|
| 319 |
Disassembly of g: |
| 320 |
5 RESUME 0 |
| 321 |
RETURN_CONST 0 (None) |
| 322 |
|
| 323 |
""" |
| 324 |
|
| 325 |
expr_str = "x + 1" |
| 326 |
|
| 327 |
dis_expr_str = """\ |
| 328 |
0 RESUME 0 |
| 329 |
|
| 330 |
1 LOAD_NAME 0 (x) |
| 331 |
LOAD_CONST 0 (1) |
| 332 |
BINARY_OP 0 (+) |
| 333 |
RETURN_VALUE |
| 334 |
""" |
| 335 |
|
| 336 |
simple_stmt_str = "x = x + 1" |
| 337 |
|
| 338 |
dis_simple_stmt_str = """\ |
| 339 |
0 RESUME 0 |
| 340 |
|
| 341 |
1 LOAD_NAME 0 (x) |
| 342 |
LOAD_CONST 0 (1) |
| 343 |
BINARY_OP 0 (+) |
| 344 |
STORE_NAME 0 (x) |
| 345 |
RETURN_CONST 1 (None) |
| 346 |
""" |
| 347 |
|
| 348 |
annot_stmt_str = """\ |
| 349 |
|
| 350 |
x: int = 1 |
| 351 |
y: fun(1) |
| 352 |
lst[fun(0)]: int = 1 |
| 353 |
""" |
| 354 |
# leading newline is for a reason (tests lineno) |
| 355 |
|
| 356 |
dis_annot_stmt_str = """\ |
| 357 |
0 RESUME 0 |
| 358 |
|
| 359 |
2 SETUP_ANNOTATIONS |
| 360 |
LOAD_CONST 0 (1) |
| 361 |
STORE_NAME 0 (x) |
| 362 |
LOAD_NAME 1 (int) |
| 363 |
LOAD_NAME 2 (__annotations__) |
| 364 |
LOAD_CONST 1 ('x') |
| 365 |
STORE_SUBSCR |
| 366 |
|
| 367 |
3 LOAD_NAME 3 (fun) |
| 368 |
PUSH_NULL |
| 369 |
LOAD_CONST 0 (1) |
| 370 |
CALL 1 |
| 371 |
LOAD_NAME 2 (__annotations__) |
| 372 |
LOAD_CONST 2 ('y') |
| 373 |
STORE_SUBSCR |
| 374 |
|
| 375 |
4 LOAD_CONST 0 (1) |
| 376 |
LOAD_NAME 4 (lst) |
| 377 |
LOAD_NAME 3 (fun) |
| 378 |
PUSH_NULL |
| 379 |
LOAD_CONST 3 (0) |
| 380 |
CALL 1 |
| 381 |
STORE_SUBSCR |
| 382 |
LOAD_NAME 1 (int) |
| 383 |
POP_TOP |
| 384 |
RETURN_CONST 4 (None) |
| 385 |
""" |
| 386 |
|
| 387 |
compound_stmt_str = """\ |
| 388 |
x = 0 |
| 389 |
while 1: |
| 390 |
x += 1""" |
| 391 |
# Trailing newline has been deliberately omitted |
| 392 |
|
| 393 |
dis_compound_stmt_str = """\ |
| 394 |
0 RESUME 0 |
| 395 |
|
| 396 |
1 LOAD_CONST 0 (0) |
| 397 |
STORE_NAME 0 (x) |
| 398 |
|
| 399 |
2 NOP |
| 400 |
|
| 401 |
3 L1: LOAD_NAME 0 (x) |
| 402 |
LOAD_CONST 1 (1) |
| 403 |
BINARY_OP 13 (+=) |
| 404 |
STORE_NAME 0 (x) |
| 405 |
|
| 406 |
2 JUMP_BACKWARD 7 (to L1) |
| 407 |
""" |
| 408 |
|
| 409 |
dis_traceback = """\ |
| 410 |
%4d RESUME 0 |
| 411 |
|
| 412 |
%4d NOP |
| 413 |
|
| 414 |
%4d L1: LOAD_CONST 1 (1) |
| 415 |
LOAD_CONST 2 (0) |
| 416 |
--> BINARY_OP 11 (/) |
| 417 |
POP_TOP |
| 418 |
|
| 419 |
%4d L2: LOAD_FAST_CHECK 1 (tb) |
| 420 |
RETURN_VALUE |
| 421 |
|
| 422 |
-- L3: PUSH_EXC_INFO |
| 423 |
|
| 424 |
%4d LOAD_GLOBAL 0 (Exception) |
| 425 |
CHECK_EXC_MATCH |
| 426 |
POP_JUMP_IF_FALSE 23 (to L7) |
| 427 |
STORE_FAST 0 (e) |
| 428 |
|
| 429 |
%4d L4: LOAD_FAST 0 (e) |
| 430 |
LOAD_ATTR 2 (__traceback__) |
| 431 |
STORE_FAST 1 (tb) |
| 432 |
L5: POP_EXCEPT |
| 433 |
LOAD_CONST 0 (None) |
| 434 |
STORE_FAST 0 (e) |
| 435 |
DELETE_FAST 0 (e) |
| 436 |
|
| 437 |
%4d LOAD_FAST 1 (tb) |
| 438 |
RETURN_VALUE |
| 439 |
|
| 440 |
-- L6: LOAD_CONST 0 (None) |
| 441 |
STORE_FAST 0 (e) |
| 442 |
DELETE_FAST 0 (e) |
| 443 |
RERAISE 1 |
| 444 |
|
| 445 |
%4d L7: RERAISE 0 |
| 446 |
|
| 447 |
-- L8: COPY 3 |
| 448 |
POP_EXCEPT |
| 449 |
RERAISE 1 |
| 450 |
ExceptionTable: |
| 451 |
L1 to L2 -> L3 [0] |
| 452 |
L3 to L4 -> L8 [1] lasti |
| 453 |
L4 to L5 -> L6 [1] lasti |
| 454 |
L6 to L8 -> L8 [1] lasti |
| 455 |
""" % (TRACEBACK_CODE.co_firstlineno, |
| 456 |
TRACEBACK_CODE.co_firstlineno + 1, |
| 457 |
TRACEBACK_CODE.co_firstlineno + 2, |
| 458 |
TRACEBACK_CODE.co_firstlineno + 5, |
| 459 |
TRACEBACK_CODE.co_firstlineno + 3, |
| 460 |
TRACEBACK_CODE.co_firstlineno + 4, |
| 461 |
TRACEBACK_CODE.co_firstlineno + 5, |
| 462 |
TRACEBACK_CODE.co_firstlineno + 3) |
| 463 |
|
| 464 |
def _fstring(a, b, c, d): |
| 465 |
return f'{a} {b:4} {c!r} {d!r:4}' |
| 466 |
|
| 467 |
dis_fstring = """\ |
| 468 |
%3d RESUME 0 |
| 469 |
|
| 470 |
%3d LOAD_FAST 0 (a) |
| 471 |
FORMAT_SIMPLE |
| 472 |
LOAD_CONST 1 (' ') |
| 473 |
LOAD_FAST 1 (b) |
| 474 |
LOAD_CONST 2 ('4') |
| 475 |
FORMAT_WITH_SPEC |
| 476 |
LOAD_CONST 1 (' ') |
| 477 |
LOAD_FAST 2 (c) |
| 478 |
CONVERT_VALUE 2 (repr) |
| 479 |
FORMAT_SIMPLE |
| 480 |
LOAD_CONST 1 (' ') |
| 481 |
LOAD_FAST 3 (d) |
| 482 |
CONVERT_VALUE 2 (repr) |
| 483 |
LOAD_CONST 2 ('4') |
| 484 |
FORMAT_WITH_SPEC |
| 485 |
BUILD_STRING 7 |
| 486 |
RETURN_VALUE |
| 487 |
""" % (_fstring.__code__.co_firstlineno, _fstring.__code__.co_firstlineno + 1) |
| 488 |
|
| 489 |
def _with(c): |
| 490 |
with c: |
| 491 |
x = 1 |
| 492 |
y = 2 |
| 493 |
|
| 494 |
dis_with = """\ |
| 495 |
%4d RESUME 0 |
| 496 |
|
| 497 |
%4d LOAD_FAST 0 (c) |
| 498 |
BEFORE_WITH |
| 499 |
L1: POP_TOP |
| 500 |
|
| 501 |
%4d LOAD_CONST 1 (1) |
| 502 |
STORE_FAST 1 (x) |
| 503 |
|
| 504 |
%4d L2: LOAD_CONST 0 (None) |
| 505 |
LOAD_CONST 0 (None) |
| 506 |
LOAD_CONST 0 (None) |
| 507 |
CALL 2 |
| 508 |
POP_TOP |
| 509 |
|
| 510 |
%4d LOAD_CONST 2 (2) |
| 511 |
STORE_FAST 2 (y) |
| 512 |
RETURN_CONST 0 (None) |
| 513 |
|
| 514 |
%4d L3: PUSH_EXC_INFO |
| 515 |
WITH_EXCEPT_START |
| 516 |
TO_BOOL |
| 517 |
POP_JUMP_IF_TRUE 1 (to L4) |
| 518 |
RERAISE 2 |
| 519 |
L4: POP_TOP |
| 520 |
L5: POP_EXCEPT |
| 521 |
POP_TOP |
| 522 |
POP_TOP |
| 523 |
|
| 524 |
%4d LOAD_CONST 2 (2) |
| 525 |
STORE_FAST 2 (y) |
| 526 |
RETURN_CONST 0 (None) |
| 527 |
|
| 528 |
-- L6: COPY 3 |
| 529 |
POP_EXCEPT |
| 530 |
RERAISE 1 |
| 531 |
ExceptionTable: |
| 532 |
L1 to L2 -> L3 [1] lasti |
| 533 |
L3 to L5 -> L6 [3] lasti |
| 534 |
""" % (_with.__code__.co_firstlineno, |
| 535 |
_with.__code__.co_firstlineno + 1, |
| 536 |
_with.__code__.co_firstlineno + 2, |
| 537 |
_with.__code__.co_firstlineno + 1, |
| 538 |
_with.__code__.co_firstlineno + 3, |
| 539 |
_with.__code__.co_firstlineno + 1, |
| 540 |
_with.__code__.co_firstlineno + 3, |
| 541 |
) |
| 542 |
|
| 543 |
async def _asyncwith(c): |
| 544 |
async with c: |
| 545 |
x = 1 |
| 546 |
y = 2 |
| 547 |
|
| 548 |
dis_asyncwith = """\ |
| 549 |
%4d RETURN_GENERATOR |
| 550 |
POP_TOP |
| 551 |
L1: RESUME 0 |
| 552 |
|
| 553 |
%4d LOAD_FAST 0 (c) |
| 554 |
BEFORE_ASYNC_WITH |
| 555 |
GET_AWAITABLE 1 |
| 556 |
LOAD_CONST 0 (None) |
| 557 |
L2: SEND 3 (to L5) |
| 558 |
L3: YIELD_VALUE 1 |
| 559 |
L4: RESUME 3 |
| 560 |
JUMP_BACKWARD_NO_INTERRUPT 5 (to L2) |
| 561 |
L5: END_SEND |
| 562 |
L6: POP_TOP |
| 563 |
|
| 564 |
%4d LOAD_CONST 1 (1) |
| 565 |
STORE_FAST 1 (x) |
| 566 |
|
| 567 |
%4d L7: LOAD_CONST 0 (None) |
| 568 |
LOAD_CONST 0 (None) |
| 569 |
LOAD_CONST 0 (None) |
| 570 |
CALL 2 |
| 571 |
GET_AWAITABLE 2 |
| 572 |
LOAD_CONST 0 (None) |
| 573 |
L8: SEND 3 (to L11) |
| 574 |
L9: YIELD_VALUE 1 |
| 575 |
L10: RESUME 3 |
| 576 |
JUMP_BACKWARD_NO_INTERRUPT 5 (to L8) |
| 577 |
L11: END_SEND |
| 578 |
POP_TOP |
| 579 |
|
| 580 |
%4d LOAD_CONST 2 (2) |
| 581 |
STORE_FAST 2 (y) |
| 582 |
RETURN_CONST 0 (None) |
| 583 |
|
| 584 |
%4d L12: CLEANUP_THROW |
| 585 |
L13: JUMP_BACKWARD_NO_INTERRUPT 25 (to L5) |
| 586 |
L14: CLEANUP_THROW |
| 587 |
L15: JUMP_BACKWARD_NO_INTERRUPT 9 (to L11) |
| 588 |
L16: PUSH_EXC_INFO |
| 589 |
WITH_EXCEPT_START |
| 590 |
GET_AWAITABLE 2 |
| 591 |
LOAD_CONST 0 (None) |
| 592 |
L17: SEND 4 (to L21) |
| 593 |
L18: YIELD_VALUE 1 |
| 594 |
L19: RESUME 3 |
| 595 |
JUMP_BACKWARD_NO_INTERRUPT 5 (to L17) |
| 596 |
L20: CLEANUP_THROW |
| 597 |
L21: END_SEND |
| 598 |
TO_BOOL |
| 599 |
POP_JUMP_IF_TRUE 1 (to L22) |
| 600 |
RERAISE 2 |
| 601 |
L22: POP_TOP |
| 602 |
L23: POP_EXCEPT |
| 603 |
POP_TOP |
| 604 |
POP_TOP |
| 605 |
|
| 606 |
%4d LOAD_CONST 2 (2) |
| 607 |
STORE_FAST 2 (y) |
| 608 |
RETURN_CONST 0 (None) |
| 609 |
|
| 610 |
-- L24: COPY 3 |
| 611 |
POP_EXCEPT |
| 612 |
RERAISE 1 |
| 613 |
L25: CALL_INTRINSIC_1 3 (INTRINSIC_STOPITERATION_ERROR) |
| 614 |
RERAISE 1 |
| 615 |
ExceptionTable: |
| 616 |
L1 to L3 -> L25 [0] lasti |
| 617 |
L3 to L4 -> L12 [3] |
| 618 |
L4 to L6 -> L25 [0] lasti |
| 619 |
L6 to L7 -> L16 [1] lasti |
| 620 |
L7 to L9 -> L25 [0] lasti |
| 621 |
L9 to L10 -> L14 [2] |
| 622 |
L10 to L13 -> L25 [0] lasti |
| 623 |
L14 to L15 -> L25 [0] lasti |
| 624 |
L16 to L18 -> L24 [3] lasti |
| 625 |
L18 to L19 -> L20 [6] |
| 626 |
L19 to L23 -> L24 [3] lasti |
| 627 |
L23 to L25 -> L25 [0] lasti |
| 628 |
""" % (_asyncwith.__code__.co_firstlineno, |
| 629 |
_asyncwith.__code__.co_firstlineno + 1, |
| 630 |
_asyncwith.__code__.co_firstlineno + 2, |
| 631 |
_asyncwith.__code__.co_firstlineno + 1, |
| 632 |
_asyncwith.__code__.co_firstlineno + 3, |
| 633 |
_asyncwith.__code__.co_firstlineno + 1, |
| 634 |
_asyncwith.__code__.co_firstlineno + 3, |
| 635 |
) |
| 636 |
|
| 637 |
|
| 638 |
def _tryfinally(a, b): |
| 639 |
try: |
| 640 |
return a |
| 641 |
finally: |
| 642 |
b() |
| 643 |
|
| 644 |
def _tryfinallyconst(b): |
| 645 |
try: |
| 646 |
return 1 |
| 647 |
finally: |
| 648 |
b() |
| 649 |
|
| 650 |
dis_tryfinally = """\ |
| 651 |
%4d RESUME 0 |
| 652 |
|
| 653 |
%4d NOP |
| 654 |
|
| 655 |
%4d L1: LOAD_FAST 0 (a) |
| 656 |
|
| 657 |
%4d L2: LOAD_FAST 1 (b) |
| 658 |
PUSH_NULL |
| 659 |
CALL 0 |
| 660 |
POP_TOP |
| 661 |
RETURN_VALUE |
| 662 |
|
| 663 |
-- L3: PUSH_EXC_INFO |
| 664 |
|
| 665 |
%4d LOAD_FAST 1 (b) |
| 666 |
PUSH_NULL |
| 667 |
CALL 0 |
| 668 |
POP_TOP |
| 669 |
RERAISE 0 |
| 670 |
|
| 671 |
-- L4: COPY 3 |
| 672 |
POP_EXCEPT |
| 673 |
RERAISE 1 |
| 674 |
ExceptionTable: |
| 675 |
L1 to L2 -> L3 [0] |
| 676 |
L3 to L4 -> L4 [1] lasti |
| 677 |
""" % (_tryfinally.__code__.co_firstlineno, |
| 678 |
_tryfinally.__code__.co_firstlineno + 1, |
| 679 |
_tryfinally.__code__.co_firstlineno + 2, |
| 680 |
_tryfinally.__code__.co_firstlineno + 4, |
| 681 |
_tryfinally.__code__.co_firstlineno + 4, |
| 682 |
) |
| 683 |
|
| 684 |
dis_tryfinallyconst = """\ |
| 685 |
%4d RESUME 0 |
| 686 |
|
| 687 |
%4d NOP |
| 688 |
|
| 689 |
%4d NOP |
| 690 |
|
| 691 |
%4d LOAD_FAST 0 (b) |
| 692 |
PUSH_NULL |
| 693 |
CALL 0 |
| 694 |
POP_TOP |
| 695 |
RETURN_CONST 1 (1) |
| 696 |
|
| 697 |
-- L1: PUSH_EXC_INFO |
| 698 |
|
| 699 |
%4d LOAD_FAST 0 (b) |
| 700 |
PUSH_NULL |
| 701 |
CALL 0 |
| 702 |
POP_TOP |
| 703 |
RERAISE 0 |
| 704 |
|
| 705 |
-- L2: COPY 3 |
| 706 |
POP_EXCEPT |
| 707 |
RERAISE 1 |
| 708 |
ExceptionTable: |
| 709 |
L1 to L2 -> L2 [1] lasti |
| 710 |
""" % (_tryfinallyconst.__code__.co_firstlineno, |
| 711 |
_tryfinallyconst.__code__.co_firstlineno + 1, |
| 712 |
_tryfinallyconst.__code__.co_firstlineno + 2, |
| 713 |
_tryfinallyconst.__code__.co_firstlineno + 4, |
| 714 |
_tryfinallyconst.__code__.co_firstlineno + 4, |
| 715 |
) |
| 716 |
|
| 717 |
def _g(x): |
| 718 |
yield x |
| 719 |
|
| 720 |
async def _ag(x): |
| 721 |
yield x |
| 722 |
|
| 723 |
async def _co(x): |
| 724 |
async for item in _ag(x): |
| 725 |
pass |
| 726 |
|
| 727 |
def _h(y): |
| 728 |
def foo(x): |
| 729 |
'''funcdoc''' |
| 730 |
return list(x + z for z in y) |
| 731 |
return foo |
| 732 |
|
| 733 |
dis_nested_0 = """\ |
| 734 |
-- MAKE_CELL 0 (y) |
| 735 |
|
| 736 |
%4d RESUME 0 |
| 737 |
|
| 738 |
%4d LOAD_FAST 0 (y) |
| 739 |
BUILD_TUPLE 1 |
| 740 |
LOAD_CONST 1 (<code object foo at 0x..., file "%s", line %d>) |
| 741 |
MAKE_FUNCTION |
| 742 |
SET_FUNCTION_ATTRIBUTE 8 (closure) |
| 743 |
STORE_FAST 1 (foo) |
| 744 |
|
| 745 |
%4d LOAD_FAST 1 (foo) |
| 746 |
RETURN_VALUE |
| 747 |
""" % (_h.__code__.co_firstlineno, |
| 748 |
_h.__code__.co_firstlineno + 1, |
| 749 |
__file__, |
| 750 |
_h.__code__.co_firstlineno + 1, |
| 751 |
_h.__code__.co_firstlineno + 4, |
| 752 |
) |
| 753 |
|
| 754 |
dis_nested_1 = """%s |
| 755 |
Disassembly of <code object foo at 0x..., file "%s", line %d>: |
| 756 |
-- COPY_FREE_VARS 1 |
| 757 |
MAKE_CELL 0 (x) |
| 758 |
|
| 759 |
%4d RESUME 0 |
| 760 |
|
| 761 |
%4d LOAD_GLOBAL 1 (list + NULL) |
| 762 |
LOAD_FAST 0 (x) |
| 763 |
BUILD_TUPLE 1 |
| 764 |
LOAD_CONST 1 (<code object <genexpr> at 0x..., file "%s", line %d>) |
| 765 |
MAKE_FUNCTION |
| 766 |
SET_FUNCTION_ATTRIBUTE 8 (closure) |
| 767 |
LOAD_DEREF 1 (y) |
| 768 |
GET_ITER |
| 769 |
CALL 0 |
| 770 |
CALL 1 |
| 771 |
RETURN_VALUE |
| 772 |
""" % (dis_nested_0, |
| 773 |
__file__, |
| 774 |
_h.__code__.co_firstlineno + 1, |
| 775 |
_h.__code__.co_firstlineno + 1, |
| 776 |
_h.__code__.co_firstlineno + 3, |
| 777 |
__file__, |
| 778 |
_h.__code__.co_firstlineno + 3, |
| 779 |
) |
| 780 |
|
| 781 |
dis_nested_2 = """%s |
| 782 |
Disassembly of <code object <genexpr> at 0x..., file "%s", line %d>: |
| 783 |
-- COPY_FREE_VARS 1 |
| 784 |
|
| 785 |
%4d RETURN_GENERATOR |
| 786 |
POP_TOP |
| 787 |
L1: RESUME 0 |
| 788 |
LOAD_FAST 0 (.0) |
| 789 |
GET_ITER |
| 790 |
L2: FOR_ITER 10 (to L3) |
| 791 |
STORE_FAST 1 (z) |
| 792 |
LOAD_DEREF 2 (x) |
| 793 |
LOAD_FAST 1 (z) |
| 794 |
BINARY_OP 0 (+) |
| 795 |
YIELD_VALUE 0 |
| 796 |
RESUME 5 |
| 797 |
POP_TOP |
| 798 |
JUMP_BACKWARD 12 (to L2) |
| 799 |
L3: END_FOR |
| 800 |
POP_TOP |
| 801 |
RETURN_CONST 0 (None) |
| 802 |
|
| 803 |
-- L4: CALL_INTRINSIC_1 3 (INTRINSIC_STOPITERATION_ERROR) |
| 804 |
RERAISE 1 |
| 805 |
ExceptionTable: |
| 806 |
L1 to L4 -> L4 [0] lasti |
| 807 |
""" % (dis_nested_1, |
| 808 |
__file__, |
| 809 |
_h.__code__.co_firstlineno + 3, |
| 810 |
_h.__code__.co_firstlineno + 3, |
| 811 |
) |
| 812 |
|
| 813 |
def load_test(x, y=0): |
| 814 |
a, b = x, y |
| 815 |
return a, b |
| 816 |
|
| 817 |
dis_load_test_quickened_code = """\ |
| 818 |
%3d RESUME_CHECK 0 |
| 819 |
|
| 820 |
%3d LOAD_FAST_LOAD_FAST 1 (x, y) |
| 821 |
STORE_FAST_STORE_FAST 50 (b, a) |
| 822 |
|
| 823 |
%3d LOAD_FAST_LOAD_FAST 35 (a, b) |
| 824 |
BUILD_TUPLE 2 |
| 825 |
RETURN_VALUE |
| 826 |
""" % (load_test.__code__.co_firstlineno, |
| 827 |
load_test.__code__.co_firstlineno + 1, |
| 828 |
load_test.__code__.co_firstlineno + 2) |
| 829 |
|
| 830 |
def loop_test(): |
| 831 |
for i in [1, 2, 3] * 3: |
| 832 |
load_test(i) |
| 833 |
|
| 834 |
dis_loop_test_quickened_code = """\ |
| 835 |
%3d RESUME_CHECK 0 |
| 836 |
|
| 837 |
%3d BUILD_LIST 0 |
| 838 |
LOAD_CONST 1 ((1, 2, 3)) |
| 839 |
LIST_EXTEND 1 |
| 840 |
LOAD_CONST 2 (3) |
| 841 |
BINARY_OP 5 (*) |
| 842 |
GET_ITER |
| 843 |
L1: FOR_ITER_LIST 14 (to L2) |
| 844 |
STORE_FAST 0 (i) |
| 845 |
|
| 846 |
%3d LOAD_GLOBAL_MODULE 1 (load_test + NULL) |
| 847 |
LOAD_FAST 0 (i) |
| 848 |
CALL_PY_GENERAL 1 |
| 849 |
POP_TOP |
| 850 |
JUMP_BACKWARD 16 (to L1) |
| 851 |
|
| 852 |
%3d L2: END_FOR |
| 853 |
POP_TOP |
| 854 |
RETURN_CONST 0 (None) |
| 855 |
""" % (loop_test.__code__.co_firstlineno, |
| 856 |
loop_test.__code__.co_firstlineno + 1, |
| 857 |
loop_test.__code__.co_firstlineno + 2, |
| 858 |
loop_test.__code__.co_firstlineno + 1,) |
| 859 |
|
| 860 |
def extended_arg_quick(): |
| 861 |
*_, _ = ... |
| 862 |
|
| 863 |
dis_extended_arg_quick_code = """\ |
| 864 |
%3d RESUME 0 |
| 865 |
|
| 866 |
%3d LOAD_CONST 1 (Ellipsis) |
| 867 |
EXTENDED_ARG 1 |
| 868 |
UNPACK_EX 256 |
| 869 |
POP_TOP |
| 870 |
STORE_FAST 0 (_) |
| 871 |
RETURN_CONST 0 (None) |
| 872 |
"""% (extended_arg_quick.__code__.co_firstlineno, |
| 873 |
extended_arg_quick.__code__.co_firstlineno + 1,) |
| 874 |
|
| 875 |
ADAPTIVE_WARMUP_DELAY = 2 |
| 876 |
|
| 877 |
class DisTestBase(unittest.TestCase): |
| 878 |
"Common utilities for DisTests and TestDisTraceback" |
| 879 |
|
| 880 |
def strip_addresses(self, text): |
| 881 |
return re.sub(r'\b0x[0-9A-Fa-f]+\b', '0x...', text) |
| 882 |
|
| 883 |
def assert_exception_table_increasing(self, lines): |
| 884 |
prev_start, prev_end = -1, -1 |
| 885 |
count = 0 |
| 886 |
for line in lines: |
| 887 |
m = re.match(r' L(\d+) to L(\d+) -> L\d+ \[\d+\]', line) |
| 888 |
start, end = [int(g) for g in m.groups()] |
| 889 |
self.assertGreaterEqual(end, start) |
| 890 |
self.assertGreaterEqual(start, prev_end) |
| 891 |
prev_start, prev_end = start, end |
| 892 |
count += 1 |
| 893 |
return count |
| 894 |
|
| 895 |
def do_disassembly_compare(self, got, expected): |
| 896 |
if got != expected: |
| 897 |
got = self.strip_addresses(got) |
| 898 |
self.assertEqual(got, expected) |
| 899 |
|
| 900 |
|
| 901 |
class DisTests(DisTestBase): |
| 902 |
|
| 903 |
maxDiff = None |
| 904 |
|
| 905 |
def get_disassembly(self, func, lasti=-1, wrapper=True, **kwargs): |
| 906 |
# We want to test the default printing behaviour, not the file arg |
| 907 |
output = io.StringIO() |
| 908 |
with contextlib.redirect_stdout(output): |
| 909 |
if wrapper: |
| 910 |
dis.dis(func, **kwargs) |
| 911 |
else: |
| 912 |
dis.disassemble(func, lasti, **kwargs) |
| 913 |
return output.getvalue() |
| 914 |
|
| 915 |
def get_disassemble_as_string(self, func, lasti=-1): |
| 916 |
return self.get_disassembly(func, lasti, False) |
| 917 |
|
| 918 |
def do_disassembly_test(self, func, expected, **kwargs): |
| 919 |
self.maxDiff = None |
| 920 |
got = self.get_disassembly(func, depth=0, **kwargs) |
| 921 |
self.do_disassembly_compare(got, expected) |
| 922 |
# Add checks for dis.disco |
| 923 |
if hasattr(func, '__code__'): |
| 924 |
got_disco = io.StringIO() |
| 925 |
with contextlib.redirect_stdout(got_disco): |
| 926 |
dis.disco(func.__code__, **kwargs) |
| 927 |
self.do_disassembly_compare(got_disco.getvalue(), expected) |
| 928 |
|
| 929 |
def test_opmap(self): |
| 930 |
self.assertEqual(dis.opmap["CACHE"], 0) |
| 931 |
self.assertIn(dis.opmap["LOAD_CONST"], dis.hasconst) |
| 932 |
self.assertIn(dis.opmap["STORE_NAME"], dis.hasname) |
| 933 |
|
| 934 |
def test_opname(self): |
| 935 |
self.assertEqual(dis.opname[dis.opmap["LOAD_FAST"]], "LOAD_FAST") |
| 936 |
|
| 937 |
def test_boundaries(self): |
| 938 |
self.assertEqual(dis.opmap["EXTENDED_ARG"], dis.EXTENDED_ARG) |
| 939 |
|
| 940 |
def test_widths(self): |
| 941 |
long_opcodes = set(['JUMP_BACKWARD_NO_INTERRUPT', |
| 942 |
'INSTRUMENTED_CALL_FUNCTION_EX']) |
| 943 |
for opcode, opname in enumerate(dis.opname): |
| 944 |
if opname in long_opcodes or opname.startswith("INSTRUMENTED"): |
| 945 |
continue |
| 946 |
with self.subTest(opname=opname): |
| 947 |
width = dis._OPNAME_WIDTH |
| 948 |
if opcode in dis.hasarg: |
| 949 |
width += 1 + dis._OPARG_WIDTH |
| 950 |
self.assertLessEqual(len(opname), width) |
| 951 |
|
| 952 |
def test_dis(self): |
| 953 |
self.do_disassembly_test(_f, dis_f) |
| 954 |
|
| 955 |
def test_dis_with_offsets(self): |
| 956 |
self.do_disassembly_test(_f, dis_f_with_offsets, show_offsets=True) |
| 957 |
|
| 958 |
def test_bug_708901(self): |
| 959 |
self.do_disassembly_test(bug708901, dis_bug708901) |
| 960 |
|
| 961 |
def test_bug_1333982(self): |
| 962 |
# This one is checking bytecodes generated for an `assert` statement, |
| 963 |
# so fails if the tests are run with -O. Skip this test then. |
| 964 |
if not __debug__: |
| 965 |
self.skipTest('need asserts, run without -O') |
| 966 |
|
| 967 |
self.do_disassembly_test(bug1333982, dis_bug1333982) |
| 968 |
|
| 969 |
def test_bug_42562(self): |
| 970 |
self.do_disassembly_test(bug42562, dis_bug42562) |
| 971 |
|
| 972 |
def test_bug_45757(self): |
| 973 |
# Extended arg followed by NOP |
| 974 |
self.do_disassembly_test(code_bug_45757, dis_bug_45757) |
| 975 |
|
| 976 |
def test_bug_46724(self): |
| 977 |
# Test that negative operargs are handled properly |
| 978 |
self.do_disassembly_test(bug46724, dis_bug46724) |
| 979 |
|
| 980 |
def test_kw_names(self): |
| 981 |
# Test that value is displayed for keyword argument names: |
| 982 |
self.do_disassembly_test(wrap_func_w_kwargs, dis_kw_names) |
| 983 |
|
| 984 |
def test_intrinsic_1(self): |
| 985 |
# Test that argrepr is displayed for CALL_INTRINSIC_1 |
| 986 |
self.do_disassembly_test("from math import *", dis_intrinsic_1_2) |
| 987 |
self.do_disassembly_test("+a", dis_intrinsic_1_5) |
| 988 |
self.do_disassembly_test("(*a,)", dis_intrinsic_1_6) |
| 989 |
|
| 990 |
def test_intrinsic_2(self): |
| 991 |
self.assertIn("CALL_INTRINSIC_2 1 (INTRINSIC_PREP_RERAISE_STAR)", |
| 992 |
self.get_disassembly("try: pass\nexcept* Exception: x")) |
| 993 |
|
| 994 |
def test_big_linenos(self): |
| 995 |
def func(count): |
| 996 |
namespace = {} |
| 997 |
func = "def foo():\n " + "".join(["\n "] * count + ["spam\n"]) |
| 998 |
exec(func, namespace) |
| 999 |
return namespace['foo'] |
| 1000 |
|
| 1001 |
# Test all small ranges |
| 1002 |
for i in range(1, 300): |
| 1003 |
expected = _BIG_LINENO_FORMAT % (i + 2) |
| 1004 |
self.do_disassembly_test(func(i), expected) |
| 1005 |
|
| 1006 |
# Test some larger ranges too |
| 1007 |
for i in range(300, 1000, 10): |
| 1008 |
expected = _BIG_LINENO_FORMAT % (i + 2) |
| 1009 |
self.do_disassembly_test(func(i), expected) |
| 1010 |
|
| 1011 |
for i in range(1000, 5000, 10): |
| 1012 |
expected = _BIG_LINENO_FORMAT2 % (i + 2) |
| 1013 |
self.do_disassembly_test(func(i), expected) |
| 1014 |
|
| 1015 |
from test import dis_module |
| 1016 |
self.do_disassembly_test(dis_module, dis_module_expected_results) |
| 1017 |
|
| 1018 |
def test_disassemble_str(self): |
| 1019 |
self.do_disassembly_test(expr_str, dis_expr_str) |
| 1020 |
self.do_disassembly_test(simple_stmt_str, dis_simple_stmt_str) |
| 1021 |
self.do_disassembly_test(annot_stmt_str, dis_annot_stmt_str) |
| 1022 |
self.do_disassembly_test(compound_stmt_str, dis_compound_stmt_str) |
| 1023 |
|
| 1024 |
def test_disassemble_bytes(self): |
| 1025 |
self.do_disassembly_test(_f.__code__.co_code, dis_f_co_code) |
| 1026 |
|
| 1027 |
def test_disassemble_class(self): |
| 1028 |
self.do_disassembly_test(_C, dis_c) |
| 1029 |
|
| 1030 |
def test_disassemble_instance_method(self): |
| 1031 |
self.do_disassembly_test(_C(1).__init__, dis_c_instance_method) |
| 1032 |
|
| 1033 |
def test_disassemble_instance_method_bytes(self): |
| 1034 |
method_bytecode = _C(1).__init__.__code__.co_code |
| 1035 |
self.do_disassembly_test(method_bytecode, dis_c_instance_method_bytes) |
| 1036 |
|
| 1037 |
def test_disassemble_static_method(self): |
| 1038 |
self.do_disassembly_test(_C.sm, dis_c_static_method) |
| 1039 |
|
| 1040 |
def test_disassemble_class_method(self): |
| 1041 |
self.do_disassembly_test(_C.cm, dis_c_class_method) |
| 1042 |
|
| 1043 |
def test_disassemble_generator(self): |
| 1044 |
gen_func_disas = self.get_disassembly(_g) # Generator function |
| 1045 |
gen_disas = self.get_disassembly(_g(1)) # Generator iterator |
| 1046 |
self.assertEqual(gen_disas, gen_func_disas) |
| 1047 |
|
| 1048 |
def test_disassemble_async_generator(self): |
| 1049 |
agen_func_disas = self.get_disassembly(_ag) # Async generator function |
| 1050 |
agen_disas = self.get_disassembly(_ag(1)) # Async generator iterator |
| 1051 |
self.assertEqual(agen_disas, agen_func_disas) |
| 1052 |
|
| 1053 |
def test_disassemble_coroutine(self): |
| 1054 |
coro_func_disas = self.get_disassembly(_co) # Coroutine function |
| 1055 |
coro = _co(1) # Coroutine object |
| 1056 |
coro.close() # Avoid a RuntimeWarning (never awaited) |
| 1057 |
coro_disas = self.get_disassembly(coro) |
| 1058 |
self.assertEqual(coro_disas, coro_func_disas) |
| 1059 |
|
| 1060 |
def test_disassemble_fstring(self): |
| 1061 |
self.do_disassembly_test(_fstring, dis_fstring) |
| 1062 |
|
| 1063 |
def test_disassemble_with(self): |
| 1064 |
self.do_disassembly_test(_with, dis_with) |
| 1065 |
|
| 1066 |
def test_disassemble_asyncwith(self): |
| 1067 |
self.do_disassembly_test(_asyncwith, dis_asyncwith) |
| 1068 |
|
| 1069 |
def test_disassemble_try_finally(self): |
| 1070 |
self.do_disassembly_test(_tryfinally, dis_tryfinally) |
| 1071 |
self.do_disassembly_test(_tryfinallyconst, dis_tryfinallyconst) |
| 1072 |
|
| 1073 |
def test_dis_none(self): |
| 1074 |
try: |
| 1075 |
del sys.last_exc |
| 1076 |
except AttributeError: |
| 1077 |
pass |
| 1078 |
try: |
| 1079 |
del sys.last_traceback |
| 1080 |
except AttributeError: |
| 1081 |
pass |
| 1082 |
self.assertRaises(RuntimeError, dis.dis, None) |
| 1083 |
|
| 1084 |
def test_dis_traceback(self): |
| 1085 |
self.maxDiff = None |
| 1086 |
try: |
| 1087 |
del sys.last_traceback |
| 1088 |
except AttributeError: |
| 1089 |
pass |
| 1090 |
|
| 1091 |
try: |
| 1092 |
1/0 |
| 1093 |
except Exception as e: |
| 1094 |
tb = e.__traceback__ |
| 1095 |
sys.last_exc = e |
| 1096 |
|
| 1097 |
tb_dis = self.get_disassemble_as_string(tb.tb_frame.f_code, tb.tb_lasti) |
| 1098 |
self.do_disassembly_test(None, tb_dis) |
| 1099 |
|
| 1100 |
def test_dis_object(self): |
| 1101 |
self.assertRaises(TypeError, dis.dis, object()) |
| 1102 |
|
| 1103 |
def test_disassemble_recursive(self): |
| 1104 |
def check(expected, **kwargs): |
| 1105 |
dis = self.get_disassembly(_h, **kwargs) |
| 1106 |
dis = self.strip_addresses(dis) |
| 1107 |
self.assertEqual(dis, expected) |
| 1108 |
|
| 1109 |
check(dis_nested_0, depth=0) |
| 1110 |
check(dis_nested_1, depth=1) |
| 1111 |
check(dis_nested_2, depth=2) |
| 1112 |
check(dis_nested_2, depth=3) |
| 1113 |
check(dis_nested_2, depth=None) |
| 1114 |
check(dis_nested_2) |
| 1115 |
|
| 1116 |
def test__try_compile_no_context_exc_on_error(self): |
| 1117 |
# see gh-102114 |
| 1118 |
try: |
| 1119 |
dis._try_compile(")", "") |
| 1120 |
except Exception as e: |
| 1121 |
self.assertIsNone(e.__context__) |
| 1122 |
|
| 1123 |
@staticmethod |
| 1124 |
def code_quicken(f, times=ADAPTIVE_WARMUP_DELAY): |
| 1125 |
for _ in range(times): |
| 1126 |
f() |
| 1127 |
|
| 1128 |
@cpython_only |
| 1129 |
@requires_specialization |
| 1130 |
def test_super_instructions(self): |
| 1131 |
self.code_quicken(lambda: load_test(0, 0)) |
| 1132 |
got = self.get_disassembly(load_test, adaptive=True) |
| 1133 |
self.do_disassembly_compare(got, dis_load_test_quickened_code) |
| 1134 |
|
| 1135 |
@cpython_only |
| 1136 |
@requires_specialization |
| 1137 |
def test_binary_specialize(self): |
| 1138 |
binary_op_quicken = """\ |
| 1139 |
0 RESUME_CHECK 0 |
| 1140 |
|
| 1141 |
1 LOAD_NAME 0 (a) |
| 1142 |
LOAD_NAME 1 (b) |
| 1143 |
%s |
| 1144 |
RETURN_VALUE |
| 1145 |
""" |
| 1146 |
co_int = compile('a + b', "<int>", "eval") |
| 1147 |
self.code_quicken(lambda: exec(co_int, {}, {'a': 1, 'b': 2})) |
| 1148 |
got = self.get_disassembly(co_int, adaptive=True) |
| 1149 |
self.do_disassembly_compare(got, binary_op_quicken % "BINARY_OP_ADD_INT 0 (+)") |
| 1150 |
|
| 1151 |
co_unicode = compile('a + b', "<unicode>", "eval") |
| 1152 |
self.code_quicken(lambda: exec(co_unicode, {}, {'a': 'a', 'b': 'b'})) |
| 1153 |
got = self.get_disassembly(co_unicode, adaptive=True) |
| 1154 |
self.do_disassembly_compare(got, binary_op_quicken % "BINARY_OP_ADD_UNICODE 0 (+)") |
| 1155 |
|
| 1156 |
binary_subscr_quicken = """\ |
| 1157 |
0 RESUME_CHECK 0 |
| 1158 |
|
| 1159 |
1 LOAD_NAME 0 (a) |
| 1160 |
LOAD_CONST 0 (0) |
| 1161 |
%s |
| 1162 |
RETURN_VALUE |
| 1163 |
""" |
| 1164 |
co_list = compile('a[0]', "<list>", "eval") |
| 1165 |
self.code_quicken(lambda: exec(co_list, {}, {'a': [0]})) |
| 1166 |
got = self.get_disassembly(co_list, adaptive=True) |
| 1167 |
self.do_disassembly_compare(got, binary_subscr_quicken % "BINARY_SUBSCR_LIST_INT") |
| 1168 |
|
| 1169 |
co_dict = compile('a[0]', "<dict>", "eval") |
| 1170 |
self.code_quicken(lambda: exec(co_dict, {}, {'a': {0: '1'}})) |
| 1171 |
got = self.get_disassembly(co_dict, adaptive=True) |
| 1172 |
self.do_disassembly_compare(got, binary_subscr_quicken % "BINARY_SUBSCR_DICT") |
| 1173 |
|
| 1174 |
@cpython_only |
| 1175 |
@requires_specialization |
| 1176 |
def test_load_attr_specialize(self): |
| 1177 |
load_attr_quicken = """\ |
| 1178 |
0 RESUME_CHECK 0 |
| 1179 |
|
| 1180 |
1 LOAD_CONST 0 ('a') |
| 1181 |
LOAD_ATTR_SLOT 0 (__class__) |
| 1182 |
RETURN_VALUE |
| 1183 |
""" |
| 1184 |
co = compile("'a'.__class__", "", "eval") |
| 1185 |
self.code_quicken(lambda: exec(co, {}, {})) |
| 1186 |
got = self.get_disassembly(co, adaptive=True) |
| 1187 |
self.do_disassembly_compare(got, load_attr_quicken) |
| 1188 |
|
| 1189 |
@cpython_only |
| 1190 |
@requires_specialization |
| 1191 |
def test_call_specialize(self): |
| 1192 |
call_quicken = """\ |
| 1193 |
0 RESUME_CHECK 0 |
| 1194 |
|
| 1195 |
1 LOAD_NAME 0 (str) |
| 1196 |
PUSH_NULL |
| 1197 |
LOAD_CONST 0 (1) |
| 1198 |
CALL_STR_1 1 |
| 1199 |
RETURN_VALUE |
| 1200 |
""" |
| 1201 |
co = compile("str(1)", "", "eval") |
| 1202 |
self.code_quicken(lambda: exec(co, {}, {})) |
| 1203 |
got = self.get_disassembly(co, adaptive=True) |
| 1204 |
self.do_disassembly_compare(got, call_quicken) |
| 1205 |
|
| 1206 |
@cpython_only |
| 1207 |
@requires_specialization |
| 1208 |
def test_loop_quicken(self): |
| 1209 |
# Loop can trigger a quicken where the loop is located |
| 1210 |
self.code_quicken(loop_test, 4) |
| 1211 |
got = self.get_disassembly(loop_test, adaptive=True) |
| 1212 |
expected = dis_loop_test_quickened_code |
| 1213 |
self.do_disassembly_compare(got, expected) |
| 1214 |
|
| 1215 |
@cpython_only |
| 1216 |
def test_extended_arg_quick(self): |
| 1217 |
got = self.get_disassembly(extended_arg_quick) |
| 1218 |
self.do_disassembly_compare(got, dis_extended_arg_quick_code) |
| 1219 |
|
| 1220 |
def get_cached_values(self, quickened, adaptive): |
| 1221 |
def f(): |
| 1222 |
l = [] |
| 1223 |
for i in range(42): |
| 1224 |
l.append(i) |
| 1225 |
if quickened: |
| 1226 |
self.code_quicken(f) |
| 1227 |
else: |
| 1228 |
# "copy" the code to un-quicken it: |
| 1229 |
f.__code__ = f.__code__.replace() |
| 1230 |
for instruction in _unroll_caches_as_Instructions(dis.get_instructions( |
| 1231 |
f, show_caches=True, adaptive=adaptive |
| 1232 |
), show_caches=True): |
| 1233 |
if instruction.opname == "CACHE": |
| 1234 |
yield instruction.argrepr |
| 1235 |
|
| 1236 |
@cpython_only |
| 1237 |
def test_show_caches(self): |
| 1238 |
for quickened in (False, True): |
| 1239 |
for adaptive in (False, True): |
| 1240 |
with self.subTest(f"{quickened=}, {adaptive=}"): |
| 1241 |
if adaptive: |
| 1242 |
pattern = r"^(\w+: \d+)?$" |
| 1243 |
else: |
| 1244 |
pattern = r"^(\w+: 0)?$" |
| 1245 |
caches = list(self.get_cached_values(quickened, adaptive)) |
| 1246 |
for cache in caches: |
| 1247 |
self.assertRegex(cache, pattern) |
| 1248 |
total_caches = 21 |
| 1249 |
empty_caches = 7 |
| 1250 |
self.assertEqual(caches.count(""), empty_caches) |
| 1251 |
self.assertEqual(len(caches), total_caches) |
| 1252 |
|
| 1253 |
@cpython_only |
| 1254 |
def test_show_currinstr_with_cache(self): |
| 1255 |
""" |
| 1256 |
Make sure that with lasti pointing to CACHE, it still shows the current |
| 1257 |
line correctly |
| 1258 |
""" |
| 1259 |
def f(): |
| 1260 |
print(a) |
| 1261 |
# The code above should generate a LOAD_GLOBAL which has CACHE instr after |
| 1262 |
# However, this might change in the future. So we explicitly try to find |
| 1263 |
# a CACHE entry in the instructions. If we can't do that, fail the test |
| 1264 |
|
| 1265 |
for inst in _unroll_caches_as_Instructions( |
| 1266 |
dis.get_instructions(f, show_caches=True), show_caches=True): |
| 1267 |
if inst.opname == "CACHE": |
| 1268 |
op_offset = inst.offset - 2 |
| 1269 |
cache_offset = inst.offset |
| 1270 |
break |
| 1271 |
else: |
| 1272 |
opname = inst.opname |
| 1273 |
else: |
| 1274 |
self.fail("Can't find a CACHE entry in the function provided to do the test") |
| 1275 |
|
| 1276 |
assem_op = self.get_disassembly(f.__code__, lasti=op_offset, wrapper=False) |
| 1277 |
assem_cache = self.get_disassembly(f.__code__, lasti=cache_offset, wrapper=False) |
| 1278 |
|
| 1279 |
# Make sure --> exists and points to the correct op |
| 1280 |
self.assertRegex(assem_op, fr"--> {opname}") |
| 1281 |
# Make sure when lasti points to cache, it shows the same disassembly |
| 1282 |
self.assertEqual(assem_op, assem_cache) |
| 1283 |
|
| 1284 |
|
| 1285 |
class DisWithFileTests(DisTests): |
| 1286 |
|
| 1287 |
# Run the tests again, using the file arg instead of print |
| 1288 |
def get_disassembly(self, func, lasti=-1, wrapper=True, **kwargs): |
| 1289 |
output = io.StringIO() |
| 1290 |
if wrapper: |
| 1291 |
dis.dis(func, file=output, **kwargs) |
| 1292 |
else: |
| 1293 |
dis.disassemble(func, lasti, file=output, **kwargs) |
| 1294 |
return output.getvalue() |
| 1295 |
|
| 1296 |
|
| 1297 |
if dis.code_info.__doc__ is None: |
| 1298 |
code_info_consts = "0: None" |
| 1299 |
else: |
| 1300 |
code_info_consts = "0: 'Formatted details of methods, functions, or code.'" |
| 1301 |
|
| 1302 |
code_info_code_info = f"""\ |
| 1303 |
Name: code_info |
| 1304 |
Filename: (.*) |
| 1305 |
Argument count: 1 |
| 1306 |
Positional-only arguments: 0 |
| 1307 |
Kw-only arguments: 0 |
| 1308 |
Number of locals: 1 |
| 1309 |
Stack size: \\d+ |
| 1310 |
Flags: OPTIMIZED, NEWLOCALS |
| 1311 |
Constants: |
| 1312 |
{code_info_consts} |
| 1313 |
Names: |
| 1314 |
0: _format_code_info |
| 1315 |
1: _get_code_object |
| 1316 |
Variable names: |
| 1317 |
0: x""" |
| 1318 |
|
| 1319 |
|
| 1320 |
@staticmethod |
| 1321 |
def tricky(a, b, /, x, y, z=True, *args, c, d, e=[], **kwds): |
| 1322 |
def f(c=c): |
| 1323 |
print(a, b, x, y, z, c, d, e, f) |
| 1324 |
yield a, b, x, y, z, c, d, e, f |
| 1325 |
|
| 1326 |
code_info_tricky = """\ |
| 1327 |
Name: tricky |
| 1328 |
Filename: (.*) |
| 1329 |
Argument count: 5 |
| 1330 |
Positional-only arguments: 2 |
| 1331 |
Kw-only arguments: 3 |
| 1332 |
Number of locals: 10 |
| 1333 |
Stack size: \\d+ |
| 1334 |
Flags: OPTIMIZED, NEWLOCALS, VARARGS, VARKEYWORDS, GENERATOR |
| 1335 |
Constants: |
| 1336 |
0: None |
| 1337 |
1: <code object f at (.*), file "(.*)", line (.*)> |
| 1338 |
Variable names: |
| 1339 |
0: a |
| 1340 |
1: b |
| 1341 |
2: x |
| 1342 |
3: y |
| 1343 |
4: z |
| 1344 |
5: c |
| 1345 |
6: d |
| 1346 |
7: e |
| 1347 |
8: args |
| 1348 |
9: kwds |
| 1349 |
Cell variables: |
| 1350 |
0: [abedfxyz] |
| 1351 |
1: [abedfxyz] |
| 1352 |
2: [abedfxyz] |
| 1353 |
3: [abedfxyz] |
| 1354 |
4: [abedfxyz] |
| 1355 |
5: [abedfxyz]""" |
| 1356 |
# NOTE: the order of the cell variables above depends on dictionary order! |
| 1357 |
|
| 1358 |
co_tricky_nested_f = tricky.__func__.__code__.co_consts[1] |
| 1359 |
|
| 1360 |
code_info_tricky_nested_f = """\ |
| 1361 |
Filename: (.*) |
| 1362 |
Argument count: 1 |
| 1363 |
Positional-only arguments: 0 |
| 1364 |
Kw-only arguments: 0 |
| 1365 |
Number of locals: 1 |
| 1366 |
Stack size: \\d+ |
| 1367 |
Flags: OPTIMIZED, NEWLOCALS, NESTED |
| 1368 |
Constants: |
| 1369 |
0: None |
| 1370 |
Names: |
| 1371 |
0: print |
| 1372 |
Variable names: |
| 1373 |
0: c |
| 1374 |
Free variables: |
| 1375 |
0: [abedfxyz] |
| 1376 |
1: [abedfxyz] |
| 1377 |
2: [abedfxyz] |
| 1378 |
3: [abedfxyz] |
| 1379 |
4: [abedfxyz] |
| 1380 |
5: [abedfxyz]""" |
| 1381 |
|
| 1382 |
code_info_expr_str = """\ |
| 1383 |
Name: <module> |
| 1384 |
Filename: <disassembly> |
| 1385 |
Argument count: 0 |
| 1386 |
Positional-only arguments: 0 |
| 1387 |
Kw-only arguments: 0 |
| 1388 |
Number of locals: 0 |
| 1389 |
Stack size: \\d+ |
| 1390 |
Flags: 0x0 |
| 1391 |
Constants: |
| 1392 |
0: 1 |
| 1393 |
Names: |
| 1394 |
0: x""" |
| 1395 |
|
| 1396 |
code_info_simple_stmt_str = """\ |
| 1397 |
Name: <module> |
| 1398 |
Filename: <disassembly> |
| 1399 |
Argument count: 0 |
| 1400 |
Positional-only arguments: 0 |
| 1401 |
Kw-only arguments: 0 |
| 1402 |
Number of locals: 0 |
| 1403 |
Stack size: \\d+ |
| 1404 |
Flags: 0x0 |
| 1405 |
Constants: |
| 1406 |
0: 1 |
| 1407 |
1: None |
| 1408 |
Names: |
| 1409 |
0: x""" |
| 1410 |
|
| 1411 |
code_info_compound_stmt_str = """\ |
| 1412 |
Name: <module> |
| 1413 |
Filename: <disassembly> |
| 1414 |
Argument count: 0 |
| 1415 |
Positional-only arguments: 0 |
| 1416 |
Kw-only arguments: 0 |
| 1417 |
Number of locals: 0 |
| 1418 |
Stack size: \\d+ |
| 1419 |
Flags: 0x0 |
| 1420 |
Constants: |
| 1421 |
0: 0 |
| 1422 |
1: 1 |
| 1423 |
Names: |
| 1424 |
0: x""" |
| 1425 |
|
| 1426 |
|
| 1427 |
async def async_def(): |
| 1428 |
await 1 |
| 1429 |
async for a in b: pass |
| 1430 |
async with c as d: pass |
| 1431 |
|
| 1432 |
code_info_async_def = """\ |
| 1433 |
Name: async_def |
| 1434 |
Filename: (.*) |
| 1435 |
Argument count: 0 |
| 1436 |
Positional-only arguments: 0 |
| 1437 |
Kw-only arguments: 0 |
| 1438 |
Number of locals: 2 |
| 1439 |
Stack size: \\d+ |
| 1440 |
Flags: OPTIMIZED, NEWLOCALS, COROUTINE |
| 1441 |
Constants: |
| 1442 |
0: None |
| 1443 |
1: 1 |
| 1444 |
Names: |
| 1445 |
0: b |
| 1446 |
1: c |
| 1447 |
Variable names: |
| 1448 |
0: a |
| 1449 |
1: d""" |
| 1450 |
|
| 1451 |
class CodeInfoTests(unittest.TestCase): |
| 1452 |
test_pairs = [ |
| 1453 |
(dis.code_info, code_info_code_info), |
| 1454 |
(tricky, code_info_tricky), |
| 1455 |
(co_tricky_nested_f, code_info_tricky_nested_f), |
| 1456 |
(expr_str, code_info_expr_str), |
| 1457 |
(simple_stmt_str, code_info_simple_stmt_str), |
| 1458 |
(compound_stmt_str, code_info_compound_stmt_str), |
| 1459 |
(async_def, code_info_async_def) |
| 1460 |
] |
| 1461 |
|
| 1462 |
def test_code_info(self): |
| 1463 |
self.maxDiff = 1000 |
| 1464 |
for x, expected in self.test_pairs: |
| 1465 |
self.assertRegex(dis.code_info(x), expected) |
| 1466 |
|
| 1467 |
def test_show_code(self): |
| 1468 |
self.maxDiff = 1000 |
| 1469 |
for x, expected in self.test_pairs: |
| 1470 |
with captured_stdout() as output: |
| 1471 |
dis.show_code(x) |
| 1472 |
self.assertRegex(output.getvalue(), expected+"\n") |
| 1473 |
output = io.StringIO() |
| 1474 |
dis.show_code(x, file=output) |
| 1475 |
self.assertRegex(output.getvalue(), expected) |
| 1476 |
|
| 1477 |
def test_code_info_object(self): |
| 1478 |
self.assertRaises(TypeError, dis.code_info, object()) |
| 1479 |
|
| 1480 |
def test_pretty_flags_no_flags(self): |
| 1481 |
self.assertEqual(dis.pretty_flags(0), '0x0') |
| 1482 |
|
| 1483 |
|
| 1484 |
# Fodder for instruction introspection tests |
| 1485 |
# Editing any of these may require recalculating the expected output |
| 1486 |
def outer(a=1, b=2): |
| 1487 |
def f(c=3, d=4): |
| 1488 |
def inner(e=5, f=6): |
| 1489 |
print(a, b, c, d, e, f) |
| 1490 |
print(a, b, c, d) |
| 1491 |
return inner |
| 1492 |
print(a, b, '', 1, [], {}, "Hello world!") |
| 1493 |
return f |
| 1494 |
|
| 1495 |
def jumpy(): |
| 1496 |
# This won't actually run (but that's OK, we only disassemble it) |
| 1497 |
for i in range(10): |
| 1498 |
print(i) |
| 1499 |
if i < 4: |
| 1500 |
continue |
| 1501 |
if i > 6: |
| 1502 |
break |
| 1503 |
else: |
| 1504 |
print("I can haz else clause?") |
| 1505 |
while i: |
| 1506 |
print(i) |
| 1507 |
i -= 1 |
| 1508 |
if i > 6: |
| 1509 |
continue |
| 1510 |
if i < 4: |
| 1511 |
break |
| 1512 |
else: |
| 1513 |
print("Who let lolcatz into this test suite?") |
| 1514 |
try: |
| 1515 |
1 / 0 |
| 1516 |
except ZeroDivisionError: |
| 1517 |
print("Here we go, here we go, here we go...") |
| 1518 |
else: |
| 1519 |
with i as dodgy: |
| 1520 |
print("Never reach this") |
| 1521 |
finally: |
| 1522 |
print("OK, now we're done") |
| 1523 |
|
| 1524 |
# End fodder for opinfo generation tests |
| 1525 |
expected_outer_line = 1 |
| 1526 |
_line_offset = outer.__code__.co_firstlineno - 1 |
| 1527 |
code_object_f = outer.__code__.co_consts[1] |
| 1528 |
expected_f_line = code_object_f.co_firstlineno - _line_offset |
| 1529 |
code_object_inner = code_object_f.co_consts[1] |
| 1530 |
expected_inner_line = code_object_inner.co_firstlineno - _line_offset |
| 1531 |
expected_jumpy_line = 1 |
| 1532 |
|
| 1533 |
# The following lines are useful to regenerate the expected results after |
| 1534 |
# either the fodder is modified or the bytecode generation changes |
| 1535 |
# After regeneration, update the references to code_object_f and |
| 1536 |
# code_object_inner before rerunning the tests |
| 1537 |
|
| 1538 |
def _stringify_instruction(instr): |
| 1539 |
# Since line numbers and other offsets change a lot for these |
| 1540 |
# test cases, ignore them. |
| 1541 |
return f" {instr._replace(positions=None)!r}," |
| 1542 |
|
| 1543 |
def _prepare_test_cases(): |
| 1544 |
ignore = io.StringIO() |
| 1545 |
with contextlib.redirect_stdout(ignore): |
| 1546 |
f = outer() |
| 1547 |
inner = f() |
| 1548 |
_instructions_outer = dis.get_instructions(outer, first_line=expected_outer_line) |
| 1549 |
_instructions_f = dis.get_instructions(f, first_line=expected_f_line) |
| 1550 |
_instructions_inner = dis.get_instructions(inner, first_line=expected_inner_line) |
| 1551 |
_instructions_jumpy = dis.get_instructions(jumpy, first_line=expected_jumpy_line) |
| 1552 |
result = "\n".join( |
| 1553 |
[ |
| 1554 |
"expected_opinfo_outer = [", |
| 1555 |
*map(_stringify_instruction, _instructions_outer), |
| 1556 |
"]", |
| 1557 |
"", |
| 1558 |
"expected_opinfo_f = [", |
| 1559 |
*map(_stringify_instruction, _instructions_f), |
| 1560 |
"]", |
| 1561 |
"", |
| 1562 |
"expected_opinfo_inner = [", |
| 1563 |
*map(_stringify_instruction, _instructions_inner), |
| 1564 |
"]", |
| 1565 |
"", |
| 1566 |
"expected_opinfo_jumpy = [", |
| 1567 |
*map(_stringify_instruction, _instructions_jumpy), |
| 1568 |
"]", |
| 1569 |
] |
| 1570 |
) |
| 1571 |
result = result.replace(repr(repr(code_object_f)), "repr(code_object_f)") |
| 1572 |
result = result.replace(repr(code_object_f), "code_object_f") |
| 1573 |
result = result.replace(repr(repr(code_object_inner)), "repr(code_object_inner)") |
| 1574 |
result = result.replace(repr(code_object_inner), "code_object_inner") |
| 1575 |
print(result) |
| 1576 |
|
| 1577 |
# from test.test_dis import _prepare_test_cases; _prepare_test_cases() |
| 1578 |
|
| 1579 |
Instruction = dis.Instruction |
| 1580 |
|
| 1581 |
expected_opinfo_outer = [ |
| 1582 |
Instruction(opname='MAKE_CELL', opcode=94, arg=0, argval='a', argrepr='a', offset=0, start_offset=0, starts_line=True, line_number=None, label=None, positions=None), |
| 1583 |
Instruction(opname='MAKE_CELL', opcode=94, arg=1, argval='b', argrepr='b', offset=2, start_offset=2, starts_line=False, line_number=None, label=None, positions=None), |
| 1584 |
Instruction(opname='RESUME', opcode=149, arg=0, argval=0, argrepr='', offset=4, start_offset=4, starts_line=True, line_number=1, label=None, positions=None), |
| 1585 |
Instruction(opname='LOAD_CONST', opcode=83, arg=5, argval=(3, 4), argrepr='(3, 4)', offset=6, start_offset=6, starts_line=True, line_number=2, label=None, positions=None), |
| 1586 |
Instruction(opname='LOAD_FAST', opcode=85, arg=0, argval='a', argrepr='a', offset=8, start_offset=8, starts_line=False, line_number=2, label=None, positions=None), |
| 1587 |
Instruction(opname='LOAD_FAST', opcode=85, arg=1, argval='b', argrepr='b', offset=10, start_offset=10, starts_line=False, line_number=2, label=None, positions=None), |
| 1588 |
Instruction(opname='BUILD_TUPLE', opcode=52, arg=2, argval=2, argrepr='', offset=12, start_offset=12, starts_line=False, line_number=2, label=None, positions=None), |
| 1589 |
Instruction(opname='LOAD_CONST', opcode=83, arg=1, argval=code_object_f, argrepr=repr(code_object_f), offset=14, start_offset=14, starts_line=False, line_number=2, label=None, positions=None), |
| 1590 |
Instruction(opname='MAKE_FUNCTION', opcode=26, arg=None, argval=None, argrepr='', offset=16, start_offset=16, starts_line=False, line_number=2, label=None, positions=None), |
| 1591 |
Instruction(opname='SET_FUNCTION_ATTRIBUTE', opcode=106, arg=8, argval=8, argrepr='closure', offset=18, start_offset=18, starts_line=False, line_number=2, label=None, positions=None), |
| 1592 |
Instruction(opname='SET_FUNCTION_ATTRIBUTE', opcode=106, arg=1, argval=1, argrepr='defaults', offset=20, start_offset=20, starts_line=False, line_number=2, label=None, positions=None), |
| 1593 |
Instruction(opname='STORE_FAST', opcode=110, arg=2, argval='f', argrepr='f', offset=22, start_offset=22, starts_line=False, line_number=2, label=None, positions=None), |
| 1594 |
Instruction(opname='LOAD_GLOBAL', opcode=91, arg=1, argval='print', argrepr='print + NULL', offset=24, start_offset=24, starts_line=True, line_number=7, label=None, positions=None), |
| 1595 |
Instruction(opname='LOAD_DEREF', opcode=84, arg=0, argval='a', argrepr='a', offset=34, start_offset=34, starts_line=False, line_number=7, label=None, positions=None), |
| 1596 |
Instruction(opname='LOAD_DEREF', opcode=84, arg=1, argval='b', argrepr='b', offset=36, start_offset=36, starts_line=False, line_number=7, label=None, positions=None), |
| 1597 |
Instruction(opname='LOAD_CONST', opcode=83, arg=2, argval='', argrepr="''", offset=38, start_offset=38, starts_line=False, line_number=7, label=None, positions=None), |
| 1598 |
Instruction(opname='LOAD_CONST', opcode=83, arg=3, argval=1, argrepr='1', offset=40, start_offset=40, starts_line=False, line_number=7, label=None, positions=None), |
| 1599 |
Instruction(opname='BUILD_LIST', opcode=47, arg=0, argval=0, argrepr='', offset=42, start_offset=42, starts_line=False, line_number=7, label=None, positions=None), |
| 1600 |
Instruction(opname='BUILD_MAP', opcode=48, arg=0, argval=0, argrepr='', offset=44, start_offset=44, starts_line=False, line_number=7, label=None, positions=None), |
| 1601 |
Instruction(opname='LOAD_CONST', opcode=83, arg=4, argval='Hello world!', argrepr="'Hello world!'", offset=46, start_offset=46, starts_line=False, line_number=7, label=None, positions=None), |
| 1602 |
Instruction(opname='CALL', opcode=53, arg=7, argval=7, argrepr='', offset=48, start_offset=48, starts_line=False, line_number=7, label=None, positions=None), |
| 1603 |
Instruction(opname='POP_TOP', opcode=32, arg=None, argval=None, argrepr='', offset=56, start_offset=56, starts_line=False, line_number=7, label=None, positions=None), |
| 1604 |
Instruction(opname='LOAD_FAST', opcode=85, arg=2, argval='f', argrepr='f', offset=58, start_offset=58, starts_line=True, line_number=8, label=None, positions=None), |
| 1605 |
Instruction(opname='RETURN_VALUE', opcode=36, arg=None, argval=None, argrepr='', offset=60, start_offset=60, starts_line=False, line_number=8, label=None, positions=None), |
| 1606 |
] |
| 1607 |
|
| 1608 |
expected_opinfo_f = [ |
| 1609 |
Instruction(opname='COPY_FREE_VARS', opcode=62, arg=2, argval=2, argrepr='', offset=0, start_offset=0, starts_line=True, line_number=None, label=None, positions=None), |
| 1610 |
Instruction(opname='MAKE_CELL', opcode=94, arg=0, argval='c', argrepr='c', offset=2, start_offset=2, starts_line=False, line_number=None, label=None, positions=None), |
| 1611 |
Instruction(opname='MAKE_CELL', opcode=94, arg=1, argval='d', argrepr='d', offset=4, start_offset=4, starts_line=False, line_number=None, label=None, positions=None), |
| 1612 |
Instruction(opname='RESUME', opcode=149, arg=0, argval=0, argrepr='', offset=6, start_offset=6, starts_line=True, line_number=2, label=None, positions=None), |
| 1613 |
Instruction(opname='LOAD_CONST', opcode=83, arg=2, argval=(5, 6), argrepr='(5, 6)', offset=8, start_offset=8, starts_line=True, line_number=3, label=None, positions=None), |
| 1614 |
Instruction(opname='LOAD_FAST', opcode=85, arg=3, argval='a', argrepr='a', offset=10, start_offset=10, starts_line=False, line_number=3, label=None, positions=None), |
| 1615 |
Instruction(opname='LOAD_FAST', opcode=85, arg=4, argval='b', argrepr='b', offset=12, start_offset=12, starts_line=False, line_number=3, label=None, positions=None), |
| 1616 |
Instruction(opname='LOAD_FAST', opcode=85, arg=0, argval='c', argrepr='c', offset=14, start_offset=14, starts_line=False, line_number=3, label=None, positions=None), |
| 1617 |
Instruction(opname='LOAD_FAST', opcode=85, arg=1, argval='d', argrepr='d', offset=16, start_offset=16, starts_line=False, line_number=3, label=None, positions=None), |
| 1618 |
Instruction(opname='BUILD_TUPLE', opcode=52, arg=4, argval=4, argrepr='', offset=18, start_offset=18, starts_line=False, line_number=3, label=None, positions=None), |
| 1619 |
Instruction(opname='LOAD_CONST', opcode=83, arg=1, argval=code_object_inner, argrepr=repr(code_object_inner), offset=20, start_offset=20, starts_line=False, line_number=3, label=None, positions=None), |
| 1620 |
Instruction(opname='MAKE_FUNCTION', opcode=26, arg=None, argval=None, argrepr='', offset=22, start_offset=22, starts_line=False, line_number=3, label=None, positions=None), |
| 1621 |
Instruction(opname='SET_FUNCTION_ATTRIBUTE', opcode=106, arg=8, argval=8, argrepr='closure', offset=24, start_offset=24, starts_line=False, line_number=3, label=None, positions=None), |
| 1622 |
Instruction(opname='SET_FUNCTION_ATTRIBUTE', opcode=106, arg=1, argval=1, argrepr='defaults', offset=26, start_offset=26, starts_line=False, line_number=3, label=None, positions=None), |
| 1623 |
Instruction(opname='STORE_FAST', opcode=110, arg=2, argval='inner', argrepr='inner', offset=28, start_offset=28, starts_line=False, line_number=3, label=None, positions=None), |
| 1624 |
Instruction(opname='LOAD_GLOBAL', opcode=91, arg=1, argval='print', argrepr='print + NULL', offset=30, start_offset=30, starts_line=True, line_number=5, label=None, positions=None), |
| 1625 |
Instruction(opname='LOAD_DEREF', opcode=84, arg=3, argval='a', argrepr='a', offset=40, start_offset=40, starts_line=False, line_number=5, label=None, positions=None), |
| 1626 |
Instruction(opname='LOAD_DEREF', opcode=84, arg=4, argval='b', argrepr='b', offset=42, start_offset=42, starts_line=False, line_number=5, label=None, positions=None), |
| 1627 |
Instruction(opname='LOAD_DEREF', opcode=84, arg=0, argval='c', argrepr='c', offset=44, start_offset=44, starts_line=False, line_number=5, label=None, positions=None), |
| 1628 |
Instruction(opname='LOAD_DEREF', opcode=84, arg=1, argval='d', argrepr='d', offset=46, start_offset=46, starts_line=False, line_number=5, label=None, positions=None), |
| 1629 |
Instruction(opname='CALL', opcode=53, arg=4, argval=4, argrepr='', offset=48, start_offset=48, starts_line=False, line_number=5, label=None, positions=None), |
| 1630 |
Instruction(opname='POP_TOP', opcode=32, arg=None, argval=None, argrepr='', offset=56, start_offset=56, starts_line=False, line_number=5, label=None, positions=None), |
| 1631 |
Instruction(opname='LOAD_FAST', opcode=85, arg=2, argval='inner', argrepr='inner', offset=58, start_offset=58, starts_line=True, line_number=6, label=None, positions=None), |
| 1632 |
Instruction(opname='RETURN_VALUE', opcode=36, arg=None, argval=None, argrepr='', offset=60, start_offset=60, starts_line=False, line_number=6, label=None, positions=None), |
| 1633 |
] |
| 1634 |
|
| 1635 |
expected_opinfo_inner = [ |
| 1636 |
Instruction(opname='COPY_FREE_VARS', opcode=62, arg=4, argval=4, argrepr='', offset=0, start_offset=0, starts_line=True, line_number=None, label=None, positions=None), |
| 1637 |
Instruction(opname='RESUME', opcode=149, arg=0, argval=0, argrepr='', offset=2, start_offset=2, starts_line=True, line_number=3, label=None, positions=None), |
| 1638 |
Instruction(opname='LOAD_GLOBAL', opcode=91, arg=1, argval='print', argrepr='print + NULL', offset=4, start_offset=4, starts_line=True, line_number=4, label=None, positions=None), |
| 1639 |
Instruction(opname='LOAD_DEREF', opcode=84, arg=2, argval='a', argrepr='a', offset=14, start_offset=14, starts_line=False, line_number=4, label=None, positions=None), |
| 1640 |
Instruction(opname='LOAD_DEREF', opcode=84, arg=3, argval='b', argrepr='b', offset=16, start_offset=16, starts_line=False, line_number=4, label=None, positions=None), |
| 1641 |
Instruction(opname='LOAD_DEREF', opcode=84, arg=4, argval='c', argrepr='c', offset=18, start_offset=18, starts_line=False, line_number=4, label=None, positions=None), |
| 1642 |
Instruction(opname='LOAD_DEREF', opcode=84, arg=5, argval='d', argrepr='d', offset=20, start_offset=20, starts_line=False, line_number=4, label=None, positions=None), |
| 1643 |
Instruction(opname='LOAD_FAST_LOAD_FAST', opcode=88, arg=1, argval=('e', 'f'), argrepr='e, f', offset=22, start_offset=22, starts_line=False, line_number=4, label=None, positions=None), |
| 1644 |
Instruction(opname='CALL', opcode=53, arg=6, argval=6, argrepr='', offset=24, start_offset=24, starts_line=False, line_number=4, label=None, positions=None), |
| 1645 |
Instruction(opname='POP_TOP', opcode=32, arg=None, argval=None, argrepr='', offset=32, start_offset=32, starts_line=False, line_number=4, label=None, positions=None), |
| 1646 |
Instruction(opname='RETURN_CONST', opcode=103, arg=0, argval=None, argrepr='None', offset=34, start_offset=34, starts_line=False, line_number=4, label=None, positions=None), |
| 1647 |
] |
| 1648 |
|
| 1649 |
expected_opinfo_jumpy = [ |
| 1650 |
Instruction(opname='RESUME', opcode=149, arg=0, argval=0, argrepr='', offset=0, start_offset=0, starts_line=True, line_number=1, label=None, positions=None, cache_info=None), |
| 1651 |
Instruction(opname='LOAD_GLOBAL', opcode=91, arg=1, argval='range', argrepr='range + NULL', offset=2, start_offset=2, starts_line=True, line_number=3, label=None, positions=None, cache_info=[('counter', 1, b'\x00\x00'), ('index', 1, b'\x00\x00'), ('module_keys_version', 1, b'\x00\x00'), ('builtin_keys_version', 1, b'\x00\x00')]), |
| 1652 |
Instruction(opname='LOAD_CONST', opcode=83, arg=1, argval=10, argrepr='10', offset=12, start_offset=12, starts_line=False, line_number=3, label=None, positions=None, cache_info=None), |
| 1653 |
Instruction(opname='CALL', opcode=53, arg=1, argval=1, argrepr='', offset=14, start_offset=14, starts_line=False, line_number=3, label=None, positions=None, cache_info=[('counter', 1, b'\x00\x00'), ('func_version', 2, b'\x00\x00\x00\x00')]), |
| 1654 |
Instruction(opname='GET_ITER', opcode=19, arg=None, argval=None, argrepr='', offset=22, start_offset=22, starts_line=False, line_number=3, label=None, positions=None, cache_info=None), |
| 1655 |
Instruction(opname='FOR_ITER', opcode=72, arg=30, argval=88, argrepr='to L4', offset=24, start_offset=24, starts_line=False, line_number=3, label=1, positions=None, cache_info=[('counter', 1, b'\x00\x00')]), |
| 1656 |
Instruction(opname='STORE_FAST', opcode=110, arg=0, argval='i', argrepr='i', offset=28, start_offset=28, starts_line=False, line_number=3, label=None, positions=None, cache_info=None), |
| 1657 |
Instruction(opname='LOAD_GLOBAL', opcode=91, arg=3, argval='print', argrepr='print + NULL', offset=30, start_offset=30, starts_line=True, line_number=4, label=None, positions=None, cache_info=[('counter', 1, b'\x00\x00'), ('index', 1, b'\x00\x00'), ('module_keys_version', 1, b'\x00\x00'), ('builtin_keys_version', 1, b'\x00\x00')]), |
| 1658 |
Instruction(opname='LOAD_FAST', opcode=85, arg=0, argval='i', argrepr='i', offset=40, start_offset=40, starts_line=False, line_number=4, label=None, positions=None, cache_info=None), |
| 1659 |
Instruction(opname='CALL', opcode=53, arg=1, argval=1, argrepr='', offset=42, start_offset=42, starts_line=False, line_number=4, label=None, positions=None, cache_info=[('counter', 1, b'\x00\x00'), ('func_version', 2, b'\x00\x00\x00\x00')]), |
| 1660 |
Instruction(opname='POP_TOP', opcode=32, arg=None, argval=None, argrepr='', offset=50, start_offset=50, starts_line=False, line_number=4, label=None, positions=None, cache_info=None), |
| 1661 |
Instruction(opname='LOAD_FAST', opcode=85, arg=0, argval='i', argrepr='i', offset=52, start_offset=52, starts_line=True, line_number=5, label=None, positions=None, cache_info=None), |
| 1662 |
Instruction(opname='LOAD_CONST', opcode=83, arg=2, argval=4, argrepr='4', offset=54, start_offset=54, starts_line=False, line_number=5, label=None, positions=None, cache_info=None), |
| 1663 |
Instruction(opname='COMPARE_OP', opcode=58, arg=18, argval='<', argrepr='bool(<)', offset=56, start_offset=56, starts_line=False, line_number=5, label=None, positions=None, cache_info=[('counter', 1, b'\x00\x00')]), |
| 1664 |
Instruction(opname='POP_JUMP_IF_FALSE', opcode=97, arg=2, argval=68, argrepr='to L2', offset=60, start_offset=60, starts_line=False, line_number=5, label=None, positions=None, cache_info=[('counter', 1, b'\x00\x00')]), |
| 1665 |
Instruction(opname='JUMP_BACKWARD', opcode=77, arg=22, argval=24, argrepr='to L1', offset=64, start_offset=64, starts_line=True, line_number=6, label=None, positions=None, cache_info=[('counter', 1, b'\x00\x00')]), |
| 1666 |
Instruction(opname='LOAD_FAST', opcode=85, arg=0, argval='i', argrepr='i', offset=68, start_offset=68, starts_line=True, line_number=7, label=2, positions=None, cache_info=None), |
| 1667 |
Instruction(opname='LOAD_CONST', opcode=83, arg=3, argval=6, argrepr='6', offset=70, start_offset=70, starts_line=False, line_number=7, label=None, positions=None, cache_info=None), |
| 1668 |
Instruction(opname='COMPARE_OP', opcode=58, arg=148, argval='>', argrepr='bool(>)', offset=72, start_offset=72, starts_line=False, line_number=7, label=None, positions=None, cache_info=[('counter', 1, b'\x00\x00')]), |
| 1669 |
Instruction(opname='POP_JUMP_IF_TRUE', opcode=100, arg=2, argval=84, argrepr='to L3', offset=76, start_offset=76, starts_line=False, line_number=7, label=None, positions=None, cache_info=[('counter', 1, b'\x00\x00')]), |
| 1670 |
Instruction(opname='JUMP_BACKWARD', opcode=77, arg=30, argval=24, argrepr='to L1', offset=80, start_offset=80, starts_line=False, line_number=7, label=None, positions=None, cache_info=[('counter', 1, b'\x00\x00')]), |
| 1671 |
Instruction(opname='POP_TOP', opcode=32, arg=None, argval=None, argrepr='', offset=84, start_offset=84, starts_line=True, line_number=8, label=3, positions=None, cache_info=None), |
| 1672 |
Instruction(opname='JUMP_FORWARD', opcode=79, arg=13, argval=114, argrepr='to L5', offset=86, start_offset=86, starts_line=False, line_number=8, label=None, positions=None, cache_info=None), |
| 1673 |
Instruction(opname='END_FOR', opcode=11, arg=None, argval=None, argrepr='', offset=88, start_offset=88, starts_line=True, line_number=3, label=4, positions=None, cache_info=None), |
| 1674 |
Instruction(opname='POP_TOP', opcode=32, arg=None, argval=None, argrepr='', offset=90, start_offset=90, starts_line=False, line_number=3, label=None, positions=None, cache_info=None), |
| 1675 |
Instruction(opname='LOAD_GLOBAL', opcode=91, arg=3, argval='print', argrepr='print + NULL', offset=92, start_offset=92, starts_line=True, line_number=10, label=None, positions=None, cache_info=[('counter', 1, b'\x00\x00'), ('index', 1, b'\x00\x00'), ('module_keys_version', 1, b'\x00\x00'), ('builtin_keys_version', 1, b'\x00\x00')]), |
| 1676 |
Instruction(opname='LOAD_CONST', opcode=83, arg=4, argval='I can haz else clause?', argrepr="'I can haz else clause?'", offset=102, start_offset=102, starts_line=False, line_number=10, label=None, positions=None, cache_info=None), |
| 1677 |
Instruction(opname='CALL', opcode=53, arg=1, argval=1, argrepr='', offset=104, start_offset=104, starts_line=False, line_number=10, label=None, positions=None, cache_info=[('counter', 1, b'\x00\x00'), ('func_version', 2, b'\x00\x00\x00\x00')]), |
| 1678 |
Instruction(opname='POP_TOP', opcode=32, arg=None, argval=None, argrepr='', offset=112, start_offset=112, starts_line=False, line_number=10, label=None, positions=None, cache_info=None), |
| 1679 |
Instruction(opname='LOAD_FAST_CHECK', opcode=87, arg=0, argval='i', argrepr='i', offset=114, start_offset=114, starts_line=True, line_number=11, label=5, positions=None, cache_info=None), |
| 1680 |
Instruction(opname='TO_BOOL', opcode=40, arg=None, argval=None, argrepr='', offset=116, start_offset=116, starts_line=False, line_number=11, label=None, positions=None, cache_info=[('counter', 1, b'\x00\x00'), ('version', 2, b'\x00\x00\x00\x00')]), |
| 1681 |
Instruction(opname='POP_JUMP_IF_FALSE', opcode=97, arg=40, argval=208, argrepr='to L9', offset=124, start_offset=124, starts_line=False, line_number=11, label=None, positions=None, cache_info=[('counter', 1, b'\x00\x00')]), |
| 1682 |
Instruction(opname='LOAD_GLOBAL', opcode=91, arg=3, argval='print', argrepr='print + NULL', offset=128, start_offset=128, starts_line=True, line_number=12, label=6, positions=None, cache_info=[('counter', 1, b'\x00\x00'), ('index', 1, b'\x00\x00'), ('module_keys_version', 1, b'\x00\x00'), ('builtin_keys_version', 1, b'\x00\x00')]), |
| 1683 |
Instruction(opname='LOAD_FAST', opcode=85, arg=0, argval='i', argrepr='i', offset=138, start_offset=138, starts_line=False, line_number=12, label=None, positions=None, cache_info=None), |
| 1684 |
Instruction(opname='CALL', opcode=53, arg=1, argval=1, argrepr='', offset=140, start_offset=140, starts_line=False, line_number=12, label=None, positions=None, cache_info=[('counter', 1, b'\x00\x00'), ('func_version', 2, b'\x00\x00\x00\x00')]), |
| 1685 |
Instruction(opname='POP_TOP', opcode=32, arg=None, argval=None, argrepr='', offset=148, start_offset=148, starts_line=False, line_number=12, label=None, positions=None, cache_info=None), |
| 1686 |
Instruction(opname='LOAD_FAST', opcode=85, arg=0, argval='i', argrepr='i', offset=150, start_offset=150, starts_line=True, line_number=13, label=None, positions=None, cache_info=None), |
| 1687 |
Instruction(opname='LOAD_CONST', opcode=83, arg=5, argval=1, argrepr='1', offset=152, start_offset=152, starts_line=False, line_number=13, label=None, positions=None, cache_info=None), |
| 1688 |
Instruction(opname='BINARY_OP', opcode=45, arg=23, argval=23, argrepr='-=', offset=154, start_offset=154, starts_line=False, line_number=13, label=None, positions=None, cache_info=[('counter', 1, b'\x00\x00')]), |
| 1689 |
Instruction(opname='STORE_FAST', opcode=110, arg=0, argval='i', argrepr='i', offset=158, start_offset=158, starts_line=False, line_number=13, label=None, positions=None, cache_info=None), |
| 1690 |
Instruction(opname='LOAD_FAST', opcode=85, arg=0, argval='i', argrepr='i', offset=160, start_offset=160, starts_line=True, line_number=14, label=None, positions=None, cache_info=None), |
| 1691 |
Instruction(opname='LOAD_CONST', opcode=83, arg=3, argval=6, argrepr='6', offset=162, start_offset=162, starts_line=False, line_number=14, label=None, positions=None, cache_info=None), |
| 1692 |
Instruction(opname='COMPARE_OP', opcode=58, arg=148, argval='>', argrepr='bool(>)', offset=164, start_offset=164, starts_line=False, line_number=14, label=None, positions=None, cache_info=[('counter', 1, b'\x00\x00')]), |
| 1693 |
Instruction(opname='POP_JUMP_IF_FALSE', opcode=97, arg=2, argval=176, argrepr='to L7', offset=168, start_offset=168, starts_line=False, line_number=14, label=None, positions=None, cache_info=[('counter', 1, b'\x00\x00')]), |
| 1694 |
Instruction(opname='JUMP_BACKWARD', opcode=77, arg=31, argval=114, argrepr='to L5', offset=172, start_offset=172, starts_line=True, line_number=15, label=None, positions=None, cache_info=[('counter', 1, b'\x00\x00')]), |
| 1695 |
Instruction(opname='LOAD_FAST', opcode=85, arg=0, argval='i', argrepr='i', offset=176, start_offset=176, starts_line=True, line_number=16, label=7, positions=None, cache_info=None), |
| 1696 |
Instruction(opname='LOAD_CONST', opcode=83, arg=2, argval=4, argrepr='4', offset=178, start_offset=178, starts_line=False, line_number=16, label=None, positions=None, cache_info=None), |
| 1697 |
Instruction(opname='COMPARE_OP', opcode=58, arg=18, argval='<', argrepr='bool(<)', offset=180, start_offset=180, starts_line=False, line_number=16, label=None, positions=None, cache_info=[('counter', 1, b'\x00\x00')]), |
| 1698 |
Instruction(opname='POP_JUMP_IF_FALSE', opcode=97, arg=1, argval=190, argrepr='to L8', offset=184, start_offset=184, starts_line=False, line_number=16, label=None, positions=None, cache_info=[('counter', 1, b'\x00\x00')]), |
| 1699 |
Instruction(opname='JUMP_FORWARD', opcode=79, arg=20, argval=230, argrepr='to L10', offset=188, start_offset=188, starts_line=True, line_number=17, label=None, positions=None, cache_info=None), |
| 1700 |
Instruction(opname='LOAD_FAST', opcode=85, arg=0, argval='i', argrepr='i', offset=190, start_offset=190, starts_line=True, line_number=11, label=8, positions=None, cache_info=None), |
| 1701 |
Instruction(opname='TO_BOOL', opcode=40, arg=None, argval=None, argrepr='', offset=192, start_offset=192, starts_line=False, line_number=11, label=None, positions=None, cache_info=[('counter', 1, b'\x00\x00'), ('version', 2, b'\x00\x00\x00\x00')]), |
| 1702 |
Instruction(opname='POP_JUMP_IF_FALSE', opcode=97, arg=2, argval=208, argrepr='to L9', offset=200, start_offset=200, starts_line=False, line_number=11, label=None, positions=None, cache_info=[('counter', 1, b'\x00\x00')]), |
| 1703 |
Instruction(opname='JUMP_BACKWARD', opcode=77, arg=40, argval=128, argrepr='to L6', offset=204, start_offset=204, starts_line=False, line_number=11, label=None, positions=None, cache_info=[('counter', 1, b'\x00\x00')]), |
| 1704 |
Instruction(opname='LOAD_GLOBAL', opcode=91, arg=3, argval='print', argrepr='print + NULL', offset=208, start_offset=208, starts_line=True, line_number=19, label=9, positions=None, cache_info=[('counter', 1, b'\x00\x00'), ('index', 1, b'\x00\x00'), ('module_keys_version', 1, b'\x00\x00'), ('builtin_keys_version', 1, b'\x00\x00')]), |
| 1705 |
Instruction(opname='LOAD_CONST', opcode=83, arg=6, argval='Who let lolcatz into this test suite?', argrepr="'Who let lolcatz into this test suite?'", offset=218, start_offset=218, starts_line=False, line_number=19, label=None, positions=None, cache_info=None), |
| 1706 |
Instruction(opname='CALL', opcode=53, arg=1, argval=1, argrepr='', offset=220, start_offset=220, starts_line=False, line_number=19, label=None, positions=None, cache_info=[('counter', 1, b'\x00\x00'), ('func_version', 2, b'\x00\x00\x00\x00')]), |
| 1707 |
Instruction(opname='POP_TOP', opcode=32, arg=None, argval=None, argrepr='', offset=228, start_offset=228, starts_line=False, line_number=19, label=None, positions=None, cache_info=None), |
| 1708 |
Instruction(opname='NOP', opcode=30, arg=None, argval=None, argrepr='', offset=230, start_offset=230, starts_line=True, line_number=20, label=10, positions=None, cache_info=None), |
| 1709 |
Instruction(opname='LOAD_CONST', opcode=83, arg=5, argval=1, argrepr='1', offset=232, start_offset=232, starts_line=True, line_number=21, label=None, positions=None, cache_info=None), |
| 1710 |
Instruction(opname='LOAD_CONST', opcode=83, arg=7, argval=0, argrepr='0', offset=234, start_offset=234, starts_line=False, line_number=21, label=None, positions=None, cache_info=None), |
| 1711 |
Instruction(opname='BINARY_OP', opcode=45, arg=11, argval=11, argrepr='/', offset=236, start_offset=236, starts_line=False, line_number=21, label=None, positions=None, cache_info=[('counter', 1, b'\x00\x00')]), |
| 1712 |
Instruction(opname='POP_TOP', opcode=32, arg=None, argval=None, argrepr='', offset=240, start_offset=240, starts_line=False, line_number=21, label=None, positions=None, cache_info=None), |
| 1713 |
Instruction(opname='LOAD_FAST', opcode=85, arg=0, argval='i', argrepr='i', offset=242, start_offset=242, starts_line=True, line_number=25, label=None, positions=None, cache_info=None), |
| 1714 |
Instruction(opname='BEFORE_WITH', opcode=2, arg=None, argval=None, argrepr='', offset=244, start_offset=244, starts_line=False, line_number=25, label=None, positions=None, cache_info=None), |
| 1715 |
Instruction(opname='STORE_FAST', opcode=110, arg=1, argval='dodgy', argrepr='dodgy', offset=246, start_offset=246, starts_line=False, line_number=25, label=None, positions=None, cache_info=None), |
| 1716 |
Instruction(opname='LOAD_GLOBAL', opcode=91, arg=3, argval='print', argrepr='print + NULL', offset=248, start_offset=248, starts_line=True, line_number=26, label=None, positions=None, cache_info=[('counter', 1, b'\x00\x00'), ('index', 1, b'\x00\x00'), ('module_keys_version', 1, b'\x00\x00'), ('builtin_keys_version', 1, b'\x00\x00')]), |
| 1717 |
Instruction(opname='LOAD_CONST', opcode=83, arg=8, argval='Never reach this', argrepr="'Never reach this'", offset=258, start_offset=258, starts_line=False, line_number=26, label=None, positions=None, cache_info=None), |
| 1718 |
Instruction(opname='CALL', opcode=53, arg=1, argval=1, argrepr='', offset=260, start_offset=260, starts_line=False, line_number=26, label=None, positions=None, cache_info=[('counter', 1, b'\x00\x00'), ('func_version', 2, b'\x00\x00\x00\x00')]), |
| 1719 |
Instruction(opname='POP_TOP', opcode=32, arg=None, argval=None, argrepr='', offset=268, start_offset=268, starts_line=False, line_number=26, label=None, positions=None, cache_info=None), |
| 1720 |
Instruction(opname='LOAD_CONST', opcode=83, arg=0, argval=None, argrepr='None', offset=270, start_offset=270, starts_line=True, line_number=25, label=None, positions=None, cache_info=None), |
| 1721 |
Instruction(opname='LOAD_CONST', opcode=83, arg=0, argval=None, argrepr='None', offset=272, start_offset=272, starts_line=False, line_number=25, label=None, positions=None, cache_info=None), |
| 1722 |
Instruction(opname='LOAD_CONST', opcode=83, arg=0, argval=None, argrepr='None', offset=274, start_offset=274, starts_line=False, line_number=25, label=None, positions=None, cache_info=None), |
| 1723 |
Instruction(opname='CALL', opcode=53, arg=2, argval=2, argrepr='', offset=276, start_offset=276, starts_line=False, line_number=25, label=None, positions=None, cache_info=[('counter', 1, b'\x00\x00'), ('func_version', 2, b'\x00\x00\x00\x00')]), |
| 1724 |
Instruction(opname='POP_TOP', opcode=32, arg=None, argval=None, argrepr='', offset=284, start_offset=284, starts_line=False, line_number=25, label=None, positions=None, cache_info=None), |
| 1725 |
Instruction(opname='LOAD_GLOBAL', opcode=91, arg=3, argval='print', argrepr='print + NULL', offset=286, start_offset=286, starts_line=True, line_number=28, label=11, positions=None, cache_info=[('counter', 1, b'\x00\x00'), ('index', 1, b'\x00\x00'), ('module_keys_version', 1, b'\x00\x00'), ('builtin_keys_version', 1, b'\x00\x00')]), |
| 1726 |
Instruction(opname='LOAD_CONST', opcode=83, arg=10, argval="OK, now we're done", argrepr='"OK, now we\'re done"', offset=296, start_offset=296, starts_line=False, line_number=28, label=None, positions=None, cache_info=None), |
| 1727 |
Instruction(opname='CALL', opcode=53, arg=1, argval=1, argrepr='', offset=298, start_offset=298, starts_line=False, line_number=28, label=None, positions=None, cache_info=[('counter', 1, b'\x00\x00'), ('func_version', 2, b'\x00\x00\x00\x00')]), |
| 1728 |
Instruction(opname='POP_TOP', opcode=32, arg=None, argval=None, argrepr='', offset=306, start_offset=306, starts_line=False, line_number=28, label=None, positions=None, cache_info=None), |
| 1729 |
Instruction(opname='RETURN_CONST', opcode=103, arg=0, argval=None, argrepr='None', offset=308, start_offset=308, starts_line=False, line_number=28, label=None, positions=None, cache_info=None), |
| 1730 |
Instruction(opname='PUSH_EXC_INFO', opcode=33, arg=None, argval=None, argrepr='', offset=310, start_offset=310, starts_line=True, line_number=25, label=None, positions=None, cache_info=None), |
| 1731 |
Instruction(opname='WITH_EXCEPT_START', opcode=44, arg=None, argval=None, argrepr='', offset=312, start_offset=312, starts_line=False, line_number=25, label=None, positions=None, cache_info=None), |
| 1732 |
Instruction(opname='TO_BOOL', opcode=40, arg=None, argval=None, argrepr='', offset=314, start_offset=314, starts_line=False, line_number=25, label=None, positions=None, cache_info=[('counter', 1, b'\x00\x00'), ('version', 2, b'\x00\x00\x00\x00')]), |
| 1733 |
Instruction(opname='POP_JUMP_IF_TRUE', opcode=100, arg=1, argval=328, argrepr='to L12', offset=322, start_offset=322, starts_line=False, line_number=25, label=None, positions=None, cache_info=[('counter', 1, b'\x00\x00')]), |
| 1734 |
Instruction(opname='RERAISE', opcode=102, arg=2, argval=2, argrepr='', offset=326, start_offset=326, starts_line=False, line_number=25, label=None, positions=None, cache_info=None), |
| 1735 |
Instruction(opname='POP_TOP', opcode=32, arg=None, argval=None, argrepr='', offset=328, start_offset=328, starts_line=False, line_number=25, label=12, positions=None, cache_info=None), |
| 1736 |
Instruction(opname='POP_EXCEPT', opcode=31, arg=None, argval=None, argrepr='', offset=330, start_offset=330, starts_line=False, line_number=25, label=None, positions=None, cache_info=None), |
| 1737 |
Instruction(opname='POP_TOP', opcode=32, arg=None, argval=None, argrepr='', offset=332, start_offset=332, starts_line=False, line_number=25, label=None, positions=None, cache_info=None), |
| 1738 |
Instruction(opname='POP_TOP', opcode=32, arg=None, argval=None, argrepr='', offset=334, start_offset=334, starts_line=False, line_number=25, label=None, positions=None, cache_info=None), |
| 1739 |
Instruction(opname='JUMP_BACKWARD_NO_INTERRUPT', opcode=78, arg=26, argval=286, argrepr='to L11', offset=336, start_offset=336, starts_line=False, line_number=25, label=None, positions=None, cache_info=None), |
| 1740 |
Instruction(opname='COPY', opcode=61, arg=3, argval=3, argrepr='', offset=338, start_offset=338, starts_line=True, line_number=None, label=None, positions=None, cache_info=None), |
| 1741 |
Instruction(opname='POP_EXCEPT', opcode=31, arg=None, argval=None, argrepr='', offset=340, start_offset=340, starts_line=False, line_number=None, label=None, positions=None, cache_info=None), |
| 1742 |
Instruction(opname='RERAISE', opcode=102, arg=1, argval=1, argrepr='', offset=342, start_offset=342, starts_line=False, line_number=None, label=None, positions=None, cache_info=None), |
| 1743 |
Instruction(opname='PUSH_EXC_INFO', opcode=33, arg=None, argval=None, argrepr='', offset=344, start_offset=344, starts_line=False, line_number=None, label=None, positions=None, cache_info=None), |
| 1744 |
Instruction(opname='LOAD_GLOBAL', opcode=91, arg=4, argval='ZeroDivisionError', argrepr='ZeroDivisionError', offset=346, start_offset=346, starts_line=True, line_number=22, label=None, positions=None, cache_info=[('counter', 1, b'\x00\x00'), ('index', 1, b'\x00\x00'), ('module_keys_version', 1, b'\x00\x00'), ('builtin_keys_version', 1, b'\x00\x00')]), |
| 1745 |
Instruction(opname='CHECK_EXC_MATCH', opcode=7, arg=None, argval=None, argrepr='', offset=356, start_offset=356, starts_line=False, line_number=22, label=None, positions=None, cache_info=None), |
| 1746 |
Instruction(opname='POP_JUMP_IF_FALSE', opcode=97, arg=14, argval=390, argrepr='to L13', offset=358, start_offset=358, starts_line=False, line_number=22, label=None, positions=None, cache_info=[('counter', 1, b'\x00\x00')]), |
| 1747 |
Instruction(opname='POP_TOP', opcode=32, arg=None, argval=None, argrepr='', offset=362, start_offset=362, starts_line=False, line_number=22, label=None, positions=None, cache_info=None), |
| 1748 |
Instruction(opname='LOAD_GLOBAL', opcode=91, arg=3, argval='print', argrepr='print + NULL', offset=364, start_offset=364, starts_line=True, line_number=23, label=None, positions=None, cache_info=[('counter', 1, b'\x00\x00'), ('index', 1, b'\x00\x00'), ('module_keys_version', 1, b'\x00\x00'), ('builtin_keys_version', 1, b'\x00\x00')]), |
| 1749 |
Instruction(opname='LOAD_CONST', opcode=83, arg=9, argval='Here we go, here we go, here we go...', argrepr="'Here we go, here we go, here we go...'", offset=374, start_offset=374, starts_line=False, line_number=23, label=None, positions=None, cache_info=None), |
| 1750 |
Instruction(opname='CALL', opcode=53, arg=1, argval=1, argrepr='', offset=376, start_offset=376, starts_line=False, line_number=23, label=None, positions=None, cache_info=[('counter', 1, b'\x00\x00'), ('func_version', 2, b'\x00\x00\x00\x00')]), |
| 1751 |
Instruction(opname='POP_TOP', opcode=32, arg=None, argval=None, argrepr='', offset=384, start_offset=384, starts_line=False, line_number=23, label=None, positions=None, cache_info=None), |
| 1752 |
Instruction(opname='POP_EXCEPT', opcode=31, arg=None, argval=None, argrepr='', offset=386, start_offset=386, starts_line=False, line_number=23, label=None, positions=None, cache_info=None), |
| 1753 |
Instruction(opname='JUMP_BACKWARD_NO_INTERRUPT', opcode=78, arg=52, argval=286, argrepr='to L11', offset=388, start_offset=388, starts_line=False, line_number=23, label=None, positions=None, cache_info=None), |
| 1754 |
Instruction(opname='RERAISE', opcode=102, arg=0, argval=0, argrepr='', offset=390, start_offset=390, starts_line=True, line_number=22, label=13, positions=None, cache_info=None), |
| 1755 |
Instruction(opname='COPY', opcode=61, arg=3, argval=3, argrepr='', offset=392, start_offset=392, starts_line=True, line_number=None, label=None, positions=None, cache_info=None), |
| 1756 |
Instruction(opname='POP_EXCEPT', opcode=31, arg=None, argval=None, argrepr='', offset=394, start_offset=394, starts_line=False, line_number=None, label=None, positions=None, cache_info=None), |
| 1757 |
Instruction(opname='RERAISE', opcode=102, arg=1, argval=1, argrepr='', offset=396, start_offset=396, starts_line=False, line_number=None, label=None, positions=None, cache_info=None), |
| 1758 |
Instruction(opname='PUSH_EXC_INFO', opcode=33, arg=None, argval=None, argrepr='', offset=398, start_offset=398, starts_line=False, line_number=None, label=None, positions=None, cache_info=None), |
| 1759 |
Instruction(opname='LOAD_GLOBAL', opcode=91, arg=3, argval='print', argrepr='print + NULL', offset=400, start_offset=400, starts_line=True, line_number=28, label=None, positions=None, cache_info=[('counter', 1, b'\x00\x00'), ('index', 1, b'\x00\x00'), ('module_keys_version', 1, b'\x00\x00'), ('builtin_keys_version', 1, b'\x00\x00')]), |
| 1760 |
Instruction(opname='LOAD_CONST', opcode=83, arg=10, argval="OK, now we're done", argrepr='"OK, now we\'re done"', offset=410, start_offset=410, starts_line=False, line_number=28, label=None, positions=None, cache_info=None), |
| 1761 |
Instruction(opname='CALL', opcode=53, arg=1, argval=1, argrepr='', offset=412, start_offset=412, starts_line=False, line_number=28, label=None, positions=None, cache_info=[('counter', 1, b'\x00\x00'), ('func_version', 2, b'\x00\x00\x00\x00')]), |
| 1762 |
Instruction(opname='POP_TOP', opcode=32, arg=None, argval=None, argrepr='', offset=420, start_offset=420, starts_line=False, line_number=28, label=None, positions=None, cache_info=None), |
| 1763 |
Instruction(opname='RERAISE', opcode=102, arg=0, argval=0, argrepr='', offset=422, start_offset=422, starts_line=False, line_number=28, label=None, positions=None, cache_info=None), |
| 1764 |
Instruction(opname='COPY', opcode=61, arg=3, argval=3, argrepr='', offset=424, start_offset=424, starts_line=True, line_number=None, label=None, positions=None, cache_info=None), |
| 1765 |
Instruction(opname='POP_EXCEPT', opcode=31, arg=None, argval=None, argrepr='', offset=426, start_offset=426, starts_line=False, line_number=None, label=None, positions=None, cache_info=None), |
| 1766 |
Instruction(opname='RERAISE', opcode=102, arg=1, argval=1, argrepr='', offset=428, start_offset=428, starts_line=False, line_number=None, label=None, positions=None, cache_info=None), |
| 1767 |
] |
| 1768 |
|
| 1769 |
# One last piece of inspect fodder to check the default line number handling |
| 1770 |
def simple(): pass |
| 1771 |
expected_opinfo_simple = [ |
| 1772 |
Instruction(opname='RESUME', opcode=149, arg=0, argval=0, argrepr='', offset=0, start_offset=0, starts_line=True, line_number=simple.__code__.co_firstlineno, label=None, positions=None), |
| 1773 |
Instruction(opname='RETURN_CONST', opcode=103, arg=0, argval=None, argrepr='None', offset=2, start_offset=2, starts_line=False, line_number=simple.__code__.co_firstlineno, label=None), |
| 1774 |
] |
| 1775 |
|
| 1776 |
|
| 1777 |
class InstructionTestCase(BytecodeTestCase): |
| 1778 |
|
| 1779 |
def assertInstructionsEqual(self, instrs_1, instrs_2, /): |
| 1780 |
instrs_1 = [instr_1._replace(positions=None, cache_info=None) for instr_1 in instrs_1] |
| 1781 |
instrs_2 = [instr_2._replace(positions=None, cache_info=None) for instr_2 in instrs_2] |
| 1782 |
self.assertEqual(instrs_1, instrs_2) |
| 1783 |
|
| 1784 |
class InstructionTests(InstructionTestCase): |
| 1785 |
|
| 1786 |
def __init__(self, *args): |
| 1787 |
super().__init__(*args) |
| 1788 |
self.maxDiff = None |
| 1789 |
|
| 1790 |
def test_instruction_str(self): |
| 1791 |
# smoke test for __str__ |
| 1792 |
instrs = dis.get_instructions(simple) |
| 1793 |
for instr in instrs: |
| 1794 |
str(instr) |
| 1795 |
|
| 1796 |
def test_default_first_line(self): |
| 1797 |
actual = dis.get_instructions(simple) |
| 1798 |
self.assertInstructionsEqual(list(actual), expected_opinfo_simple) |
| 1799 |
|
| 1800 |
def test_first_line_set_to_None(self): |
| 1801 |
actual = dis.get_instructions(simple, first_line=None) |
| 1802 |
self.assertInstructionsEqual(list(actual), expected_opinfo_simple) |
| 1803 |
|
| 1804 |
def test_outer(self): |
| 1805 |
actual = dis.get_instructions(outer, first_line=expected_outer_line) |
| 1806 |
self.assertInstructionsEqual(list(actual), expected_opinfo_outer) |
| 1807 |
|
| 1808 |
def test_nested(self): |
| 1809 |
with captured_stdout(): |
| 1810 |
f = outer() |
| 1811 |
actual = dis.get_instructions(f, first_line=expected_f_line) |
| 1812 |
self.assertInstructionsEqual(list(actual), expected_opinfo_f) |
| 1813 |
|
| 1814 |
def test_doubly_nested(self): |
| 1815 |
with captured_stdout(): |
| 1816 |
inner = outer()() |
| 1817 |
actual = dis.get_instructions(inner, first_line=expected_inner_line) |
| 1818 |
self.assertInstructionsEqual(list(actual), expected_opinfo_inner) |
| 1819 |
|
| 1820 |
def test_jumpy(self): |
| 1821 |
actual = dis.get_instructions(jumpy, first_line=expected_jumpy_line) |
| 1822 |
self.assertInstructionsEqual(list(actual), expected_opinfo_jumpy) |
| 1823 |
|
| 1824 |
@requires_debug_ranges() |
| 1825 |
def test_co_positions(self): |
| 1826 |
code = compile('f(\n x, y, z\n)', '<test>', 'exec') |
| 1827 |
positions = [ |
| 1828 |
instr.positions |
| 1829 |
for instr in dis.get_instructions(code) |
| 1830 |
] |
| 1831 |
expected = [ |
| 1832 |
(0, 1, 0, 0), |
| 1833 |
(1, 1, 0, 1), |
| 1834 |
(1, 1, 0, 1), |
| 1835 |
(2, 2, 2, 3), |
| 1836 |
(2, 2, 5, 6), |
| 1837 |
(2, 2, 8, 9), |
| 1838 |
(1, 3, 0, 1), |
| 1839 |
(1, 3, 0, 1), |
| 1840 |
(1, 3, 0, 1) |
| 1841 |
] |
| 1842 |
self.assertEqual(positions, expected) |
| 1843 |
|
| 1844 |
named_positions = [ |
| 1845 |
(pos.lineno, pos.end_lineno, pos.col_offset, pos.end_col_offset) |
| 1846 |
for pos in positions |
| 1847 |
] |
| 1848 |
self.assertEqual(named_positions, expected) |
| 1849 |
|
| 1850 |
@requires_debug_ranges() |
| 1851 |
def test_co_positions_missing_info(self): |
| 1852 |
code = compile('x, y, z', '<test>', 'exec') |
| 1853 |
code_without_location_table = code.replace(co_linetable=b'') |
| 1854 |
actual = dis.get_instructions(code_without_location_table) |
| 1855 |
for instruction in actual: |
| 1856 |
with self.subTest(instruction=instruction): |
| 1857 |
positions = instruction.positions |
| 1858 |
self.assertEqual(len(positions), 4) |
| 1859 |
if instruction.opname == "RESUME": |
| 1860 |
continue |
| 1861 |
self.assertIsNone(positions.lineno) |
| 1862 |
self.assertIsNone(positions.end_lineno) |
| 1863 |
self.assertIsNone(positions.col_offset) |
| 1864 |
self.assertIsNone(positions.end_col_offset) |
| 1865 |
|
| 1866 |
@requires_debug_ranges() |
| 1867 |
def test_co_positions_with_lots_of_caches(self): |
| 1868 |
def roots(a, b, c): |
| 1869 |
d = b**2 - 4 * a * c |
| 1870 |
yield (-b - cmath.sqrt(d)) / (2 * a) |
| 1871 |
if d: |
| 1872 |
yield (-b + cmath.sqrt(d)) / (2 * a) |
| 1873 |
code = roots.__code__ |
| 1874 |
ops = code.co_code[::2] |
| 1875 |
cache_opcode = opcode.opmap["CACHE"] |
| 1876 |
caches = sum(op == cache_opcode for op in ops) |
| 1877 |
non_caches = len(ops) - caches |
| 1878 |
# Make sure we have "lots of caches". If not, roots should be changed: |
| 1879 |
assert 1 / 3 <= caches / non_caches, "this test needs more caches!" |
| 1880 |
for show_caches in (False, True): |
| 1881 |
for adaptive in (False, True): |
| 1882 |
with self.subTest(f"{adaptive=}, {show_caches=}"): |
| 1883 |
co_positions = [ |
| 1884 |
positions |
| 1885 |
for op, positions in zip(ops, code.co_positions(), strict=True) |
| 1886 |
if show_caches or op != cache_opcode |
| 1887 |
] |
| 1888 |
dis_positions = [ |
| 1889 |
None if instruction.positions is None else ( |
| 1890 |
instruction.positions.lineno, |
| 1891 |
instruction.positions.end_lineno, |
| 1892 |
instruction.positions.col_offset, |
| 1893 |
instruction.positions.end_col_offset, |
| 1894 |
) |
| 1895 |
for instruction in _unroll_caches_as_Instructions(dis.get_instructions( |
| 1896 |
code, adaptive=adaptive, show_caches=show_caches |
| 1897 |
), show_caches=show_caches) |
| 1898 |
] |
| 1899 |
self.assertEqual(co_positions, dis_positions) |
| 1900 |
|
| 1901 |
def test_oparg_alias(self): |
| 1902 |
instruction = Instruction(opname="NOP", opcode=dis.opmap["NOP"], arg=None, argval=None, |
| 1903 |
argrepr='', offset=10, start_offset=10, starts_line=True, line_number=1, label=None, |
| 1904 |
positions=None) |
| 1905 |
self.assertEqual(instruction.arg, instruction.oparg) |
| 1906 |
|
| 1907 |
def test_show_caches_with_label(self): |
| 1908 |
def f(x, y, z): |
| 1909 |
if x: |
| 1910 |
res = y |
| 1911 |
else: |
| 1912 |
res = z |
| 1913 |
return res |
| 1914 |
|
| 1915 |
output = io.StringIO() |
| 1916 |
dis.dis(f.__code__, file=output, show_caches=True) |
| 1917 |
self.assertIn("L1:", output.getvalue()) |
| 1918 |
|
| 1919 |
def test_baseopname_and_baseopcode(self): |
| 1920 |
# Standard instructions |
| 1921 |
for name, code in dis.opmap.items(): |
| 1922 |
instruction = Instruction(opname=name, opcode=code, arg=None, argval=None, argrepr='', offset=0, |
| 1923 |
start_offset=0, starts_line=True, line_number=1, label=None, positions=None) |
| 1924 |
baseopname = instruction.baseopname |
| 1925 |
baseopcode = instruction.baseopcode |
| 1926 |
self.assertIsNotNone(baseopname) |
| 1927 |
self.assertIsNotNone(baseopcode) |
| 1928 |
self.assertEqual(name, baseopname) |
| 1929 |
self.assertEqual(code, baseopcode) |
| 1930 |
|
| 1931 |
# Specialized instructions |
| 1932 |
for name in opcode._specialized_opmap: |
| 1933 |
instruction = Instruction(opname=name, opcode=dis._all_opmap[name], arg=None, argval=None, argrepr='', |
| 1934 |
offset=0, start_offset=0, starts_line=True, line_number=1, label=None, positions=None) |
| 1935 |
baseopname = instruction.baseopname |
| 1936 |
baseopcode = instruction.baseopcode |
| 1937 |
self.assertIn(name, opcode._specializations[baseopname]) |
| 1938 |
self.assertEqual(opcode.opmap[baseopname], baseopcode) |
| 1939 |
|
| 1940 |
def test_jump_target(self): |
| 1941 |
# Non-jump instructions should return None |
| 1942 |
instruction = Instruction(opname="NOP", opcode=dis.opmap["NOP"], arg=None, argval=None, |
| 1943 |
argrepr='', offset=10, start_offset=10, starts_line=True, line_number=1, label=None, |
| 1944 |
positions=None) |
| 1945 |
self.assertIsNone(instruction.jump_target) |
| 1946 |
|
| 1947 |
delta = 100 |
| 1948 |
instruction = Instruction(opname="JUMP_FORWARD", opcode=dis.opmap["JUMP_FORWARD"], arg=delta, argval=delta, |
| 1949 |
argrepr='', offset=10, start_offset=10, starts_line=True, line_number=1, label=None, |
| 1950 |
positions=None) |
| 1951 |
self.assertEqual(10 + 2 + 100*2, instruction.jump_target) |
| 1952 |
|
| 1953 |
# Test negative deltas |
| 1954 |
instruction = Instruction(opname="JUMP_BACKWARD", opcode=dis.opmap["JUMP_BACKWARD"], arg=delta, argval=delta, |
| 1955 |
argrepr='', offset=200, start_offset=200, starts_line=True, line_number=1, label=None, |
| 1956 |
positions=None) |
| 1957 |
self.assertEqual(200 + 2 - 100*2 + 2*1, instruction.jump_target) |
| 1958 |
|
| 1959 |
# Make sure cache entries are handled |
| 1960 |
instruction = Instruction(opname="SEND", opcode=dis.opmap["SEND"], arg=delta, argval=delta, |
| 1961 |
argrepr='', offset=10, start_offset=10, starts_line=True, line_number=1, label=None, |
| 1962 |
positions=None) |
| 1963 |
self.assertEqual(10 + 2 + 1*2 + 100*2, instruction.jump_target) |
| 1964 |
|
| 1965 |
def test_argval_argrepr(self): |
| 1966 |
def f(opcode, oparg, offset, *init_args): |
| 1967 |
arg_resolver = dis.ArgResolver(*init_args) |
| 1968 |
return arg_resolver.get_argval_argrepr(opcode, oparg, offset) |
| 1969 |
|
| 1970 |
offset = 42 |
| 1971 |
co_consts = (0, 1, 2, 3) |
| 1972 |
names = {1: 'a', 2: 'b'} |
| 1973 |
varname_from_oparg = lambda i : names[i] |
| 1974 |
labels_map = {24: 1} |
| 1975 |
args = (offset, co_consts, names, varname_from_oparg, labels_map) |
| 1976 |
self.assertEqual(f(opcode.opmap["POP_TOP"], None, *args), (None, '')) |
| 1977 |
self.assertEqual(f(opcode.opmap["LOAD_CONST"], 1, *args), (1, '1')) |
| 1978 |
self.assertEqual(f(opcode.opmap["LOAD_GLOBAL"], 2, *args), ('a', 'a')) |
| 1979 |
self.assertEqual(f(opcode.opmap["JUMP_BACKWARD"], 11, *args), (24, 'to L1')) |
| 1980 |
self.assertEqual(f(opcode.opmap["COMPARE_OP"], 3, *args), ('<', '<')) |
| 1981 |
self.assertEqual(f(opcode.opmap["SET_FUNCTION_ATTRIBUTE"], 2, *args), (2, 'kwdefaults')) |
| 1982 |
self.assertEqual(f(opcode.opmap["BINARY_OP"], 3, *args), (3, '<<')) |
| 1983 |
self.assertEqual(f(opcode.opmap["CALL_INTRINSIC_1"], 2, *args), (2, 'INTRINSIC_IMPORT_STAR')) |
| 1984 |
|
| 1985 |
def test_custom_arg_resolver(self): |
| 1986 |
class MyArgResolver(dis.ArgResolver): |
| 1987 |
def offset_from_jump_arg(self, op, arg, offset): |
| 1988 |
return arg + 1 |
| 1989 |
|
| 1990 |
def get_label_for_offset(self, offset): |
| 1991 |
return 2 * offset |
| 1992 |
|
| 1993 |
def f(opcode, oparg, offset, *init_args): |
| 1994 |
arg_resolver = MyArgResolver(*init_args) |
| 1995 |
return arg_resolver.get_argval_argrepr(opcode, oparg, offset) |
| 1996 |
offset = 42 |
| 1997 |
self.assertEqual(f(opcode.opmap["JUMP_BACKWARD"], 1, offset), (2, 'to L4')) |
| 1998 |
self.assertEqual(f(opcode.opmap["SETUP_FINALLY"], 2, offset), (3, 'to L6')) |
| 1999 |
|
| 2000 |
|
| 2001 |
def get_instructions(self, code): |
| 2002 |
return dis._get_instructions_bytes(code) |
| 2003 |
|
| 2004 |
def test_start_offset(self): |
| 2005 |
# When no extended args are present, |
| 2006 |
# start_offset should be equal to offset |
| 2007 |
|
| 2008 |
instructions = list(dis.Bytecode(_f)) |
| 2009 |
for instruction in instructions: |
| 2010 |
self.assertEqual(instruction.offset, instruction.start_offset) |
| 2011 |
|
| 2012 |
def last_item(iterable): |
| 2013 |
return functools.reduce(lambda a, b : b, iterable) |
| 2014 |
|
| 2015 |
code = bytes([ |
| 2016 |
opcode.opmap["LOAD_FAST"], 0x00, |
| 2017 |
opcode.opmap["EXTENDED_ARG"], 0x01, |
| 2018 |
opcode.opmap["POP_JUMP_IF_TRUE"], 0xFF, |
| 2019 |
]) |
| 2020 |
labels_map = dis._make_labels_map(code) |
| 2021 |
jump = last_item(self.get_instructions(code)) |
| 2022 |
self.assertEqual(4, jump.offset) |
| 2023 |
self.assertEqual(2, jump.start_offset) |
| 2024 |
|
| 2025 |
code = bytes([ |
| 2026 |
opcode.opmap["LOAD_FAST"], 0x00, |
| 2027 |
opcode.opmap["EXTENDED_ARG"], 0x01, |
| 2028 |
opcode.opmap["EXTENDED_ARG"], 0x01, |
| 2029 |
opcode.opmap["EXTENDED_ARG"], 0x01, |
| 2030 |
opcode.opmap["POP_JUMP_IF_TRUE"], 0xFF, |
| 2031 |
opcode.opmap["CACHE"], 0x00, |
| 2032 |
]) |
| 2033 |
jump = last_item(self.get_instructions(code)) |
| 2034 |
self.assertEqual(8, jump.offset) |
| 2035 |
self.assertEqual(2, jump.start_offset) |
| 2036 |
|
| 2037 |
code = bytes([ |
| 2038 |
opcode.opmap["LOAD_FAST"], 0x00, |
| 2039 |
opcode.opmap["EXTENDED_ARG"], 0x01, |
| 2040 |
opcode.opmap["POP_JUMP_IF_TRUE"], 0xFF, |
| 2041 |
opcode.opmap["CACHE"], 0x00, |
| 2042 |
opcode.opmap["EXTENDED_ARG"], 0x01, |
| 2043 |
opcode.opmap["EXTENDED_ARG"], 0x01, |
| 2044 |
opcode.opmap["EXTENDED_ARG"], 0x01, |
| 2045 |
opcode.opmap["POP_JUMP_IF_TRUE"], 0xFF, |
| 2046 |
opcode.opmap["CACHE"], 0x00, |
| 2047 |
]) |
| 2048 |
instructions = list(self.get_instructions(code)) |
| 2049 |
# 1st jump |
| 2050 |
self.assertEqual(4, instructions[2].offset) |
| 2051 |
self.assertEqual(2, instructions[2].start_offset) |
| 2052 |
# 2nd jump |
| 2053 |
self.assertEqual(14, instructions[6].offset) |
| 2054 |
self.assertEqual(8, instructions[6].start_offset) |
| 2055 |
|
| 2056 |
def test_cache_offset_and_end_offset(self): |
| 2057 |
code = bytes([ |
| 2058 |
opcode.opmap["LOAD_GLOBAL"], 0x01, |
| 2059 |
opcode.opmap["CACHE"], 0x00, |
| 2060 |
opcode.opmap["CACHE"], 0x00, |
| 2061 |
opcode.opmap["CACHE"], 0x00, |
| 2062 |
opcode.opmap["CACHE"], 0x00, |
| 2063 |
opcode.opmap["LOAD_FAST"], 0x00, |
| 2064 |
opcode.opmap["CALL"], 0x01, |
| 2065 |
opcode.opmap["CACHE"], 0x00, |
| 2066 |
opcode.opmap["CACHE"], 0x00, |
| 2067 |
opcode.opmap["CACHE"], 0x00 |
| 2068 |
]) |
| 2069 |
instructions = list(self.get_instructions(code)) |
| 2070 |
self.assertEqual(2, instructions[0].cache_offset) |
| 2071 |
self.assertEqual(10, instructions[0].end_offset) |
| 2072 |
self.assertEqual(12, instructions[1].cache_offset) |
| 2073 |
self.assertEqual(12, instructions[1].end_offset) |
| 2074 |
self.assertEqual(14, instructions[2].cache_offset) |
| 2075 |
self.assertEqual(20, instructions[2].end_offset) |
| 2076 |
|
| 2077 |
# end_offset of the previous instruction should be equal to the |
| 2078 |
# start_offset of the following instruction |
| 2079 |
instructions = list(dis.Bytecode(self.test_cache_offset_and_end_offset)) |
| 2080 |
for prev, curr in zip(instructions, instructions[1:]): |
| 2081 |
self.assertEqual(prev.end_offset, curr.start_offset) |
| 2082 |
|
| 2083 |
|
| 2084 |
# get_instructions has its own tests above, so can rely on it to validate |
| 2085 |
# the object oriented API |
| 2086 |
class BytecodeTests(InstructionTestCase, DisTestBase): |
| 2087 |
|
| 2088 |
def test_instantiation(self): |
| 2089 |
# Test with function, method, code string and code object |
| 2090 |
for obj in [_f, _C(1).__init__, "a=1", _f.__code__]: |
| 2091 |
with self.subTest(obj=obj): |
| 2092 |
b = dis.Bytecode(obj) |
| 2093 |
self.assertIsInstance(b.codeobj, types.CodeType) |
| 2094 |
|
| 2095 |
self.assertRaises(TypeError, dis.Bytecode, object()) |
| 2096 |
|
| 2097 |
def test_iteration(self): |
| 2098 |
for obj in [_f, _C(1).__init__, "a=1", _f.__code__]: |
| 2099 |
with self.subTest(obj=obj): |
| 2100 |
via_object = list(dis.Bytecode(obj)) |
| 2101 |
via_generator = list(dis.get_instructions(obj)) |
| 2102 |
self.assertInstructionsEqual(via_object, via_generator) |
| 2103 |
|
| 2104 |
def test_explicit_first_line(self): |
| 2105 |
actual = dis.Bytecode(outer, first_line=expected_outer_line) |
| 2106 |
self.assertInstructionsEqual(list(actual), expected_opinfo_outer) |
| 2107 |
|
| 2108 |
def test_source_line_in_disassembly(self): |
| 2109 |
# Use the line in the source code |
| 2110 |
actual = dis.Bytecode(simple).dis() |
| 2111 |
actual = actual.strip().partition(" ")[0] # extract the line no |
| 2112 |
expected = str(simple.__code__.co_firstlineno) |
| 2113 |
self.assertEqual(actual, expected) |
| 2114 |
# Use an explicit first line number |
| 2115 |
actual = dis.Bytecode(simple, first_line=350).dis() |
| 2116 |
actual = actual.strip().partition(" ")[0] # extract the line no |
| 2117 |
self.assertEqual(actual, "350") |
| 2118 |
|
| 2119 |
def test_info(self): |
| 2120 |
self.maxDiff = 1000 |
| 2121 |
for x, expected in CodeInfoTests.test_pairs: |
| 2122 |
b = dis.Bytecode(x) |
| 2123 |
self.assertRegex(b.info(), expected) |
| 2124 |
|
| 2125 |
def test_disassembled(self): |
| 2126 |
actual = dis.Bytecode(_f).dis() |
| 2127 |
self.do_disassembly_compare(actual, dis_f) |
| 2128 |
|
| 2129 |
def test_from_traceback(self): |
| 2130 |
tb = get_tb() |
| 2131 |
b = dis.Bytecode.from_traceback(tb) |
| 2132 |
while tb.tb_next: tb = tb.tb_next |
| 2133 |
|
| 2134 |
self.assertEqual(b.current_offset, tb.tb_lasti) |
| 2135 |
|
| 2136 |
def test_from_traceback_dis(self): |
| 2137 |
self.maxDiff = None |
| 2138 |
tb = get_tb() |
| 2139 |
b = dis.Bytecode.from_traceback(tb) |
| 2140 |
self.assertEqual(b.dis(), dis_traceback) |
| 2141 |
|
| 2142 |
@requires_debug_ranges() |
| 2143 |
def test_bytecode_co_positions(self): |
| 2144 |
bytecode = dis.Bytecode("a=1") |
| 2145 |
for instr, positions in zip(bytecode, bytecode.codeobj.co_positions()): |
| 2146 |
assert instr.positions == positions |
| 2147 |
|
| 2148 |
class TestBytecodeTestCase(BytecodeTestCase): |
| 2149 |
def test_assert_not_in_with_op_not_in_bytecode(self): |
| 2150 |
code = compile("a = 1", "<string>", "exec") |
| 2151 |
self.assertInBytecode(code, "LOAD_CONST", 1) |
| 2152 |
self.assertNotInBytecode(code, "LOAD_NAME") |
| 2153 |
self.assertNotInBytecode(code, "LOAD_NAME", "a") |
| 2154 |
|
| 2155 |
def test_assert_not_in_with_arg_not_in_bytecode(self): |
| 2156 |
code = compile("a = 1", "<string>", "exec") |
| 2157 |
self.assertInBytecode(code, "LOAD_CONST") |
| 2158 |
self.assertInBytecode(code, "LOAD_CONST", 1) |
| 2159 |
self.assertNotInBytecode(code, "LOAD_CONST", 2) |
| 2160 |
|
| 2161 |
def test_assert_not_in_with_arg_in_bytecode(self): |
| 2162 |
code = compile("a = 1", "<string>", "exec") |
| 2163 |
with self.assertRaises(AssertionError): |
| 2164 |
self.assertNotInBytecode(code, "LOAD_CONST", 1) |
| 2165 |
|
| 2166 |
class TestFinderMethods(unittest.TestCase): |
| 2167 |
def test__find_imports(self): |
| 2168 |
cases = [ |
| 2169 |
("import a.b.c", ('a.b.c', 0, None)), |
| 2170 |
("from a.b import c", ('a.b', 0, ('c',))), |
| 2171 |
("from a.b import c as d", ('a.b', 0, ('c',))), |
| 2172 |
("from a.b import *", ('a.b', 0, ('*',))), |
| 2173 |
("from ...a.b import c as d", ('a.b', 3, ('c',))), |
| 2174 |
("from ..a.b import c as d, e as f", ('a.b', 2, ('c', 'e'))), |
| 2175 |
("from ..a.b import *", ('a.b', 2, ('*',))), |
| 2176 |
] |
| 2177 |
for src, expected in cases: |
| 2178 |
with self.subTest(src=src): |
| 2179 |
code = compile(src, "<string>", "exec") |
| 2180 |
res = tuple(dis._find_imports(code)) |
| 2181 |
self.assertEqual(len(res), 1) |
| 2182 |
self.assertEqual(res[0], expected) |
| 2183 |
|
| 2184 |
def test__find_store_names(self): |
| 2185 |
cases = [ |
| 2186 |
("x+y", ()), |
| 2187 |
("x=y=1", ('x', 'y')), |
| 2188 |
("x+=y", ('x',)), |
| 2189 |
("global x\nx=y=1", ('x', 'y')), |
| 2190 |
("global x\nz=x", ('z',)), |
| 2191 |
] |
| 2192 |
for src, expected in cases: |
| 2193 |
with self.subTest(src=src): |
| 2194 |
code = compile(src, "<string>", "exec") |
| 2195 |
res = tuple(dis._find_store_names(code)) |
| 2196 |
self.assertEqual(res, expected) |
| 2197 |
|
| 2198 |
def test_findlabels(self): |
| 2199 |
labels = dis.findlabels(jumpy.__code__.co_code) |
| 2200 |
jumps = [ |
| 2201 |
instr.offset |
| 2202 |
for instr in expected_opinfo_jumpy |
| 2203 |
if instr.is_jump_target |
| 2204 |
] |
| 2205 |
|
| 2206 |
self.assertEqual(sorted(labels), sorted(jumps)) |
| 2207 |
|
| 2208 |
def test_findlinestarts(self): |
| 2209 |
def func(): |
| 2210 |
pass |
| 2211 |
|
| 2212 |
code = func.__code__ |
| 2213 |
offsets = [linestart[0] for linestart in dis.findlinestarts(code)] |
| 2214 |
self.assertEqual(offsets, [0, 2]) |
| 2215 |
|
| 2216 |
|
| 2217 |
class TestDisTraceback(DisTestBase): |
| 2218 |
def setUp(self) -> None: |
| 2219 |
try: # We need to clean up existing tracebacks |
| 2220 |
del sys.last_exc |
| 2221 |
except AttributeError: |
| 2222 |
pass |
| 2223 |
try: # We need to clean up existing tracebacks |
| 2224 |
del sys.last_traceback |
| 2225 |
except AttributeError: |
| 2226 |
pass |
| 2227 |
return super().setUp() |
| 2228 |
|
| 2229 |
def get_disassembly(self, tb): |
| 2230 |
output = io.StringIO() |
| 2231 |
with contextlib.redirect_stdout(output): |
| 2232 |
dis.distb(tb) |
| 2233 |
return output.getvalue() |
| 2234 |
|
| 2235 |
def test_distb_empty(self): |
| 2236 |
with self.assertRaises(RuntimeError): |
| 2237 |
dis.distb() |
| 2238 |
|
| 2239 |
def test_distb_last_traceback(self): |
| 2240 |
self.maxDiff = None |
| 2241 |
# We need to have an existing last traceback in `sys`: |
| 2242 |
tb = get_tb() |
| 2243 |
sys.last_traceback = tb |
| 2244 |
|
| 2245 |
self.do_disassembly_compare(self.get_disassembly(None), dis_traceback) |
| 2246 |
|
| 2247 |
def test_distb_explicit_arg(self): |
| 2248 |
self.maxDiff = None |
| 2249 |
tb = get_tb() |
| 2250 |
|
| 2251 |
self.do_disassembly_compare(self.get_disassembly(tb), dis_traceback) |
| 2252 |
|
| 2253 |
|
| 2254 |
class TestDisTracebackWithFile(TestDisTraceback): |
| 2255 |
# Run the `distb` tests again, using the file arg instead of print |
| 2256 |
def get_disassembly(self, tb): |
| 2257 |
output = io.StringIO() |
| 2258 |
with contextlib.redirect_stdout(output): |
| 2259 |
dis.distb(tb, file=output) |
| 2260 |
return output.getvalue() |
| 2261 |
|
| 2262 |
def _unroll_caches_as_Instructions(instrs, show_caches=False): |
| 2263 |
# Cache entries are no longer reported by dis as fake instructions, |
| 2264 |
# but some tests assume that do. We should rewrite the tests to assume |
| 2265 |
# the new API, but it will be clearer to keep the tests working as |
| 2266 |
# before and do that in a separate PR. |
| 2267 |
|
| 2268 |
for instr in instrs: |
| 2269 |
yield instr |
| 2270 |
if not show_caches: |
| 2271 |
continue |
| 2272 |
|
| 2273 |
offset = instr.offset |
| 2274 |
for name, size, data in (instr.cache_info or ()): |
| 2275 |
for i in range(size): |
| 2276 |
offset += 2 |
| 2277 |
# Only show the fancy argrepr for a CACHE instruction when it's |
| 2278 |
# the first entry for a particular cache value: |
| 2279 |
if i == 0: |
| 2280 |
argrepr = f"{name}: {int.from_bytes(data, sys.byteorder)}" |
| 2281 |
else: |
| 2282 |
argrepr = "" |
| 2283 |
|
| 2284 |
yield Instruction("CACHE", CACHE, 0, None, argrepr, offset, offset, |
| 2285 |
False, None, None, instr.positions) |
| 2286 |
|
| 2287 |
|
| 2288 |
class TestDisCLI(unittest.TestCase): |
| 2289 |
|
| 2290 |
def setUp(self): |
| 2291 |
self.filename = tempfile.mktemp() |
| 2292 |
self.addCleanup(os_helper.unlink, self.filename) |
| 2293 |
|
| 2294 |
@staticmethod |
| 2295 |
def text_normalize(string): |
| 2296 |
"""Dedent *string* and strip it from its surrounding whitespaces. |
| 2297 |
|
| 2298 |
This method is used by the other utility functions so that any |
| 2299 |
string to write or to match against can be freely indented. |
| 2300 |
""" |
| 2301 |
return textwrap.dedent(string).strip() |
| 2302 |
|
| 2303 |
def set_source(self, content): |
| 2304 |
with open(self.filename, 'w') as fp: |
| 2305 |
fp.write(self.text_normalize(content)) |
| 2306 |
|
| 2307 |
def invoke_dis(self, *flags): |
| 2308 |
output = io.StringIO() |
| 2309 |
with contextlib.redirect_stdout(output): |
| 2310 |
dis.main(args=[*flags, self.filename]) |
| 2311 |
return self.text_normalize(output.getvalue()) |
| 2312 |
|
| 2313 |
def check_output(self, source, expect, *flags): |
| 2314 |
with self.subTest(source=source, flags=flags): |
| 2315 |
self.set_source(source) |
| 2316 |
res = self.invoke_dis(*flags) |
| 2317 |
expect = self.text_normalize(expect) |
| 2318 |
self.assertListEqual(res.splitlines(), expect.splitlines()) |
| 2319 |
|
| 2320 |
def test_invocation(self): |
| 2321 |
# test various combinations of parameters |
| 2322 |
base_flags = [ |
| 2323 |
('-C', '--show-caches'), |
| 2324 |
('-O', '--show-offsets'), |
| 2325 |
] |
| 2326 |
|
| 2327 |
self.set_source(''' |
| 2328 |
def f(): |
| 2329 |
print(x) |
| 2330 |
return None |
| 2331 |
''') |
| 2332 |
|
| 2333 |
for r in range(1, len(base_flags) + 1): |
| 2334 |
for choices in itertools.combinations(base_flags, r=r): |
| 2335 |
for args in itertools.product(*choices): |
| 2336 |
with self.subTest(args=args[1:]): |
| 2337 |
_ = self.invoke_dis(*args) |
| 2338 |
|
| 2339 |
with self.assertRaises(SystemExit): |
| 2340 |
# suppress argparse error message |
| 2341 |
with contextlib.redirect_stderr(io.StringIO()): |
| 2342 |
_ = self.invoke_dis('--unknown') |
| 2343 |
|
| 2344 |
def test_show_cache(self): |
| 2345 |
# test 'python -m dis -C/--show-caches' |
| 2346 |
source = 'print()' |
| 2347 |
expect = ''' |
| 2348 |
0 RESUME 0 |
| 2349 |
|
| 2350 |
1 LOAD_NAME 0 (print) |
| 2351 |
PUSH_NULL |
| 2352 |
CALL 0 |
| 2353 |
CACHE 0 (counter: 0) |
| 2354 |
CACHE 0 (func_version: 0) |
| 2355 |
CACHE 0 |
| 2356 |
POP_TOP |
| 2357 |
RETURN_CONST 0 (None) |
| 2358 |
''' |
| 2359 |
for flag in ['-C', '--show-caches']: |
| 2360 |
self.check_output(source, expect, flag) |
| 2361 |
|
| 2362 |
def test_show_offsets(self): |
| 2363 |
# test 'python -m dis -O/--show-offsets' |
| 2364 |
source = 'pass' |
| 2365 |
expect = ''' |
| 2366 |
0 0 RESUME 0 |
| 2367 |
|
| 2368 |
1 2 RETURN_CONST 0 (None) |
| 2369 |
''' |
| 2370 |
for flag in ['-O', '--show-offsets']: |
| 2371 |
self.check_output(source, expect, flag) |
| 2372 |
|
| 2373 |
|
| 2374 |
if __name__ == "__main__": |
| 2375 |
unittest.main() |