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.
This commit is contained in:
Batuhan Taskaya 2021-06-27 17:58:32 +03:00 committed by GitHub
parent 9eea201b7c
commit 107a2c59c9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 10 additions and 6 deletions

View File

@ -71,16 +71,20 @@ def reflow_lines(s, depth):
def reflow_c_string(s, depth): def reflow_c_string(s, depth):
return '"%s"' % s.replace('\n', '\\n"\n%s"' % (' ' * depth * TABSIZE)) 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. """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 unaryop = Invert | Not | UAdd | USub
""" """
for t in sum.types:
if t.fields: return not (
return False sum_type.attributes or
return True any(constructor.fields for constructor in sum_type.types)
)
def asdl_of(name, obj): def asdl_of(name, obj):
if isinstance(obj, asdl.Product) or isinstance(obj, asdl.Constructor): if isinstance(obj, asdl.Product) or isinstance(obj, asdl.Constructor):