|
| 1 | +From fd7df4a91fb08060914c7b1d9e94583d18f3371b Mon Sep 17 00:00:00 2001 |
| 2 | +From: Felix Yan <felixonmars@archlinux.org> |
| 3 | +Date: Wed, 17 Apr 2024 16:47:47 +0300 |
| 4 | +Subject: [PATCH] Fix bytecode for Python 3.12 |
| 5 | + |
| 6 | +`LOAD_ATTR` has been changed in Python 3.12 and it seems reusing the |
| 7 | +`LOAD_GLOBAL` logic makes the simple tests passing. |
| 8 | + |
| 9 | +I am not sure if this is correct since I'm pretty new to the code, but |
| 10 | +maybe it's still helpful. |
| 11 | +--- |
| 12 | + js2py/translators/translating_nodes.py | 2 +- |
| 13 | + js2py/utils/injector.py | 4 +++- |
| 14 | + 2 files changed, 4 insertions(+), 2 deletions(-) |
| 15 | + |
| 16 | +Index: Js2Py-0.74/js2py/translators/translating_nodes.py |
| 17 | +=================================================================== |
| 18 | +--- Js2Py-0.74.orig/js2py/translators/translating_nodes.py |
| 19 | ++++ Js2Py-0.74/js2py/translators/translating_nodes.py |
| 20 | +@@ -538,7 +538,7 @@ def TryStatement(type, block, handler, h |
| 21 | + if handler: |
| 22 | + identifier = handler['param']['name'] |
| 23 | + holder = 'PyJsHolder_%s_%d' % (to_hex(identifier), |
| 24 | +- random.randrange(1e8)) |
| 25 | ++ random.randrange(six.integer_types[-1](1e8))) |
| 26 | + identifier = repr(identifier) |
| 27 | + result += 'except PyJsException as PyJsTempException:\n' |
| 28 | + # fill in except ( catch ) block and remember to recover holder variable to its previous state |
| 29 | +Index: Js2Py-0.74/js2py/utils/injector.py |
| 30 | +=================================================================== |
| 31 | +--- Js2Py-0.74.orig/js2py/utils/injector.py |
| 32 | ++++ Js2Py-0.74/js2py/utils/injector.py |
| 33 | +@@ -13,6 +13,7 @@ chr = lambda x: x |
| 34 | + # Opcode constants used for comparison and replacecment |
| 35 | + LOAD_FAST = opcode.opmap['LOAD_FAST'] |
| 36 | + LOAD_GLOBAL = opcode.opmap['LOAD_GLOBAL'] |
| 37 | ++LOAD_ATTR = opcode.opmap['LOAD_ATTR'] |
| 38 | + STORE_FAST = opcode.opmap['STORE_FAST'] |
| 39 | + |
| 40 | + |
| 41 | +@@ -88,6 +89,7 @@ def append_arguments(code_obj, new_local |
| 42 | + (co_names.index(name), varnames.index(name)) for name in new_locals) |
| 43 | + |
| 44 | + is_new_bytecode = sys.version_info >= (3, 11) |
| 45 | ++ is_new_load_attr = sys.version_info >= (3, 12) |
| 46 | + # Now we modify the actual bytecode |
| 47 | + modified = [] |
| 48 | + drop_future_cache = False |
| 49 | +@@ -106,7 +108,7 @@ def append_arguments(code_obj, new_local |
| 50 | + # it's one of the globals that we are replacing. Either way, |
| 51 | + # update its arg using the appropriate dict. |
| 52 | + drop_future_cache = False |
| 53 | +- if inst.opcode == LOAD_GLOBAL: |
| 54 | ++ if inst.opcode == LOAD_GLOBAL or (is_new_load_attr and inst.opcode == LOAD_ATTR): |
| 55 | + idx = inst.arg |
| 56 | + if is_new_bytecode: |
| 57 | + idx = idx // 2 |
0 commit comments