parens around consts when taking attr again

This commit is contained in:
rocky
2020-01-16 22:15:28 -05:00
parent 549c33113b
commit 6d368d2b30
6 changed files with 26 additions and 0 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@@ -8,8 +8,19 @@
# classdef ::= LOAD_CONST expr mkfunc CALL_FUNCTION_0 BUILD_CLASS store # classdef ::= LOAD_CONST expr mkfunc CALL_FUNCTION_0 BUILD_CLASS store
# mkfunc ::= LOAD_CONST MAKE_FUNCTION_0 # mkfunc ::= LOAD_CONST MAKE_FUNCTION_0
# RUNNABLE!
class A: class A:
pass pass
class B(Exception): class B(Exception):
pass pass
# From 3.x test_descr.py
class MyInt(int):
class MyInt(int):
__slots__ = ()
try:
(1).__class__ = MyInt
assert False, "builtin types don't support __class__ assignment."
except TypeError:
pass

View File

@@ -268,6 +268,10 @@ TABLE_DIRECT = {
'attribute_w_parens': ( '(%c).%[1]{pattr}', 'attribute_w_parens': ( '(%c).%[1]{pattr}',
(0, 'expr')), (0, 'expr')),
# This nonterminal we create on the fly in semantic routines
'store_w_parens': ( '(%c).%[1]{pattr}',
(0, 'expr')),
'unpack_list': ( '[%C]', 'unpack_list': ( '[%C]',
(1, maxint, ', ') ), (1, maxint, ', ') ),
'build_tuple2': ( '%P', 'build_tuple2': ( '%P',

View File

@@ -1889,6 +1889,17 @@ class SourceWalker(GenericASTTraversal, object):
n_set = n_tuple = n_build_set = n_list n_set = n_tuple = n_build_set = n_list
def n_store(self, node):
expr = node[0]
if expr == "expr" and expr[0] == "LOAD_CONST" and node[1] == "STORE_ATTR":
# FIXME: I didn't record which constants parenthesis is
# necessary. However, I suspect that we could further
# refine this by looking at operator precedence and
# eval'ing the constant value (pattr) and comparing with
# the type of the constant.
node.kind = "store_w_parens"
self.default(node)
def n_unpack(self, node): def n_unpack(self, node):
if node[0].kind.startswith("UNPACK_EX"): if node[0].kind.startswith("UNPACK_EX"):
# Python 3+ # Python 3+