Skip to content

Commit 53a0d63

Browse files
committed
Fix bytecode for Python 3.12
`LOAD_ATTR` has been changed in Python 3.12 and it seems reusing the `LOAD_GLOBAL` logic makes the simple tests passing. I am not sure if this is correct since I'm pretty new to the code, but maybe it's still helpful.
1 parent 2e017b8 commit 53a0d63

File tree

2 files changed

+4
-2
lines changed

2 files changed

+4
-2
lines changed

js2py/translators/translating_nodes.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -543,7 +543,7 @@ def TryStatement(type, block, handler, handlers, guardedHandlers, finalizer):
543543
if handler:
544544
identifier = handler['param']['name']
545545
holder = 'PyJsHolder_%s_%d' % (to_hex(identifier),
546-
random.randrange(1e8))
546+
random.randrange(six.integer_types[-1](1e8)))
547547
identifier = repr(identifier)
548548
result += 'except PyJsException as PyJsTempException:\n'
549549
# fill in except ( catch ) block and remember to recover holder variable to its previous state

js2py/utils/injector.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
# Opcode constants used for comparison and replacecment
1515
LOAD_FAST = opcode.opmap['LOAD_FAST']
1616
LOAD_GLOBAL = opcode.opmap['LOAD_GLOBAL']
17+
LOAD_ATTR = opcode.opmap['LOAD_ATTR']
1718
STORE_FAST = opcode.opmap['STORE_FAST']
1819

1920

@@ -79,6 +80,7 @@ def append_arguments(code_obj, new_locals):
7980
(co_names.index(name), varnames.index(name)) for name in new_locals)
8081

8182
is_new_bytecode = sys.version_info >= (3, 11)
83+
is_new_load_attr = sys.version_info >= (3, 12)
8284
# Now we modify the actual bytecode
8385
modified = []
8486
drop_future_cache = False
@@ -97,7 +99,7 @@ def append_arguments(code_obj, new_locals):
9799
# it's one of the globals that we are replacing. Either way,
98100
# update its arg using the appropriate dict.
99101
drop_future_cache = False
100-
if inst.opcode == LOAD_GLOBAL:
102+
if inst.opcode == LOAD_GLOBAL or is_new_load_attr and inst.opcode == LOAD_ATTR:
101103
idx = inst.arg
102104
if is_new_bytecode:
103105
idx = idx // 2

0 commit comments

Comments
 (0)