Skip to content

Commit

Permalink
Don't update global tables...
Browse files Browse the repository at this point in the history
Work off of copies of them instead. Issue #503
  • Loading branch information
rocky committed Nov 9, 2024
1 parent ab79803 commit 37f38e4
Show file tree
Hide file tree
Showing 10 changed files with 114 additions and 78 deletions.
32 changes: 17 additions & 15 deletions uncompyle6/semantics/customize.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2018-2019, 2021-2022 by Rocky Bernstein
# Copyright (c) 2018-2019, 2021-2022 2024 by Rocky Bernstein
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
Expand All @@ -17,24 +17,25 @@
"""

from uncompyle6.parsers.treenode import SyntaxTree
from uncompyle6.scanners.tok import Token
from uncompyle6.semantics.consts import (
INDENT_PER_LEVEL,
NO_PARENTHESIS_EVER,
PRECEDENCE,
TABLE_R,
TABLE_DIRECT,
TABLE_R,
)
from uncompyle6.semantics.helper import flatten_list
from uncompyle6.scanners.tok import Token


def customize_for_version(self, is_pypy, version):
self.TABLE_DIRECT = TABLE_DIRECT.copy()
if is_pypy:
########################
# PyPy changes
#######################
# fmt: off
TABLE_DIRECT.update({
self.TABLE_DIRECT.update({

"assert": ("%|assert %c\n", 0),
# This can happen as a result of an if transformation
Expand Down Expand Up @@ -114,7 +115,7 @@ def n_call_kw_pypy37(node):
########################
# Without PyPy
#######################
TABLE_DIRECT.update(
self.TABLE_DIRECT.update(
{
# "assert" and "assert_expr" are added via transform rules.
"assert": ("%|assert %c\n", 0),
Expand All @@ -133,23 +134,23 @@ def n_call_kw_pypy37(node):
)
if version >= (3, 0):
if version >= (3, 2):
TABLE_DIRECT.update(
self.TABLE_DIRECT.update(
{"del_deref_stmt": ("%|del %c\n", 0), "DELETE_DEREF": ("%{pattr}", 0)}
)
from uncompyle6.semantics.customize3 import customize_for_version3

customize_for_version3(self, version)
else: # < 3.0
TABLE_DIRECT.update(
self.TABLE_DIRECT.update(
{"except_cond3": ("%|except %c, %c:\n", (1, "expr"), (-2, "store"))}
)
if version <= (2, 6):
TABLE_DIRECT["testtrue_then"] = TABLE_DIRECT["testtrue"]
self.TABLE_DIRECT["testtrue_then"] = self.TABLE_DIRECT["testtrue"]

if (2, 4) <= version <= (2, 6):
TABLE_DIRECT.update({"comp_for": (" for %c in %c", 3, 1)})
self.TABLE_DIRECT.update({"comp_for": (" for %c in %c", 3, 1)})
else:
TABLE_DIRECT.update({"comp_for": (" for %c in %c%c", 2, 0, 3)})
self.TABLE_DIRECT.update({"comp_for": (" for %c in %c%c", 2, 0, 3)})

if version >= (2, 5):
from uncompyle6.semantics.customize25 import customize_for_version25
Expand Down Expand Up @@ -197,7 +198,7 @@ def n_call_kw_pypy37(node):
)
],
)
TABLE_DIRECT.update(
self.TABLE_DIRECT.update(
{
"importmultiple": ("%|import %c%c\n", 2, 3),
"import_cont": (", %c", 2),
Expand Down Expand Up @@ -247,9 +248,9 @@ def n_call(node):
self.n_call = n_call

else: # 1.0 <= version <= 2.3:
TABLE_DIRECT.update({"if1_stmt": ("%|if 1\n%+%c%-", 5)})
self.TABLE_DIRECT.update({"if1_stmt": ("%|if 1\n%+%c%-", 5)})
if version <= (2, 1):
TABLE_DIRECT.update(
self.TABLE_DIRECT.update(
{
"importmultiple": ("%c", 2),
# FIXME: not quite right. We have indiividual imports
Expand All @@ -263,7 +264,8 @@ def n_call(node):

# < 3.0 continues

TABLE_R.update(
self.TABLE_R = TABLE_R.copy()
self.TABLE_R.update(
{
"STORE_SLICE+0": ("%c[:]", 0),
"STORE_SLICE+1": ("%c[%p:]", 0, (1, -1)),
Expand All @@ -275,7 +277,7 @@ def n_call(node):
"DELETE_SLICE+3": ("%|del %c[%c:%c]\n", 0, 1, 2),
}
)
TABLE_DIRECT.update({"raise_stmt2": ("%|raise %c, %c\n", 0, 1)})
self.TABLE_DIRECT.update({"raise_stmt2": ("%|raise %c, %c\n", 0, 1)})

# exec as a built-in statement is only in Python 2.x
def n_exec_stmt(node):
Expand Down
11 changes: 4 additions & 7 deletions uncompyle6/semantics/customize14.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2022 by Rocky Bernstein
# Copyright (c) 2022, 2024 by Rocky Bernstein
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
Expand All @@ -15,16 +15,13 @@
"""Isolate Python 1.4- version-specific semantic actions here.
"""

from uncompyle6.semantics.consts import TABLE_DIRECT

#######################
# Python 1.4- Changes #
#######################
def customize_for_version14(self, version):
TABLE_DIRECT.update(
def customize_for_version14(self, version: tuple):
self.TABLE_DIRECT.update(
{
"print_expr_stmt": (
("%|print %c\n", 0)
),
"print_expr_stmt": (("%|print %c\n", 0)),
}
)
37 changes: 22 additions & 15 deletions uncompyle6/semantics/customize26_27.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2019 2021 by Rocky Bernstein
# Copyright (c) 2019 2021, 2024 by Rocky Bernstein
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
Expand All @@ -17,8 +17,9 @@

from uncompyle6.semantics.consts import TABLE_DIRECT

def customize_for_version26_27(self, version):

def customize_for_version26_27(self, version: tuple):
self.TABLE_DIRECT = TABLE_DIRECT.copy()
########################################
# Python 2.6+
# except <condition> as <var>
Expand All @@ -29,16 +30,20 @@ def customize_for_version26_27(self, version):
# matches how we parse this in bytecode
########################################
if version > (2, 6):
TABLE_DIRECT.update({
"except_cond2": ( "%|except %c as %c:\n", 1, 5 ),
# When a generator is a single parameter of a function,
# it doesn't need the surrounding parenethesis.
"call_generator": ('%c%P', 0, (1, -1, ', ', 100)),
})
self.TABLE_DIRECT.update(
{
"except_cond2": ("%|except %c as %c:\n", 1, 5),
# When a generator is a single parameter of a function,
# it doesn't need the surrounding parenethesis.
"call_generator": ("%c%P", 0, (1, -1, ", ", 100)),
}
)
else:
TABLE_DIRECT.update({
'testtrue_then': ( 'not %p', (0, 22) ),
})
self.TABLE_DIRECT.update(
{
"testtrue_then": ("not %p", (0, 22)),
}
)

# FIXME: this should be a transformation
def n_call(node):
Expand All @@ -47,22 +52,24 @@ def n_call(node):
for i in mapping[1:]:
key = key[i]
pass
if key.kind == 'CALL_FUNCTION_1':
if key.kind == "CALL_FUNCTION_1":
# A function with one argument. If this is a generator,
# no parenthesis is needed.
args_node = node[-2]
if args_node == 'expr':
if args_node == "expr":
n = args_node[0]
if n == 'generator_exp':
node.kind = 'call_generator'
if n == "generator_exp":
node.kind = "call_generator"
pass
pass

self.default(node)

self.n_call = n_call

def n_import_from(node):
if node[0].pattr > 0:
node[2].pattr = ("." * node[0].pattr) + node[2].pattr
self.default(node)

self.n_import_from = n_import_from
11 changes: 6 additions & 5 deletions uncompyle6/semantics/customize3.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@
from uncompyle6.util import get_code_name


def customize_for_version3(self, version):
TABLE_DIRECT.update(
def customize_for_version3(self, version: tuple):
self.TABLE_DIRECT = TABLE_DIRECT.copy()
self.TABLE_DIRECT.update(
{
"comp_for": (" for %c in %c", (2, "store"), (0, "expr")),
"if_exp_not": (
Expand Down Expand Up @@ -183,7 +184,7 @@ def n_classdef3(node):
# the iteration variable. These rules we can ignore
# since we pick up the iteration variable some other way and
# we definitely don't include in the source _[dd].
TABLE_DIRECT.update(
self.TABLE_DIRECT.update(
{
"ifstmt30": (
"%|if %c:\n%+%c%-",
Expand Down Expand Up @@ -335,7 +336,7 @@ def n_mkfunc_annotate(node):

self.n_mkfunc_annotate = n_mkfunc_annotate

TABLE_DIRECT.update(
self.TABLE_DIRECT.update(
{
"tryelsestmtl3": (
"%|try:\n%+%c%-%c%|else:\n%+%c%-",
Expand All @@ -350,7 +351,7 @@ def n_mkfunc_annotate(node):
#######################
# Python 3.4+ Changes #
#######################
TABLE_DIRECT.update(
self.TABLE_DIRECT.update(
{
"LOAD_CLASSDEREF": ("%{pattr}",),
"yield_from": ("yield from %c", (0, "expr")),
Expand Down
19 changes: 10 additions & 9 deletions uncompyle6/semantics/customize35.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2019-2020, 2022 by Rocky Bernstein
# Copyright (c) 2019-2020, 2022, 2024 by Rocky Bernstein
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
Expand All @@ -16,21 +16,18 @@
"""

from xdis import co_flags_is_async, iscode
from uncompyle6.semantics.consts import (
INDENT_PER_LEVEL,
PRECEDENCE,
TABLE_DIRECT,
)

from uncompyle6.semantics.consts import INDENT_PER_LEVEL, PRECEDENCE, TABLE_DIRECT
from uncompyle6.semantics.helper import flatten_list, gen_function_parens_adjust


#######################
# Python 3.5+ Changes #
#######################
def customize_for_version35(self, version):
def customize_for_version35(self, version: tuple):
# fmt: off
TABLE_DIRECT.update(
self.TABLE_DIRECT = TABLE_DIRECT.copy()
self.TABLE_DIRECT.update(
{
# nested await expressions like:
# return await (await bar())
Expand Down Expand Up @@ -197,7 +194,11 @@ def n_call(node):
self.template_engine(template, args_node)
else:
if len(node) - nargs > 3:
template = ("*%c, %P)", nargs + 1, (nargs + kwargs + 1, -1, ", ", 100))
template = (
"*%c, %P)",
nargs + 1,
(nargs + kwargs + 1, -1, ", ", 100),
)
else:
template = ("*%c)", nargs + 1)
self.template_engine(template, node)
Expand Down
7 changes: 4 additions & 3 deletions uncompyle6/semantics/customize36.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def escape_format(s):
#######################


def customize_for_version36(self, version):
def customize_for_version36(self, version: tuple):
# fmt: off
PRECEDENCE["call_kw"] = 0
PRECEDENCE["call_kw36"] = 1
Expand All @@ -50,7 +50,7 @@ def customize_for_version36(self, version):
PRECEDENCE["dict_pack"] = 0 # **{ ... }
PRECEDENCE["formatted_value1"] = 100

TABLE_DIRECT.update(
self.TABLE_DIRECT.update(
{
"ann_assign_init_value": (
"%|%c = %p\n",
Expand Down Expand Up @@ -96,7 +96,8 @@ def customize_for_version36(self, version):
}
)

TABLE_R.update(
self.TABLE_R = TABLE_R.copy()
self.TABLE_R.update(
{
"CALL_FUNCTION_EX": ("%c(*%P)", 0, (1, 2, ", ", 100)),
# Not quite right
Expand Down
8 changes: 5 additions & 3 deletions uncompyle6/semantics/customize37.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2019-2023 by Rocky Bernstein
# Copyright (c) 2019-2024 by Rocky Bernstein
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
Expand All @@ -25,11 +25,13 @@


#######################
def customize_for_version37(self, version):
def customize_for_version37(self, version: tuple):
########################
# Python 3.7+ changes
#######################

self.TABLE_DIRECT = TABLE_DIRECT.copy()

# fmt: off
PRECEDENCE["attribute37"] = 2
PRECEDENCE["call_ex"] = 1
Expand All @@ -47,7 +49,7 @@ def customize_for_version37(self, version):
PRECEDENCE["dict_unpack"] = 0 # **{...}

# fmt: on
TABLE_DIRECT.update(
self.TABLE_DIRECT.update(
{
"and_not": ("%c and not %c", (0, "expr"), (2, "expr")),
"ann_assign": (
Expand Down
7 changes: 4 additions & 3 deletions uncompyle6/semantics/customize38.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2019-2020, 2022 by Rocky Bernstein
# Copyright (c) 2019-2020, 2022, 2024 by Rocky Bernstein
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
Expand All @@ -24,13 +24,14 @@
from uncompyle6.semantics.helper import escape_string, strip_quotes


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

TABLE_DIRECT.update(
self.TABLE_DIRECT = TABLE_DIRECT.copy()
self.TABLE_DIRECT.update(
{
"async_for_stmt38": (
"%|async for %c in %c:\n%+%c%-%-\n\n",
Expand Down
Loading

0 comments on commit 37f38e4

Please sign in to comment.