Skip to content

Commit 37f38e4

Browse files
committed
Don't update global tables...
Work off of copies of them instead. Issue rocky#503
1 parent ab79803 commit 37f38e4

File tree

10 files changed

+114
-78
lines changed

10 files changed

+114
-78
lines changed

uncompyle6/semantics/customize.py

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright (c) 2018-2019, 2021-2022 by Rocky Bernstein
1+
# Copyright (c) 2018-2019, 2021-2022 2024 by Rocky Bernstein
22
#
33
# This program is free software: you can redistribute it and/or modify
44
# it under the terms of the GNU General Public License as published by
@@ -17,24 +17,25 @@
1717
"""
1818

1919
from uncompyle6.parsers.treenode import SyntaxTree
20+
from uncompyle6.scanners.tok import Token
2021
from uncompyle6.semantics.consts import (
2122
INDENT_PER_LEVEL,
2223
NO_PARENTHESIS_EVER,
2324
PRECEDENCE,
24-
TABLE_R,
2525
TABLE_DIRECT,
26+
TABLE_R,
2627
)
2728
from uncompyle6.semantics.helper import flatten_list
28-
from uncompyle6.scanners.tok import Token
2929

3030

3131
def customize_for_version(self, is_pypy, version):
32+
self.TABLE_DIRECT = TABLE_DIRECT.copy()
3233
if is_pypy:
3334
########################
3435
# PyPy changes
3536
#######################
3637
# fmt: off
37-
TABLE_DIRECT.update({
38+
self.TABLE_DIRECT.update({
3839

3940
"assert": ("%|assert %c\n", 0),
4041
# This can happen as a result of an if transformation
@@ -114,7 +115,7 @@ def n_call_kw_pypy37(node):
114115
########################
115116
# Without PyPy
116117
#######################
117-
TABLE_DIRECT.update(
118+
self.TABLE_DIRECT.update(
118119
{
119120
# "assert" and "assert_expr" are added via transform rules.
120121
"assert": ("%|assert %c\n", 0),
@@ -133,23 +134,23 @@ def n_call_kw_pypy37(node):
133134
)
134135
if version >= (3, 0):
135136
if version >= (3, 2):
136-
TABLE_DIRECT.update(
137+
self.TABLE_DIRECT.update(
137138
{"del_deref_stmt": ("%|del %c\n", 0), "DELETE_DEREF": ("%{pattr}", 0)}
138139
)
139140
from uncompyle6.semantics.customize3 import customize_for_version3
140141

141142
customize_for_version3(self, version)
142143
else: # < 3.0
143-
TABLE_DIRECT.update(
144+
self.TABLE_DIRECT.update(
144145
{"except_cond3": ("%|except %c, %c:\n", (1, "expr"), (-2, "store"))}
145146
)
146147
if version <= (2, 6):
147-
TABLE_DIRECT["testtrue_then"] = TABLE_DIRECT["testtrue"]
148+
self.TABLE_DIRECT["testtrue_then"] = self.TABLE_DIRECT["testtrue"]
148149

149150
if (2, 4) <= version <= (2, 6):
150-
TABLE_DIRECT.update({"comp_for": (" for %c in %c", 3, 1)})
151+
self.TABLE_DIRECT.update({"comp_for": (" for %c in %c", 3, 1)})
151152
else:
152-
TABLE_DIRECT.update({"comp_for": (" for %c in %c%c", 2, 0, 3)})
153+
self.TABLE_DIRECT.update({"comp_for": (" for %c in %c%c", 2, 0, 3)})
153154

154155
if version >= (2, 5):
155156
from uncompyle6.semantics.customize25 import customize_for_version25
@@ -197,7 +198,7 @@ def n_call_kw_pypy37(node):
197198
)
198199
],
199200
)
200-
TABLE_DIRECT.update(
201+
self.TABLE_DIRECT.update(
201202
{
202203
"importmultiple": ("%|import %c%c\n", 2, 3),
203204
"import_cont": (", %c", 2),
@@ -247,9 +248,9 @@ def n_call(node):
247248
self.n_call = n_call
248249

249250
else: # 1.0 <= version <= 2.3:
250-
TABLE_DIRECT.update({"if1_stmt": ("%|if 1\n%+%c%-", 5)})
251+
self.TABLE_DIRECT.update({"if1_stmt": ("%|if 1\n%+%c%-", 5)})
251252
if version <= (2, 1):
252-
TABLE_DIRECT.update(
253+
self.TABLE_DIRECT.update(
253254
{
254255
"importmultiple": ("%c", 2),
255256
# FIXME: not quite right. We have indiividual imports
@@ -263,7 +264,8 @@ def n_call(node):
263264

264265
# < 3.0 continues
265266

266-
TABLE_R.update(
267+
self.TABLE_R = TABLE_R.copy()
268+
self.TABLE_R.update(
267269
{
268270
"STORE_SLICE+0": ("%c[:]", 0),
269271
"STORE_SLICE+1": ("%c[%p:]", 0, (1, -1)),
@@ -275,7 +277,7 @@ def n_call(node):
275277
"DELETE_SLICE+3": ("%|del %c[%c:%c]\n", 0, 1, 2),
276278
}
277279
)
278-
TABLE_DIRECT.update({"raise_stmt2": ("%|raise %c, %c\n", 0, 1)})
280+
self.TABLE_DIRECT.update({"raise_stmt2": ("%|raise %c, %c\n", 0, 1)})
279281

280282
# exec as a built-in statement is only in Python 2.x
281283
def n_exec_stmt(node):

uncompyle6/semantics/customize14.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright (c) 2022 by Rocky Bernstein
1+
# Copyright (c) 2022, 2024 by Rocky Bernstein
22
#
33
# This program is free software: you can redistribute it and/or modify
44
# it under the terms of the GNU General Public License as published by
@@ -15,16 +15,13 @@
1515
"""Isolate Python 1.4- version-specific semantic actions here.
1616
"""
1717

18-
from uncompyle6.semantics.consts import TABLE_DIRECT
1918

2019
#######################
2120
# Python 1.4- Changes #
2221
#######################
23-
def customize_for_version14(self, version):
24-
TABLE_DIRECT.update(
22+
def customize_for_version14(self, version: tuple):
23+
self.TABLE_DIRECT.update(
2524
{
26-
"print_expr_stmt": (
27-
("%|print %c\n", 0)
28-
),
25+
"print_expr_stmt": (("%|print %c\n", 0)),
2926
}
3027
)
Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright (c) 2019 2021 by Rocky Bernstein
1+
# Copyright (c) 2019 2021, 2024 by Rocky Bernstein
22
#
33
# This program is free software: you can redistribute it and/or modify
44
# it under the terms of the GNU General Public License as published by
@@ -17,8 +17,9 @@
1717

1818
from uncompyle6.semantics.consts import TABLE_DIRECT
1919

20-
def customize_for_version26_27(self, version):
2120

21+
def customize_for_version26_27(self, version: tuple):
22+
self.TABLE_DIRECT = TABLE_DIRECT.copy()
2223
########################################
2324
# Python 2.6+
2425
# except <condition> as <var>
@@ -29,16 +30,20 @@ def customize_for_version26_27(self, version):
2930
# matches how we parse this in bytecode
3031
########################################
3132
if version > (2, 6):
32-
TABLE_DIRECT.update({
33-
"except_cond2": ( "%|except %c as %c:\n", 1, 5 ),
34-
# When a generator is a single parameter of a function,
35-
# it doesn't need the surrounding parenethesis.
36-
"call_generator": ('%c%P', 0, (1, -1, ', ', 100)),
37-
})
33+
self.TABLE_DIRECT.update(
34+
{
35+
"except_cond2": ("%|except %c as %c:\n", 1, 5),
36+
# When a generator is a single parameter of a function,
37+
# it doesn't need the surrounding parenethesis.
38+
"call_generator": ("%c%P", 0, (1, -1, ", ", 100)),
39+
}
40+
)
3841
else:
39-
TABLE_DIRECT.update({
40-
'testtrue_then': ( 'not %p', (0, 22) ),
41-
})
42+
self.TABLE_DIRECT.update(
43+
{
44+
"testtrue_then": ("not %p", (0, 22)),
45+
}
46+
)
4247

4348
# FIXME: this should be a transformation
4449
def n_call(node):
@@ -47,22 +52,24 @@ def n_call(node):
4752
for i in mapping[1:]:
4853
key = key[i]
4954
pass
50-
if key.kind == 'CALL_FUNCTION_1':
55+
if key.kind == "CALL_FUNCTION_1":
5156
# A function with one argument. If this is a generator,
5257
# no parenthesis is needed.
5358
args_node = node[-2]
54-
if args_node == 'expr':
59+
if args_node == "expr":
5560
n = args_node[0]
56-
if n == 'generator_exp':
57-
node.kind = 'call_generator'
61+
if n == "generator_exp":
62+
node.kind = "call_generator"
5863
pass
5964
pass
6065

6166
self.default(node)
67+
6268
self.n_call = n_call
6369

6470
def n_import_from(node):
6571
if node[0].pattr > 0:
6672
node[2].pattr = ("." * node[0].pattr) + node[2].pattr
6773
self.default(node)
74+
6875
self.n_import_from = n_import_from

uncompyle6/semantics/customize3.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,9 @@
2929
from uncompyle6.util import get_code_name
3030

3131

32-
def customize_for_version3(self, version):
33-
TABLE_DIRECT.update(
32+
def customize_for_version3(self, version: tuple):
33+
self.TABLE_DIRECT = TABLE_DIRECT.copy()
34+
self.TABLE_DIRECT.update(
3435
{
3536
"comp_for": (" for %c in %c", (2, "store"), (0, "expr")),
3637
"if_exp_not": (
@@ -183,7 +184,7 @@ def n_classdef3(node):
183184
# the iteration variable. These rules we can ignore
184185
# since we pick up the iteration variable some other way and
185186
# we definitely don't include in the source _[dd].
186-
TABLE_DIRECT.update(
187+
self.TABLE_DIRECT.update(
187188
{
188189
"ifstmt30": (
189190
"%|if %c:\n%+%c%-",
@@ -335,7 +336,7 @@ def n_mkfunc_annotate(node):
335336

336337
self.n_mkfunc_annotate = n_mkfunc_annotate
337338

338-
TABLE_DIRECT.update(
339+
self.TABLE_DIRECT.update(
339340
{
340341
"tryelsestmtl3": (
341342
"%|try:\n%+%c%-%c%|else:\n%+%c%-",
@@ -350,7 +351,7 @@ def n_mkfunc_annotate(node):
350351
#######################
351352
# Python 3.4+ Changes #
352353
#######################
353-
TABLE_DIRECT.update(
354+
self.TABLE_DIRECT.update(
354355
{
355356
"LOAD_CLASSDEREF": ("%{pattr}",),
356357
"yield_from": ("yield from %c", (0, "expr")),

uncompyle6/semantics/customize35.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright (c) 2019-2020, 2022 by Rocky Bernstein
1+
# Copyright (c) 2019-2020, 2022, 2024 by Rocky Bernstein
22
#
33
# This program is free software: you can redistribute it and/or modify
44
# it under the terms of the GNU General Public License as published by
@@ -16,21 +16,18 @@
1616
"""
1717

1818
from xdis import co_flags_is_async, iscode
19-
from uncompyle6.semantics.consts import (
20-
INDENT_PER_LEVEL,
21-
PRECEDENCE,
22-
TABLE_DIRECT,
23-
)
2419

20+
from uncompyle6.semantics.consts import INDENT_PER_LEVEL, PRECEDENCE, TABLE_DIRECT
2521
from uncompyle6.semantics.helper import flatten_list, gen_function_parens_adjust
2622

2723

2824
#######################
2925
# Python 3.5+ Changes #
3026
#######################
31-
def customize_for_version35(self, version):
27+
def customize_for_version35(self, version: tuple):
3228
# fmt: off
33-
TABLE_DIRECT.update(
29+
self.TABLE_DIRECT = TABLE_DIRECT.copy()
30+
self.TABLE_DIRECT.update(
3431
{
3532
# nested await expressions like:
3633
# return await (await bar())
@@ -197,7 +194,11 @@ def n_call(node):
197194
self.template_engine(template, args_node)
198195
else:
199196
if len(node) - nargs > 3:
200-
template = ("*%c, %P)", nargs + 1, (nargs + kwargs + 1, -1, ", ", 100))
197+
template = (
198+
"*%c, %P)",
199+
nargs + 1,
200+
(nargs + kwargs + 1, -1, ", ", 100),
201+
)
201202
else:
202203
template = ("*%c)", nargs + 1)
203204
self.template_engine(template, node)

uncompyle6/semantics/customize36.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def escape_format(s):
3838
#######################
3939

4040

41-
def customize_for_version36(self, version):
41+
def customize_for_version36(self, version: tuple):
4242
# fmt: off
4343
PRECEDENCE["call_kw"] = 0
4444
PRECEDENCE["call_kw36"] = 1
@@ -50,7 +50,7 @@ def customize_for_version36(self, version):
5050
PRECEDENCE["dict_pack"] = 0 # **{ ... }
5151
PRECEDENCE["formatted_value1"] = 100
5252

53-
TABLE_DIRECT.update(
53+
self.TABLE_DIRECT.update(
5454
{
5555
"ann_assign_init_value": (
5656
"%|%c = %p\n",
@@ -96,7 +96,8 @@ def customize_for_version36(self, version):
9696
}
9797
)
9898

99-
TABLE_R.update(
99+
self.TABLE_R = TABLE_R.copy()
100+
self.TABLE_R.update(
100101
{
101102
"CALL_FUNCTION_EX": ("%c(*%P)", 0, (1, 2, ", ", 100)),
102103
# Not quite right

uncompyle6/semantics/customize37.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright (c) 2019-2023 by Rocky Bernstein
1+
# Copyright (c) 2019-2024 by Rocky Bernstein
22
#
33
# This program is free software: you can redistribute it and/or modify
44
# it under the terms of the GNU General Public License as published by
@@ -25,11 +25,13 @@
2525

2626

2727
#######################
28-
def customize_for_version37(self, version):
28+
def customize_for_version37(self, version: tuple):
2929
########################
3030
# Python 3.7+ changes
3131
#######################
3232

33+
self.TABLE_DIRECT = TABLE_DIRECT.copy()
34+
3335
# fmt: off
3436
PRECEDENCE["attribute37"] = 2
3537
PRECEDENCE["call_ex"] = 1
@@ -47,7 +49,7 @@ def customize_for_version37(self, version):
4749
PRECEDENCE["dict_unpack"] = 0 # **{...}
4850

4951
# fmt: on
50-
TABLE_DIRECT.update(
52+
self.TABLE_DIRECT.update(
5153
{
5254
"and_not": ("%c and not %c", (0, "expr"), (2, "expr")),
5355
"ann_assign": (

uncompyle6/semantics/customize38.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright (c) 2019-2020, 2022 by Rocky Bernstein
1+
# Copyright (c) 2019-2020, 2022, 2024 by Rocky Bernstein
22
#
33
# This program is free software: you can redistribute it and/or modify
44
# it under the terms of the GNU General Public License as published by
@@ -24,13 +24,14 @@
2424
from uncompyle6.semantics.helper import escape_string, strip_quotes
2525

2626

27-
def customize_for_version38(self, version):
27+
def customize_for_version38(self, version: tuple):
2828
# FIXME: pytest doesn't add proper keys in testing. Reinstate after we have fixed pytest.
2929
# for lhs in 'for forelsestmt forelselaststmt '
3030
# 'forelselaststmtc tryfinally38'.split():
3131
# del TABLE_DIRECT[lhs]
3232

33-
TABLE_DIRECT.update(
33+
self.TABLE_DIRECT = TABLE_DIRECT.copy()
34+
self.TABLE_DIRECT.update(
3435
{
3536
"async_for_stmt38": (
3637
"%|async for %c in %c:\n%+%c%-%-\n\n",

0 commit comments

Comments
 (0)