Skip to content

Commit 3b7a93b

Browse files
DinoVmeta-codesync[bot]
authored andcommitted
Light up to emit eager imports on meta Python
Summary: Currently on 3.14 we don't ever emit `EAGER_IMPORT_NAME`. This changes us to check if the opcode is defined by the interpreter and emit it only in that case. That way we'll work on stock CPython or meta-Python. Reviewed By: alexmalyshev Differential Revision: D83846390 fbshipit-source-id: d83e474d4f838c1902f556e80961f6f519057470
1 parent ef9ff51 commit 3b7a93b

2 files changed

Lines changed: 8 additions & 4 deletions

File tree

cinderx/PythonLib/cinderx/compiler/opcodes.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,7 @@
304304
DELETE_SUBSCR=2,
305305
DICT_MERGE=lambda oparg: 5 + (oparg - 1),
306306
DICT_UPDATE=lambda oparg: 2 + (oparg - 1),
307+
EAGER_IMPORT_NAME=2,
307308
END_ASYNC_FOR=2,
308309
END_FOR=1,
309310
END_SEND=2,
@@ -545,6 +546,7 @@
545546
DELETE_SUBSCR=0,
546547
DICT_MERGE=lambda oparg: 4 + (oparg - 1),
547548
DICT_UPDATE=lambda oparg: 1 + (oparg - 1),
549+
EAGER_IMPORT_NAME=1,
548550
END_ASYNC_FOR=0,
549551
END_FOR=0,
550552
END_SEND=1,

cinderx/PythonLib/cinderx/compiler/pycodegen.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import importlib.util
1414
import itertools
1515
import marshal
16+
import opcode
1617
import os
1718
import sys
1819
from ast import AST, ClassDef
@@ -3897,7 +3898,11 @@ def check_name(self, name: str) -> int:
38973898
return super().check_name(name)
38983899

38993900
def emit_import_name(self, name: str) -> None:
3900-
if isinstance(self.scope, ModuleScope) and not self.setups:
3901+
# If we're not on meta-Python we don't EAGER_IMPORT_NAME so only
3902+
# emit it when it's available.
3903+
if "EAGER_IMPORT_NAME" not in opcode.opmap or (
3904+
isinstance(self.scope, ModuleScope) and not self.setups
3905+
):
39013906
self.emit("IMPORT_NAME", name)
39023907
else:
39033908
self.emit("EAGER_IMPORT_NAME", name)
@@ -6359,9 +6364,6 @@ def make_try_body_block(self) -> Block | None:
63596364
def emit_binary_subscr(self) -> None:
63606365
self.emit("BINARY_OP", find_op_idx("NB_SUBSCR"))
63616366

6362-
def emit_import_name(self, name: str) -> None:
6363-
self.emit("IMPORT_NAME", name)
6364-
63656367
def emit_load_assertion_error(self, loc: AST | SrcLocation | None = None) -> None:
63666368
if loc:
63676369
self.graph.emit_with_loc("LOAD_COMMON_CONSTANT", AssertionError, loc)

0 commit comments

Comments
 (0)