Skip to content

Commit eb27cd8

Browse files
remove as many conditions as possible from the code and prefer opcode comparison to name comparison
1 parent 75a6b04 commit eb27cd8

6 files changed

Lines changed: 353 additions & 533 deletions

File tree

src/bytecode/bytecode.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
# alias to keep the 'bytecode' variable free
2-
import sys
32
import types
43
from abc import abstractmethod
54
from typing import (
@@ -28,7 +27,6 @@
2827
TryBegin,
2928
TryEnd,
3029
)
31-
from bytecode.utils import PY311
3230

3331

3432
class BaseBytecode:
@@ -298,7 +296,7 @@ def to_code(
298296
) -> types.CodeType:
299297
# Prevent reconverting the concrete bytecode to bytecode and cfg to do the
300298
# calculation if we need to do it.
301-
if stacksize is None or (PY311 and compute_exception_stack_depths):
299+
if stacksize is None or compute_exception_stack_depths:
302300
cfg = _bytecode.ControlFlowGraph.from_bytecode(self)
303301
stacksize = cfg.compute_stacksize(
304302
check_pre_and_post=check_pre_and_post,

src/bytecode/cfg.py

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import sys
21
import types
32
from collections import defaultdict
43
from dataclasses import dataclass
@@ -23,7 +22,7 @@
2322
from bytecode.concrete import ConcreteInstr
2423
from bytecode.flags import CompilerFlags
2524
from bytecode.instr import UNSET, Instr, Label, SetLineno, TryBegin, TryEnd
26-
from bytecode.utils import PY310, PY311, PY313
25+
from bytecode.utils import PY313
2726

2827
T = TypeVar("T", bound="BasicBlock")
2928
U = TypeVar("U", bound="ControlFlowGraph")
@@ -447,18 +446,10 @@ def _compute_exception_handler_stack_usage(
447446
def _is_stacksize_computation_relevant(
448447
self, block_id: int, fingerprint: Tuple[int, Optional[bool]]
449448
) -> bool:
450-
if PY311:
451-
# The computation is relevant if the block was not visited previously
452-
# with the same starting size and exception handler status than the
453-
# one in use
454-
return fingerprint not in self.common.blocks_startsizes[block_id]
455-
else:
456-
# The computation is relevant if the block was only visited with smaller
457-
# starting sizes than the one in use
458-
if sizes := self.common.blocks_startsizes[block_id]:
459-
return fingerprint[0] > max(f[0] for f in sizes)
460-
else:
461-
return True
449+
# The computation is relevant if the block was not visited previously
450+
# with the same starting size and exception handler status than the
451+
# one in use
452+
return fingerprint not in self.common.blocks_startsizes[block_id]
462453

463454

464455
class ControlFlowGraph(_bytecode.BaseBytecode):
@@ -521,11 +512,10 @@ def compute_stacksize(
521512
)
522513

523514
# Starting with Python 3.10, generator and coroutines start with one object
524-
# on the stack (None, anything is an error).
515+
# on the stack (None, anything else is an error).
525516
initial_stack_size = 0
526517
if (
527518
not PY313 # under 3.13+ RETURN_GENERATOR make this explicit
528-
and PY310
529519
and self.flags
530520
& (
531521
CompilerFlags.GENERATOR
@@ -628,7 +618,7 @@ def _get_instructions(
628618
# argument rather than a Label. This is fine for comparison
629619
# purposes which is our sole goal here.
630620
c_instr = ConcreteInstr(
631-
instr.name,
621+
instr._name,
632622
self.get_block_index(target_block),
633623
location=instr.location,
634624
)

0 commit comments

Comments
 (0)