summaryrefslogtreecommitdiffstats
path: root/Parser/asdl_c.py
diff options
context:
space:
mode:
authorBatuhan Taskaya <batuhan@python.org>2021-06-27 14:58:32 (GMT)
committerGitHub <noreply@github.com>2021-06-27 14:58:32 (GMT)
commit107a2c59c91b3911bdd6dfdb83271c588c506a5a (patch)
tree77d8a1c089b367f734ee46f549940ed01eb7191e /Parser/asdl_c.py
parent9eea201b7c39a69afa2a7978dde40266104154f5 (diff)
downloadcpython-107a2c59c91b3911bdd6dfdb83271c588c506a5a.zip
cpython-107a2c59c91b3911bdd6dfdb83271c588c506a5a.tar.gz
cpython-107a2c59c91b3911bdd6dfdb83271c588c506a5a.tar.bz2
bpo-40528: fix is_simple(sum)s behavior for attributes (GH-26918)
This is something I noticed while (now discontinued) experimenting with the idea of annotating operators with location information. Unfortunately without this addition, adding any `attributes` to stuff like `unaryop` doesn't change anything since the code assumes they are singletons and caches all instances. This patch fixes this assumption with including the attributes as well as constructor fields.
Diffstat (limited to 'Parser/asdl_c.py')
-rwxr-xr-xParser/asdl_c.py16
1 files changed, 10 insertions, 6 deletions
diff --git a/Parser/asdl_c.py b/Parser/asdl_c.py
index 21e5044..5f0b89b 100755
--- a/Parser/asdl_c.py
+++ b/Parser/asdl_c.py
@@ -71,16 +71,20 @@ def reflow_lines(s, depth):
def reflow_c_string(s, depth):
return '"%s"' % s.replace('\n', '\\n"\n%s"' % (' ' * depth * TABSIZE))
-def is_simple(sum):
+def is_simple(sum_type):
"""Return True if a sum is a simple.
- A sum is simple if its types have no fields, e.g.
+ A sum is simple if it's types have no fields and itself
+ doesn't have any attributes. Instances of these types are
+ cached at C level, and they act like singletons when propagating
+ parser generated nodes into Python level, e.g.
unaryop = Invert | Not | UAdd | USub
"""
- for t in sum.types:
- if t.fields:
- return False
- return True
+
+ return not (
+ sum_type.attributes or
+ any(constructor.fields for constructor in sum_type.types)
+ )
def asdl_of(name, obj):
if isinstance(obj, asdl.Product) or isinstance(obj, asdl.Constructor):