| 
									
										
										
										
											2011-12-29 12:07:21 -06:00
										 |  |  | #! /usr/bin/env python | 
					
						
							| 
									
										
										
										
											2005-10-20 19:59:25 +00:00
										 |  |  | """Generate C code from an ASDL description.""" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-18 20:42:10 +03:00
										 |  |  | import sys | 
					
						
							| 
									
										
										
										
											2020-11-02 22:03:28 +01:00
										 |  |  | import textwrap | 
					
						
							| 
									
										
										
										
											2021-06-22 19:29:42 +03:00
										 |  |  | import types | 
					
						
							| 
									
										
										
										
											2020-05-18 20:42:10 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | from argparse import ArgumentParser | 
					
						
							| 
									
										
										
										
											2021-06-03 23:01:02 +03:00
										 |  |  | from contextlib import contextmanager | 
					
						
							| 
									
										
										
										
											2020-05-18 20:42:10 +03:00
										 |  |  | from pathlib import Path | 
					
						
							| 
									
										
										
										
											2005-10-20 19:59:25 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | import asdl | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-27 00:00:36 +02:00
										 |  |  | TABSIZE = 4 | 
					
						
							| 
									
										
										
										
											2005-10-20 19:59:25 +00:00
										 |  |  | MAX_COL = 80 | 
					
						
							| 
									
										
										
										
											2020-11-02 22:03:28 +01:00
										 |  |  | AUTOGEN_MESSAGE = "// File automatically generated by {}.\n\n" | 
					
						
							| 
									
										
										
										
											2005-10-20 19:59:25 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-27 18:13:03 -08:00
										 |  |  | builtin_type_to_c_type = { | 
					
						
							|  |  |  |     "identifier": "PyUnicode_Type", | 
					
						
							|  |  |  |     "string": "PyUnicode_Type", | 
					
						
							|  |  |  |     "int": "PyLong_Type", | 
					
						
							|  |  |  |     "constant": "PyBaseObject_Type", | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-10-20 19:59:25 +00:00
										 |  |  | def get_c_type(name): | 
					
						
							|  |  |  |     """Return a string for the C name of the type.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-09 17:58:22 -07:00
										 |  |  |     This function special cases the default types provided by asdl. | 
					
						
							| 
									
										
										
										
											2005-10-20 19:59:25 +00:00
										 |  |  |     """
 | 
					
						
							|  |  |  |     if name in asdl.builtin_types: | 
					
						
							|  |  |  |         return name | 
					
						
							|  |  |  |     else: | 
					
						
							|  |  |  |         return "%s_ty" % name | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def reflow_lines(s, depth): | 
					
						
							|  |  |  |     """Reflow the line s indented depth tabs.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     Return a sequence of lines where no line extends beyond MAX_COL | 
					
						
							|  |  |  |     when properly indented.  The first line is properly indented based | 
					
						
							|  |  |  |     exclusively on depth * TABSIZE.  All following lines -- these are | 
					
						
							|  |  |  |     the reflowed lines generated by this function -- start at the same | 
					
						
							|  |  |  |     column as the first character beyond the opening { in the first | 
					
						
							|  |  |  |     line. | 
					
						
							|  |  |  |     """
 | 
					
						
							|  |  |  |     size = MAX_COL - depth * TABSIZE | 
					
						
							|  |  |  |     if len(s) < size: | 
					
						
							|  |  |  |         return [s] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     lines = [] | 
					
						
							|  |  |  |     cur = s | 
					
						
							|  |  |  |     padding = "" | 
					
						
							|  |  |  |     while len(cur) > size: | 
					
						
							|  |  |  |         i = cur.rfind(' ', 0, size) | 
					
						
							|  |  |  |         # XXX this should be fixed for real | 
					
						
							|  |  |  |         if i == -1 and 'GeneratorExp' in cur: | 
					
						
							|  |  |  |             i = size + 3 | 
					
						
							| 
									
										
										
										
											2006-08-25 04:06:31 +00:00
										 |  |  |         assert i != -1, "Impossible line %d to reflow: %r" % (size, s) | 
					
						
							| 
									
										
										
										
											2005-10-20 19:59:25 +00:00
										 |  |  |         lines.append(padding + cur[:i]) | 
					
						
							|  |  |  |         if len(lines) == 1: | 
					
						
							|  |  |  |             # find new size based on brace | 
					
						
							|  |  |  |             j = cur.find('{', 0, i) | 
					
						
							|  |  |  |             if j >= 0: | 
					
						
							|  |  |  |                 j += 2 # account for the brace and the space after it | 
					
						
							|  |  |  |                 size -= j | 
					
						
							|  |  |  |                 padding = " " * j | 
					
						
							|  |  |  |             else: | 
					
						
							|  |  |  |                 j = cur.find('(', 0, i) | 
					
						
							|  |  |  |                 if j >= 0: | 
					
						
							|  |  |  |                     j += 1 # account for the paren (no space after it) | 
					
						
							|  |  |  |                     size -= j | 
					
						
							|  |  |  |                     padding = " " * j | 
					
						
							|  |  |  |         cur = cur[i+1:] | 
					
						
							|  |  |  |     else: | 
					
						
							|  |  |  |         lines.append(padding + cur) | 
					
						
							|  |  |  |     return lines | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-16 11:12:53 +03:00
										 |  |  | def reflow_c_string(s, depth): | 
					
						
							|  |  |  |     return '"%s"' % s.replace('\n', '\\n"\n%s"' % (' ' * depth * TABSIZE)) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-27 17:58:32 +03:00
										 |  |  | def is_simple(sum_type): | 
					
						
							| 
									
										
										
										
											2005-10-20 19:59:25 +00:00
										 |  |  |     """Return True if a sum is a simple.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-10 06:10:46 +03:00
										 |  |  |     A sum is simple if its types have no fields and itself | 
					
						
							| 
									
										
										
										
											2021-06-27 17:58:32 +03:00
										 |  |  |     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. | 
					
						
							| 
									
										
										
										
											2005-10-20 19:59:25 +00:00
										 |  |  |     unaryop = Invert | Not | UAdd | USub | 
					
						
							|  |  |  |     """
 | 
					
						
							| 
									
										
										
										
											2021-06-27 17:58:32 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  |     return not ( | 
					
						
							|  |  |  |         sum_type.attributes or | 
					
						
							|  |  |  |         any(constructor.fields for constructor in sum_type.types) | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2005-10-20 19:59:25 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-16 11:12:53 +03:00
										 |  |  | def asdl_of(name, obj): | 
					
						
							|  |  |  |     if isinstance(obj, asdl.Product) or isinstance(obj, asdl.Constructor): | 
					
						
							|  |  |  |         fields = ", ".join(map(str, obj.fields)) | 
					
						
							|  |  |  |         if fields: | 
					
						
							|  |  |  |             fields = "({})".format(fields) | 
					
						
							|  |  |  |         return "{}{}".format(name, fields) | 
					
						
							|  |  |  |     else: | 
					
						
							|  |  |  |         if is_simple(obj): | 
					
						
							|  |  |  |             types = " | ".join(type.name for type in obj.types) | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             sep = "\n{}| ".format(" " * (len(name) + 1)) | 
					
						
							|  |  |  |             types = sep.join( | 
					
						
							|  |  |  |                 asdl_of(type.name, type) for type in obj.types | 
					
						
							|  |  |  |             ) | 
					
						
							|  |  |  |         return "{} = {}".format(name, types) | 
					
						
							| 
									
										
										
										
											2008-03-30 20:03:44 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-10-20 19:59:25 +00:00
										 |  |  | class EmitVisitor(asdl.VisitorBase): | 
					
						
							|  |  |  |     """Visit that emits lines""" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-22 19:29:42 +03:00
										 |  |  |     def __init__(self, file, metadata = None): | 
					
						
							| 
									
										
										
										
											2005-10-20 19:59:25 +00:00
										 |  |  |         self.file = file | 
					
						
							| 
									
										
										
										
											2021-06-22 19:29:42 +03:00
										 |  |  |         self._metadata = metadata | 
					
						
							| 
									
										
										
										
											2005-10-20 19:59:25 +00:00
										 |  |  |         super(EmitVisitor, self).__init__() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
											  
											
												Merged revisions 73196,73278-73280,73299,73308,73312-73313,73317-73318,73321,73324,73331,73335,73340,73363 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
  r73196 | benjamin.peterson | 2009-06-03 20:40:29 -0500 (Wed, 03 Jun 2009) | 1 line
  use the offical api
........
  r73278 | benjamin.peterson | 2009-06-07 17:33:11 -0500 (Sun, 07 Jun 2009) | 1 line
  inherit from object
........
  r73279 | benjamin.peterson | 2009-06-07 17:35:00 -0500 (Sun, 07 Jun 2009) | 1 line
  always inherit from an appropiate base class
........
  r73280 | benjamin.peterson | 2009-06-07 17:54:35 -0500 (Sun, 07 Jun 2009) | 1 line
  use booleans for flags
........
  r73299 | georg.brandl | 2009-06-08 13:41:36 -0500 (Mon, 08 Jun 2009) | 1 line
  Typo fix.
........
  r73308 | benjamin.peterson | 2009-06-08 17:18:32 -0500 (Mon, 08 Jun 2009) | 1 line
  remove useless assertion
........
  r73312 | benjamin.peterson | 2009-06-08 18:44:13 -0500 (Mon, 08 Jun 2009) | 1 line
  remove error checks already done in set_context()
........
  r73313 | r.david.murray | 2009-06-08 19:44:22 -0500 (Mon, 08 Jun 2009) | 4 lines
  Issue 2947: document how return code handling translates from
  os.popen to subprocess.  Also fixes reference link in the
  os.spawn documentation.
........
  r73317 | benjamin.peterson | 2009-06-09 12:24:26 -0500 (Tue, 09 Jun 2009) | 1 line
  make ast.c depend on the grammar
........
  r73318 | benjamin.peterson | 2009-06-09 12:29:51 -0500 (Tue, 09 Jun 2009) | 1 line
  explain why keyword names are not just NAME
........
  r73321 | benjamin.peterson | 2009-06-09 16:13:43 -0500 (Tue, 09 Jun 2009) | 1 line
  update symbol.py from with statement changes
........
  r73324 | amaury.forgeotdarc | 2009-06-09 17:53:16 -0500 (Tue, 09 Jun 2009) | 2 lines
  Avoid invoking the parser/compiler just to test the presence of a function.
........
  r73331 | benjamin.peterson | 2009-06-10 08:45:31 -0500 (Wed, 10 Jun 2009) | 1 line
  fix spelling
........
  r73335 | raymond.hettinger | 2009-06-10 11:15:40 -0500 (Wed, 10 Jun 2009) | 1 line
  Fix signed/unsigned compiler warning.
........
  r73340 | amaury.forgeotdarc | 2009-06-10 15:30:19 -0500 (Wed, 10 Jun 2009) | 2 lines
  Fix a typo spotted by Nick Coghlan.
........
  r73363 | benjamin.peterson | 2009-06-11 12:51:17 -0500 (Thu, 11 Jun 2009) | 1 line
  use multi-with syntax
........
											
										 
											2009-06-11 22:54:11 +00:00
										 |  |  |     def emit(self, s, depth, reflow=True): | 
					
						
							| 
									
										
										
										
											2005-10-20 19:59:25 +00:00
										 |  |  |         # XXX reflow long lines? | 
					
						
							|  |  |  |         if reflow: | 
					
						
							|  |  |  |             lines = reflow_lines(s, depth) | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             lines = [s] | 
					
						
							|  |  |  |         for line in lines: | 
					
						
							| 
									
										
										
										
											2017-09-15 06:28:22 +03:00
										 |  |  |             if line: | 
					
						
							|  |  |  |                 line = (" " * TABSIZE * depth) + line | 
					
						
							|  |  |  |             self.file.write(line + "\n") | 
					
						
							| 
									
										
										
										
											2005-10-20 19:59:25 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-22 19:29:42 +03:00
										 |  |  |     @property | 
					
						
							|  |  |  |     def metadata(self): | 
					
						
							|  |  |  |         if self._metadata is None: | 
					
						
							|  |  |  |             raise ValueError( | 
					
						
							| 
									
										
										
										
											2024-07-23 17:04:14 +08:00
										 |  |  |                 "%s was expecting to be annotated with metadata" | 
					
						
							| 
									
										
										
										
											2021-06-22 19:29:42 +03:00
										 |  |  |                 % type(self).__name__ | 
					
						
							|  |  |  |             ) | 
					
						
							|  |  |  |         return self._metadata | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @metadata.setter | 
					
						
							|  |  |  |     def metadata(self, value): | 
					
						
							|  |  |  |         self._metadata = value | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class MetadataVisitor(asdl.VisitorBase): | 
					
						
							| 
									
										
										
										
											2021-06-24 15:16:00 +03:00
										 |  |  |     ROOT_TYPE = "AST" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-22 19:29:42 +03:00
										 |  |  |     def __init__(self, *args, **kwargs): | 
					
						
							|  |  |  |         super().__init__(*args, **kwargs) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # Metadata: | 
					
						
							|  |  |  |         #    - simple_sums: Tracks the list of compound type | 
					
						
							|  |  |  |         #                   names where all the constructors | 
					
						
							|  |  |  |         #                   belonging to that type lack of any | 
					
						
							|  |  |  |         #                   fields. | 
					
						
							| 
									
										
										
										
											2021-09-28 16:26:41 +05:30
										 |  |  |         #    - identifiers: All identifiers used in the AST declarations | 
					
						
							| 
									
										
										
										
											2021-06-24 15:16:00 +03:00
										 |  |  |         #    - singletons:  List of all constructors that originates from | 
					
						
							|  |  |  |         #                   simple sums. | 
					
						
							|  |  |  |         #    - types:       List of all top level type names | 
					
						
							|  |  |  |         # | 
					
						
							| 
									
										
										
										
											2021-06-22 19:29:42 +03:00
										 |  |  |         self.metadata = types.SimpleNamespace( | 
					
						
							| 
									
										
										
										
											2021-06-24 15:16:00 +03:00
										 |  |  |             simple_sums=set(), | 
					
						
							|  |  |  |             identifiers=set(), | 
					
						
							|  |  |  |             singletons=set(), | 
					
						
							|  |  |  |             types={self.ROOT_TYPE}, | 
					
						
							| 
									
										
										
										
											2021-06-22 19:29:42 +03:00
										 |  |  |         ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def visitModule(self, mod): | 
					
						
							|  |  |  |         for dfn in mod.dfns: | 
					
						
							|  |  |  |             self.visit(dfn) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def visitType(self, type): | 
					
						
							|  |  |  |         self.visit(type.value, type.name) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def visitSum(self, sum, name): | 
					
						
							| 
									
										
										
										
											2021-06-24 15:16:00 +03:00
										 |  |  |         self.metadata.types.add(name) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         simple_sum = is_simple(sum) | 
					
						
							|  |  |  |         if simple_sum: | 
					
						
							| 
									
										
										
										
											2021-06-22 19:29:42 +03:00
										 |  |  |             self.metadata.simple_sums.add(name) | 
					
						
							| 
									
										
										
										
											2008-03-30 20:03:44 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-24 15:16:00 +03:00
										 |  |  |         for constructor in sum.types: | 
					
						
							|  |  |  |             if simple_sum: | 
					
						
							|  |  |  |                 self.metadata.singletons.add(constructor.name) | 
					
						
							|  |  |  |             self.visitConstructor(constructor) | 
					
						
							|  |  |  |         self.visitFields(sum.attributes) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def visitConstructor(self, constructor): | 
					
						
							|  |  |  |         self.metadata.types.add(constructor.name) | 
					
						
							|  |  |  |         self.visitFields(constructor.fields) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def visitProduct(self, product, name): | 
					
						
							|  |  |  |         self.metadata.types.add(name) | 
					
						
							|  |  |  |         self.visitFields(product.attributes) | 
					
						
							|  |  |  |         self.visitFields(product.fields) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def visitFields(self, fields): | 
					
						
							|  |  |  |         for field in fields: | 
					
						
							|  |  |  |             self.visitField(field) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def visitField(self, field): | 
					
						
							|  |  |  |         self.metadata.identifiers.add(field.name) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-10-20 19:59:25 +00:00
										 |  |  | class TypeDefVisitor(EmitVisitor): | 
					
						
							|  |  |  |     def visitModule(self, mod): | 
					
						
							|  |  |  |         for dfn in mod.dfns: | 
					
						
							|  |  |  |             self.visit(dfn) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def visitType(self, type, depth=0): | 
					
						
							|  |  |  |         self.visit(type.value, type.name, depth) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def visitSum(self, sum, name, depth): | 
					
						
							|  |  |  |         if is_simple(sum): | 
					
						
							|  |  |  |             self.simple_sum(sum, name, depth) | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             self.sum_with_constructors(sum, name, depth) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def simple_sum(self, sum, name, depth): | 
					
						
							|  |  |  |         enum = [] | 
					
						
							|  |  |  |         for i in range(len(sum.types)): | 
					
						
							|  |  |  |             type = sum.types[i] | 
					
						
							|  |  |  |             enum.append("%s=%d" % (type.name, i + 1)) | 
					
						
							|  |  |  |         enums = ", ".join(enum) | 
					
						
							|  |  |  |         ctype = get_c_type(name) | 
					
						
							|  |  |  |         s = "typedef enum _%s { %s } %s;" % (name, enums, ctype) | 
					
						
							|  |  |  |         self.emit(s, depth) | 
					
						
							|  |  |  |         self.emit("", depth) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def sum_with_constructors(self, sum, name, depth): | 
					
						
							|  |  |  |         ctype = get_c_type(name) | 
					
						
							|  |  |  |         s = "typedef struct _%(name)s *%(ctype)s;" % locals() | 
					
						
							|  |  |  |         self.emit(s, depth) | 
					
						
							|  |  |  |         self.emit("", depth) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def visitProduct(self, product, name, depth): | 
					
						
							|  |  |  |         ctype = get_c_type(name) | 
					
						
							|  |  |  |         s = "typedef struct _%(name)s *%(ctype)s;" % locals() | 
					
						
							|  |  |  |         self.emit(s, depth) | 
					
						
							|  |  |  |         self.emit("", depth) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-16 19:42:00 +01:00
										 |  |  | class SequenceDefVisitor(EmitVisitor): | 
					
						
							|  |  |  |     def visitModule(self, mod): | 
					
						
							|  |  |  |         for dfn in mod.dfns: | 
					
						
							|  |  |  |             self.visit(dfn) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def visitType(self, type, depth=0): | 
					
						
							|  |  |  |         self.visit(type.value, type.name, depth) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def visitSum(self, sum, name, depth): | 
					
						
							|  |  |  |         if is_simple(sum): | 
					
						
							|  |  |  |             return | 
					
						
							|  |  |  |         self.emit_sequence_constructor(name, depth) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def emit_sequence_constructor(self, name,depth): | 
					
						
							|  |  |  |         ctype = get_c_type(name) | 
					
						
							|  |  |  |         self.emit("""\
 | 
					
						
							|  |  |  | typedef struct { | 
					
						
							|  |  |  |     _ASDL_SEQ_HEAD | 
					
						
							|  |  |  |     %(ctype)s typed_elements[1]; | 
					
						
							|  |  |  | } asdl_%(name)s_seq;""" % locals(), reflow=False, depth=depth)
 | 
					
						
							|  |  |  |         self.emit("", depth) | 
					
						
							|  |  |  |         self.emit("asdl_%(name)s_seq *_Py_asdl_%(name)s_seq_new(Py_ssize_t size, PyArena *arena);" % locals(), depth) | 
					
						
							|  |  |  |         self.emit("", depth) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def visitProduct(self, product, name, depth): | 
					
						
							|  |  |  |         self.emit_sequence_constructor(name, depth) | 
					
						
							| 
									
										
										
										
											2008-03-30 20:03:44 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-10-20 19:59:25 +00:00
										 |  |  | class StructVisitor(EmitVisitor): | 
					
						
							| 
									
										
										
										
											2014-05-09 17:58:22 -07:00
										 |  |  |     """Visitor to generate typedefs for AST.""" | 
					
						
							| 
									
										
										
										
											2005-10-20 19:59:25 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def visitModule(self, mod): | 
					
						
							|  |  |  |         for dfn in mod.dfns: | 
					
						
							|  |  |  |             self.visit(dfn) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def visitType(self, type, depth=0): | 
					
						
							|  |  |  |         self.visit(type.value, type.name, depth) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def visitSum(self, sum, name, depth): | 
					
						
							|  |  |  |         if not is_simple(sum): | 
					
						
							|  |  |  |             self.sum_with_constructors(sum, name, depth) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def sum_with_constructors(self, sum, name, depth): | 
					
						
							|  |  |  |         def emit(s, depth=depth): | 
					
						
							|  |  |  |             self.emit(s % sys._getframe(1).f_locals, depth) | 
					
						
							|  |  |  |         enum = [] | 
					
						
							|  |  |  |         for i in range(len(sum.types)): | 
					
						
							|  |  |  |             type = sum.types[i] | 
					
						
							|  |  |  |             enum.append("%s_kind=%d" % (type.name, i + 1)) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-04-21 10:40:58 +00:00
										 |  |  |         emit("enum _%(name)s_kind {" + ", ".join(enum) + "};") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-10-20 19:59:25 +00:00
										 |  |  |         emit("struct _%(name)s {") | 
					
						
							| 
									
										
										
										
											2006-04-21 10:40:58 +00:00
										 |  |  |         emit("enum _%(name)s_kind kind;", depth + 1) | 
					
						
							| 
									
										
										
										
											2005-10-20 19:59:25 +00:00
										 |  |  |         emit("union {", depth + 1) | 
					
						
							|  |  |  |         for t in sum.types: | 
					
						
							|  |  |  |             self.visit(t, depth + 2) | 
					
						
							|  |  |  |         emit("} v;", depth + 1) | 
					
						
							|  |  |  |         for field in sum.attributes: | 
					
						
							|  |  |  |             # rudimentary attribute handling | 
					
						
							|  |  |  |             type = str(field.type) | 
					
						
							|  |  |  |             assert type in asdl.builtin_types, type | 
					
						
							|  |  |  |             emit("%s %s;" % (type, field.name), depth + 1); | 
					
						
							|  |  |  |         emit("};") | 
					
						
							|  |  |  |         emit("") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def visitConstructor(self, cons, depth): | 
					
						
							|  |  |  |         if cons.fields: | 
					
						
							|  |  |  |             self.emit("struct {", depth) | 
					
						
							|  |  |  |             for f in cons.fields: | 
					
						
							|  |  |  |                 self.visit(f, depth + 1) | 
					
						
							|  |  |  |             self.emit("} %s;" % cons.name, depth) | 
					
						
							|  |  |  |             self.emit("", depth) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def visitField(self, field, depth): | 
					
						
							|  |  |  |         # XXX need to lookup field.type, because it might be something | 
					
						
							|  |  |  |         # like a builtin... | 
					
						
							|  |  |  |         ctype = get_c_type(field.type) | 
					
						
							|  |  |  |         name = field.name | 
					
						
							|  |  |  |         if field.seq: | 
					
						
							| 
									
										
										
										
											2021-06-22 19:29:42 +03:00
										 |  |  |             if field.type in self.metadata.simple_sums: | 
					
						
							| 
									
										
										
										
											2006-04-21 10:40:58 +00:00
										 |  |  |                 self.emit("asdl_int_seq *%(name)s;" % locals(), depth) | 
					
						
							|  |  |  |             else: | 
					
						
							| 
									
										
										
										
											2020-09-16 19:42:00 +01:00
										 |  |  |                 _type = field.type | 
					
						
							|  |  |  |                 self.emit("asdl_%(_type)s_seq *%(name)s;" % locals(), depth) | 
					
						
							| 
									
										
										
										
											2005-10-20 19:59:25 +00:00
										 |  |  |         else: | 
					
						
							|  |  |  |             self.emit("%(ctype)s %(name)s;" % locals(), depth) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def visitProduct(self, product, name, depth): | 
					
						
							|  |  |  |         self.emit("struct _%(name)s {" % locals(), depth) | 
					
						
							|  |  |  |         for f in product.fields: | 
					
						
							|  |  |  |             self.visit(f, depth + 1) | 
					
						
							| 
									
										
										
										
											2013-03-18 10:48:58 -07:00
										 |  |  |         for field in product.attributes: | 
					
						
							|  |  |  |             # rudimentary attribute handling | 
					
						
							|  |  |  |             type = str(field.type) | 
					
						
							|  |  |  |             assert type in asdl.builtin_types, type | 
					
						
							|  |  |  |             self.emit("%s %s;" % (type, field.name), depth + 1); | 
					
						
							| 
									
										
										
										
											2005-10-20 19:59:25 +00:00
										 |  |  |         self.emit("};", depth) | 
					
						
							|  |  |  |         self.emit("", depth) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-03-30 20:03:44 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-07 21:34:22 +02:00
										 |  |  | def ast_func_name(name): | 
					
						
							|  |  |  |     return f"_PyAST_{name}" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-10-20 19:59:25 +00:00
										 |  |  | class PrototypeVisitor(EmitVisitor): | 
					
						
							|  |  |  |     """Generate function prototypes for the .h file""" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def visitModule(self, mod): | 
					
						
							|  |  |  |         for dfn in mod.dfns: | 
					
						
							|  |  |  |             self.visit(dfn) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def visitType(self, type): | 
					
						
							|  |  |  |         self.visit(type.value, type.name) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def visitSum(self, sum, name): | 
					
						
							|  |  |  |         if is_simple(sum): | 
					
						
							|  |  |  |             pass # XXX | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             for t in sum.types: | 
					
						
							|  |  |  |                 self.visit(t, name, sum.attributes) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def get_args(self, fields): | 
					
						
							| 
									
										
										
										
											2023-02-10 06:10:46 +03:00
										 |  |  |         """Return list of C argument info, one for each field.
 | 
					
						
							| 
									
										
										
										
											2005-10-20 19:59:25 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |         Argument info is 3-tuple of a C type, variable name, and flag | 
					
						
							|  |  |  |         that is true if type can be NULL. | 
					
						
							|  |  |  |         """
 | 
					
						
							|  |  |  |         args = [] | 
					
						
							|  |  |  |         unnamed = {} | 
					
						
							|  |  |  |         for f in fields: | 
					
						
							|  |  |  |             if f.name is None: | 
					
						
							|  |  |  |                 name = f.type | 
					
						
							|  |  |  |                 c = unnamed[name] = unnamed.get(name, 0) + 1 | 
					
						
							|  |  |  |                 if c > 1: | 
					
						
							|  |  |  |                     name = "name%d" % (c - 1) | 
					
						
							|  |  |  |             else: | 
					
						
							|  |  |  |                 name = f.name | 
					
						
							|  |  |  |             # XXX should extend get_c_type() to handle this | 
					
						
							|  |  |  |             if f.seq: | 
					
						
							| 
									
										
										
										
											2021-06-22 19:29:42 +03:00
										 |  |  |                 if f.type in self.metadata.simple_sums: | 
					
						
							| 
									
										
										
										
											2006-04-21 10:40:58 +00:00
										 |  |  |                     ctype = "asdl_int_seq *" | 
					
						
							|  |  |  |                 else: | 
					
						
							| 
									
										
										
										
											2020-09-16 19:42:00 +01:00
										 |  |  |                     ctype = f"asdl_{f.type}_seq *" | 
					
						
							| 
									
										
										
										
											2005-10-20 19:59:25 +00:00
										 |  |  |             else: | 
					
						
							|  |  |  |                 ctype = get_c_type(f.type) | 
					
						
							|  |  |  |             args.append((ctype, name, f.opt or f.seq)) | 
					
						
							|  |  |  |         return args | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def visitConstructor(self, cons, type, attrs): | 
					
						
							|  |  |  |         args = self.get_args(cons.fields) | 
					
						
							|  |  |  |         attrs = self.get_args(attrs) | 
					
						
							|  |  |  |         ctype = get_c_type(type) | 
					
						
							|  |  |  |         self.emit_function(cons.name, ctype, args, attrs) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
											  
											
												Merged revisions 73196,73278-73280,73299,73308,73312-73313,73317-73318,73321,73324,73331,73335,73340,73363 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
  r73196 | benjamin.peterson | 2009-06-03 20:40:29 -0500 (Wed, 03 Jun 2009) | 1 line
  use the offical api
........
  r73278 | benjamin.peterson | 2009-06-07 17:33:11 -0500 (Sun, 07 Jun 2009) | 1 line
  inherit from object
........
  r73279 | benjamin.peterson | 2009-06-07 17:35:00 -0500 (Sun, 07 Jun 2009) | 1 line
  always inherit from an appropiate base class
........
  r73280 | benjamin.peterson | 2009-06-07 17:54:35 -0500 (Sun, 07 Jun 2009) | 1 line
  use booleans for flags
........
  r73299 | georg.brandl | 2009-06-08 13:41:36 -0500 (Mon, 08 Jun 2009) | 1 line
  Typo fix.
........
  r73308 | benjamin.peterson | 2009-06-08 17:18:32 -0500 (Mon, 08 Jun 2009) | 1 line
  remove useless assertion
........
  r73312 | benjamin.peterson | 2009-06-08 18:44:13 -0500 (Mon, 08 Jun 2009) | 1 line
  remove error checks already done in set_context()
........
  r73313 | r.david.murray | 2009-06-08 19:44:22 -0500 (Mon, 08 Jun 2009) | 4 lines
  Issue 2947: document how return code handling translates from
  os.popen to subprocess.  Also fixes reference link in the
  os.spawn documentation.
........
  r73317 | benjamin.peterson | 2009-06-09 12:24:26 -0500 (Tue, 09 Jun 2009) | 1 line
  make ast.c depend on the grammar
........
  r73318 | benjamin.peterson | 2009-06-09 12:29:51 -0500 (Tue, 09 Jun 2009) | 1 line
  explain why keyword names are not just NAME
........
  r73321 | benjamin.peterson | 2009-06-09 16:13:43 -0500 (Tue, 09 Jun 2009) | 1 line
  update symbol.py from with statement changes
........
  r73324 | amaury.forgeotdarc | 2009-06-09 17:53:16 -0500 (Tue, 09 Jun 2009) | 2 lines
  Avoid invoking the parser/compiler just to test the presence of a function.
........
  r73331 | benjamin.peterson | 2009-06-10 08:45:31 -0500 (Wed, 10 Jun 2009) | 1 line
  fix spelling
........
  r73335 | raymond.hettinger | 2009-06-10 11:15:40 -0500 (Wed, 10 Jun 2009) | 1 line
  Fix signed/unsigned compiler warning.
........
  r73340 | amaury.forgeotdarc | 2009-06-10 15:30:19 -0500 (Wed, 10 Jun 2009) | 2 lines
  Fix a typo spotted by Nick Coghlan.
........
  r73363 | benjamin.peterson | 2009-06-11 12:51:17 -0500 (Thu, 11 Jun 2009) | 1 line
  use multi-with syntax
........
											
										 
											2009-06-11 22:54:11 +00:00
										 |  |  |     def emit_function(self, name, ctype, args, attrs, union=True): | 
					
						
							| 
									
										
										
										
											2005-10-20 19:59:25 +00:00
										 |  |  |         args = args + attrs | 
					
						
							|  |  |  |         if args: | 
					
						
							|  |  |  |             argstr = ", ".join(["%s %s" % (atype, aname) | 
					
						
							|  |  |  |                                 for atype, aname, opt in args]) | 
					
						
							| 
									
										
										
										
											2005-12-17 20:54:49 +00:00
										 |  |  |             argstr += ", PyArena *arena" | 
					
						
							| 
									
										
										
										
											2005-10-20 19:59:25 +00:00
										 |  |  |         else: | 
					
						
							| 
									
										
										
										
											2005-12-17 20:54:49 +00:00
										 |  |  |             argstr = "PyArena *arena" | 
					
						
							| 
									
										
										
										
											2021-04-07 21:34:22 +02:00
										 |  |  |         self.emit("%s %s(%s);" % (ctype, ast_func_name(name), argstr), False) | 
					
						
							| 
									
										
										
										
											2005-10-20 19:59:25 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def visitProduct(self, prod, name): | 
					
						
							|  |  |  |         self.emit_function(name, get_c_type(name), | 
					
						
							| 
									
										
										
										
											2015-11-06 17:01:48 +01:00
										 |  |  |                            self.get_args(prod.fields), | 
					
						
							|  |  |  |                            self.get_args(prod.attributes), | 
					
						
							|  |  |  |                            union=False) | 
					
						
							| 
									
										
										
										
											2005-10-20 19:59:25 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-03-30 20:03:44 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-10-20 19:59:25 +00:00
										 |  |  | class FunctionVisitor(PrototypeVisitor): | 
					
						
							|  |  |  |     """Visitor to generate constructor functions for AST.""" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
											  
											
												Merged revisions 73196,73278-73280,73299,73308,73312-73313,73317-73318,73321,73324,73331,73335,73340,73363 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
  r73196 | benjamin.peterson | 2009-06-03 20:40:29 -0500 (Wed, 03 Jun 2009) | 1 line
  use the offical api
........
  r73278 | benjamin.peterson | 2009-06-07 17:33:11 -0500 (Sun, 07 Jun 2009) | 1 line
  inherit from object
........
  r73279 | benjamin.peterson | 2009-06-07 17:35:00 -0500 (Sun, 07 Jun 2009) | 1 line
  always inherit from an appropiate base class
........
  r73280 | benjamin.peterson | 2009-06-07 17:54:35 -0500 (Sun, 07 Jun 2009) | 1 line
  use booleans for flags
........
  r73299 | georg.brandl | 2009-06-08 13:41:36 -0500 (Mon, 08 Jun 2009) | 1 line
  Typo fix.
........
  r73308 | benjamin.peterson | 2009-06-08 17:18:32 -0500 (Mon, 08 Jun 2009) | 1 line
  remove useless assertion
........
  r73312 | benjamin.peterson | 2009-06-08 18:44:13 -0500 (Mon, 08 Jun 2009) | 1 line
  remove error checks already done in set_context()
........
  r73313 | r.david.murray | 2009-06-08 19:44:22 -0500 (Mon, 08 Jun 2009) | 4 lines
  Issue 2947: document how return code handling translates from
  os.popen to subprocess.  Also fixes reference link in the
  os.spawn documentation.
........
  r73317 | benjamin.peterson | 2009-06-09 12:24:26 -0500 (Tue, 09 Jun 2009) | 1 line
  make ast.c depend on the grammar
........
  r73318 | benjamin.peterson | 2009-06-09 12:29:51 -0500 (Tue, 09 Jun 2009) | 1 line
  explain why keyword names are not just NAME
........
  r73321 | benjamin.peterson | 2009-06-09 16:13:43 -0500 (Tue, 09 Jun 2009) | 1 line
  update symbol.py from with statement changes
........
  r73324 | amaury.forgeotdarc | 2009-06-09 17:53:16 -0500 (Tue, 09 Jun 2009) | 2 lines
  Avoid invoking the parser/compiler just to test the presence of a function.
........
  r73331 | benjamin.peterson | 2009-06-10 08:45:31 -0500 (Wed, 10 Jun 2009) | 1 line
  fix spelling
........
  r73335 | raymond.hettinger | 2009-06-10 11:15:40 -0500 (Wed, 10 Jun 2009) | 1 line
  Fix signed/unsigned compiler warning.
........
  r73340 | amaury.forgeotdarc | 2009-06-10 15:30:19 -0500 (Wed, 10 Jun 2009) | 2 lines
  Fix a typo spotted by Nick Coghlan.
........
  r73363 | benjamin.peterson | 2009-06-11 12:51:17 -0500 (Thu, 11 Jun 2009) | 1 line
  use multi-with syntax
........
											
										 
											2009-06-11 22:54:11 +00:00
										 |  |  |     def emit_function(self, name, ctype, args, attrs, union=True): | 
					
						
							|  |  |  |         def emit(s, depth=0, reflow=True): | 
					
						
							| 
									
										
										
										
											2005-10-20 19:59:25 +00:00
										 |  |  |             self.emit(s, depth, reflow) | 
					
						
							|  |  |  |         argstr = ", ".join(["%s %s" % (atype, aname) | 
					
						
							|  |  |  |                             for atype, aname, opt in args + attrs]) | 
					
						
							| 
									
										
										
										
											2005-12-17 20:54:49 +00:00
										 |  |  |         if argstr: | 
					
						
							|  |  |  |             argstr += ", PyArena *arena" | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             argstr = "PyArena *arena" | 
					
						
							| 
									
										
										
										
											2005-10-20 19:59:25 +00:00
										 |  |  |         self.emit("%s" % ctype, 0) | 
					
						
							| 
									
										
										
										
											2021-04-07 21:34:22 +02:00
										 |  |  |         emit("%s(%s)" % (ast_func_name(name), argstr)) | 
					
						
							| 
									
										
										
										
											2005-10-20 19:59:25 +00:00
										 |  |  |         emit("{") | 
					
						
							|  |  |  |         emit("%s p;" % ctype, 1) | 
					
						
							|  |  |  |         for argtype, argname, opt in args: | 
					
						
							| 
									
										
										
										
											2007-02-26 19:04:49 +00:00
										 |  |  |             if not opt and argtype != "int": | 
					
						
							| 
									
										
										
										
											2005-10-20 19:59:25 +00:00
										 |  |  |                 emit("if (!%s) {" % argname, 1) | 
					
						
							|  |  |  |                 emit("PyErr_SetString(PyExc_ValueError,", 2) | 
					
						
							| 
									
										
										
										
											2020-05-06 17:29:32 +03:00
										 |  |  |                 msg = "field '%s' is required for %s" % (argname, name) | 
					
						
							| 
									
										
										
										
											2005-10-20 19:59:25 +00:00
										 |  |  |                 emit('                "%s");' % msg, | 
					
						
							| 
									
										
											  
											
												Merged revisions 73196,73278-73280,73299,73308,73312-73313,73317-73318,73321,73324,73331,73335,73340,73363 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
  r73196 | benjamin.peterson | 2009-06-03 20:40:29 -0500 (Wed, 03 Jun 2009) | 1 line
  use the offical api
........
  r73278 | benjamin.peterson | 2009-06-07 17:33:11 -0500 (Sun, 07 Jun 2009) | 1 line
  inherit from object
........
  r73279 | benjamin.peterson | 2009-06-07 17:35:00 -0500 (Sun, 07 Jun 2009) | 1 line
  always inherit from an appropiate base class
........
  r73280 | benjamin.peterson | 2009-06-07 17:54:35 -0500 (Sun, 07 Jun 2009) | 1 line
  use booleans for flags
........
  r73299 | georg.brandl | 2009-06-08 13:41:36 -0500 (Mon, 08 Jun 2009) | 1 line
  Typo fix.
........
  r73308 | benjamin.peterson | 2009-06-08 17:18:32 -0500 (Mon, 08 Jun 2009) | 1 line
  remove useless assertion
........
  r73312 | benjamin.peterson | 2009-06-08 18:44:13 -0500 (Mon, 08 Jun 2009) | 1 line
  remove error checks already done in set_context()
........
  r73313 | r.david.murray | 2009-06-08 19:44:22 -0500 (Mon, 08 Jun 2009) | 4 lines
  Issue 2947: document how return code handling translates from
  os.popen to subprocess.  Also fixes reference link in the
  os.spawn documentation.
........
  r73317 | benjamin.peterson | 2009-06-09 12:24:26 -0500 (Tue, 09 Jun 2009) | 1 line
  make ast.c depend on the grammar
........
  r73318 | benjamin.peterson | 2009-06-09 12:29:51 -0500 (Tue, 09 Jun 2009) | 1 line
  explain why keyword names are not just NAME
........
  r73321 | benjamin.peterson | 2009-06-09 16:13:43 -0500 (Tue, 09 Jun 2009) | 1 line
  update symbol.py from with statement changes
........
  r73324 | amaury.forgeotdarc | 2009-06-09 17:53:16 -0500 (Tue, 09 Jun 2009) | 2 lines
  Avoid invoking the parser/compiler just to test the presence of a function.
........
  r73331 | benjamin.peterson | 2009-06-10 08:45:31 -0500 (Wed, 10 Jun 2009) | 1 line
  fix spelling
........
  r73335 | raymond.hettinger | 2009-06-10 11:15:40 -0500 (Wed, 10 Jun 2009) | 1 line
  Fix signed/unsigned compiler warning.
........
  r73340 | amaury.forgeotdarc | 2009-06-10 15:30:19 -0500 (Wed, 10 Jun 2009) | 2 lines
  Fix a typo spotted by Nick Coghlan.
........
  r73363 | benjamin.peterson | 2009-06-11 12:51:17 -0500 (Thu, 11 Jun 2009) | 1 line
  use multi-with syntax
........
											
										 
											2009-06-11 22:54:11 +00:00
										 |  |  |                      2, reflow=False) | 
					
						
							| 
									
										
										
										
											2005-10-20 19:59:25 +00:00
										 |  |  |                 emit('return NULL;', 2) | 
					
						
							|  |  |  |                 emit('}', 1) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-24 02:23:01 +01:00
										 |  |  |         emit("p = (%s)_PyArena_Malloc(arena, sizeof(*p));" % ctype, 1); | 
					
						
							| 
									
										
										
										
											2007-02-26 18:20:15 +00:00
										 |  |  |         emit("if (!p)", 1) | 
					
						
							| 
									
										
										
										
											2005-10-20 19:59:25 +00:00
										 |  |  |         emit("return NULL;", 2) | 
					
						
							|  |  |  |         if union: | 
					
						
							|  |  |  |             self.emit_body_union(name, args, attrs) | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             self.emit_body_struct(name, args, attrs) | 
					
						
							|  |  |  |         emit("return p;", 1) | 
					
						
							|  |  |  |         emit("}") | 
					
						
							|  |  |  |         emit("") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def emit_body_union(self, name, args, attrs): | 
					
						
							| 
									
										
											  
											
												Merged revisions 73196,73278-73280,73299,73308,73312-73313,73317-73318,73321,73324,73331,73335,73340,73363 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
  r73196 | benjamin.peterson | 2009-06-03 20:40:29 -0500 (Wed, 03 Jun 2009) | 1 line
  use the offical api
........
  r73278 | benjamin.peterson | 2009-06-07 17:33:11 -0500 (Sun, 07 Jun 2009) | 1 line
  inherit from object
........
  r73279 | benjamin.peterson | 2009-06-07 17:35:00 -0500 (Sun, 07 Jun 2009) | 1 line
  always inherit from an appropiate base class
........
  r73280 | benjamin.peterson | 2009-06-07 17:54:35 -0500 (Sun, 07 Jun 2009) | 1 line
  use booleans for flags
........
  r73299 | georg.brandl | 2009-06-08 13:41:36 -0500 (Mon, 08 Jun 2009) | 1 line
  Typo fix.
........
  r73308 | benjamin.peterson | 2009-06-08 17:18:32 -0500 (Mon, 08 Jun 2009) | 1 line
  remove useless assertion
........
  r73312 | benjamin.peterson | 2009-06-08 18:44:13 -0500 (Mon, 08 Jun 2009) | 1 line
  remove error checks already done in set_context()
........
  r73313 | r.david.murray | 2009-06-08 19:44:22 -0500 (Mon, 08 Jun 2009) | 4 lines
  Issue 2947: document how return code handling translates from
  os.popen to subprocess.  Also fixes reference link in the
  os.spawn documentation.
........
  r73317 | benjamin.peterson | 2009-06-09 12:24:26 -0500 (Tue, 09 Jun 2009) | 1 line
  make ast.c depend on the grammar
........
  r73318 | benjamin.peterson | 2009-06-09 12:29:51 -0500 (Tue, 09 Jun 2009) | 1 line
  explain why keyword names are not just NAME
........
  r73321 | benjamin.peterson | 2009-06-09 16:13:43 -0500 (Tue, 09 Jun 2009) | 1 line
  update symbol.py from with statement changes
........
  r73324 | amaury.forgeotdarc | 2009-06-09 17:53:16 -0500 (Tue, 09 Jun 2009) | 2 lines
  Avoid invoking the parser/compiler just to test the presence of a function.
........
  r73331 | benjamin.peterson | 2009-06-10 08:45:31 -0500 (Wed, 10 Jun 2009) | 1 line
  fix spelling
........
  r73335 | raymond.hettinger | 2009-06-10 11:15:40 -0500 (Wed, 10 Jun 2009) | 1 line
  Fix signed/unsigned compiler warning.
........
  r73340 | amaury.forgeotdarc | 2009-06-10 15:30:19 -0500 (Wed, 10 Jun 2009) | 2 lines
  Fix a typo spotted by Nick Coghlan.
........
  r73363 | benjamin.peterson | 2009-06-11 12:51:17 -0500 (Thu, 11 Jun 2009) | 1 line
  use multi-with syntax
........
											
										 
											2009-06-11 22:54:11 +00:00
										 |  |  |         def emit(s, depth=0, reflow=True): | 
					
						
							| 
									
										
										
										
											2005-10-20 19:59:25 +00:00
										 |  |  |             self.emit(s, depth, reflow) | 
					
						
							|  |  |  |         emit("p->kind = %s_kind;" % name, 1) | 
					
						
							|  |  |  |         for argtype, argname, opt in args: | 
					
						
							|  |  |  |             emit("p->v.%s.%s = %s;" % (name, argname, argname), 1) | 
					
						
							|  |  |  |         for argtype, argname, opt in attrs: | 
					
						
							|  |  |  |             emit("p->%s = %s;" % (argname, argname), 1) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def emit_body_struct(self, name, args, attrs): | 
					
						
							| 
									
										
											  
											
												Merged revisions 73196,73278-73280,73299,73308,73312-73313,73317-73318,73321,73324,73331,73335,73340,73363 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
  r73196 | benjamin.peterson | 2009-06-03 20:40:29 -0500 (Wed, 03 Jun 2009) | 1 line
  use the offical api
........
  r73278 | benjamin.peterson | 2009-06-07 17:33:11 -0500 (Sun, 07 Jun 2009) | 1 line
  inherit from object
........
  r73279 | benjamin.peterson | 2009-06-07 17:35:00 -0500 (Sun, 07 Jun 2009) | 1 line
  always inherit from an appropiate base class
........
  r73280 | benjamin.peterson | 2009-06-07 17:54:35 -0500 (Sun, 07 Jun 2009) | 1 line
  use booleans for flags
........
  r73299 | georg.brandl | 2009-06-08 13:41:36 -0500 (Mon, 08 Jun 2009) | 1 line
  Typo fix.
........
  r73308 | benjamin.peterson | 2009-06-08 17:18:32 -0500 (Mon, 08 Jun 2009) | 1 line
  remove useless assertion
........
  r73312 | benjamin.peterson | 2009-06-08 18:44:13 -0500 (Mon, 08 Jun 2009) | 1 line
  remove error checks already done in set_context()
........
  r73313 | r.david.murray | 2009-06-08 19:44:22 -0500 (Mon, 08 Jun 2009) | 4 lines
  Issue 2947: document how return code handling translates from
  os.popen to subprocess.  Also fixes reference link in the
  os.spawn documentation.
........
  r73317 | benjamin.peterson | 2009-06-09 12:24:26 -0500 (Tue, 09 Jun 2009) | 1 line
  make ast.c depend on the grammar
........
  r73318 | benjamin.peterson | 2009-06-09 12:29:51 -0500 (Tue, 09 Jun 2009) | 1 line
  explain why keyword names are not just NAME
........
  r73321 | benjamin.peterson | 2009-06-09 16:13:43 -0500 (Tue, 09 Jun 2009) | 1 line
  update symbol.py from with statement changes
........
  r73324 | amaury.forgeotdarc | 2009-06-09 17:53:16 -0500 (Tue, 09 Jun 2009) | 2 lines
  Avoid invoking the parser/compiler just to test the presence of a function.
........
  r73331 | benjamin.peterson | 2009-06-10 08:45:31 -0500 (Wed, 10 Jun 2009) | 1 line
  fix spelling
........
  r73335 | raymond.hettinger | 2009-06-10 11:15:40 -0500 (Wed, 10 Jun 2009) | 1 line
  Fix signed/unsigned compiler warning.
........
  r73340 | amaury.forgeotdarc | 2009-06-10 15:30:19 -0500 (Wed, 10 Jun 2009) | 2 lines
  Fix a typo spotted by Nick Coghlan.
........
  r73363 | benjamin.peterson | 2009-06-11 12:51:17 -0500 (Thu, 11 Jun 2009) | 1 line
  use multi-with syntax
........
											
										 
											2009-06-11 22:54:11 +00:00
										 |  |  |         def emit(s, depth=0, reflow=True): | 
					
						
							| 
									
										
										
										
											2005-10-20 19:59:25 +00:00
										 |  |  |             self.emit(s, depth, reflow) | 
					
						
							|  |  |  |         for argtype, argname, opt in args: | 
					
						
							|  |  |  |             emit("p->%s = %s;" % (argname, argname), 1) | 
					
						
							| 
									
										
										
										
											2015-11-06 17:01:48 +01:00
										 |  |  |         for argtype, argname, opt in attrs: | 
					
						
							|  |  |  |             emit("p->%s = %s;" % (argname, argname), 1) | 
					
						
							| 
									
										
										
										
											2005-10-20 19:59:25 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-03-30 20:03:44 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-10-20 19:59:25 +00:00
										 |  |  | class PickleVisitor(EmitVisitor): | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def visitModule(self, mod): | 
					
						
							|  |  |  |         for dfn in mod.dfns: | 
					
						
							|  |  |  |             self.visit(dfn) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def visitType(self, type): | 
					
						
							|  |  |  |         self.visit(type.value, type.name) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def visitSum(self, sum, name): | 
					
						
							|  |  |  |         pass | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def visitProduct(self, sum, name): | 
					
						
							|  |  |  |         pass | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def visitConstructor(self, cons, name): | 
					
						
							|  |  |  |         pass | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def visitField(self, sum): | 
					
						
							|  |  |  |         pass | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-03-30 20:03:44 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | class Obj2ModPrototypeVisitor(PickleVisitor): | 
					
						
							|  |  |  |     def visitProduct(self, prod, name): | 
					
						
							| 
									
										
										
										
											2020-11-02 22:03:28 +01:00
										 |  |  |         code = "static int obj2ast_%s(struct ast_state *state, PyObject* obj, %s* out, PyArena* arena);" | 
					
						
							| 
									
										
										
										
											2008-03-30 20:03:44 +00:00
										 |  |  |         self.emit(code % (name, get_c_type(name)), 0) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     visitSum = visitProduct | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class Obj2ModVisitor(PickleVisitor): | 
					
						
							| 
									
										
										
										
											2022-06-01 00:00:47 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     attribute_special_defaults = { | 
					
						
							|  |  |  |         "end_lineno": "lineno", | 
					
						
							|  |  |  |         "end_col_offset": "col_offset", | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-03 23:01:02 +03:00
										 |  |  |     @contextmanager | 
					
						
							|  |  |  |     def recursive_call(self, node, level): | 
					
						
							| 
									
										
										
										
											2022-05-04 13:30:23 +02:00
										 |  |  |         self.emit('if (_Py_EnterRecursiveCall(" while traversing \'%s\' node")) {' % node, level, reflow=False) | 
					
						
							| 
									
										
										
										
											2021-06-03 23:01:02 +03:00
										 |  |  |         self.emit('goto failed;', level + 1) | 
					
						
							|  |  |  |         self.emit('}', level) | 
					
						
							|  |  |  |         yield | 
					
						
							| 
									
										
										
										
											2022-05-04 13:30:23 +02:00
										 |  |  |         self.emit('_Py_LeaveRecursiveCall();', level) | 
					
						
							| 
									
										
										
										
											2021-06-03 23:01:02 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-03-30 20:03:44 +00:00
										 |  |  |     def funcHeader(self, name): | 
					
						
							|  |  |  |         ctype = get_c_type(name) | 
					
						
							|  |  |  |         self.emit("int", 0) | 
					
						
							| 
									
										
										
										
											2020-11-02 22:03:28 +01:00
										 |  |  |         self.emit("obj2ast_%s(struct ast_state *state, PyObject* obj, %s* out, PyArena* arena)" % (name, ctype), 0) | 
					
						
							| 
									
										
										
										
											2008-03-30 20:03:44 +00:00
										 |  |  |         self.emit("{", 0) | 
					
						
							| 
									
										
										
										
											2010-11-20 02:01:45 +00:00
										 |  |  |         self.emit("int isinstance;", 1) | 
					
						
							| 
									
										
										
										
											2008-03-30 20:03:44 +00:00
										 |  |  |         self.emit("", 0) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-11-20 01:38:49 +00:00
										 |  |  |     def sumTrailer(self, name, add_label=False): | 
					
						
							| 
									
										
										
										
											2008-03-30 20:03:44 +00:00
										 |  |  |         self.emit("", 0) | 
					
						
							|  |  |  |         # there's really nothing more we can do if this fails ... | 
					
						
							| 
									
										
										
										
											2010-11-20 01:38:49 +00:00
										 |  |  |         error = "expected some sort of %s, but got %%R" % name | 
					
						
							|  |  |  |         format = "PyErr_Format(PyExc_TypeError, \"%s\", obj);" | 
					
						
							| 
									
										
										
										
											2008-03-30 20:03:44 +00:00
										 |  |  |         self.emit(format % error, 1, reflow=False) | 
					
						
							| 
									
										
										
										
											2010-11-20 01:38:49 +00:00
										 |  |  |         if add_label: | 
					
						
							|  |  |  |             self.emit("failed:", 1) | 
					
						
							| 
									
										
										
										
											2010-11-21 15:12:34 +00:00
										 |  |  |             self.emit("Py_XDECREF(tmp);", 1) | 
					
						
							| 
									
										
										
										
											2023-11-16 14:19:54 -05:00
										 |  |  |         self.emit("return -1;", 1) | 
					
						
							| 
									
										
										
										
											2008-03-30 20:03:44 +00:00
										 |  |  |         self.emit("}", 0) | 
					
						
							|  |  |  |         self.emit("", 0) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def simpleSum(self, sum, name): | 
					
						
							|  |  |  |         self.funcHeader(name) | 
					
						
							|  |  |  |         for t in sum.types: | 
					
						
							| 
									
										
										
										
											2009-12-13 01:23:39 +00:00
										 |  |  |             line = ("isinstance = PyObject_IsInstance(obj, " | 
					
						
							| 
									
										
										
										
											2020-07-03 11:35:37 +02:00
										 |  |  |                     "state->%s_type);") | 
					
						
							| 
									
										
										
										
											2009-12-13 01:23:39 +00:00
										 |  |  |             self.emit(line % (t.name,), 1) | 
					
						
							|  |  |  |             self.emit("if (isinstance == -1) {", 1) | 
					
						
							| 
									
										
										
										
											2023-11-16 14:19:54 -05:00
										 |  |  |             self.emit("return -1;", 2) | 
					
						
							| 
									
										
										
										
											2009-12-13 01:23:39 +00:00
										 |  |  |             self.emit("}", 1) | 
					
						
							|  |  |  |             self.emit("if (isinstance) {", 1) | 
					
						
							| 
									
										
										
										
											2008-03-30 20:03:44 +00:00
										 |  |  |             self.emit("*out = %s;" % t.name, 2) | 
					
						
							|  |  |  |             self.emit("return 0;", 2) | 
					
						
							|  |  |  |             self.emit("}", 1) | 
					
						
							|  |  |  |         self.sumTrailer(name) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def buildArgs(self, fields): | 
					
						
							|  |  |  |         return ", ".join(fields + ["arena"]) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def complexSum(self, sum, name): | 
					
						
							|  |  |  |         self.funcHeader(name) | 
					
						
							| 
									
										
										
										
											2010-11-20 04:31:07 +00:00
										 |  |  |         self.emit("PyObject *tmp = NULL;", 1) | 
					
						
							| 
									
										
										
										
											2019-09-11 10:16:34 -07:00
										 |  |  |         self.emit("PyObject *tp;", 1) | 
					
						
							| 
									
										
										
										
											2008-03-30 20:03:44 +00:00
										 |  |  |         for a in sum.attributes: | 
					
						
							|  |  |  |             self.visitAttributeDeclaration(a, name, sum=sum) | 
					
						
							|  |  |  |         self.emit("", 0) | 
					
						
							|  |  |  |         # XXX: should we only do this for 'expr'? | 
					
						
							|  |  |  |         self.emit("if (obj == Py_None) {", 1) | 
					
						
							|  |  |  |         self.emit("*out = NULL;", 2) | 
					
						
							|  |  |  |         self.emit("return 0;", 2) | 
					
						
							|  |  |  |         self.emit("}", 1) | 
					
						
							|  |  |  |         for a in sum.attributes: | 
					
						
							|  |  |  |             self.visitField(a, name, sum=sum, depth=1) | 
					
						
							|  |  |  |         for t in sum.types: | 
					
						
							| 
									
										
										
										
											2020-07-03 11:35:37 +02:00
										 |  |  |             self.emit("tp = state->%s_type;" % (t.name,), 1) | 
					
						
							| 
									
										
										
										
											2019-09-11 10:16:34 -07:00
										 |  |  |             self.emit("isinstance = PyObject_IsInstance(obj, tp);", 1) | 
					
						
							| 
									
										
										
										
											2009-12-13 01:23:39 +00:00
										 |  |  |             self.emit("if (isinstance == -1) {", 1) | 
					
						
							| 
									
										
										
										
											2023-11-16 14:19:54 -05:00
										 |  |  |             self.emit("return -1;", 2) | 
					
						
							| 
									
										
										
										
											2009-12-13 01:23:39 +00:00
										 |  |  |             self.emit("}", 1) | 
					
						
							|  |  |  |             self.emit("if (isinstance) {", 1) | 
					
						
							| 
									
										
										
										
											2008-03-30 20:03:44 +00:00
										 |  |  |             for f in t.fields: | 
					
						
							|  |  |  |                 self.visitFieldDeclaration(f, t.name, sum=sum, depth=2) | 
					
						
							|  |  |  |             self.emit("", 0) | 
					
						
							|  |  |  |             for f in t.fields: | 
					
						
							|  |  |  |                 self.visitField(f, t.name, sum=sum, depth=2) | 
					
						
							| 
									
										
										
										
											2014-05-09 17:58:22 -07:00
										 |  |  |             args = [f.name for f in t.fields] + [a.name for a in sum.attributes] | 
					
						
							| 
									
										
										
										
											2021-04-07 21:34:22 +02:00
										 |  |  |             self.emit("*out = %s(%s);" % (ast_func_name(t.name), self.buildArgs(args)), 2) | 
					
						
							| 
									
										
										
										
											2008-03-30 20:03:44 +00:00
										 |  |  |             self.emit("if (*out == NULL) goto failed;", 2) | 
					
						
							|  |  |  |             self.emit("return 0;", 2) | 
					
						
							|  |  |  |             self.emit("}", 1) | 
					
						
							| 
									
										
										
										
											2010-11-20 01:38:49 +00:00
										 |  |  |         self.sumTrailer(name, True) | 
					
						
							| 
									
										
										
										
											2008-03-30 20:03:44 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def visitAttributeDeclaration(self, a, name, sum=sum): | 
					
						
							|  |  |  |         ctype = get_c_type(a.type) | 
					
						
							|  |  |  |         self.emit("%s %s;" % (ctype, a.name), 1) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def visitSum(self, sum, name): | 
					
						
							|  |  |  |         if is_simple(sum): | 
					
						
							|  |  |  |             self.simpleSum(sum, name) | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             self.complexSum(sum, name) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def visitProduct(self, prod, name): | 
					
						
							|  |  |  |         ctype = get_c_type(name) | 
					
						
							|  |  |  |         self.emit("int", 0) | 
					
						
							| 
									
										
										
										
											2020-11-02 22:03:28 +01:00
										 |  |  |         self.emit("obj2ast_%s(struct ast_state *state, PyObject* obj, %s* out, PyArena* arena)" % (name, ctype), 0) | 
					
						
							| 
									
										
										
										
											2008-03-30 20:03:44 +00:00
										 |  |  |         self.emit("{", 0) | 
					
						
							|  |  |  |         self.emit("PyObject* tmp = NULL;", 1) | 
					
						
							|  |  |  |         for f in prod.fields: | 
					
						
							|  |  |  |             self.visitFieldDeclaration(f, name, prod=prod, depth=1) | 
					
						
							| 
									
										
										
										
											2015-11-06 17:01:48 +01:00
										 |  |  |         for a in prod.attributes: | 
					
						
							|  |  |  |             self.visitFieldDeclaration(a, name, prod=prod, depth=1) | 
					
						
							| 
									
										
										
										
											2008-03-30 20:03:44 +00:00
										 |  |  |         self.emit("", 0) | 
					
						
							|  |  |  |         for f in prod.fields: | 
					
						
							|  |  |  |             self.visitField(f, name, prod=prod, depth=1) | 
					
						
							| 
									
										
										
										
											2015-11-06 17:01:48 +01:00
										 |  |  |         for a in prod.attributes: | 
					
						
							|  |  |  |             self.visitField(a, name, prod=prod, depth=1) | 
					
						
							| 
									
										
										
										
											2014-05-09 17:58:22 -07:00
										 |  |  |         args = [f.name for f in prod.fields] | 
					
						
							| 
									
										
										
										
											2015-11-06 17:01:48 +01:00
										 |  |  |         args.extend([a.name for a in prod.attributes]) | 
					
						
							| 
									
										
										
										
											2021-04-07 21:34:22 +02:00
										 |  |  |         self.emit("*out = %s(%s);" % (ast_func_name(name), self.buildArgs(args)), 1) | 
					
						
							| 
									
										
										
										
											2023-06-15 15:45:13 -07:00
										 |  |  |         self.emit("if (*out == NULL) goto failed;", 1) | 
					
						
							| 
									
										
										
										
											2008-03-30 20:03:44 +00:00
										 |  |  |         self.emit("return 0;", 1) | 
					
						
							|  |  |  |         self.emit("failed:", 0) | 
					
						
							|  |  |  |         self.emit("Py_XDECREF(tmp);", 1) | 
					
						
							| 
									
										
										
										
											2023-11-16 14:19:54 -05:00
										 |  |  |         self.emit("return -1;", 1) | 
					
						
							| 
									
										
										
										
											2008-03-30 20:03:44 +00:00
										 |  |  |         self.emit("}", 0) | 
					
						
							|  |  |  |         self.emit("", 0) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def visitFieldDeclaration(self, field, name, sum=None, prod=None, depth=0): | 
					
						
							|  |  |  |         ctype = get_c_type(field.type) | 
					
						
							|  |  |  |         if field.seq: | 
					
						
							|  |  |  |             if self.isSimpleType(field): | 
					
						
							|  |  |  |                 self.emit("asdl_int_seq* %s;" % field.name, depth) | 
					
						
							|  |  |  |             else: | 
					
						
							| 
									
										
										
										
											2020-09-16 19:42:00 +01:00
										 |  |  |                 _type = field.type | 
					
						
							|  |  |  |                 self.emit(f"asdl_{field.type}_seq* {field.name};", depth) | 
					
						
							| 
									
										
										
										
											2008-03-30 20:03:44 +00:00
										 |  |  |         else: | 
					
						
							|  |  |  |             ctype = get_c_type(field.type) | 
					
						
							|  |  |  |             self.emit("%s %s;" % (ctype, field.name), depth) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def isNumeric(self, field): | 
					
						
							|  |  |  |         return get_c_type(field.type) in ("int", "bool") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def isSimpleType(self, field): | 
					
						
							| 
									
										
										
										
											2021-06-22 19:29:42 +03:00
										 |  |  |         return field.type in self.metadata.simple_sums or self.isNumeric(field) | 
					
						
							| 
									
										
										
										
											2008-03-30 20:03:44 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def visitField(self, field, name, sum=None, prod=None, depth=0): | 
					
						
							|  |  |  |         ctype = get_c_type(field.type) | 
					
						
							| 
									
										
										
										
											2023-07-12 08:57:10 +03:00
										 |  |  |         line = "if (PyObject_GetOptionalAttr(obj, state->%s, &tmp) < 0) {" | 
					
						
							| 
									
										
										
										
											2019-09-11 10:16:34 -07:00
										 |  |  |         self.emit(line % field.name, depth) | 
					
						
							| 
									
										
										
										
											2023-11-16 14:19:54 -05:00
										 |  |  |         self.emit("return -1;", depth+1) | 
					
						
							| 
									
										
										
										
											2018-01-25 10:49:40 +02:00
										 |  |  |         self.emit("}", depth) | 
					
						
							| 
									
										
										
										
											2023-06-01 18:39:39 -07:00
										 |  |  |         if field.seq: | 
					
						
							| 
									
										
										
										
											2018-01-25 10:49:40 +02:00
										 |  |  |             self.emit("if (tmp == NULL) {", depth) | 
					
						
							| 
									
										
										
										
											2023-06-01 18:39:39 -07:00
										 |  |  |             self.emit("tmp = PyList_New(0);", depth+1) | 
					
						
							|  |  |  |             self.emit("if (tmp == NULL) {", depth+1) | 
					
						
							| 
									
										
										
										
											2023-11-16 14:19:54 -05:00
										 |  |  |             self.emit("return -1;", depth+2) | 
					
						
							| 
									
										
										
										
											2023-06-01 18:39:39 -07:00
										 |  |  |             self.emit("}", depth+1) | 
					
						
							|  |  |  |             self.emit("}", depth) | 
					
						
							|  |  |  |             self.emit("{", depth) | 
					
						
							| 
									
										
										
										
											2013-03-18 10:48:58 -07:00
										 |  |  |         else: | 
					
						
							| 
									
										
										
										
											2023-06-01 18:39:39 -07:00
										 |  |  |             if not field.opt: | 
					
						
							|  |  |  |                 self.emit("if (tmp == NULL) {", depth) | 
					
						
							|  |  |  |                 message = "required field \\\"%s\\\" missing from %s" % (field.name, name) | 
					
						
							|  |  |  |                 format = "PyErr_SetString(PyExc_TypeError, \"%s\");" | 
					
						
							|  |  |  |                 self.emit(format % message, depth+1, reflow=False) | 
					
						
							| 
									
										
										
										
											2023-11-16 14:19:54 -05:00
										 |  |  |                 self.emit("return -1;", depth+1) | 
					
						
							| 
									
										
										
										
											2018-01-25 10:49:40 +02:00
										 |  |  |             else: | 
					
						
							| 
									
										
										
										
											2023-06-01 18:39:39 -07:00
										 |  |  |                 self.emit("if (tmp == NULL || tmp == Py_None) {", depth) | 
					
						
							|  |  |  |                 self.emit("Py_CLEAR(tmp);", depth+1) | 
					
						
							|  |  |  |                 if self.isNumeric(field): | 
					
						
							|  |  |  |                     if field.name in self.attribute_special_defaults: | 
					
						
							|  |  |  |                         self.emit( | 
					
						
							|  |  |  |                             "%s = %s;" % (field.name, self.attribute_special_defaults[field.name]), | 
					
						
							|  |  |  |                             depth+1, | 
					
						
							|  |  |  |                         ) | 
					
						
							|  |  |  |                     else: | 
					
						
							|  |  |  |                         self.emit("%s = 0;" % field.name, depth+1) | 
					
						
							|  |  |  |                 elif not self.isSimpleType(field): | 
					
						
							|  |  |  |                     self.emit("%s = NULL;" % field.name, depth+1) | 
					
						
							|  |  |  |                 else: | 
					
						
							|  |  |  |                     raise TypeError("could not determine the default value for %s" % field.name) | 
					
						
							|  |  |  |             self.emit("}", depth) | 
					
						
							|  |  |  |             self.emit("else {", depth) | 
					
						
							| 
									
										
										
										
											2018-01-25 10:49:40 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-03-30 20:03:44 +00:00
										 |  |  |         self.emit("int res;", depth+1) | 
					
						
							|  |  |  |         if field.seq: | 
					
						
							|  |  |  |             self.emit("Py_ssize_t len;", depth+1) | 
					
						
							|  |  |  |             self.emit("Py_ssize_t i;", depth+1) | 
					
						
							|  |  |  |             self.emit("if (!PyList_Check(tmp)) {", depth+1) | 
					
						
							|  |  |  |             self.emit("PyErr_Format(PyExc_TypeError, \"%s field \\\"%s\\\" must " | 
					
						
							| 
									
										
										
										
											2019-09-11 10:16:34 -07:00
										 |  |  |                       "be a list, not a %%.200s\", _PyType_Name(Py_TYPE(tmp)));" % | 
					
						
							| 
									
										
										
										
											2008-03-30 20:03:44 +00:00
										 |  |  |                       (name, field.name), | 
					
						
							|  |  |  |                       depth+2, reflow=False) | 
					
						
							|  |  |  |             self.emit("goto failed;", depth+2) | 
					
						
							|  |  |  |             self.emit("}", depth+1) | 
					
						
							|  |  |  |             self.emit("len = PyList_GET_SIZE(tmp);", depth+1) | 
					
						
							|  |  |  |             if self.isSimpleType(field): | 
					
						
							| 
									
										
										
										
											2013-10-12 22:52:43 +02:00
										 |  |  |                 self.emit("%s = _Py_asdl_int_seq_new(len, arena);" % field.name, depth+1) | 
					
						
							| 
									
										
										
										
											2008-03-30 20:03:44 +00:00
										 |  |  |             else: | 
					
						
							| 
									
										
										
										
											2020-09-16 19:42:00 +01:00
										 |  |  |                 self.emit("%s = _Py_asdl_%s_seq_new(len, arena);" % (field.name, field.type), depth+1) | 
					
						
							| 
									
										
										
										
											2008-03-30 20:03:44 +00:00
										 |  |  |             self.emit("if (%s == NULL) goto failed;" % field.name, depth+1) | 
					
						
							|  |  |  |             self.emit("for (i = 0; i < len; i++) {", depth+1) | 
					
						
							| 
									
										
										
										
											2017-08-04 10:53:12 -07:00
										 |  |  |             self.emit("%s val;" % ctype, depth+2) | 
					
						
							| 
									
										
										
										
											2022-11-15 10:29:56 +01:00
										 |  |  |             self.emit("PyObject *tmp2 = Py_NewRef(PyList_GET_ITEM(tmp, i));", depth+2) | 
					
						
							| 
									
										
										
										
											2021-06-03 23:01:02 +03:00
										 |  |  |             with self.recursive_call(name, depth+2): | 
					
						
							|  |  |  |                 self.emit("res = obj2ast_%s(state, tmp2, &val, arena);" % | 
					
						
							|  |  |  |                           field.type, depth+2, reflow=False) | 
					
						
							| 
									
										
										
										
											2019-09-10 13:02:30 +03:00
										 |  |  |             self.emit("Py_DECREF(tmp2);", depth+2) | 
					
						
							| 
									
										
										
										
											2008-03-30 20:03:44 +00:00
										 |  |  |             self.emit("if (res != 0) goto failed;", depth+2) | 
					
						
							| 
									
										
										
										
											2016-10-07 21:51:28 +03:00
										 |  |  |             self.emit("if (len != PyList_GET_SIZE(tmp)) {", depth+2) | 
					
						
							|  |  |  |             self.emit("PyErr_SetString(PyExc_RuntimeError, \"%s field \\\"%s\\\" " | 
					
						
							|  |  |  |                       "changed size during iteration\");" % | 
					
						
							|  |  |  |                       (name, field.name), | 
					
						
							|  |  |  |                       depth+3, reflow=False) | 
					
						
							|  |  |  |             self.emit("goto failed;", depth+3) | 
					
						
							|  |  |  |             self.emit("}", depth+2) | 
					
						
							| 
									
										
										
										
											2017-08-04 10:53:12 -07:00
										 |  |  |             self.emit("asdl_seq_SET(%s, i, val);" % field.name, depth+2) | 
					
						
							| 
									
										
										
										
											2008-03-30 20:03:44 +00:00
										 |  |  |             self.emit("}", depth+1) | 
					
						
							|  |  |  |         else: | 
					
						
							| 
									
										
										
										
											2021-06-03 23:01:02 +03:00
										 |  |  |             with self.recursive_call(name, depth+1): | 
					
						
							|  |  |  |                 self.emit("res = obj2ast_%s(state, tmp, &%s, arena);" % | 
					
						
							|  |  |  |                           (field.type, field.name), depth+1) | 
					
						
							| 
									
										
										
										
											2008-03-30 20:03:44 +00:00
										 |  |  |             self.emit("if (res != 0) goto failed;", depth+1) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-27 00:03:47 +02:00
										 |  |  |         self.emit("Py_CLEAR(tmp);", depth+1) | 
					
						
							| 
									
										
										
										
											2008-03-30 20:03:44 +00:00
										 |  |  |         self.emit("}", depth) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-16 19:42:00 +01:00
										 |  |  | class SequenceConstructorVisitor(EmitVisitor): | 
					
						
							|  |  |  |     def visitModule(self, mod): | 
					
						
							|  |  |  |         for dfn in mod.dfns: | 
					
						
							|  |  |  |             self.visit(dfn) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def visitType(self, type): | 
					
						
							|  |  |  |         self.visit(type.value, type.name) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def visitProduct(self, prod, name): | 
					
						
							|  |  |  |         self.emit_sequence_constructor(name, get_c_type(name)) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def visitSum(self, sum, name): | 
					
						
							|  |  |  |         if not is_simple(sum): | 
					
						
							|  |  |  |             self.emit_sequence_constructor(name, get_c_type(name)) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def emit_sequence_constructor(self, name, type): | 
					
						
							|  |  |  |         self.emit(f"GENERATE_ASDL_SEQ_CONSTRUCTOR({name}, {type})", depth=0) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-02-26 19:42:26 +00:00
										 |  |  | class PyTypesDeclareVisitor(PickleVisitor): | 
					
						
							| 
									
										
										
										
											2005-10-20 19:59:25 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-02-26 19:42:26 +00:00
										 |  |  |     def visitProduct(self, prod, name): | 
					
						
							| 
									
										
										
										
											2023-12-25 09:36:59 -08:00
										 |  |  |         self.emit("static PyObject* ast2obj_%s(struct ast_state *state, struct validator *vstate, void*);" % name, 0) | 
					
						
							| 
									
										
										
										
											2013-03-18 10:48:58 -07:00
										 |  |  |         if prod.attributes: | 
					
						
							| 
									
										
										
										
											2019-09-10 13:02:30 +03:00
										 |  |  |             self.emit("static const char * const %s_attributes[] = {" % name, 0) | 
					
						
							| 
									
										
										
										
											2013-03-18 10:48:58 -07:00
										 |  |  |             for a in prod.attributes: | 
					
						
							|  |  |  |                 self.emit('"%s",' % a.name, 1) | 
					
						
							|  |  |  |             self.emit("};", 0) | 
					
						
							| 
									
										
										
										
											2006-02-26 23:40:20 +00:00
										 |  |  |         if prod.fields: | 
					
						
							| 
									
										
										
										
											2019-09-10 13:02:30 +03:00
										 |  |  |             self.emit("static const char * const %s_fields[]={" % name,0) | 
					
						
							| 
									
										
										
										
											2006-02-26 23:40:20 +00:00
										 |  |  |             for f in prod.fields: | 
					
						
							|  |  |  |                 self.emit('"%s",' % f.name, 1) | 
					
						
							|  |  |  |             self.emit("};", 0) | 
					
						
							| 
									
										
										
										
											2006-02-28 18:30:36 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-02-26 19:42:26 +00:00
										 |  |  |     def visitSum(self, sum, name): | 
					
						
							| 
									
										
										
										
											2006-02-27 15:23:19 +00:00
										 |  |  |         if sum.attributes: | 
					
						
							| 
									
										
										
										
											2019-09-10 13:02:30 +03:00
										 |  |  |             self.emit("static const char * const %s_attributes[] = {" % name, 0) | 
					
						
							| 
									
										
										
										
											2006-02-27 15:23:19 +00:00
										 |  |  |             for a in sum.attributes: | 
					
						
							|  |  |  |                 self.emit('"%s",' % a.name, 1) | 
					
						
							|  |  |  |             self.emit("};", 0) | 
					
						
							| 
									
										
										
										
											2006-02-26 19:42:26 +00:00
										 |  |  |         ptype = "void*" | 
					
						
							|  |  |  |         if is_simple(sum): | 
					
						
							|  |  |  |             ptype = get_c_type(name) | 
					
						
							| 
									
										
										
										
											2023-12-25 09:36:59 -08:00
										 |  |  |         self.emit("static PyObject* ast2obj_%s(struct ast_state *state, struct validator *vstate, %s);" % (name, ptype), 0) | 
					
						
							| 
									
										
										
										
											2006-02-26 19:42:26 +00:00
										 |  |  |         for t in sum.types: | 
					
						
							|  |  |  |             self.visitConstructor(t, name) | 
					
						
							| 
									
										
										
										
											2006-02-28 18:30:36 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-02-26 19:42:26 +00:00
										 |  |  |     def visitConstructor(self, cons, name): | 
					
						
							| 
									
										
										
										
											2006-02-26 23:40:20 +00:00
										 |  |  |         if cons.fields: | 
					
						
							| 
									
										
										
										
											2019-09-10 13:02:30 +03:00
										 |  |  |             self.emit("static const char * const %s_fields[]={" % cons.name, 0) | 
					
						
							| 
									
										
										
										
											2006-02-26 23:40:20 +00:00
										 |  |  |             for t in cons.fields: | 
					
						
							|  |  |  |                 self.emit('"%s",' % t.name, 1) | 
					
						
							|  |  |  |             self.emit("};",0) | 
					
						
							| 
									
										
										
										
											2006-02-26 19:42:26 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-16 19:42:00 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-27 18:13:03 -08:00
										 |  |  | class AnnotationsVisitor(PickleVisitor): | 
					
						
							|  |  |  |     def visitModule(self, mod): | 
					
						
							|  |  |  |         self.file.write(textwrap.dedent('''
 | 
					
						
							|  |  |  |             static int | 
					
						
							|  |  |  |             add_ast_annotations(struct ast_state *state) | 
					
						
							|  |  |  |             { | 
					
						
							|  |  |  |                 bool cond; | 
					
						
							|  |  |  |         '''))
 | 
					
						
							|  |  |  |         for dfn in mod.dfns: | 
					
						
							|  |  |  |             self.visit(dfn) | 
					
						
							|  |  |  |         self.file.write(textwrap.dedent('''
 | 
					
						
							|  |  |  |                 return 1; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         '''))
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def visitProduct(self, prod, name): | 
					
						
							|  |  |  |         self.emit_annotations(name, prod.fields) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def visitSum(self, sum, name): | 
					
						
							|  |  |  |         for t in sum.types: | 
					
						
							|  |  |  |             self.visitConstructor(t, name) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def visitConstructor(self, cons, name): | 
					
						
							|  |  |  |         self.emit_annotations(cons.name, cons.fields) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def emit_annotations(self, name, fields): | 
					
						
							|  |  |  |         self.emit(f"PyObject *{name}_annotations = PyDict_New();", 1) | 
					
						
							|  |  |  |         self.emit(f"if (!{name}_annotations) return 0;", 1) | 
					
						
							|  |  |  |         for field in fields: | 
					
						
							|  |  |  |             self.emit("{", 1) | 
					
						
							|  |  |  |             if field.type in builtin_type_to_c_type: | 
					
						
							|  |  |  |                 self.emit(f"PyObject *type = (PyObject *)&{builtin_type_to_c_type[field.type]};", 2) | 
					
						
							|  |  |  |             else: | 
					
						
							|  |  |  |                 self.emit(f"PyObject *type = state->{field.type}_type;", 2) | 
					
						
							|  |  |  |             if field.opt: | 
					
						
							|  |  |  |                 self.emit("type = _Py_union_type_or(type, Py_None);", 2) | 
					
						
							|  |  |  |                 self.emit("cond = type != NULL;", 2) | 
					
						
							|  |  |  |                 self.emit_annotations_error(name, 2) | 
					
						
							|  |  |  |             elif field.seq: | 
					
						
							|  |  |  |                 self.emit("type = Py_GenericAlias((PyObject *)&PyList_Type, type);", 2) | 
					
						
							|  |  |  |                 self.emit("cond = type != NULL;", 2) | 
					
						
							|  |  |  |                 self.emit_annotations_error(name, 2) | 
					
						
							|  |  |  |             else: | 
					
						
							|  |  |  |                 self.emit("Py_INCREF(type);", 2) | 
					
						
							|  |  |  |             self.emit(f"cond = PyDict_SetItemString({name}_annotations, \"{field.name}\", type) == 0;", 2) | 
					
						
							|  |  |  |             self.emit("Py_DECREF(type);", 2) | 
					
						
							|  |  |  |             self.emit_annotations_error(name, 2) | 
					
						
							|  |  |  |             self.emit("}", 1) | 
					
						
							|  |  |  |         self.emit(f'cond = PyObject_SetAttrString(state->{name}_type, "_field_types", {name}_annotations) == 0;', 1) | 
					
						
							|  |  |  |         self.emit_annotations_error(name, 1) | 
					
						
							|  |  |  |         self.emit(f'cond = PyObject_SetAttrString(state->{name}_type, "__annotations__", {name}_annotations) == 0;', 1) | 
					
						
							|  |  |  |         self.emit_annotations_error(name, 1) | 
					
						
							|  |  |  |         self.emit(f"Py_DECREF({name}_annotations);", 1) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def emit_annotations_error(self, name, depth): | 
					
						
							|  |  |  |         self.emit("if (!cond) {", depth) | 
					
						
							|  |  |  |         self.emit(f"Py_DECREF({name}_annotations);", depth + 1) | 
					
						
							|  |  |  |         self.emit("return 0;", depth + 1) | 
					
						
							|  |  |  |         self.emit("}", depth) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-02-26 19:42:26 +00:00
										 |  |  | class PyTypesVisitor(PickleVisitor): | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def visitModule(self, mod): | 
					
						
							|  |  |  |         self.emit("""
 | 
					
						
							| 
									
										
										
										
											2017-01-25 22:33:43 +09:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-03-12 09:46:44 -07:00
										 |  |  | typedef struct { | 
					
						
							| 
									
										
										
										
											2012-03-13 01:17:31 +01:00
										 |  |  |     PyObject_HEAD | 
					
						
							| 
									
										
										
										
											2012-03-12 09:46:44 -07:00
										 |  |  |     PyObject *dict; | 
					
						
							|  |  |  | } AST_object; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-03-14 21:50:29 -05:00
										 |  |  | static void | 
					
						
							| 
									
										
										
										
											2024-10-04 11:59:08 +02:00
										 |  |  | ast_dealloc(PyObject *op) | 
					
						
							| 
									
										
										
										
											2012-03-14 21:50:29 -05:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2024-10-04 11:59:08 +02:00
										 |  |  |     AST_object *self = (AST_object*)op; | 
					
						
							| 
									
										
										
										
											2017-08-24 14:55:17 +09:00
										 |  |  |     /* bpo-31095: UnTrack is needed before calling any callbacks */ | 
					
						
							| 
									
										
										
										
											2019-09-14 09:38:17 -04:00
										 |  |  |     PyTypeObject *tp = Py_TYPE(self); | 
					
						
							| 
									
										
										
										
											2017-08-24 14:55:17 +09:00
										 |  |  |     PyObject_GC_UnTrack(self); | 
					
						
							| 
									
										
										
										
											2012-03-14 21:50:29 -05:00
										 |  |  |     Py_CLEAR(self->dict); | 
					
						
							| 
									
										
										
										
											2019-09-14 09:38:17 -04:00
										 |  |  |     freefunc free_func = PyType_GetSlot(tp, Py_tp_free); | 
					
						
							|  |  |  |     assert(free_func != NULL); | 
					
						
							|  |  |  |     free_func(self); | 
					
						
							|  |  |  |     Py_DECREF(tp); | 
					
						
							| 
									
										
										
										
											2012-03-14 21:50:29 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-07-08 11:03:46 -07:00
										 |  |  | static int | 
					
						
							| 
									
										
										
										
											2024-10-04 11:59:08 +02:00
										 |  |  | ast_traverse(PyObject *op, visitproc visit, void *arg) | 
					
						
							| 
									
										
										
										
											2012-07-08 11:03:46 -07:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2024-10-04 11:59:08 +02:00
										 |  |  |     AST_object *self = (AST_object*)op; | 
					
						
							| 
									
										
										
										
											2020-05-27 10:03:38 +01:00
										 |  |  |     Py_VISIT(Py_TYPE(self)); | 
					
						
							| 
									
										
										
										
											2012-07-08 11:03:46 -07:00
										 |  |  |     Py_VISIT(self->dict); | 
					
						
							|  |  |  |     return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-31 07:34:34 +03:00
										 |  |  | static int | 
					
						
							| 
									
										
										
										
											2024-10-04 11:59:08 +02:00
										 |  |  | ast_clear(PyObject *op) | 
					
						
							| 
									
										
										
										
											2012-07-08 11:03:46 -07:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2024-10-04 11:59:08 +02:00
										 |  |  |     AST_object *self = (AST_object*)op; | 
					
						
							| 
									
										
										
										
											2012-07-08 11:03:46 -07:00
										 |  |  |     Py_CLEAR(self->dict); | 
					
						
							| 
									
										
										
										
											2018-05-31 07:34:34 +03:00
										 |  |  |     return 0; | 
					
						
							| 
									
										
										
										
											2012-07-08 11:03:46 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-03-31 04:42:11 +00:00
										 |  |  | static int | 
					
						
							|  |  |  | ast_type_init(PyObject *self, PyObject *args, PyObject *kw) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2020-11-02 22:03:28 +01:00
										 |  |  |     struct ast_state *state = get_ast_state(); | 
					
						
							| 
									
										
										
										
											2020-07-03 20:01:46 +02:00
										 |  |  |     if (state == NULL) { | 
					
						
							| 
									
										
										
										
											2020-07-04 23:18:15 +02:00
										 |  |  |         return -1; | 
					
						
							| 
									
										
										
										
											2020-07-03 20:01:46 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-07-04 23:18:15 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     Py_ssize_t i, numfields = 0; | 
					
						
							|  |  |  |     int res = -1; | 
					
						
							| 
									
										
										
										
											2024-07-11 07:34:53 -07:00
										 |  |  |     PyObject *key, *value, *fields, *attributes = NULL, *remaining_fields = NULL; | 
					
						
							| 
									
										
										
										
											2024-10-29 18:42:48 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  |     fields = PyObject_GetAttr((PyObject*)Py_TYPE(self), state->_fields); | 
					
						
							|  |  |  |     if (fields == NULL) { | 
					
						
							| 
									
										
										
										
											2018-01-25 10:49:40 +02:00
										 |  |  |         goto cleanup; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2024-10-29 18:42:48 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  |     numfields = PySequence_Size(fields); | 
					
						
							|  |  |  |     if (numfields == -1) { | 
					
						
							|  |  |  |         goto cleanup; | 
					
						
							| 
									
										
										
										
											2024-02-27 18:13:03 -08:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2024-10-29 18:42:48 +03:00
										 |  |  |     remaining_fields = PySet_New(fields); | 
					
						
							| 
									
										
										
										
											2024-02-27 18:13:03 -08:00
										 |  |  |     if (remaining_fields == NULL) { | 
					
						
							|  |  |  |         goto cleanup; | 
					
						
							| 
									
										
										
										
											2008-03-31 04:42:11 +00:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-02-24 02:48:17 +09:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-03-31 04:42:11 +00:00
										 |  |  |     res = 0; /* if no error occurs, this stays 0 to the end */ | 
					
						
							| 
									
										
										
										
											2017-02-24 02:48:17 +09:00
										 |  |  |     if (numfields < PyTuple_GET_SIZE(args)) { | 
					
						
							|  |  |  |         PyErr_Format(PyExc_TypeError, "%.400s constructor takes at most " | 
					
						
							|  |  |  |                      "%zd positional argument%s", | 
					
						
							| 
									
										
										
										
											2019-09-11 10:16:34 -07:00
										 |  |  |                      _PyType_Name(Py_TYPE(self)), | 
					
						
							| 
									
										
										
										
											2017-02-24 02:48:17 +09:00
										 |  |  |                      numfields, numfields == 1 ? "" : "s"); | 
					
						
							|  |  |  |         res = -1; | 
					
						
							|  |  |  |         goto cleanup; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     for (i = 0; i < PyTuple_GET_SIZE(args); i++) { | 
					
						
							|  |  |  |         /* cannot be reached when fields is NULL */ | 
					
						
							|  |  |  |         PyObject *name = PySequence_GetItem(fields, i); | 
					
						
							|  |  |  |         if (!name) { | 
					
						
							| 
									
										
										
										
											2008-03-31 04:42:11 +00:00
										 |  |  |             res = -1; | 
					
						
							|  |  |  |             goto cleanup; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2017-02-24 02:48:17 +09:00
										 |  |  |         res = PyObject_SetAttr(self, name, PyTuple_GET_ITEM(args, i)); | 
					
						
							| 
									
										
										
										
											2024-02-27 18:13:03 -08:00
										 |  |  |         if (PySet_Discard(remaining_fields, name) < 0) { | 
					
						
							|  |  |  |             res = -1; | 
					
						
							|  |  |  |             Py_DECREF(name); | 
					
						
							|  |  |  |             goto cleanup; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2017-02-24 02:48:17 +09:00
										 |  |  |         Py_DECREF(name); | 
					
						
							| 
									
										
										
										
											2020-05-24 23:12:57 +02:00
										 |  |  |         if (res < 0) { | 
					
						
							| 
									
										
										
										
											2017-02-24 02:48:17 +09:00
										 |  |  |             goto cleanup; | 
					
						
							| 
									
										
										
										
											2020-05-24 23:12:57 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2008-03-31 04:42:11 +00:00
										 |  |  |     } | 
					
						
							|  |  |  |     if (kw) { | 
					
						
							|  |  |  |         i = 0;  /* needed by PyDict_Next */ | 
					
						
							|  |  |  |         while (PyDict_Next(kw, &i, &key, &value)) { | 
					
						
							| 
									
										
										
										
											2020-05-24 23:12:57 +02:00
										 |  |  |             int contains = PySequence_Contains(fields, key); | 
					
						
							|  |  |  |             if (contains == -1) { | 
					
						
							|  |  |  |                 res = -1; | 
					
						
							|  |  |  |                 goto cleanup; | 
					
						
							| 
									
										
										
										
											2024-02-27 18:13:03 -08:00
										 |  |  |             } | 
					
						
							|  |  |  |             else if (contains == 1) { | 
					
						
							|  |  |  |                 int p = PySet_Discard(remaining_fields, key); | 
					
						
							| 
									
										
										
										
											2020-05-24 23:12:57 +02:00
										 |  |  |                 if (p == -1) { | 
					
						
							|  |  |  |                     res = -1; | 
					
						
							|  |  |  |                     goto cleanup; | 
					
						
							|  |  |  |                 } | 
					
						
							| 
									
										
										
										
											2024-02-27 18:13:03 -08:00
										 |  |  |                 if (p == 0) { | 
					
						
							| 
									
										
										
										
											2020-05-24 23:12:57 +02:00
										 |  |  |                     PyErr_Format(PyExc_TypeError, | 
					
						
							|  |  |  |                         "%.400s got multiple values for argument '%U'", | 
					
						
							|  |  |  |                         Py_TYPE(self)->tp_name, key); | 
					
						
							|  |  |  |                     res = -1; | 
					
						
							|  |  |  |                     goto cleanup; | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2024-07-11 07:34:53 -07:00
										 |  |  |             else { | 
					
						
							|  |  |  |                 // Lazily initialize "attributes" | 
					
						
							|  |  |  |                 if (attributes == NULL) { | 
					
						
							|  |  |  |                     attributes = PyObject_GetAttr((PyObject*)Py_TYPE(self), state->_attributes); | 
					
						
							|  |  |  |                     if (attributes == NULL) { | 
					
						
							|  |  |  |                         res = -1; | 
					
						
							|  |  |  |                         goto cleanup; | 
					
						
							|  |  |  |                     } | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |                 int contains = PySequence_Contains(attributes, key); | 
					
						
							|  |  |  |                 if (contains == -1) { | 
					
						
							| 
									
										
										
										
											2024-02-27 18:13:03 -08:00
										 |  |  |                     res = -1; | 
					
						
							|  |  |  |                     goto cleanup; | 
					
						
							|  |  |  |                 } | 
					
						
							| 
									
										
										
										
											2024-07-11 07:34:53 -07:00
										 |  |  |                 else if (contains == 0) { | 
					
						
							|  |  |  |                     if (PyErr_WarnFormat( | 
					
						
							|  |  |  |                         PyExc_DeprecationWarning, 1, | 
					
						
							|  |  |  |                         "%.400s.__init__ got an unexpected keyword argument '%U'. " | 
					
						
							|  |  |  |                         "Support for arbitrary keyword arguments is deprecated " | 
					
						
							|  |  |  |                         "and will be removed in Python 3.15.", | 
					
						
							|  |  |  |                         Py_TYPE(self)->tp_name, key | 
					
						
							|  |  |  |                     ) < 0) { | 
					
						
							|  |  |  |                         res = -1; | 
					
						
							|  |  |  |                         goto cleanup; | 
					
						
							|  |  |  |                     } | 
					
						
							|  |  |  |                 } | 
					
						
							| 
									
										
										
										
											2024-02-27 18:13:03 -08:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2008-03-31 04:42:11 +00:00
										 |  |  |             res = PyObject_SetAttr(self, key, value); | 
					
						
							| 
									
										
										
										
											2020-05-24 23:12:57 +02:00
										 |  |  |             if (res < 0) { | 
					
						
							| 
									
										
										
										
											2008-03-31 04:42:11 +00:00
										 |  |  |                 goto cleanup; | 
					
						
							| 
									
										
										
										
											2020-05-24 23:12:57 +02:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2008-03-31 04:42:11 +00:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2024-02-27 18:13:03 -08:00
										 |  |  |     Py_ssize_t size = PySet_Size(remaining_fields); | 
					
						
							|  |  |  |     PyObject *field_types = NULL, *remaining_list = NULL; | 
					
						
							|  |  |  |     if (size > 0) { | 
					
						
							| 
									
										
										
										
											2024-03-28 04:30:31 -06:00
										 |  |  |         if (PyObject_GetOptionalAttr((PyObject*)Py_TYPE(self), &_Py_ID(_field_types), | 
					
						
							|  |  |  |                                      &field_types) < 0) { | 
					
						
							| 
									
										
										
										
											2024-02-27 18:13:03 -08:00
										 |  |  |             res = -1; | 
					
						
							|  |  |  |             goto cleanup; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2024-03-28 04:30:31 -06:00
										 |  |  |         if (field_types == NULL) { | 
					
						
							| 
									
										
										
										
											2024-05-06 15:57:27 -07:00
										 |  |  |             // Probably a user-defined subclass of AST that lacks _field_types. | 
					
						
							|  |  |  |             // This will continue to work as it did before 3.13; i.e., attributes | 
					
						
							|  |  |  |             // that are not passed in simply do not exist on the instance. | 
					
						
							| 
									
										
										
										
											2024-03-28 04:30:31 -06:00
										 |  |  |             goto cleanup; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2024-02-27 18:13:03 -08:00
										 |  |  |         remaining_list = PySequence_List(remaining_fields); | 
					
						
							|  |  |  |         if (!remaining_list) { | 
					
						
							|  |  |  |             goto set_remaining_cleanup; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         for (Py_ssize_t i = 0; i < size; i++) { | 
					
						
							|  |  |  |             PyObject *name = PyList_GET_ITEM(remaining_list, i); | 
					
						
							|  |  |  |             PyObject *type = PyDict_GetItemWithError(field_types, name); | 
					
						
							|  |  |  |             if (!type) { | 
					
						
							| 
									
										
										
										
											2024-05-06 15:57:27 -07:00
										 |  |  |                 if (PyErr_Occurred()) { | 
					
						
							|  |  |  |                     goto set_remaining_cleanup; | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |                 else { | 
					
						
							|  |  |  |                     if (PyErr_WarnFormat( | 
					
						
							|  |  |  |                         PyExc_DeprecationWarning, 1, | 
					
						
							|  |  |  |                         "Field '%U' is missing from %.400s._field_types. " | 
					
						
							|  |  |  |                         "This will become an error in Python 3.15.", | 
					
						
							|  |  |  |                         name, Py_TYPE(self)->tp_name | 
					
						
							|  |  |  |                     ) < 0) { | 
					
						
							|  |  |  |                         goto set_remaining_cleanup; | 
					
						
							|  |  |  |                     } | 
					
						
							| 
									
										
										
										
											2024-02-27 18:13:03 -08:00
										 |  |  |                 } | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2024-05-06 15:57:27 -07:00
										 |  |  |             else if (_PyUnion_Check(type)) { | 
					
						
							| 
									
										
										
										
											2024-02-27 18:13:03 -08:00
										 |  |  |                 // optional field | 
					
						
							|  |  |  |                 // do nothing, we'll have set a None default on the class | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             else if (Py_IS_TYPE(type, &Py_GenericAliasType)) { | 
					
						
							|  |  |  |                 // list field | 
					
						
							|  |  |  |                 PyObject *empty = PyList_New(0); | 
					
						
							|  |  |  |                 if (!empty) { | 
					
						
							|  |  |  |                     goto set_remaining_cleanup; | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |                 res = PyObject_SetAttr(self, name, empty); | 
					
						
							|  |  |  |                 Py_DECREF(empty); | 
					
						
							|  |  |  |                 if (res < 0) { | 
					
						
							|  |  |  |                     goto set_remaining_cleanup; | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2024-05-09 15:30:14 -07:00
										 |  |  |             else if (type == state->expr_context_type) { | 
					
						
							|  |  |  |                 // special case for expr_context: default to Load() | 
					
						
							|  |  |  |                 res = PyObject_SetAttr(self, name, state->Load_singleton); | 
					
						
							|  |  |  |                 if (res < 0) { | 
					
						
							|  |  |  |                     goto set_remaining_cleanup; | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2024-02-27 18:13:03 -08:00
										 |  |  |             else { | 
					
						
							|  |  |  |                 // simple field (e.g., identifier) | 
					
						
							|  |  |  |                 if (PyErr_WarnFormat( | 
					
						
							|  |  |  |                     PyExc_DeprecationWarning, 1, | 
					
						
							|  |  |  |                     "%.400s.__init__ missing 1 required positional argument: '%U'. " | 
					
						
							|  |  |  |                     "This will become an error in Python 3.15.", | 
					
						
							|  |  |  |                     Py_TYPE(self)->tp_name, name | 
					
						
							|  |  |  |                 ) < 0) { | 
					
						
							| 
									
										
										
										
											2024-05-06 15:57:27 -07:00
										 |  |  |                     goto set_remaining_cleanup; | 
					
						
							| 
									
										
										
										
											2024-02-27 18:13:03 -08:00
										 |  |  |                 } | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         Py_DECREF(remaining_list); | 
					
						
							|  |  |  |         Py_DECREF(field_types); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2008-03-31 04:42:11 +00:00
										 |  |  |   cleanup: | 
					
						
							| 
									
										
										
										
											2024-07-11 07:34:53 -07:00
										 |  |  |     Py_XDECREF(attributes); | 
					
						
							| 
									
										
										
										
											2008-03-31 04:42:11 +00:00
										 |  |  |     Py_XDECREF(fields); | 
					
						
							| 
									
										
										
										
											2024-02-27 18:13:03 -08:00
										 |  |  |     Py_XDECREF(remaining_fields); | 
					
						
							| 
									
										
										
										
											2008-03-31 04:42:11 +00:00
										 |  |  |     return res; | 
					
						
							| 
									
										
										
										
											2024-02-27 18:13:03 -08:00
										 |  |  |   set_remaining_cleanup: | 
					
						
							|  |  |  |     Py_XDECREF(remaining_list); | 
					
						
							|  |  |  |     Py_XDECREF(field_types); | 
					
						
							|  |  |  |     res = -1; | 
					
						
							|  |  |  |     goto cleanup; | 
					
						
							| 
									
										
										
										
											2008-03-31 04:42:11 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
											  
											
												Merged revisions 61440-61441,61443,61445-61448,61451-61452,61455-61457,61459-61464,61466-61467,61469-61470,61476-61477,61479,61481-61482,61485,61487,61490,61493-61494,61497,61499-61502,61505-61506,61508,61511-61514,61519,61521-61522,61530-61531,61533-61537,61541-61555,61557-61558,61561-61562,61566-61569,61572-61574,61578-61579,61583-61584,61588-61589,61592,61594,61598-61601,61603-61604,61607-61612,61617,61619-61620,61624,61626,61628-61630,61635-61638,61640-61643,61645,61648,61653-61655,61659-61662,61664,61666,61668-61671,61673,61675,61679-61680,61682,61685-61686,61689-61695,61697-61699,61701-61703,61706,61710,61713,61717,61723,61726-61730,61736,61738,61740,61742,61745-61752,61754-61760,61762-61764,61768,61770-61772,61774-61775,61784-61787,61789-61792,61794-61795,61797-61806,61808-61809,61811-61812,61814-61819,61824,61826-61833,61835-61840,61843-61845,61848,61850,61854-61862,61865-61866,61868,61872-61873,61876-61877,61883-61888,61890-61891,61893-61899,61901-61903,61905-61912,61914,61917,61920-61921,61927,61930,61932-61934,61939,61941-61942,61944-61951,61955,61960-61963,61980,61982-61983,61991,61994-61996,62001-62003,62008-62010,62016-62017,62022,62024,62027,62031-62034,62041,62045-62046,62048,62050-62051,62055-62066,62068-62074,62076-62078 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
  r62048 | georg.brandl | 2008-03-29 23:53:55 -0700 (Sat, 29 Mar 2008) | 2 lines
  Adapt test_ast to the new ExceptHandler type.
........
  r62050 | georg.brandl | 2008-03-30 00:09:22 -0700 (Sun, 30 Mar 2008) | 2 lines
  Convert test_ast to unittest and add a test for r62049.
........
  r62051 | georg.brandl | 2008-03-30 12:00:49 -0700 (Sun, 30 Mar 2008) | 2 lines
  Make _fields attr for no fields consistent with _attributes attr.
........
  r62059 | georg.brandl | 2008-03-30 13:20:39 -0700 (Sun, 30 Mar 2008) | 2 lines
  Make AST nodes pickleable.
........
											
										 
											2008-03-31 05:29:39 +00:00
										 |  |  | /* Pickling support */ | 
					
						
							|  |  |  | static PyObject * | 
					
						
							|  |  |  | ast_type_reduce(PyObject *self, PyObject *unused) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2020-11-02 22:03:28 +01:00
										 |  |  |     struct ast_state *state = get_ast_state(); | 
					
						
							| 
									
										
										
										
											2020-07-03 20:01:46 +02:00
										 |  |  |     if (state == NULL) { | 
					
						
							|  |  |  |         return NULL; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-25 08:12:11 -07:00
										 |  |  |     PyObject *dict = NULL, *fields = NULL, *positional_args = NULL; | 
					
						
							| 
									
										
										
										
											2023-07-12 08:57:10 +03:00
										 |  |  |     if (PyObject_GetOptionalAttr(self, state->__dict__, &dict) < 0) { | 
					
						
							| 
									
										
										
										
											2018-01-25 10:49:40 +02:00
										 |  |  |         return NULL; | 
					
						
							| 
									
										
											  
											
												Merged revisions 61440-61441,61443,61445-61448,61451-61452,61455-61457,61459-61464,61466-61467,61469-61470,61476-61477,61479,61481-61482,61485,61487,61490,61493-61494,61497,61499-61502,61505-61506,61508,61511-61514,61519,61521-61522,61530-61531,61533-61537,61541-61555,61557-61558,61561-61562,61566-61569,61572-61574,61578-61579,61583-61584,61588-61589,61592,61594,61598-61601,61603-61604,61607-61612,61617,61619-61620,61624,61626,61628-61630,61635-61638,61640-61643,61645,61648,61653-61655,61659-61662,61664,61666,61668-61671,61673,61675,61679-61680,61682,61685-61686,61689-61695,61697-61699,61701-61703,61706,61710,61713,61717,61723,61726-61730,61736,61738,61740,61742,61745-61752,61754-61760,61762-61764,61768,61770-61772,61774-61775,61784-61787,61789-61792,61794-61795,61797-61806,61808-61809,61811-61812,61814-61819,61824,61826-61833,61835-61840,61843-61845,61848,61850,61854-61862,61865-61866,61868,61872-61873,61876-61877,61883-61888,61890-61891,61893-61899,61901-61903,61905-61912,61914,61917,61920-61921,61927,61930,61932-61934,61939,61941-61942,61944-61951,61955,61960-61963,61980,61982-61983,61991,61994-61996,62001-62003,62008-62010,62016-62017,62022,62024,62027,62031-62034,62041,62045-62046,62048,62050-62051,62055-62066,62068-62074,62076-62078 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
  r62048 | georg.brandl | 2008-03-29 23:53:55 -0700 (Sat, 29 Mar 2008) | 2 lines
  Adapt test_ast to the new ExceptHandler type.
........
  r62050 | georg.brandl | 2008-03-30 00:09:22 -0700 (Sun, 30 Mar 2008) | 2 lines
  Convert test_ast to unittest and add a test for r62049.
........
  r62051 | georg.brandl | 2008-03-30 12:00:49 -0700 (Sun, 30 Mar 2008) | 2 lines
  Make _fields attr for no fields consistent with _attributes attr.
........
  r62059 | georg.brandl | 2008-03-30 13:20:39 -0700 (Sun, 30 Mar 2008) | 2 lines
  Make AST nodes pickleable.
........
											
										 
											2008-03-31 05:29:39 +00:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2024-02-27 18:13:03 -08:00
										 |  |  |     PyObject *result = NULL; | 
					
						
							| 
									
										
											  
											
												Merged revisions 61440-61441,61443,61445-61448,61451-61452,61455-61457,61459-61464,61466-61467,61469-61470,61476-61477,61479,61481-61482,61485,61487,61490,61493-61494,61497,61499-61502,61505-61506,61508,61511-61514,61519,61521-61522,61530-61531,61533-61537,61541-61555,61557-61558,61561-61562,61566-61569,61572-61574,61578-61579,61583-61584,61588-61589,61592,61594,61598-61601,61603-61604,61607-61612,61617,61619-61620,61624,61626,61628-61630,61635-61638,61640-61643,61645,61648,61653-61655,61659-61662,61664,61666,61668-61671,61673,61675,61679-61680,61682,61685-61686,61689-61695,61697-61699,61701-61703,61706,61710,61713,61717,61723,61726-61730,61736,61738,61740,61742,61745-61752,61754-61760,61762-61764,61768,61770-61772,61774-61775,61784-61787,61789-61792,61794-61795,61797-61806,61808-61809,61811-61812,61814-61819,61824,61826-61833,61835-61840,61843-61845,61848,61850,61854-61862,61865-61866,61868,61872-61873,61876-61877,61883-61888,61890-61891,61893-61899,61901-61903,61905-61912,61914,61917,61920-61921,61927,61930,61932-61934,61939,61941-61942,61944-61951,61955,61960-61963,61980,61982-61983,61991,61994-61996,62001-62003,62008-62010,62016-62017,62022,62024,62027,62031-62034,62041,62045-62046,62048,62050-62051,62055-62066,62068-62074,62076-62078 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
  r62048 | georg.brandl | 2008-03-29 23:53:55 -0700 (Sat, 29 Mar 2008) | 2 lines
  Adapt test_ast to the new ExceptHandler type.
........
  r62050 | georg.brandl | 2008-03-30 00:09:22 -0700 (Sun, 30 Mar 2008) | 2 lines
  Convert test_ast to unittest and add a test for r62049.
........
  r62051 | georg.brandl | 2008-03-30 12:00:49 -0700 (Sun, 30 Mar 2008) | 2 lines
  Make _fields attr for no fields consistent with _attributes attr.
........
  r62059 | georg.brandl | 2008-03-30 13:20:39 -0700 (Sun, 30 Mar 2008) | 2 lines
  Make AST nodes pickleable.
........
											
										 
											2008-03-31 05:29:39 +00:00
										 |  |  |     if (dict) { | 
					
						
							| 
									
										
										
										
											2024-06-25 08:12:11 -07:00
										 |  |  |         // Unpickling (or copying) works as follows: | 
					
						
							|  |  |  |         // - Construct the object with only positional arguments | 
					
						
							|  |  |  |         // - Set the fields from the dict | 
					
						
							|  |  |  |         // We have two constraints: | 
					
						
							|  |  |  |         // - We must set all the required fields in the initial constructor call, | 
					
						
							|  |  |  |         //   or the unpickling or deepcopying of the object will trigger DeprecationWarnings. | 
					
						
							|  |  |  |         // - We must not include child nodes in the positional args, because | 
					
						
							|  |  |  |         //   that may trigger runaway recursion during copying (gh-120108). | 
					
						
							|  |  |  |         // To satisfy both constraints, we set all the fields to None in the | 
					
						
							|  |  |  |         // initial list of positional args, and then set the fields from the dict. | 
					
						
							| 
									
										
										
										
											2024-02-27 18:13:03 -08:00
										 |  |  |         if (PyObject_GetOptionalAttr((PyObject*)Py_TYPE(self), state->_fields, &fields) < 0) { | 
					
						
							|  |  |  |             goto cleanup; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if (fields) { | 
					
						
							|  |  |  |             Py_ssize_t numfields = PySequence_Size(fields); | 
					
						
							|  |  |  |             if (numfields == -1) { | 
					
						
							|  |  |  |                 Py_DECREF(dict); | 
					
						
							|  |  |  |                 goto cleanup; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             positional_args = PyList_New(0); | 
					
						
							|  |  |  |             if (!positional_args) { | 
					
						
							|  |  |  |                 goto cleanup; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             for (Py_ssize_t i = 0; i < numfields; i++) { | 
					
						
							|  |  |  |                 PyObject *name = PySequence_GetItem(fields, i); | 
					
						
							|  |  |  |                 if (!name) { | 
					
						
							|  |  |  |                     goto cleanup; | 
					
						
							|  |  |  |                 } | 
					
						
							| 
									
										
										
										
											2024-03-07 11:21:08 +02:00
										 |  |  |                 PyObject *value; | 
					
						
							| 
									
										
										
										
											2024-06-25 08:12:11 -07:00
										 |  |  |                 int rc = PyDict_GetItemRef(dict, name, &value); | 
					
						
							| 
									
										
										
										
											2024-03-07 11:21:08 +02:00
										 |  |  |                 Py_DECREF(name); | 
					
						
							|  |  |  |                 if (rc < 0) { | 
					
						
							|  |  |  |                     goto cleanup; | 
					
						
							|  |  |  |                 } | 
					
						
							| 
									
										
										
										
											2024-02-27 18:13:03 -08:00
										 |  |  |                 if (!value) { | 
					
						
							|  |  |  |                     break; | 
					
						
							|  |  |  |                 } | 
					
						
							| 
									
										
										
										
											2024-06-25 08:12:11 -07:00
										 |  |  |                 rc = PyList_Append(positional_args, Py_None); | 
					
						
							| 
									
										
										
										
											2024-03-07 11:21:08 +02:00
										 |  |  |                 Py_DECREF(value); | 
					
						
							|  |  |  |                 if (rc < 0) { | 
					
						
							| 
									
										
										
										
											2024-02-27 18:13:03 -08:00
										 |  |  |                     goto cleanup; | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             PyObject *args_tuple = PyList_AsTuple(positional_args); | 
					
						
							|  |  |  |             if (!args_tuple) { | 
					
						
							|  |  |  |                 goto cleanup; | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2024-06-25 08:12:11 -07:00
										 |  |  |             result = Py_BuildValue("ONN", Py_TYPE(self), args_tuple, dict); | 
					
						
							| 
									
										
										
										
											2024-02-27 18:13:03 -08:00
										 |  |  |         } | 
					
						
							|  |  |  |         else { | 
					
						
							|  |  |  |             result = Py_BuildValue("O()N", Py_TYPE(self), dict); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else { | 
					
						
							|  |  |  |         result = Py_BuildValue("O()", Py_TYPE(self)); | 
					
						
							| 
									
										
											  
											
												Merged revisions 61440-61441,61443,61445-61448,61451-61452,61455-61457,61459-61464,61466-61467,61469-61470,61476-61477,61479,61481-61482,61485,61487,61490,61493-61494,61497,61499-61502,61505-61506,61508,61511-61514,61519,61521-61522,61530-61531,61533-61537,61541-61555,61557-61558,61561-61562,61566-61569,61572-61574,61578-61579,61583-61584,61588-61589,61592,61594,61598-61601,61603-61604,61607-61612,61617,61619-61620,61624,61626,61628-61630,61635-61638,61640-61643,61645,61648,61653-61655,61659-61662,61664,61666,61668-61671,61673,61675,61679-61680,61682,61685-61686,61689-61695,61697-61699,61701-61703,61706,61710,61713,61717,61723,61726-61730,61736,61738,61740,61742,61745-61752,61754-61760,61762-61764,61768,61770-61772,61774-61775,61784-61787,61789-61792,61794-61795,61797-61806,61808-61809,61811-61812,61814-61819,61824,61826-61833,61835-61840,61843-61845,61848,61850,61854-61862,61865-61866,61868,61872-61873,61876-61877,61883-61888,61890-61891,61893-61899,61901-61903,61905-61912,61914,61917,61920-61921,61927,61930,61932-61934,61939,61941-61942,61944-61951,61955,61960-61963,61980,61982-61983,61991,61994-61996,62001-62003,62008-62010,62016-62017,62022,62024,62027,62031-62034,62041,62045-62046,62048,62050-62051,62055-62066,62068-62074,62076-62078 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
  r62048 | georg.brandl | 2008-03-29 23:53:55 -0700 (Sat, 29 Mar 2008) | 2 lines
  Adapt test_ast to the new ExceptHandler type.
........
  r62050 | georg.brandl | 2008-03-30 00:09:22 -0700 (Sun, 30 Mar 2008) | 2 lines
  Convert test_ast to unittest and add a test for r62049.
........
  r62051 | georg.brandl | 2008-03-30 12:00:49 -0700 (Sun, 30 Mar 2008) | 2 lines
  Make _fields attr for no fields consistent with _attributes attr.
........
  r62059 | georg.brandl | 2008-03-30 13:20:39 -0700 (Sun, 30 Mar 2008) | 2 lines
  Make AST nodes pickleable.
........
											
										 
											2008-03-31 05:29:39 +00:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2024-02-27 18:13:03 -08:00
										 |  |  | cleanup: | 
					
						
							|  |  |  |     Py_XDECREF(fields); | 
					
						
							|  |  |  |     Py_XDECREF(positional_args); | 
					
						
							|  |  |  |     return result; | 
					
						
							| 
									
										
											  
											
												Merged revisions 61440-61441,61443,61445-61448,61451-61452,61455-61457,61459-61464,61466-61467,61469-61470,61476-61477,61479,61481-61482,61485,61487,61490,61493-61494,61497,61499-61502,61505-61506,61508,61511-61514,61519,61521-61522,61530-61531,61533-61537,61541-61555,61557-61558,61561-61562,61566-61569,61572-61574,61578-61579,61583-61584,61588-61589,61592,61594,61598-61601,61603-61604,61607-61612,61617,61619-61620,61624,61626,61628-61630,61635-61638,61640-61643,61645,61648,61653-61655,61659-61662,61664,61666,61668-61671,61673,61675,61679-61680,61682,61685-61686,61689-61695,61697-61699,61701-61703,61706,61710,61713,61717,61723,61726-61730,61736,61738,61740,61742,61745-61752,61754-61760,61762-61764,61768,61770-61772,61774-61775,61784-61787,61789-61792,61794-61795,61797-61806,61808-61809,61811-61812,61814-61819,61824,61826-61833,61835-61840,61843-61845,61848,61850,61854-61862,61865-61866,61868,61872-61873,61876-61877,61883-61888,61890-61891,61893-61899,61901-61903,61905-61912,61914,61917,61920-61921,61927,61930,61932-61934,61939,61941-61942,61944-61951,61955,61960-61963,61980,61982-61983,61991,61994-61996,62001-62003,62008-62010,62016-62017,62022,62024,62027,62031-62034,62041,62045-62046,62048,62050-62051,62055-62066,62068-62074,62076-62078 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
  r62048 | georg.brandl | 2008-03-29 23:53:55 -0700 (Sat, 29 Mar 2008) | 2 lines
  Adapt test_ast to the new ExceptHandler type.
........
  r62050 | georg.brandl | 2008-03-30 00:09:22 -0700 (Sun, 30 Mar 2008) | 2 lines
  Convert test_ast to unittest and add a test for r62049.
........
  r62051 | georg.brandl | 2008-03-30 12:00:49 -0700 (Sun, 30 Mar 2008) | 2 lines
  Make _fields attr for no fields consistent with _attributes attr.
........
  r62059 | georg.brandl | 2008-03-30 13:20:39 -0700 (Sun, 30 Mar 2008) | 2 lines
  Make AST nodes pickleable.
........
											
										 
											2008-03-31 05:29:39 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-04 05:10:54 +02:00
										 |  |  | /* | 
					
						
							|  |  |  |  * Perform the following validations: | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  *   - All keyword arguments are known 'fields' or 'attributes'. | 
					
						
							|  |  |  |  *   - No field or attribute would be left unfilled after copy.replace(). | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * On success, this returns 1. Otherwise, set a TypeError | 
					
						
							|  |  |  |  * exception and returns -1 (no exception is set if some | 
					
						
							|  |  |  |  * other internal errors occur). | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Parameters | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  *      self          The AST node instance. | 
					
						
							|  |  |  |  *      dict          The AST node instance dictionary (self.__dict__). | 
					
						
							|  |  |  |  *      fields        The list of fields (self._fields). | 
					
						
							|  |  |  |  *      attributes    The list of attributes (self._attributes). | 
					
						
							|  |  |  |  *      kwargs        Keyword arguments passed to ast_type_replace(). | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * The 'dict', 'fields', 'attributes' and 'kwargs' arguments can be NULL. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Note: this function can be removed in 3.15 since the verification | 
					
						
							|  |  |  |  *       will be done inside the constructor. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | static inline int | 
					
						
							|  |  |  | ast_type_replace_check(PyObject *self, | 
					
						
							|  |  |  |                        PyObject *dict, | 
					
						
							|  |  |  |                        PyObject *fields, | 
					
						
							|  |  |  |                        PyObject *attributes, | 
					
						
							|  |  |  |                        PyObject *kwargs) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     // While it is possible to make some fast paths that would avoid | 
					
						
							|  |  |  |     // allocating objects on the stack, this would cost us readability. | 
					
						
							|  |  |  |     // For instance, if 'fields' and 'attributes' are both empty, and | 
					
						
							|  |  |  |     // 'kwargs' is not empty, we could raise a TypeError immediately. | 
					
						
							|  |  |  |     PyObject *expecting = PySet_New(fields); | 
					
						
							|  |  |  |     if (expecting == NULL) { | 
					
						
							|  |  |  |         return -1; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     if (attributes) { | 
					
						
							|  |  |  |         if (_PySet_Update(expecting, attributes) < 0) { | 
					
						
							|  |  |  |             Py_DECREF(expecting); | 
					
						
							|  |  |  |             return -1; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     // Any keyword argument that is neither a field nor attribute is rejected. | 
					
						
							|  |  |  |     // We first need to check whether a keyword argument is accepted or not. | 
					
						
							|  |  |  |     // If all keyword arguments are accepted, we compute the required fields | 
					
						
							|  |  |  |     // and attributes. A field or attribute is not needed if: | 
					
						
							|  |  |  |     // | 
					
						
							|  |  |  |     //  1) it is given in 'kwargs', or | 
					
						
							|  |  |  |     //  2) it already exists on 'self'. | 
					
						
							|  |  |  |     if (kwargs) { | 
					
						
							|  |  |  |         Py_ssize_t pos = 0; | 
					
						
							|  |  |  |         PyObject *key, *value; | 
					
						
							|  |  |  |         while (PyDict_Next(kwargs, &pos, &key, &value)) { | 
					
						
							|  |  |  |             int rc = PySet_Discard(expecting, key); | 
					
						
							|  |  |  |             if (rc < 0) { | 
					
						
							|  |  |  |                 Py_DECREF(expecting); | 
					
						
							|  |  |  |                 return -1; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             if (rc == 0) { | 
					
						
							|  |  |  |                 PyErr_Format(PyExc_TypeError, | 
					
						
							|  |  |  |                              "%.400s.__replace__ got an unexpected keyword " | 
					
						
							|  |  |  |                              "argument '%U'.", Py_TYPE(self)->tp_name, key); | 
					
						
							|  |  |  |                 Py_DECREF(expecting); | 
					
						
							|  |  |  |                 return -1; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     // check that the remaining fields or attributes would be filled | 
					
						
							|  |  |  |     if (dict) { | 
					
						
							|  |  |  |         Py_ssize_t pos = 0; | 
					
						
							|  |  |  |         PyObject *key, *value; | 
					
						
							|  |  |  |         while (PyDict_Next(dict, &pos, &key, &value)) { | 
					
						
							|  |  |  |             // Mark fields or attributes that are found on the instance | 
					
						
							|  |  |  |             // as non-mandatory. If they are not given in 'kwargs', they | 
					
						
							|  |  |  |             // will be shallow-coied; otherwise, they would be replaced | 
					
						
							|  |  |  |             // (not in this function). | 
					
						
							|  |  |  |             if (PySet_Discard(expecting, key) < 0) { | 
					
						
							|  |  |  |                 Py_DECREF(expecting); | 
					
						
							|  |  |  |                 return -1; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if (attributes) { | 
					
						
							|  |  |  |             // Some attributes may or may not be present at runtime. | 
					
						
							|  |  |  |             // In particular, now that we checked whether 'kwargs' | 
					
						
							|  |  |  |             // is correct or not, we allow any attribute to be missing. | 
					
						
							|  |  |  |             // | 
					
						
							|  |  |  |             // Note that fields must still be entirely determined when | 
					
						
							|  |  |  |             // calling the constructor later. | 
					
						
							|  |  |  |             PyObject *unused = PyObject_CallMethodOneArg(expecting, | 
					
						
							|  |  |  |                                                          &_Py_ID(difference_update), | 
					
						
							|  |  |  |                                                          attributes); | 
					
						
							|  |  |  |             if (unused == NULL) { | 
					
						
							|  |  |  |                 Py_DECREF(expecting); | 
					
						
							|  |  |  |                 return -1; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             Py_DECREF(unused); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     // Now 'expecting' contains the fields or attributes | 
					
						
							|  |  |  |     // that would not be filled inside ast_type_replace(). | 
					
						
							|  |  |  |     Py_ssize_t m = PySet_GET_SIZE(expecting); | 
					
						
							|  |  |  |     if (m > 0) { | 
					
						
							|  |  |  |         PyObject *names = PyList_New(m); | 
					
						
							|  |  |  |         if (names == NULL) { | 
					
						
							|  |  |  |             Py_DECREF(expecting); | 
					
						
							|  |  |  |             return -1; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         Py_ssize_t i = 0, pos = 0; | 
					
						
							|  |  |  |         PyObject *item; | 
					
						
							|  |  |  |         Py_hash_t hash; | 
					
						
							|  |  |  |         while (_PySet_NextEntry(expecting, &pos, &item, &hash)) { | 
					
						
							|  |  |  |             PyObject *name = PyObject_Repr(item); | 
					
						
							|  |  |  |             if (name == NULL) { | 
					
						
							|  |  |  |                 Py_DECREF(expecting); | 
					
						
							|  |  |  |                 Py_DECREF(names); | 
					
						
							|  |  |  |                 return -1; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             // steal the reference 'name' | 
					
						
							|  |  |  |             PyList_SET_ITEM(names, i++, name); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         Py_DECREF(expecting); | 
					
						
							|  |  |  |         if (PyList_Sort(names) < 0) { | 
					
						
							|  |  |  |             Py_DECREF(names); | 
					
						
							|  |  |  |             return -1; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         PyObject *sep = PyUnicode_FromString(", "); | 
					
						
							|  |  |  |         if (sep == NULL) { | 
					
						
							|  |  |  |             Py_DECREF(names); | 
					
						
							|  |  |  |             return -1; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         PyObject *str_names = PyUnicode_Join(sep, names); | 
					
						
							|  |  |  |         Py_DECREF(sep); | 
					
						
							|  |  |  |         Py_DECREF(names); | 
					
						
							|  |  |  |         if (str_names == NULL) { | 
					
						
							|  |  |  |             return -1; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         PyErr_Format(PyExc_TypeError, | 
					
						
							|  |  |  |                      "%.400s.__replace__ missing %ld keyword argument%s: %U.", | 
					
						
							|  |  |  |                      Py_TYPE(self)->tp_name, m, m == 1 ? "" : "s", str_names); | 
					
						
							|  |  |  |         Py_DECREF(str_names); | 
					
						
							|  |  |  |         return -1; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else { | 
					
						
							|  |  |  |         Py_DECREF(expecting); | 
					
						
							|  |  |  |         return 1; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* | 
					
						
							|  |  |  |  * Python equivalent: | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  *   for key in keys: | 
					
						
							|  |  |  |  *       if hasattr(self, key): | 
					
						
							|  |  |  |  *           payload[key] = getattr(self, key) | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * The 'keys' argument is a sequence corresponding to | 
					
						
							|  |  |  |  * the '_fields' or the '_attributes' of an AST node. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This returns -1 if an error occurs and 0 otherwise. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Parameters | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  *      payload   A dictionary to fill. | 
					
						
							|  |  |  |  *      keys      A sequence of keys or NULL for an empty sequence. | 
					
						
							|  |  |  |  *      dict      The AST node instance dictionary (must not be NULL). | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | static inline int | 
					
						
							|  |  |  | ast_type_replace_update_payload(PyObject *payload, | 
					
						
							|  |  |  |                                 PyObject *keys, | 
					
						
							|  |  |  |                                 PyObject *dict) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     assert(dict != NULL); | 
					
						
							|  |  |  |     if (keys == NULL) { | 
					
						
							|  |  |  |         return 0; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     Py_ssize_t n = PySequence_Size(keys); | 
					
						
							|  |  |  |     if (n == -1) { | 
					
						
							|  |  |  |         return -1; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     for (Py_ssize_t i = 0; i < n; i++) { | 
					
						
							|  |  |  |         PyObject *key = PySequence_GetItem(keys, i); | 
					
						
							|  |  |  |         if (key == NULL) { | 
					
						
							|  |  |  |             return -1; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         PyObject *value; | 
					
						
							|  |  |  |         if (PyDict_GetItemRef(dict, key, &value) < 0) { | 
					
						
							|  |  |  |             Py_DECREF(key); | 
					
						
							|  |  |  |             return -1; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if (value == NULL) { | 
					
						
							|  |  |  |             Py_DECREF(key); | 
					
						
							|  |  |  |             // If a field or attribute is not present at runtime, it should | 
					
						
							|  |  |  |             // be explicitly given in 'kwargs'. If not, the constructor will | 
					
						
							|  |  |  |             // issue a warning (which becomes an error in 3.15). | 
					
						
							|  |  |  |             continue; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         int rc = PyDict_SetItem(payload, key, value); | 
					
						
							|  |  |  |         Py_DECREF(key); | 
					
						
							|  |  |  |         Py_DECREF(value); | 
					
						
							|  |  |  |         if (rc < 0) { | 
					
						
							|  |  |  |             return -1; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* copy.replace() support (shallow copy) */ | 
					
						
							|  |  |  | static PyObject * | 
					
						
							|  |  |  | ast_type_replace(PyObject *self, PyObject *args, PyObject *kwargs) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (!_PyArg_NoPositional("__replace__", args)) { | 
					
						
							|  |  |  |         return NULL; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     struct ast_state *state = get_ast_state(); | 
					
						
							|  |  |  |     if (state == NULL) { | 
					
						
							|  |  |  |         return NULL; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     PyObject *result = NULL; | 
					
						
							|  |  |  |     // known AST class fields and attributes | 
					
						
							|  |  |  |     PyObject *fields = NULL, *attributes = NULL; | 
					
						
							|  |  |  |     // current instance dictionary | 
					
						
							|  |  |  |     PyObject *dict = NULL; | 
					
						
							|  |  |  |     // constructor positional and keyword arguments | 
					
						
							|  |  |  |     PyObject *empty_tuple = NULL, *payload = NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     PyObject *type = (PyObject *)Py_TYPE(self); | 
					
						
							|  |  |  |     if (PyObject_GetOptionalAttr(type, state->_fields, &fields) < 0) { | 
					
						
							|  |  |  |         goto cleanup; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     if (PyObject_GetOptionalAttr(type, state->_attributes, &attributes) < 0) { | 
					
						
							|  |  |  |         goto cleanup; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     if (PyObject_GetOptionalAttr(self, state->__dict__, &dict) < 0) { | 
					
						
							|  |  |  |         goto cleanup; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     if (ast_type_replace_check(self, dict, fields, attributes, kwargs) < 0) { | 
					
						
							|  |  |  |         goto cleanup; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     empty_tuple = PyTuple_New(0); | 
					
						
							|  |  |  |     if (empty_tuple == NULL) { | 
					
						
							|  |  |  |         goto cleanup; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     payload = PyDict_New(); | 
					
						
							|  |  |  |     if (payload == NULL) { | 
					
						
							|  |  |  |         goto cleanup; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     if (dict) { // in case __dict__ is missing (for some obscure reason) | 
					
						
							|  |  |  |         // copy the instance's fields (possibly NULL) | 
					
						
							|  |  |  |         if (ast_type_replace_update_payload(payload, fields, dict) < 0) { | 
					
						
							|  |  |  |             goto cleanup; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         // copy the instance's attributes (possibly NULL) | 
					
						
							|  |  |  |         if (ast_type_replace_update_payload(payload, attributes, dict) < 0) { | 
					
						
							|  |  |  |             goto cleanup; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     if (kwargs && PyDict_Update(payload, kwargs) < 0) { | 
					
						
							|  |  |  |         goto cleanup; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     result = PyObject_Call(type, empty_tuple, payload); | 
					
						
							|  |  |  | cleanup: | 
					
						
							|  |  |  |     Py_XDECREF(payload); | 
					
						
							|  |  |  |     Py_XDECREF(empty_tuple); | 
					
						
							|  |  |  |     Py_XDECREF(dict); | 
					
						
							|  |  |  |     Py_XDECREF(attributes); | 
					
						
							|  |  |  |     Py_XDECREF(fields); | 
					
						
							|  |  |  |     return result; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-19 09:29:05 -07:00
										 |  |  | static PyMemberDef ast_type_members[] = { | 
					
						
							| 
									
										
										
										
											2023-07-25 15:28:30 +02:00
										 |  |  |     {"__dictoffset__", Py_T_PYSSIZET, offsetof(AST_object, dict), Py_READONLY}, | 
					
						
							| 
									
										
										
										
											2019-09-19 09:29:05 -07:00
										 |  |  |     {NULL}  /* Sentinel */ | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
											  
											
												Merged revisions 61440-61441,61443,61445-61448,61451-61452,61455-61457,61459-61464,61466-61467,61469-61470,61476-61477,61479,61481-61482,61485,61487,61490,61493-61494,61497,61499-61502,61505-61506,61508,61511-61514,61519,61521-61522,61530-61531,61533-61537,61541-61555,61557-61558,61561-61562,61566-61569,61572-61574,61578-61579,61583-61584,61588-61589,61592,61594,61598-61601,61603-61604,61607-61612,61617,61619-61620,61624,61626,61628-61630,61635-61638,61640-61643,61645,61648,61653-61655,61659-61662,61664,61666,61668-61671,61673,61675,61679-61680,61682,61685-61686,61689-61695,61697-61699,61701-61703,61706,61710,61713,61717,61723,61726-61730,61736,61738,61740,61742,61745-61752,61754-61760,61762-61764,61768,61770-61772,61774-61775,61784-61787,61789-61792,61794-61795,61797-61806,61808-61809,61811-61812,61814-61819,61824,61826-61833,61835-61840,61843-61845,61848,61850,61854-61862,61865-61866,61868,61872-61873,61876-61877,61883-61888,61890-61891,61893-61899,61901-61903,61905-61912,61914,61917,61920-61921,61927,61930,61932-61934,61939,61941-61942,61944-61951,61955,61960-61963,61980,61982-61983,61991,61994-61996,62001-62003,62008-62010,62016-62017,62022,62024,62027,62031-62034,62041,62045-62046,62048,62050-62051,62055-62066,62068-62074,62076-62078 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
  r62048 | georg.brandl | 2008-03-29 23:53:55 -0700 (Sat, 29 Mar 2008) | 2 lines
  Adapt test_ast to the new ExceptHandler type.
........
  r62050 | georg.brandl | 2008-03-30 00:09:22 -0700 (Sun, 30 Mar 2008) | 2 lines
  Convert test_ast to unittest and add a test for r62049.
........
  r62051 | georg.brandl | 2008-03-30 12:00:49 -0700 (Sun, 30 Mar 2008) | 2 lines
  Make _fields attr for no fields consistent with _attributes attr.
........
  r62059 | georg.brandl | 2008-03-30 13:20:39 -0700 (Sun, 30 Mar 2008) | 2 lines
  Make AST nodes pickleable.
........
											
										 
											2008-03-31 05:29:39 +00:00
										 |  |  | static PyMethodDef ast_type_methods[] = { | 
					
						
							|  |  |  |     {"__reduce__", ast_type_reduce, METH_NOARGS, NULL}, | 
					
						
							| 
									
										
										
										
											2024-07-04 05:10:54 +02:00
										 |  |  |     {"__replace__", _PyCFunction_CAST(ast_type_replace), METH_VARARGS | METH_KEYWORDS, | 
					
						
							|  |  |  |      PyDoc_STR("__replace__($self, /, **fields)\\n--\\n\\n" | 
					
						
							|  |  |  |                "Return a copy of the AST node with new values " | 
					
						
							|  |  |  |                "for the specified fields.")}, | 
					
						
							| 
									
										
											  
											
												Merged revisions 61440-61441,61443,61445-61448,61451-61452,61455-61457,61459-61464,61466-61467,61469-61470,61476-61477,61479,61481-61482,61485,61487,61490,61493-61494,61497,61499-61502,61505-61506,61508,61511-61514,61519,61521-61522,61530-61531,61533-61537,61541-61555,61557-61558,61561-61562,61566-61569,61572-61574,61578-61579,61583-61584,61588-61589,61592,61594,61598-61601,61603-61604,61607-61612,61617,61619-61620,61624,61626,61628-61630,61635-61638,61640-61643,61645,61648,61653-61655,61659-61662,61664,61666,61668-61671,61673,61675,61679-61680,61682,61685-61686,61689-61695,61697-61699,61701-61703,61706,61710,61713,61717,61723,61726-61730,61736,61738,61740,61742,61745-61752,61754-61760,61762-61764,61768,61770-61772,61774-61775,61784-61787,61789-61792,61794-61795,61797-61806,61808-61809,61811-61812,61814-61819,61824,61826-61833,61835-61840,61843-61845,61848,61850,61854-61862,61865-61866,61868,61872-61873,61876-61877,61883-61888,61890-61891,61893-61899,61901-61903,61905-61912,61914,61917,61920-61921,61927,61930,61932-61934,61939,61941-61942,61944-61951,61955,61960-61963,61980,61982-61983,61991,61994-61996,62001-62003,62008-62010,62016-62017,62022,62024,62027,62031-62034,62041,62045-62046,62048,62050-62051,62055-62066,62068-62074,62076-62078 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
  r62048 | georg.brandl | 2008-03-29 23:53:55 -0700 (Sat, 29 Mar 2008) | 2 lines
  Adapt test_ast to the new ExceptHandler type.
........
  r62050 | georg.brandl | 2008-03-30 00:09:22 -0700 (Sun, 30 Mar 2008) | 2 lines
  Convert test_ast to unittest and add a test for r62049.
........
  r62051 | georg.brandl | 2008-03-30 12:00:49 -0700 (Sun, 30 Mar 2008) | 2 lines
  Make _fields attr for no fields consistent with _attributes attr.
........
  r62059 | georg.brandl | 2008-03-30 13:20:39 -0700 (Sun, 30 Mar 2008) | 2 lines
  Make AST nodes pickleable.
........
											
										 
											2008-03-31 05:29:39 +00:00
										 |  |  |     {NULL} | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-03-12 09:46:44 -07:00
										 |  |  | static PyGetSetDef ast_type_getsets[] = { | 
					
						
							|  |  |  |     {"__dict__", PyObject_GenericGetDict, PyObject_GenericSetDict}, | 
					
						
							|  |  |  |     {NULL} | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-18 19:28:22 +02:00
										 |  |  | static PyObject * | 
					
						
							|  |  |  | ast_repr_max_depth(AST_object *self, int depth); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* Format list and tuple properties of AST nodes. | 
					
						
							|  |  |  |    Note that, only the first and last elements are shown. | 
					
						
							|  |  |  |    Anything in between is represented with an ellipsis ('...'). | 
					
						
							|  |  |  |    For example, the list [1, 2, 3] is formatted as | 
					
						
							|  |  |  |    'List(elts=[Constant(1), ..., Constant(3)])'. */ | 
					
						
							|  |  |  | static PyObject * | 
					
						
							|  |  |  | ast_repr_list(PyObject *list, int depth) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     assert(PyList_Check(list) || PyTuple_Check(list)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     struct ast_state *state = get_ast_state(); | 
					
						
							|  |  |  |     if (state == NULL) { | 
					
						
							|  |  |  |         return NULL; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     Py_ssize_t length = PySequence_Size(list); | 
					
						
							|  |  |  |     if (length < 0) { | 
					
						
							|  |  |  |         return NULL; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else if (length == 0) { | 
					
						
							|  |  |  |         return PyObject_Repr(list); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     _PyUnicodeWriter writer; | 
					
						
							|  |  |  |     _PyUnicodeWriter_Init(&writer); | 
					
						
							|  |  |  |     writer.overallocate = 1; | 
					
						
							|  |  |  |     PyObject *items[2] = {NULL, NULL}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     items[0] = PySequence_GetItem(list, 0); | 
					
						
							|  |  |  |     if (!items[0]) { | 
					
						
							|  |  |  |         goto error; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     if (length > 1) { | 
					
						
							|  |  |  |         items[1] = PySequence_GetItem(list, length - 1); | 
					
						
							|  |  |  |         if (!items[1]) { | 
					
						
							|  |  |  |             goto error; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     bool is_list = PyList_Check(list); | 
					
						
							|  |  |  |     if (_PyUnicodeWriter_WriteChar(&writer, is_list ? '[' : '(') < 0) { | 
					
						
							|  |  |  |         goto error; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     for (Py_ssize_t i = 0; i < Py_MIN(length, 2); i++) { | 
					
						
							|  |  |  |         PyObject *item = items[i]; | 
					
						
							|  |  |  |         PyObject *item_repr; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (PyType_IsSubtype(Py_TYPE(item), (PyTypeObject *)state->AST_type)) { | 
					
						
							|  |  |  |             item_repr = ast_repr_max_depth((AST_object*)item, depth - 1); | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |             item_repr = PyObject_Repr(item); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if (!item_repr) { | 
					
						
							|  |  |  |             goto error; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if (i > 0) { | 
					
						
							|  |  |  |             if (_PyUnicodeWriter_WriteASCIIString(&writer, ", ", 2) < 0) { | 
					
						
							|  |  |  |                 goto error; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if (_PyUnicodeWriter_WriteStr(&writer, item_repr) < 0) { | 
					
						
							|  |  |  |             Py_DECREF(item_repr); | 
					
						
							|  |  |  |             goto error; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if (i == 0 && length > 2) { | 
					
						
							|  |  |  |             if (_PyUnicodeWriter_WriteASCIIString(&writer, ", ...", 5) < 0) { | 
					
						
							|  |  |  |                 Py_DECREF(item_repr); | 
					
						
							|  |  |  |                 goto error; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         Py_DECREF(item_repr); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (_PyUnicodeWriter_WriteChar(&writer, is_list ? ']' : ')') < 0) { | 
					
						
							|  |  |  |         goto error; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     Py_XDECREF(items[0]); | 
					
						
							|  |  |  |     Py_XDECREF(items[1]); | 
					
						
							|  |  |  |     return _PyUnicodeWriter_Finish(&writer); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | error: | 
					
						
							|  |  |  |     Py_XDECREF(items[0]); | 
					
						
							|  |  |  |     Py_XDECREF(items[1]); | 
					
						
							|  |  |  |     _PyUnicodeWriter_Dealloc(&writer); | 
					
						
							|  |  |  |     return NULL; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static PyObject * | 
					
						
							|  |  |  | ast_repr_max_depth(AST_object *self, int depth) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     struct ast_state *state = get_ast_state(); | 
					
						
							|  |  |  |     if (state == NULL) { | 
					
						
							|  |  |  |         return NULL; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (depth <= 0) { | 
					
						
							|  |  |  |         return PyUnicode_FromFormat("%s(...)", Py_TYPE(self)->tp_name); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     int status = Py_ReprEnter((PyObject *)self); | 
					
						
							|  |  |  |     if (status != 0) { | 
					
						
							|  |  |  |         if (status < 0) { | 
					
						
							|  |  |  |             return NULL; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         return PyUnicode_FromFormat("%s(...)", Py_TYPE(self)->tp_name); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     PyObject *fields; | 
					
						
							|  |  |  |     if (PyObject_GetOptionalAttr((PyObject *)Py_TYPE(self), state->_fields, &fields) < 0) { | 
					
						
							|  |  |  |         Py_ReprLeave((PyObject *)self); | 
					
						
							|  |  |  |         return NULL; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     Py_ssize_t numfields = PySequence_Size(fields); | 
					
						
							|  |  |  |     if (numfields < 0) { | 
					
						
							|  |  |  |         Py_ReprLeave((PyObject *)self); | 
					
						
							|  |  |  |         Py_DECREF(fields); | 
					
						
							|  |  |  |         return NULL; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (numfields == 0) { | 
					
						
							|  |  |  |         Py_ReprLeave((PyObject *)self); | 
					
						
							|  |  |  |         Py_DECREF(fields); | 
					
						
							|  |  |  |         return PyUnicode_FromFormat("%s()", Py_TYPE(self)->tp_name); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const char* tp_name = Py_TYPE(self)->tp_name; | 
					
						
							|  |  |  |     _PyUnicodeWriter writer; | 
					
						
							|  |  |  |     _PyUnicodeWriter_Init(&writer); | 
					
						
							|  |  |  |     writer.overallocate = 1; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (_PyUnicodeWriter_WriteASCIIString(&writer, tp_name, strlen(tp_name)) < 0) { | 
					
						
							|  |  |  |         goto error; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     if (_PyUnicodeWriter_WriteChar(&writer, '(') < 0) { | 
					
						
							|  |  |  |         goto error; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     for (Py_ssize_t i = 0; i < numfields; i++) { | 
					
						
							|  |  |  |         PyObject *name = PySequence_GetItem(fields, i); | 
					
						
							|  |  |  |         if (!name) { | 
					
						
							|  |  |  |             goto error; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         PyObject *value = PyObject_GetAttr((PyObject *)self, name); | 
					
						
							|  |  |  |         if (!value) { | 
					
						
							|  |  |  |             Py_DECREF(name); | 
					
						
							|  |  |  |             goto error; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         PyObject *value_repr; | 
					
						
							|  |  |  |         if (PyList_Check(value) || PyTuple_Check(value)) { | 
					
						
							|  |  |  |             value_repr = ast_repr_list(value, depth); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         else if (PyType_IsSubtype(Py_TYPE(value), (PyTypeObject *)state->AST_type)) { | 
					
						
							|  |  |  |             value_repr = ast_repr_max_depth((AST_object*)value, depth - 1); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         else { | 
					
						
							|  |  |  |             value_repr = PyObject_Repr(value); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         Py_DECREF(value); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (!value_repr) { | 
					
						
							|  |  |  |             Py_DECREF(name); | 
					
						
							|  |  |  |             goto error; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (i > 0) { | 
					
						
							|  |  |  |             if (_PyUnicodeWriter_WriteASCIIString(&writer, ", ", 2) < 0) { | 
					
						
							|  |  |  |                 Py_DECREF(name); | 
					
						
							|  |  |  |                 Py_DECREF(value_repr); | 
					
						
							|  |  |  |                 goto error; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if (_PyUnicodeWriter_WriteStr(&writer, name) < 0) { | 
					
						
							|  |  |  |             Py_DECREF(name); | 
					
						
							|  |  |  |             Py_DECREF(value_repr); | 
					
						
							|  |  |  |             goto error; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         Py_DECREF(name); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (_PyUnicodeWriter_WriteChar(&writer, '=') < 0) { | 
					
						
							|  |  |  |             Py_DECREF(value_repr); | 
					
						
							|  |  |  |             goto error; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if (_PyUnicodeWriter_WriteStr(&writer, value_repr) < 0) { | 
					
						
							|  |  |  |             Py_DECREF(value_repr); | 
					
						
							|  |  |  |             goto error; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         Py_DECREF(value_repr); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (_PyUnicodeWriter_WriteChar(&writer, ')') < 0) { | 
					
						
							|  |  |  |         goto error; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     Py_ReprLeave((PyObject *)self); | 
					
						
							|  |  |  |     Py_DECREF(fields); | 
					
						
							|  |  |  |     return _PyUnicodeWriter_Finish(&writer); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | error: | 
					
						
							|  |  |  |     Py_ReprLeave((PyObject *)self); | 
					
						
							|  |  |  |     Py_DECREF(fields); | 
					
						
							|  |  |  |     _PyUnicodeWriter_Dealloc(&writer); | 
					
						
							|  |  |  |     return NULL; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static PyObject * | 
					
						
							| 
									
										
										
										
											2024-10-04 11:59:08 +02:00
										 |  |  | ast_repr(PyObject *self) | 
					
						
							| 
									
										
										
										
											2024-09-18 19:28:22 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2024-10-04 11:59:08 +02:00
										 |  |  |     return ast_repr_max_depth((AST_object*)self, 3); | 
					
						
							| 
									
										
										
										
											2024-09-18 19:28:22 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-11 10:16:34 -07:00
										 |  |  | static PyType_Slot AST_type_slots[] = { | 
					
						
							|  |  |  |     {Py_tp_dealloc, ast_dealloc}, | 
					
						
							| 
									
										
										
										
											2024-09-18 19:28:22 +02:00
										 |  |  |     {Py_tp_repr, ast_repr}, | 
					
						
							| 
									
										
										
										
											2019-09-11 10:16:34 -07:00
										 |  |  |     {Py_tp_getattro, PyObject_GenericGetAttr}, | 
					
						
							|  |  |  |     {Py_tp_setattro, PyObject_GenericSetAttr}, | 
					
						
							|  |  |  |     {Py_tp_traverse, ast_traverse}, | 
					
						
							|  |  |  |     {Py_tp_clear, ast_clear}, | 
					
						
							| 
									
										
										
										
											2019-09-19 09:29:05 -07:00
										 |  |  |     {Py_tp_members, ast_type_members}, | 
					
						
							| 
									
										
										
										
											2019-09-11 10:16:34 -07:00
										 |  |  |     {Py_tp_methods, ast_type_methods}, | 
					
						
							|  |  |  |     {Py_tp_getset, ast_type_getsets}, | 
					
						
							|  |  |  |     {Py_tp_init, ast_type_init}, | 
					
						
							|  |  |  |     {Py_tp_alloc, PyType_GenericAlloc}, | 
					
						
							|  |  |  |     {Py_tp_new, PyType_GenericNew}, | 
					
						
							|  |  |  |     {Py_tp_free, PyObject_GC_Del}, | 
					
						
							|  |  |  |     {0, 0}, | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static PyType_Spec AST_type_spec = { | 
					
						
							| 
									
										
										
										
											2020-03-22 20:33:34 +02:00
										 |  |  |     "ast.AST", | 
					
						
							| 
									
										
										
										
											2012-03-12 09:46:44 -07:00
										 |  |  |     sizeof(AST_object), | 
					
						
							| 
									
										
										
										
											2008-03-31 04:42:11 +00:00
										 |  |  |     0, | 
					
						
							| 
									
										
										
										
											2019-09-11 10:16:34 -07:00
										 |  |  |     Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, | 
					
						
							|  |  |  |     AST_type_slots | 
					
						
							| 
									
										
										
										
											2008-03-31 04:42:11 +00:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-11 10:16:34 -07:00
										 |  |  | static PyObject * | 
					
						
							| 
									
										
										
										
											2020-11-02 22:03:28 +01:00
										 |  |  | make_type(struct ast_state *state, const char *type, PyObject* base, | 
					
						
							| 
									
										
										
										
											2020-07-03 11:35:37 +02:00
										 |  |  |           const char* const* fields, int num_fields, const char *doc) | 
					
						
							| 
									
										
										
										
											2006-02-26 19:42:26 +00:00
										 |  |  | { | 
					
						
							|  |  |  |     PyObject *fnames, *result; | 
					
						
							|  |  |  |     int i; | 
					
						
							| 
									
										
											  
											
												Merged revisions 61440-61441,61443,61445-61448,61451-61452,61455-61457,61459-61464,61466-61467,61469-61470,61476-61477,61479,61481-61482,61485,61487,61490,61493-61494,61497,61499-61502,61505-61506,61508,61511-61514,61519,61521-61522,61530-61531,61533-61537,61541-61555,61557-61558,61561-61562,61566-61569,61572-61574,61578-61579,61583-61584,61588-61589,61592,61594,61598-61601,61603-61604,61607-61612,61617,61619-61620,61624,61626,61628-61630,61635-61638,61640-61643,61645,61648,61653-61655,61659-61662,61664,61666,61668-61671,61673,61675,61679-61680,61682,61685-61686,61689-61695,61697-61699,61701-61703,61706,61710,61713,61717,61723,61726-61730,61736,61738,61740,61742,61745-61752,61754-61760,61762-61764,61768,61770-61772,61774-61775,61784-61787,61789-61792,61794-61795,61797-61806,61808-61809,61811-61812,61814-61819,61824,61826-61833,61835-61840,61843-61845,61848,61850,61854-61862,61865-61866,61868,61872-61873,61876-61877,61883-61888,61890-61891,61893-61899,61901-61903,61905-61912,61914,61917,61920-61921,61927,61930,61932-61934,61939,61941-61942,61944-61951,61955,61960-61963,61980,61982-61983,61991,61994-61996,62001-62003,62008-62010,62016-62017,62022,62024,62027,62031-62034,62041,62045-62046,62048,62050-62051,62055-62066,62068-62074,62076-62078 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
  r62048 | georg.brandl | 2008-03-29 23:53:55 -0700 (Sat, 29 Mar 2008) | 2 lines
  Adapt test_ast to the new ExceptHandler type.
........
  r62050 | georg.brandl | 2008-03-30 00:09:22 -0700 (Sun, 30 Mar 2008) | 2 lines
  Convert test_ast to unittest and add a test for r62049.
........
  r62051 | georg.brandl | 2008-03-30 12:00:49 -0700 (Sun, 30 Mar 2008) | 2 lines
  Make _fields attr for no fields consistent with _attributes attr.
........
  r62059 | georg.brandl | 2008-03-30 13:20:39 -0700 (Sun, 30 Mar 2008) | 2 lines
  Make AST nodes pickleable.
........
											
										 
											2008-03-31 05:29:39 +00:00
										 |  |  |     fnames = PyTuple_New(num_fields); | 
					
						
							|  |  |  |     if (!fnames) return NULL; | 
					
						
							|  |  |  |     for (i = 0; i < num_fields; i++) { | 
					
						
							| 
									
										
										
										
											2019-09-10 13:02:30 +03:00
										 |  |  |         PyObject *field = PyUnicode_InternFromString(fields[i]); | 
					
						
							| 
									
										
										
										
											2006-02-26 19:42:26 +00:00
										 |  |  |         if (!field) { | 
					
						
							|  |  |  |             Py_DECREF(fnames); | 
					
						
							|  |  |  |             return NULL; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         PyTuple_SET_ITEM(fnames, i, field); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-02-26 14:51:55 -08:00
										 |  |  |     result = PyObject_CallFunction((PyObject*)&PyType_Type, "s(O){OOOOOOOs}", | 
					
						
							| 
									
										
										
										
											2017-01-25 22:33:43 +09:00
										 |  |  |                     type, base, | 
					
						
							| 
									
										
										
										
											2020-07-03 11:35:37 +02:00
										 |  |  |                     state->_fields, fnames, | 
					
						
							| 
									
										
										
										
											2021-02-26 14:51:55 -08:00
										 |  |  |                     state->__match_args__, fnames, | 
					
						
							| 
									
										
										
										
											2020-07-03 11:35:37 +02:00
										 |  |  |                     state->__module__, | 
					
						
							|  |  |  |                     state->ast, | 
					
						
							|  |  |  |                     state->__doc__, doc); | 
					
						
							| 
									
										
										
										
											2006-02-26 19:42:26 +00:00
										 |  |  |     Py_DECREF(fnames); | 
					
						
							| 
									
										
										
										
											2019-09-11 10:16:34 -07:00
										 |  |  |     return result; | 
					
						
							| 
									
										
										
										
											2006-02-26 19:42:26 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-10 13:02:30 +03:00
										 |  |  | static int | 
					
						
							| 
									
										
										
										
											2020-11-02 22:03:28 +01:00
										 |  |  | add_attributes(struct ast_state *state, PyObject *type, const char * const *attrs, int num_fields) | 
					
						
							| 
									
										
										
										
											2006-02-27 15:23:19 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2006-04-21 10:40:58 +00:00
										 |  |  |     int i, result; | 
					
						
							| 
									
										
										
										
											2008-03-31 04:42:11 +00:00
										 |  |  |     PyObject *s, *l = PyTuple_New(num_fields); | 
					
						
							| 
									
										
											  
											
												Merged revisions 81465-81466,81468,81679,81735,81760,81868,82183 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
  r81465 | georg.brandl | 2010-05-22 06:29:19 -0500 (Sat, 22 May 2010) | 2 lines
  Issue #3924: Ignore cookies with invalid "version" field in cookielib.
........
  r81466 | georg.brandl | 2010-05-22 06:31:16 -0500 (Sat, 22 May 2010) | 1 line
  Underscore the name of an internal utility function.
........
  r81468 | georg.brandl | 2010-05-22 06:43:25 -0500 (Sat, 22 May 2010) | 1 line
  #8635: document enumerate() start parameter in docstring.
........
  r81679 | benjamin.peterson | 2010-06-03 16:21:03 -0500 (Thu, 03 Jun 2010) | 1 line
  use a set for membership testing
........
  r81735 | michael.foord | 2010-06-05 06:46:59 -0500 (Sat, 05 Jun 2010) | 1 line
  Extract error message truncating into a method (unittest.TestCase._truncateMessage).
........
  r81760 | michael.foord | 2010-06-05 14:38:42 -0500 (Sat, 05 Jun 2010) | 1 line
  Issue 8302. SkipTest exception is setUpClass or setUpModule is now reported as a skip rather than an error.
........
  r81868 | benjamin.peterson | 2010-06-09 14:45:04 -0500 (Wed, 09 Jun 2010) | 1 line
  fix code formatting
........
  r82183 | benjamin.peterson | 2010-06-23 15:29:26 -0500 (Wed, 23 Jun 2010) | 1 line
  cpython only gc tests
........
											
										 
											2010-06-27 21:45:24 +00:00
										 |  |  |     if (!l) | 
					
						
							| 
									
										
										
										
											2023-11-16 14:19:54 -05:00
										 |  |  |         return -1; | 
					
						
							| 
									
										
											  
											
												Merged revisions 81465-81466,81468,81679,81735,81760,81868,82183 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
  r81465 | georg.brandl | 2010-05-22 06:29:19 -0500 (Sat, 22 May 2010) | 2 lines
  Issue #3924: Ignore cookies with invalid "version" field in cookielib.
........
  r81466 | georg.brandl | 2010-05-22 06:31:16 -0500 (Sat, 22 May 2010) | 1 line
  Underscore the name of an internal utility function.
........
  r81468 | georg.brandl | 2010-05-22 06:43:25 -0500 (Sat, 22 May 2010) | 1 line
  #8635: document enumerate() start parameter in docstring.
........
  r81679 | benjamin.peterson | 2010-06-03 16:21:03 -0500 (Thu, 03 Jun 2010) | 1 line
  use a set for membership testing
........
  r81735 | michael.foord | 2010-06-05 06:46:59 -0500 (Sat, 05 Jun 2010) | 1 line
  Extract error message truncating into a method (unittest.TestCase._truncateMessage).
........
  r81760 | michael.foord | 2010-06-05 14:38:42 -0500 (Sat, 05 Jun 2010) | 1 line
  Issue 8302. SkipTest exception is setUpClass or setUpModule is now reported as a skip rather than an error.
........
  r81868 | benjamin.peterson | 2010-06-09 14:45:04 -0500 (Wed, 09 Jun 2010) | 1 line
  fix code formatting
........
  r82183 | benjamin.peterson | 2010-06-23 15:29:26 -0500 (Wed, 23 Jun 2010) | 1 line
  cpython only gc tests
........
											
										 
											2010-06-27 21:45:24 +00:00
										 |  |  |     for (i = 0; i < num_fields; i++) { | 
					
						
							| 
									
										
										
										
											2019-09-10 13:02:30 +03:00
										 |  |  |         s = PyUnicode_InternFromString(attrs[i]); | 
					
						
							| 
									
										
										
										
											2006-02-27 15:23:19 +00:00
										 |  |  |         if (!s) { | 
					
						
							|  |  |  |             Py_DECREF(l); | 
					
						
							| 
									
										
										
										
											2023-11-16 14:19:54 -05:00
										 |  |  |             return -1; | 
					
						
							| 
									
										
										
										
											2006-02-27 15:23:19 +00:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2008-03-31 04:42:11 +00:00
										 |  |  |         PyTuple_SET_ITEM(l, i, s); | 
					
						
							| 
									
										
										
										
											2006-02-27 15:23:19 +00:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2023-11-16 14:19:54 -05:00
										 |  |  |     result = PyObject_SetAttr(type, state->_attributes, l); | 
					
						
							| 
									
										
										
										
											2006-04-21 10:40:58 +00:00
										 |  |  |     Py_DECREF(l); | 
					
						
							|  |  |  |     return result; | 
					
						
							| 
									
										
										
										
											2006-02-27 15:23:19 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-03-30 20:03:44 +00:00
										 |  |  | /* Conversion AST -> Python */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-12-25 09:36:59 -08:00
										 |  |  | static PyObject* ast2obj_list(struct ast_state *state, struct validator *vstate, asdl_seq *seq, | 
					
						
							|  |  |  |                               PyObject* (*func)(struct ast_state *state, struct validator *vstate, void*)) | 
					
						
							| 
									
										
										
										
											2006-02-26 19:42:26 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2012-05-15 10:10:27 -07:00
										 |  |  |     Py_ssize_t i, n = asdl_seq_LEN(seq); | 
					
						
							| 
									
										
										
										
											2006-02-26 19:42:26 +00:00
										 |  |  |     PyObject *result = PyList_New(n); | 
					
						
							|  |  |  |     PyObject *value; | 
					
						
							|  |  |  |     if (!result) | 
					
						
							|  |  |  |         return NULL; | 
					
						
							|  |  |  |     for (i = 0; i < n; i++) { | 
					
						
							| 
									
										
										
										
											2023-12-25 09:36:59 -08:00
										 |  |  |         value = func(state, vstate, asdl_seq_GET_UNTYPED(seq, i)); | 
					
						
							| 
									
										
										
										
											2006-02-26 19:42:26 +00:00
										 |  |  |         if (!value) { | 
					
						
							|  |  |  |             Py_DECREF(result); | 
					
						
							|  |  |  |             return NULL; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         PyList_SET_ITEM(result, i, value); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return result; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-12-25 09:36:59 -08:00
										 |  |  | static PyObject* ast2obj_object(struct ast_state *Py_UNUSED(state), struct validator *Py_UNUSED(vstate), void *o) | 
					
						
							| 
									
										
										
										
											2006-02-26 19:42:26 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2022-11-10 11:23:36 +01:00
										 |  |  |     PyObject *op = (PyObject*)o; | 
					
						
							|  |  |  |     if (!op) { | 
					
						
							|  |  |  |         op = Py_None; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return Py_NewRef(op); | 
					
						
							| 
									
										
										
										
											2006-02-26 19:42:26 +00:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2016-01-26 00:40:57 +01:00
										 |  |  | #define ast2obj_constant ast2obj_object | 
					
						
							| 
									
										
										
										
											2006-02-26 19:42:26 +00:00
										 |  |  | #define ast2obj_identifier ast2obj_object | 
					
						
							|  |  |  | #define ast2obj_string ast2obj_object | 
					
						
							| 
									
										
										
										
											2006-02-27 15:23:19 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-12-25 09:36:59 -08:00
										 |  |  | static PyObject* ast2obj_int(struct ast_state *Py_UNUSED(state), struct validator *Py_UNUSED(vstate), long b) | 
					
						
							| 
									
										
										
										
											2006-02-27 15:23:19 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2007-12-02 22:43:00 +00:00
										 |  |  |     return PyLong_FromLong(b); | 
					
						
							| 
									
										
										
										
											2006-02-27 15:23:19 +00:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2008-03-30 20:03:44 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | /* Conversion Python -> AST */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-02 22:03:28 +01:00
										 |  |  | static int obj2ast_object(struct ast_state *Py_UNUSED(state), PyObject* obj, PyObject** out, PyArena* arena) | 
					
						
							| 
									
										
										
										
											2008-03-30 20:03:44 +00:00
										 |  |  | { | 
					
						
							|  |  |  |     if (obj == Py_None) | 
					
						
							|  |  |  |         obj = NULL; | 
					
						
							| 
									
										
										
										
											2013-07-27 00:33:13 +02:00
										 |  |  |     if (obj) { | 
					
						
							| 
									
										
										
										
											2021-03-24 02:23:01 +01:00
										 |  |  |         if (_PyArena_AddPyObject(arena, obj) < 0) { | 
					
						
							| 
									
										
										
										
											2013-07-27 00:33:13 +02:00
										 |  |  |             *out = NULL; | 
					
						
							|  |  |  |             return -1; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-11-15 10:29:56 +01:00
										 |  |  |         *out = Py_NewRef(obj); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else { | 
					
						
							|  |  |  |         *out = NULL; | 
					
						
							| 
									
										
										
										
											2013-07-27 00:33:13 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2008-03-30 20:03:44 +00:00
										 |  |  |     return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-02 22:03:28 +01:00
										 |  |  | static int obj2ast_constant(struct ast_state *Py_UNUSED(state), PyObject* obj, PyObject** out, PyArena* arena) | 
					
						
							| 
									
										
										
										
											2016-01-26 00:40:57 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2021-03-24 02:23:01 +01:00
										 |  |  |     if (_PyArena_AddPyObject(arena, obj) < 0) { | 
					
						
							| 
									
										
										
										
											2018-09-27 17:42:37 +03:00
										 |  |  |         *out = NULL; | 
					
						
							|  |  |  |         return -1; | 
					
						
							| 
									
										
										
										
											2016-01-26 00:40:57 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-11-10 11:23:36 +01:00
										 |  |  |     *out = Py_NewRef(obj); | 
					
						
							| 
									
										
										
										
											2016-01-26 00:40:57 +01:00
										 |  |  |     return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-02 22:03:28 +01:00
										 |  |  | static int obj2ast_identifier(struct ast_state *state, PyObject* obj, PyObject** out, PyArena* arena) | 
					
						
							| 
									
										
										
										
											2011-07-22 10:50:23 -05:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2011-07-22 11:09:07 -05:00
										 |  |  |     if (!PyUnicode_CheckExact(obj) && obj != Py_None) { | 
					
						
							|  |  |  |         PyErr_SetString(PyExc_TypeError, "AST identifier must be of type str"); | 
					
						
							| 
									
										
										
										
											2023-11-16 14:19:54 -05:00
										 |  |  |         return -1; | 
					
						
							| 
									
										
										
										
											2011-07-22 10:50:23 -05:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-07-03 11:35:37 +02:00
										 |  |  |     return obj2ast_object(state, obj, out, arena); | 
					
						
							| 
									
										
										
										
											2011-07-22 10:50:23 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-02 22:03:28 +01:00
										 |  |  | static int obj2ast_string(struct ast_state *state, PyObject* obj, PyObject** out, PyArena* arena) | 
					
						
							| 
									
										
										
										
											2019-01-31 03:40:27 -08:00
										 |  |  | { | 
					
						
							|  |  |  |     if (!PyUnicode_CheckExact(obj) && !PyBytes_CheckExact(obj)) { | 
					
						
							|  |  |  |         PyErr_SetString(PyExc_TypeError, "AST string must be of type str"); | 
					
						
							| 
									
										
										
										
											2023-11-16 14:19:54 -05:00
										 |  |  |         return -1; | 
					
						
							| 
									
										
										
										
											2019-01-31 03:40:27 -08:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-07-03 11:35:37 +02:00
										 |  |  |     return obj2ast_object(state, obj, out, arena); | 
					
						
							| 
									
										
										
										
											2019-01-31 03:40:27 -08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-02 22:03:28 +01:00
										 |  |  | static int obj2ast_int(struct ast_state* Py_UNUSED(state), PyObject* obj, int* out, PyArena* arena) | 
					
						
							| 
									
										
										
										
											2008-03-30 20:03:44 +00:00
										 |  |  | { | 
					
						
							|  |  |  |     int i; | 
					
						
							|  |  |  |     if (!PyLong_Check(obj)) { | 
					
						
							| 
									
										
										
										
											2011-11-22 21:51:55 +01:00
										 |  |  |         PyErr_Format(PyExc_ValueError, "invalid integer value: %R", obj); | 
					
						
							| 
									
										
										
										
											2023-11-16 14:19:54 -05:00
										 |  |  |         return -1; | 
					
						
							| 
									
										
										
										
											2008-03-30 20:03:44 +00:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-25 11:13:59 +02:00
										 |  |  |     i = PyLong_AsInt(obj); | 
					
						
							| 
									
										
										
										
											2008-03-30 20:03:44 +00:00
										 |  |  |     if (i == -1 && PyErr_Occurred()) | 
					
						
							| 
									
										
										
										
											2023-11-16 14:19:54 -05:00
										 |  |  |         return -1; | 
					
						
							| 
									
										
										
										
											2008-03-30 20:03:44 +00:00
										 |  |  |     *out = i; | 
					
						
							|  |  |  |     return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-02 22:03:28 +01:00
										 |  |  | static int add_ast_fields(struct ast_state *state) | 
					
						
							| 
									
										
											  
											
												Merged revisions 66887,66891,66902-66903,66905-66906,66911-66913,66922,66927-66928,66936,66939-66940,66962,66964,66973 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
................
  r66887 | benjamin.peterson | 2008-10-13 16:51:40 -0500 (Mon, 13 Oct 2008) | 1 line
  document how to disable fixers
................
  r66891 | amaury.forgeotdarc | 2008-10-14 16:47:22 -0500 (Tue, 14 Oct 2008) | 5 lines
  #4122: On Windows, Py_UNICODE_ISSPACE cannot be used in an extension module:
  compilation fails with "undefined reference to _Py_ascii_whitespace"
  Will backport to 2.6.
................
  r66902 | skip.montanaro | 2008-10-15 06:49:10 -0500 (Wed, 15 Oct 2008) | 1 line
  easter egg
................
  r66903 | benjamin.peterson | 2008-10-15 15:34:09 -0500 (Wed, 15 Oct 2008) | 1 line
  don't recurse into directories that start with '.'
................
  r66905 | benjamin.peterson | 2008-10-15 16:05:55 -0500 (Wed, 15 Oct 2008) | 1 line
  support the optional line argument for idle
................
  r66906 | benjamin.peterson | 2008-10-15 16:58:46 -0500 (Wed, 15 Oct 2008) | 1 line
  add a much requested newline
................
  r66911 | benjamin.peterson | 2008-10-15 18:10:28 -0500 (Wed, 15 Oct 2008) | 41 lines
  Merged revisions 66805,66841,66860,66884-66886,66893,66907,66910 via svnmerge from
  svn+ssh://pythondev@svn.python.org/sandbox/trunk/2to3/lib2to3
  ........
    r66805 | benjamin.peterson | 2008-10-04 20:11:02 -0500 (Sat, 04 Oct 2008) | 1 line
    mention what the fixes directory is for
  ........
    r66841 | benjamin.peterson | 2008-10-07 17:48:12 -0500 (Tue, 07 Oct 2008) | 1 line
    use assertFalse and assertTrue
  ........
    r66860 | benjamin.peterson | 2008-10-08 16:05:07 -0500 (Wed, 08 Oct 2008) | 1 line
    instead of abusing the pattern matcher, use start_tree to find a next binding
  ........
    r66884 | benjamin.peterson | 2008-10-13 15:50:30 -0500 (Mon, 13 Oct 2008) | 1 line
    don't print tokens to stdout when -v is given
  ........
    r66885 | benjamin.peterson | 2008-10-13 16:28:57 -0500 (Mon, 13 Oct 2008) | 1 line
    add the -x option to disable fixers
  ........
    r66886 | benjamin.peterson | 2008-10-13 16:33:53 -0500 (Mon, 13 Oct 2008) | 1 line
    cut down on some crud
  ........
    r66893 | benjamin.peterson | 2008-10-14 17:16:54 -0500 (Tue, 14 Oct 2008) | 1 line
    add an optional set literal fixer
  ........
    r66907 | benjamin.peterson | 2008-10-15 16:59:41 -0500 (Wed, 15 Oct 2008) | 1 line
    don't write backup files by default
  ........
    r66910 | benjamin.peterson | 2008-10-15 17:43:10 -0500 (Wed, 15 Oct 2008) | 1 line
    add the -n option; it stops backupfiles from being written
  ........
................
  r66912 | hirokazu.yamamoto | 2008-10-16 01:25:25 -0500 (Thu, 16 Oct 2008) | 2 lines
  removed unused _PyUnicode_FromFileSystemEncodedObject.
  made win32_chdir, win32_wchdir static.
................
  r66913 | benjamin.peterson | 2008-10-16 13:52:14 -0500 (Thu, 16 Oct 2008) | 1 line
  document that deque indexing is O(n) #4123
................
  r66922 | benjamin.peterson | 2008-10-16 14:40:14 -0500 (Thu, 16 Oct 2008) | 1 line
  use new showwarnings signature for idle #3391
................
  r66927 | andrew.kuchling | 2008-10-16 15:15:47 -0500 (Thu, 16 Oct 2008) | 1 line
  Fix wording (2.6.1 backport candidate)
................
  r66928 | georg.brandl | 2008-10-16 15:20:56 -0500 (Thu, 16 Oct 2008) | 2 lines
  Add more TOC to the whatsnew index page.
................
  r66936 | georg.brandl | 2008-10-16 16:20:15 -0500 (Thu, 16 Oct 2008) | 2 lines
  #4131: FF3 doesn't write cookies.txt files.
................
  r66939 | georg.brandl | 2008-10-16 16:36:39 -0500 (Thu, 16 Oct 2008) | 2 lines
  part of #4012: kill off old name "processing".
................
  r66940 | georg.brandl | 2008-10-16 16:38:48 -0500 (Thu, 16 Oct 2008) | 2 lines
  #4083: add "as" to except handler grammar as per PEP 3110.
................
  r66962 | benjamin.peterson | 2008-10-17 15:01:01 -0500 (Fri, 17 Oct 2008) | 1 line
  clarify CALL_FUNCTION #4141
................
  r66964 | georg.brandl | 2008-10-17 16:41:49 -0500 (Fri, 17 Oct 2008) | 2 lines
  Fix duplicate word.
................
  r66973 | armin.ronacher | 2008-10-19 03:27:43 -0500 (Sun, 19 Oct 2008) | 3 lines
  Fixed #4067 by implementing _attributes and _fields for the AST root node.
................
											
										 
											2008-10-19 14:07:49 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2019-09-11 10:16:34 -07:00
										 |  |  |     PyObject *empty_tuple; | 
					
						
							| 
									
										
											  
											
												Merged revisions 66887,66891,66902-66903,66905-66906,66911-66913,66922,66927-66928,66936,66939-66940,66962,66964,66973 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
................
  r66887 | benjamin.peterson | 2008-10-13 16:51:40 -0500 (Mon, 13 Oct 2008) | 1 line
  document how to disable fixers
................
  r66891 | amaury.forgeotdarc | 2008-10-14 16:47:22 -0500 (Tue, 14 Oct 2008) | 5 lines
  #4122: On Windows, Py_UNICODE_ISSPACE cannot be used in an extension module:
  compilation fails with "undefined reference to _Py_ascii_whitespace"
  Will backport to 2.6.
................
  r66902 | skip.montanaro | 2008-10-15 06:49:10 -0500 (Wed, 15 Oct 2008) | 1 line
  easter egg
................
  r66903 | benjamin.peterson | 2008-10-15 15:34:09 -0500 (Wed, 15 Oct 2008) | 1 line
  don't recurse into directories that start with '.'
................
  r66905 | benjamin.peterson | 2008-10-15 16:05:55 -0500 (Wed, 15 Oct 2008) | 1 line
  support the optional line argument for idle
................
  r66906 | benjamin.peterson | 2008-10-15 16:58:46 -0500 (Wed, 15 Oct 2008) | 1 line
  add a much requested newline
................
  r66911 | benjamin.peterson | 2008-10-15 18:10:28 -0500 (Wed, 15 Oct 2008) | 41 lines
  Merged revisions 66805,66841,66860,66884-66886,66893,66907,66910 via svnmerge from
  svn+ssh://pythondev@svn.python.org/sandbox/trunk/2to3/lib2to3
  ........
    r66805 | benjamin.peterson | 2008-10-04 20:11:02 -0500 (Sat, 04 Oct 2008) | 1 line
    mention what the fixes directory is for
  ........
    r66841 | benjamin.peterson | 2008-10-07 17:48:12 -0500 (Tue, 07 Oct 2008) | 1 line
    use assertFalse and assertTrue
  ........
    r66860 | benjamin.peterson | 2008-10-08 16:05:07 -0500 (Wed, 08 Oct 2008) | 1 line
    instead of abusing the pattern matcher, use start_tree to find a next binding
  ........
    r66884 | benjamin.peterson | 2008-10-13 15:50:30 -0500 (Mon, 13 Oct 2008) | 1 line
    don't print tokens to stdout when -v is given
  ........
    r66885 | benjamin.peterson | 2008-10-13 16:28:57 -0500 (Mon, 13 Oct 2008) | 1 line
    add the -x option to disable fixers
  ........
    r66886 | benjamin.peterson | 2008-10-13 16:33:53 -0500 (Mon, 13 Oct 2008) | 1 line
    cut down on some crud
  ........
    r66893 | benjamin.peterson | 2008-10-14 17:16:54 -0500 (Tue, 14 Oct 2008) | 1 line
    add an optional set literal fixer
  ........
    r66907 | benjamin.peterson | 2008-10-15 16:59:41 -0500 (Wed, 15 Oct 2008) | 1 line
    don't write backup files by default
  ........
    r66910 | benjamin.peterson | 2008-10-15 17:43:10 -0500 (Wed, 15 Oct 2008) | 1 line
    add the -n option; it stops backupfiles from being written
  ........
................
  r66912 | hirokazu.yamamoto | 2008-10-16 01:25:25 -0500 (Thu, 16 Oct 2008) | 2 lines
  removed unused _PyUnicode_FromFileSystemEncodedObject.
  made win32_chdir, win32_wchdir static.
................
  r66913 | benjamin.peterson | 2008-10-16 13:52:14 -0500 (Thu, 16 Oct 2008) | 1 line
  document that deque indexing is O(n) #4123
................
  r66922 | benjamin.peterson | 2008-10-16 14:40:14 -0500 (Thu, 16 Oct 2008) | 1 line
  use new showwarnings signature for idle #3391
................
  r66927 | andrew.kuchling | 2008-10-16 15:15:47 -0500 (Thu, 16 Oct 2008) | 1 line
  Fix wording (2.6.1 backport candidate)
................
  r66928 | georg.brandl | 2008-10-16 15:20:56 -0500 (Thu, 16 Oct 2008) | 2 lines
  Add more TOC to the whatsnew index page.
................
  r66936 | georg.brandl | 2008-10-16 16:20:15 -0500 (Thu, 16 Oct 2008) | 2 lines
  #4131: FF3 doesn't write cookies.txt files.
................
  r66939 | georg.brandl | 2008-10-16 16:36:39 -0500 (Thu, 16 Oct 2008) | 2 lines
  part of #4012: kill off old name "processing".
................
  r66940 | georg.brandl | 2008-10-16 16:38:48 -0500 (Thu, 16 Oct 2008) | 2 lines
  #4083: add "as" to except handler grammar as per PEP 3110.
................
  r66962 | benjamin.peterson | 2008-10-17 15:01:01 -0500 (Fri, 17 Oct 2008) | 1 line
  clarify CALL_FUNCTION #4141
................
  r66964 | georg.brandl | 2008-10-17 16:41:49 -0500 (Fri, 17 Oct 2008) | 2 lines
  Fix duplicate word.
................
  r66973 | armin.ronacher | 2008-10-19 03:27:43 -0500 (Sun, 19 Oct 2008) | 3 lines
  Fixed #4067 by implementing _attributes and _fields for the AST root node.
................
											
										 
											2008-10-19 14:07:49 +00:00
										 |  |  |     empty_tuple = PyTuple_New(0); | 
					
						
							|  |  |  |     if (!empty_tuple || | 
					
						
							| 
									
										
										
										
											2020-07-03 11:35:37 +02:00
										 |  |  |         PyObject_SetAttrString(state->AST_type, "_fields", empty_tuple) < 0 || | 
					
						
							| 
									
										
										
										
											2021-02-26 14:51:55 -08:00
										 |  |  |         PyObject_SetAttrString(state->AST_type, "__match_args__", empty_tuple) < 0 || | 
					
						
							| 
									
										
										
										
											2020-07-03 11:35:37 +02:00
										 |  |  |         PyObject_SetAttrString(state->AST_type, "_attributes", empty_tuple) < 0) { | 
					
						
							| 
									
										
											  
											
												Merged revisions 66887,66891,66902-66903,66905-66906,66911-66913,66922,66927-66928,66936,66939-66940,66962,66964,66973 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
................
  r66887 | benjamin.peterson | 2008-10-13 16:51:40 -0500 (Mon, 13 Oct 2008) | 1 line
  document how to disable fixers
................
  r66891 | amaury.forgeotdarc | 2008-10-14 16:47:22 -0500 (Tue, 14 Oct 2008) | 5 lines
  #4122: On Windows, Py_UNICODE_ISSPACE cannot be used in an extension module:
  compilation fails with "undefined reference to _Py_ascii_whitespace"
  Will backport to 2.6.
................
  r66902 | skip.montanaro | 2008-10-15 06:49:10 -0500 (Wed, 15 Oct 2008) | 1 line
  easter egg
................
  r66903 | benjamin.peterson | 2008-10-15 15:34:09 -0500 (Wed, 15 Oct 2008) | 1 line
  don't recurse into directories that start with '.'
................
  r66905 | benjamin.peterson | 2008-10-15 16:05:55 -0500 (Wed, 15 Oct 2008) | 1 line
  support the optional line argument for idle
................
  r66906 | benjamin.peterson | 2008-10-15 16:58:46 -0500 (Wed, 15 Oct 2008) | 1 line
  add a much requested newline
................
  r66911 | benjamin.peterson | 2008-10-15 18:10:28 -0500 (Wed, 15 Oct 2008) | 41 lines
  Merged revisions 66805,66841,66860,66884-66886,66893,66907,66910 via svnmerge from
  svn+ssh://pythondev@svn.python.org/sandbox/trunk/2to3/lib2to3
  ........
    r66805 | benjamin.peterson | 2008-10-04 20:11:02 -0500 (Sat, 04 Oct 2008) | 1 line
    mention what the fixes directory is for
  ........
    r66841 | benjamin.peterson | 2008-10-07 17:48:12 -0500 (Tue, 07 Oct 2008) | 1 line
    use assertFalse and assertTrue
  ........
    r66860 | benjamin.peterson | 2008-10-08 16:05:07 -0500 (Wed, 08 Oct 2008) | 1 line
    instead of abusing the pattern matcher, use start_tree to find a next binding
  ........
    r66884 | benjamin.peterson | 2008-10-13 15:50:30 -0500 (Mon, 13 Oct 2008) | 1 line
    don't print tokens to stdout when -v is given
  ........
    r66885 | benjamin.peterson | 2008-10-13 16:28:57 -0500 (Mon, 13 Oct 2008) | 1 line
    add the -x option to disable fixers
  ........
    r66886 | benjamin.peterson | 2008-10-13 16:33:53 -0500 (Mon, 13 Oct 2008) | 1 line
    cut down on some crud
  ........
    r66893 | benjamin.peterson | 2008-10-14 17:16:54 -0500 (Tue, 14 Oct 2008) | 1 line
    add an optional set literal fixer
  ........
    r66907 | benjamin.peterson | 2008-10-15 16:59:41 -0500 (Wed, 15 Oct 2008) | 1 line
    don't write backup files by default
  ........
    r66910 | benjamin.peterson | 2008-10-15 17:43:10 -0500 (Wed, 15 Oct 2008) | 1 line
    add the -n option; it stops backupfiles from being written
  ........
................
  r66912 | hirokazu.yamamoto | 2008-10-16 01:25:25 -0500 (Thu, 16 Oct 2008) | 2 lines
  removed unused _PyUnicode_FromFileSystemEncodedObject.
  made win32_chdir, win32_wchdir static.
................
  r66913 | benjamin.peterson | 2008-10-16 13:52:14 -0500 (Thu, 16 Oct 2008) | 1 line
  document that deque indexing is O(n) #4123
................
  r66922 | benjamin.peterson | 2008-10-16 14:40:14 -0500 (Thu, 16 Oct 2008) | 1 line
  use new showwarnings signature for idle #3391
................
  r66927 | andrew.kuchling | 2008-10-16 15:15:47 -0500 (Thu, 16 Oct 2008) | 1 line
  Fix wording (2.6.1 backport candidate)
................
  r66928 | georg.brandl | 2008-10-16 15:20:56 -0500 (Thu, 16 Oct 2008) | 2 lines
  Add more TOC to the whatsnew index page.
................
  r66936 | georg.brandl | 2008-10-16 16:20:15 -0500 (Thu, 16 Oct 2008) | 2 lines
  #4131: FF3 doesn't write cookies.txt files.
................
  r66939 | georg.brandl | 2008-10-16 16:36:39 -0500 (Thu, 16 Oct 2008) | 2 lines
  part of #4012: kill off old name "processing".
................
  r66940 | georg.brandl | 2008-10-16 16:38:48 -0500 (Thu, 16 Oct 2008) | 2 lines
  #4083: add "as" to except handler grammar as per PEP 3110.
................
  r66962 | benjamin.peterson | 2008-10-17 15:01:01 -0500 (Fri, 17 Oct 2008) | 1 line
  clarify CALL_FUNCTION #4141
................
  r66964 | georg.brandl | 2008-10-17 16:41:49 -0500 (Fri, 17 Oct 2008) | 2 lines
  Fix duplicate word.
................
  r66973 | armin.ronacher | 2008-10-19 03:27:43 -0500 (Sun, 19 Oct 2008) | 3 lines
  Fixed #4067 by implementing _attributes and _fields for the AST root node.
................
											
										 
											2008-10-19 14:07:49 +00:00
										 |  |  |         Py_XDECREF(empty_tuple); | 
					
						
							|  |  |  |         return -1; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     Py_DECREF(empty_tuple); | 
					
						
							|  |  |  |     return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-02-26 19:42:26 +00:00
										 |  |  | """, 0, reflow=False)
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-03 18:07:15 +01:00
										 |  |  |         self.file.write(textwrap.dedent('''
 | 
					
						
							|  |  |  |             static int | 
					
						
							| 
									
										
										
										
											2024-10-04 11:59:08 +02:00
										 |  |  |             init_types(void *arg) | 
					
						
							| 
									
										
										
										
											2020-11-03 18:07:15 +01:00
										 |  |  |             { | 
					
						
							| 
									
										
										
										
											2024-10-04 11:59:08 +02:00
										 |  |  |                 struct ast_state *state = arg; | 
					
						
							| 
									
										
										
										
											2020-11-03 18:07:15 +01:00
										 |  |  |                 if (init_identifiers(state) < 0) { | 
					
						
							| 
									
										
										
										
											2023-11-16 14:19:54 -05:00
										 |  |  |                     return -1; | 
					
						
							| 
									
										
										
										
											2020-11-03 18:07:15 +01:00
										 |  |  |                 } | 
					
						
							|  |  |  |                 state->AST_type = PyType_FromSpec(&AST_type_spec); | 
					
						
							|  |  |  |                 if (!state->AST_type) { | 
					
						
							| 
									
										
										
										
											2023-11-16 14:19:54 -05:00
										 |  |  |                     return -1; | 
					
						
							| 
									
										
										
										
											2020-11-03 18:07:15 +01:00
										 |  |  |                 } | 
					
						
							|  |  |  |                 if (add_ast_fields(state) < 0) { | 
					
						
							| 
									
										
										
										
											2023-11-16 14:19:54 -05:00
										 |  |  |                     return -1; | 
					
						
							| 
									
										
										
										
											2020-11-03 18:07:15 +01:00
										 |  |  |                 } | 
					
						
							|  |  |  |         '''))
 | 
					
						
							| 
									
										
										
										
											2006-02-26 19:42:26 +00:00
										 |  |  |         for dfn in mod.dfns: | 
					
						
							|  |  |  |             self.visit(dfn) | 
					
						
							| 
									
										
										
										
											2020-11-03 18:07:15 +01:00
										 |  |  |         self.file.write(textwrap.dedent('''
 | 
					
						
							| 
									
										
										
										
											2024-02-27 18:13:03 -08:00
										 |  |  |                 if (!add_ast_annotations(state)) { | 
					
						
							|  |  |  |                     return -1; | 
					
						
							|  |  |  |                 } | 
					
						
							| 
									
										
										
										
											2023-11-16 14:19:54 -05:00
										 |  |  |                 return 0; | 
					
						
							| 
									
										
										
										
											2020-11-03 18:07:15 +01:00
										 |  |  |             } | 
					
						
							|  |  |  |         '''))
 | 
					
						
							| 
									
										
										
										
											2006-02-26 19:42:26 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def visitProduct(self, prod, name): | 
					
						
							| 
									
										
										
										
											2006-02-26 23:40:20 +00:00
										 |  |  |         if prod.fields: | 
					
						
							| 
									
										
										
										
											2014-05-09 17:58:22 -07:00
										 |  |  |             fields = name+"_fields" | 
					
						
							| 
									
										
										
										
											2006-02-26 23:40:20 +00:00
										 |  |  |         else: | 
					
						
							|  |  |  |             fields = "NULL" | 
					
						
							| 
									
										
										
										
											2020-07-03 11:35:37 +02:00
										 |  |  |         self.emit('state->%s_type = make_type(state, "%s", state->AST_type, %s, %d,' % | 
					
						
							| 
									
										
										
										
											2006-02-26 23:40:20 +00:00
										 |  |  |                         (name, name, fields, len(prod.fields)), 1) | 
					
						
							| 
									
										
										
										
											2020-03-16 11:12:53 +03:00
										 |  |  |         self.emit('%s);' % reflow_c_string(asdl_of(name, prod), 2), 2, reflow=False) | 
					
						
							| 
									
										
										
										
											2023-11-16 14:19:54 -05:00
										 |  |  |         self.emit("if (!state->%s_type) return -1;" % name, 1) | 
					
						
							| 
									
										
										
										
											2013-03-18 10:48:58 -07:00
										 |  |  |         if prod.attributes: | 
					
						
							| 
									
										
										
										
											2023-11-16 14:19:54 -05:00
										 |  |  |             self.emit("if (add_attributes(state, state->%s_type, %s_attributes, %d) < 0) return -1;" % | 
					
						
							| 
									
										
										
										
											2013-03-18 10:48:58 -07:00
										 |  |  |                             (name, name, len(prod.attributes)), 1) | 
					
						
							|  |  |  |         else: | 
					
						
							| 
									
										
										
										
											2023-11-16 14:19:54 -05:00
										 |  |  |             self.emit("if (add_attributes(state, state->%s_type, NULL, 0) < 0) return -1;" % name, 1) | 
					
						
							| 
									
										
										
										
											2020-03-10 00:07:47 +02:00
										 |  |  |         self.emit_defaults(name, prod.fields, 1) | 
					
						
							|  |  |  |         self.emit_defaults(name, prod.attributes, 1) | 
					
						
							| 
									
										
										
										
											2006-02-28 18:30:36 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-02-26 19:42:26 +00:00
										 |  |  |     def visitSum(self, sum, name): | 
					
						
							| 
									
										
										
										
											2020-07-03 11:35:37 +02:00
										 |  |  |         self.emit('state->%s_type = make_type(state, "%s", state->AST_type, NULL, 0,' % | 
					
						
							| 
									
										
										
										
											2008-03-31 04:42:11 +00:00
										 |  |  |                   (name, name), 1) | 
					
						
							| 
									
										
										
										
											2020-03-16 11:12:53 +03:00
										 |  |  |         self.emit('%s);' % reflow_c_string(asdl_of(name, sum), 2), 2, reflow=False) | 
					
						
							| 
									
										
										
										
											2023-11-16 14:19:54 -05:00
										 |  |  |         self.emit("if (!state->%s_type) return -1;" % name, 1) | 
					
						
							| 
									
										
										
										
											2006-02-27 15:23:19 +00:00
										 |  |  |         if sum.attributes: | 
					
						
							| 
									
										
										
										
											2023-11-16 14:19:54 -05:00
										 |  |  |             self.emit("if (add_attributes(state, state->%s_type, %s_attributes, %d) < 0) return -1;" % | 
					
						
							| 
									
										
										
										
											2006-02-27 15:23:19 +00:00
										 |  |  |                             (name, name, len(sum.attributes)), 1) | 
					
						
							|  |  |  |         else: | 
					
						
							| 
									
										
										
										
											2023-11-16 14:19:54 -05:00
										 |  |  |             self.emit("if (add_attributes(state, state->%s_type, NULL, 0) < 0) return -1;" % name, 1) | 
					
						
							| 
									
										
										
										
											2020-03-10 00:07:47 +02:00
										 |  |  |         self.emit_defaults(name, sum.attributes, 1) | 
					
						
							| 
									
										
										
										
											2006-02-26 19:42:26 +00:00
										 |  |  |         simple = is_simple(sum) | 
					
						
							|  |  |  |         for t in sum.types: | 
					
						
							|  |  |  |             self.visitConstructor(t, name, simple) | 
					
						
							| 
									
										
										
										
											2006-02-28 18:30:36 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-02-26 19:42:26 +00:00
										 |  |  |     def visitConstructor(self, cons, name, simple): | 
					
						
							| 
									
										
										
										
											2006-02-26 23:40:20 +00:00
										 |  |  |         if cons.fields: | 
					
						
							| 
									
										
										
										
											2014-05-09 17:58:22 -07:00
										 |  |  |             fields = cons.name+"_fields" | 
					
						
							| 
									
										
										
										
											2006-02-26 23:40:20 +00:00
										 |  |  |         else: | 
					
						
							|  |  |  |             fields = "NULL" | 
					
						
							| 
									
										
										
										
											2020-07-03 11:35:37 +02:00
										 |  |  |         self.emit('state->%s_type = make_type(state, "%s", state->%s_type, %s, %d,' % | 
					
						
							| 
									
										
										
										
											2006-02-26 23:40:20 +00:00
										 |  |  |                             (cons.name, cons.name, name, fields, len(cons.fields)), 1) | 
					
						
							| 
									
										
										
										
											2020-03-16 11:12:53 +03:00
										 |  |  |         self.emit('%s);' % reflow_c_string(asdl_of(cons.name, cons), 2), 2, reflow=False) | 
					
						
							| 
									
										
										
										
											2023-11-16 14:19:54 -05:00
										 |  |  |         self.emit("if (!state->%s_type) return -1;" % cons.name, 1) | 
					
						
							| 
									
										
										
										
											2020-03-10 00:07:47 +02:00
										 |  |  |         self.emit_defaults(cons.name, cons.fields, 1) | 
					
						
							| 
									
										
										
										
											2006-02-26 19:42:26 +00:00
										 |  |  |         if simple: | 
					
						
							| 
									
										
										
										
											2019-09-11 10:16:34 -07:00
										 |  |  |             self.emit("state->%s_singleton = PyType_GenericNew((PyTypeObject *)" | 
					
						
							|  |  |  |                       "state->%s_type, NULL, NULL);" % | 
					
						
							| 
									
										
										
										
											2006-02-26 19:42:26 +00:00
										 |  |  |                              (cons.name, cons.name), 1) | 
					
						
							| 
									
										
										
										
											2023-11-16 14:19:54 -05:00
										 |  |  |             self.emit("if (!state->%s_singleton) return -1;" % cons.name, 1) | 
					
						
							| 
									
										
										
										
											2006-02-28 18:30:36 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-10 00:07:47 +02:00
										 |  |  |     def emit_defaults(self, name, fields, depth): | 
					
						
							|  |  |  |         for field in fields: | 
					
						
							|  |  |  |             if field.opt: | 
					
						
							|  |  |  |                 self.emit('if (PyObject_SetAttr(state->%s_type, state->%s, Py_None) == -1)' % | 
					
						
							|  |  |  |                             (name, field.name), depth) | 
					
						
							| 
									
										
										
										
											2023-11-16 14:19:54 -05:00
										 |  |  |                 self.emit("return -1;", depth+1) | 
					
						
							| 
									
										
										
										
											2020-03-10 00:07:47 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-03-30 20:03:44 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-02-27 15:23:19 +00:00
										 |  |  | class ASTModuleVisitor(PickleVisitor): | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def visitModule(self, mod): | 
					
						
							| 
									
										
										
										
											2020-07-03 20:01:46 +02:00
										 |  |  |         self.emit("static int", 0) | 
					
						
							|  |  |  |         self.emit("astmodule_exec(PyObject *m)", 0) | 
					
						
							| 
									
										
										
										
											2006-02-27 15:23:19 +00:00
										 |  |  |         self.emit("{", 0) | 
					
						
							| 
									
										
										
										
											2020-11-02 22:03:28 +01:00
										 |  |  |         self.emit('struct ast_state *state = get_ast_state();', 1) | 
					
						
							|  |  |  |         self.emit('if (state == NULL) {', 1) | 
					
						
							|  |  |  |         self.emit('return -1;', 2) | 
					
						
							|  |  |  |         self.emit('}', 1) | 
					
						
							| 
									
										
										
										
											2020-11-04 16:37:07 +01:00
										 |  |  |         self.emit('if (PyModule_AddObjectRef(m, "AST", state->AST_type) < 0) {', 1) | 
					
						
							| 
									
										
										
										
											2020-07-03 20:01:46 +02:00
										 |  |  |         self.emit('return -1;', 2) | 
					
						
							| 
									
										
										
										
											2020-02-06 06:45:46 -08:00
										 |  |  |         self.emit('}', 1) | 
					
						
							|  |  |  |         self.emit('if (PyModule_AddIntMacro(m, PyCF_ALLOW_TOP_LEVEL_AWAIT) < 0) {', 1) | 
					
						
							| 
									
										
										
										
											2020-07-03 20:01:46 +02:00
										 |  |  |         self.emit("return -1;", 2) | 
					
						
							| 
									
										
										
										
											2020-02-06 06:45:46 -08:00
										 |  |  |         self.emit('}', 1) | 
					
						
							|  |  |  |         self.emit('if (PyModule_AddIntMacro(m, PyCF_ONLY_AST) < 0) {', 1) | 
					
						
							| 
									
										
										
										
											2020-07-03 20:01:46 +02:00
										 |  |  |         self.emit("return -1;", 2) | 
					
						
							| 
									
										
										
										
											2020-02-06 06:45:46 -08:00
										 |  |  |         self.emit('}', 1) | 
					
						
							|  |  |  |         self.emit('if (PyModule_AddIntMacro(m, PyCF_TYPE_COMMENTS) < 0) {', 1) | 
					
						
							| 
									
										
										
										
											2020-07-03 20:01:46 +02:00
										 |  |  |         self.emit("return -1;", 2) | 
					
						
							| 
									
										
										
										
											2020-02-06 06:45:46 -08:00
										 |  |  |         self.emit('}', 1) | 
					
						
							| 
									
										
										
										
											2023-08-21 17:31:30 +01:00
										 |  |  |         self.emit('if (PyModule_AddIntMacro(m, PyCF_OPTIMIZED_AST) < 0) {', 1) | 
					
						
							|  |  |  |         self.emit("return -1;", 2) | 
					
						
							|  |  |  |         self.emit('}', 1) | 
					
						
							| 
									
										
										
										
											2006-02-27 15:23:19 +00:00
										 |  |  |         for dfn in mod.dfns: | 
					
						
							|  |  |  |             self.visit(dfn) | 
					
						
							| 
									
										
										
										
											2020-07-03 20:01:46 +02:00
										 |  |  |         self.emit("return 0;", 1) | 
					
						
							| 
									
										
										
										
											2006-02-27 15:23:19 +00:00
										 |  |  |         self.emit("}", 0) | 
					
						
							| 
									
										
										
										
											2020-07-03 20:01:46 +02:00
										 |  |  |         self.emit("", 0) | 
					
						
							|  |  |  |         self.emit("""
 | 
					
						
							|  |  |  | static PyModuleDef_Slot astmodule_slots[] = { | 
					
						
							|  |  |  |     {Py_mod_exec, astmodule_exec}, | 
					
						
							| 
									
										
										
										
											2023-05-05 15:11:27 -06:00
										 |  |  |     {Py_mod_multiple_interpreters, Py_MOD_PER_INTERPRETER_GIL_SUPPORTED}, | 
					
						
							| 
									
										
										
										
											2024-05-03 08:30:55 -07:00
										 |  |  |     {Py_mod_gil, Py_MOD_GIL_NOT_USED}, | 
					
						
							| 
									
										
										
										
											2020-07-03 20:01:46 +02:00
										 |  |  |     {0, NULL} | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static struct PyModuleDef _astmodule = { | 
					
						
							|  |  |  |     PyModuleDef_HEAD_INIT, | 
					
						
							|  |  |  |     .m_name = "_ast", | 
					
						
							| 
									
										
										
										
											2020-11-02 22:03:28 +01:00
										 |  |  |     // The _ast module uses a per-interpreter state (PyInterpreterState.ast) | 
					
						
							| 
									
										
										
										
											2020-09-15 18:03:34 +02:00
										 |  |  |     .m_size = 0, | 
					
						
							| 
									
										
										
										
											2020-07-03 20:01:46 +02:00
										 |  |  |     .m_slots = astmodule_slots, | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | PyMODINIT_FUNC | 
					
						
							|  |  |  | PyInit__ast(void) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return PyModuleDef_Init(&_astmodule); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | """.strip(), 0, reflow=False)
 | 
					
						
							| 
									
										
										
										
											2006-02-27 15:23:19 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def visitProduct(self, prod, name): | 
					
						
							|  |  |  |         self.addObj(name) | 
					
						
							| 
									
										
										
										
											2006-02-28 18:30:36 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-02-27 15:23:19 +00:00
										 |  |  |     def visitSum(self, sum, name): | 
					
						
							|  |  |  |         self.addObj(name) | 
					
						
							|  |  |  |         for t in sum.types: | 
					
						
							|  |  |  |             self.visitConstructor(t, name) | 
					
						
							| 
									
										
										
										
											2006-02-28 18:30:36 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-02-27 15:23:19 +00:00
										 |  |  |     def visitConstructor(self, cons, name): | 
					
						
							|  |  |  |         self.addObj(cons.name) | 
					
						
							| 
									
										
										
										
											2006-02-28 18:30:36 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-02-27 15:23:19 +00:00
										 |  |  |     def addObj(self, name): | 
					
						
							| 
									
										
										
										
											2020-11-04 16:37:07 +01:00
										 |  |  |         self.emit("if (PyModule_AddObjectRef(m, \"%s\", " | 
					
						
							| 
									
										
										
										
											2020-07-03 11:35:37 +02:00
										 |  |  |                   "state->%s_type) < 0) {" % (name, name), 1) | 
					
						
							| 
									
										
										
										
											2020-07-03 20:01:46 +02:00
										 |  |  |         self.emit("return -1;", 2) | 
					
						
							| 
									
										
										
										
											2020-02-06 06:45:46 -08:00
										 |  |  |         self.emit('}', 1) | 
					
						
							| 
									
										
										
										
											2005-10-20 19:59:25 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-03-30 20:03:44 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-10-20 19:59:25 +00:00
										 |  |  | class StaticVisitor(PickleVisitor): | 
					
						
							| 
									
										
										
										
											2013-08-26 01:32:56 +03:00
										 |  |  |     CODE = '''Very simple, always emit this static code.  Override CODE''' | 
					
						
							| 
									
										
										
										
											2005-11-13 19:14:20 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def visit(self, object): | 
					
						
							|  |  |  |         self.emit(self.CODE, 0, reflow=False) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-03-30 20:03:44 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-02-26 19:42:26 +00:00
										 |  |  | class ObjVisitor(PickleVisitor): | 
					
						
							| 
									
										
										
										
											2005-10-20 19:59:25 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-02-27 15:23:19 +00:00
										 |  |  |     def func_begin(self, name): | 
					
						
							| 
									
										
										
										
											2005-10-20 19:59:25 +00:00
										 |  |  |         ctype = get_c_type(name) | 
					
						
							| 
									
										
										
										
											2006-02-26 19:42:26 +00:00
										 |  |  |         self.emit("PyObject*", 0) | 
					
						
							| 
									
										
										
										
											2023-12-25 09:36:59 -08:00
										 |  |  |         self.emit("ast2obj_%s(struct ast_state *state, struct validator *vstate, void* _o)" % (name), 0) | 
					
						
							| 
									
										
										
										
											2005-10-20 19:59:25 +00:00
										 |  |  |         self.emit("{", 0) | 
					
						
							| 
									
										
										
										
											2006-02-26 19:42:26 +00:00
										 |  |  |         self.emit("%s o = (%s)_o;" % (ctype, ctype), 1) | 
					
						
							|  |  |  |         self.emit("PyObject *result = NULL, *value = NULL;", 1) | 
					
						
							| 
									
										
										
										
											2019-09-11 10:16:34 -07:00
										 |  |  |         self.emit("PyTypeObject *tp;", 1) | 
					
						
							| 
									
										
										
										
											2006-02-26 19:42:26 +00:00
										 |  |  |         self.emit('if (!o) {', 1) | 
					
						
							| 
									
										
										
										
											2017-01-25 22:33:43 +09:00
										 |  |  |         self.emit("Py_RETURN_NONE;", 2) | 
					
						
							| 
									
										
										
										
											2006-02-26 19:42:26 +00:00
										 |  |  |         self.emit("}", 1) | 
					
						
							| 
									
										
										
										
											2023-12-25 09:36:59 -08:00
										 |  |  |         self.emit("if (++vstate->recursion_depth > vstate->recursion_limit) {", 1) | 
					
						
							| 
									
										
										
										
											2022-07-24 15:58:52 +01:00
										 |  |  |         self.emit("PyErr_SetString(PyExc_RecursionError,", 2) | 
					
						
							|  |  |  |         self.emit('"maximum recursion depth exceeded during ast construction");', 3) | 
					
						
							| 
									
										
										
										
											2023-11-16 14:19:54 -05:00
										 |  |  |         self.emit("return NULL;", 2) | 
					
						
							| 
									
										
										
										
											2022-07-24 15:58:52 +01:00
										 |  |  |         self.emit("}", 1) | 
					
						
							| 
									
										
										
										
											2005-10-20 19:59:25 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-02-27 15:23:19 +00:00
										 |  |  |     def func_end(self): | 
					
						
							| 
									
										
										
										
											2023-12-25 09:36:59 -08:00
										 |  |  |         self.emit("vstate->recursion_depth--;", 1) | 
					
						
							| 
									
										
										
										
											2006-02-26 19:42:26 +00:00
										 |  |  |         self.emit("return result;", 1) | 
					
						
							|  |  |  |         self.emit("failed:", 0) | 
					
						
							| 
									
										
										
										
											2023-12-25 09:36:59 -08:00
										 |  |  |         self.emit("vstate->recursion_depth--;", 1) | 
					
						
							| 
									
										
										
										
											2006-02-26 19:42:26 +00:00
										 |  |  |         self.emit("Py_XDECREF(value);", 1) | 
					
						
							|  |  |  |         self.emit("Py_XDECREF(result);", 1) | 
					
						
							|  |  |  |         self.emit("return NULL;", 1) | 
					
						
							| 
									
										
										
										
											2005-10-20 19:59:25 +00:00
										 |  |  |         self.emit("}", 0) | 
					
						
							|  |  |  |         self.emit("", 0) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def visitSum(self, sum, name): | 
					
						
							| 
									
										
										
										
											2006-02-26 19:42:26 +00:00
										 |  |  |         if is_simple(sum): | 
					
						
							|  |  |  |             self.simpleSum(sum, name) | 
					
						
							|  |  |  |             return | 
					
						
							| 
									
										
										
										
											2006-02-27 15:23:19 +00:00
										 |  |  |         self.func_begin(name) | 
					
						
							| 
									
										
										
										
											2006-02-26 19:42:26 +00:00
										 |  |  |         self.emit("switch (o->kind) {", 1) | 
					
						
							|  |  |  |         for i in range(len(sum.types)): | 
					
						
							|  |  |  |             t = sum.types[i] | 
					
						
							|  |  |  |             self.visitConstructor(t, i + 1, name) | 
					
						
							|  |  |  |         self.emit("}", 1) | 
					
						
							| 
									
										
										
										
											2006-02-27 15:23:19 +00:00
										 |  |  |         for a in sum.attributes: | 
					
						
							| 
									
										
										
										
											2023-12-25 09:36:59 -08:00
										 |  |  |             self.emit("value = ast2obj_%s(state, vstate, o->%s);" % (a.type, a.name), 1) | 
					
						
							| 
									
										
										
										
											2006-02-27 15:23:19 +00:00
										 |  |  |             self.emit("if (!value) goto failed;", 1) | 
					
						
							| 
									
										
										
										
											2020-07-03 11:35:37 +02:00
										 |  |  |             self.emit('if (PyObject_SetAttr(result, state->%s, value) < 0)' % a.name, 1) | 
					
						
							| 
									
										
										
										
											2006-03-02 00:31:27 +00:00
										 |  |  |             self.emit('goto failed;', 2) | 
					
						
							|  |  |  |             self.emit('Py_DECREF(value);', 1) | 
					
						
							| 
									
										
										
										
											2006-02-27 15:23:19 +00:00
										 |  |  |         self.func_end() | 
					
						
							| 
									
										
										
										
											2006-02-28 18:30:36 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-02-26 19:42:26 +00:00
										 |  |  |     def simpleSum(self, sum, name): | 
					
						
							| 
									
										
										
										
											2023-12-25 09:36:59 -08:00
										 |  |  |         self.emit("PyObject* ast2obj_%s(struct ast_state *state, struct validator *vstate, %s_ty o)" % (name, name), 0) | 
					
						
							| 
									
										
										
										
											2006-02-26 19:42:26 +00:00
										 |  |  |         self.emit("{", 0) | 
					
						
							|  |  |  |         self.emit("switch(o) {", 1) | 
					
						
							|  |  |  |         for t in sum.types: | 
					
						
							|  |  |  |             self.emit("case %s:" % t.name, 2) | 
					
						
							| 
									
										
										
										
											2022-11-10 11:23:36 +01:00
										 |  |  |             self.emit("return Py_NewRef(state->%s_singleton);" % t.name, 3) | 
					
						
							| 
									
										
										
										
											2006-02-26 19:42:26 +00:00
										 |  |  |         self.emit("}", 1) | 
					
						
							| 
									
										
										
										
											2020-05-06 17:29:32 +03:00
										 |  |  |         self.emit("Py_UNREACHABLE();", 1); | 
					
						
							| 
									
										
										
										
											2006-02-26 19:42:26 +00:00
										 |  |  |         self.emit("}", 0) | 
					
						
							| 
									
										
										
										
											2005-10-20 19:59:25 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def visitProduct(self, prod, name): | 
					
						
							| 
									
										
										
										
											2006-02-27 15:23:19 +00:00
										 |  |  |         self.func_begin(name) | 
					
						
							| 
									
										
										
										
											2020-07-03 11:35:37 +02:00
										 |  |  |         self.emit("tp = (PyTypeObject *)state->%s_type;" % name, 1) | 
					
						
							| 
									
										
										
										
											2019-09-11 10:16:34 -07:00
										 |  |  |         self.emit("result = PyType_GenericNew(tp, NULL, NULL);", 1); | 
					
						
							| 
									
										
										
										
											2006-02-26 19:42:26 +00:00
										 |  |  |         self.emit("if (!result) return NULL;", 1) | 
					
						
							| 
									
										
										
										
											2005-10-20 19:59:25 +00:00
										 |  |  |         for field in prod.fields: | 
					
						
							|  |  |  |             self.visitField(field, name, 1, True) | 
					
						
							| 
									
										
										
										
											2013-03-18 10:48:58 -07:00
										 |  |  |         for a in prod.attributes: | 
					
						
							| 
									
										
										
										
											2023-12-25 09:36:59 -08:00
										 |  |  |             self.emit("value = ast2obj_%s(state, vstate, o->%s);" % (a.type, a.name), 1) | 
					
						
							| 
									
										
										
										
											2013-03-18 10:48:58 -07:00
										 |  |  |             self.emit("if (!value) goto failed;", 1) | 
					
						
							| 
									
										
										
										
											2020-07-03 11:35:37 +02:00
										 |  |  |             self.emit("if (PyObject_SetAttr(result, state->%s, value) < 0)" % a.name, 1) | 
					
						
							| 
									
										
										
										
											2013-03-18 10:48:58 -07:00
										 |  |  |             self.emit('goto failed;', 2) | 
					
						
							|  |  |  |             self.emit('Py_DECREF(value);', 1) | 
					
						
							| 
									
										
										
										
											2006-02-27 15:23:19 +00:00
										 |  |  |         self.func_end() | 
					
						
							| 
									
										
										
										
											2005-12-25 23:18:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-10-20 19:59:25 +00:00
										 |  |  |     def visitConstructor(self, cons, enum, name): | 
					
						
							|  |  |  |         self.emit("case %s_kind:" % cons.name, 1) | 
					
						
							| 
									
										
										
										
											2020-07-03 11:35:37 +02:00
										 |  |  |         self.emit("tp = (PyTypeObject *)state->%s_type;" % cons.name, 2) | 
					
						
							| 
									
										
										
										
											2019-09-11 10:16:34 -07:00
										 |  |  |         self.emit("result = PyType_GenericNew(tp, NULL, NULL);", 2); | 
					
						
							| 
									
										
										
										
											2006-02-26 19:42:26 +00:00
										 |  |  |         self.emit("if (!result) goto failed;", 2) | 
					
						
							| 
									
										
										
										
											2005-10-20 19:59:25 +00:00
										 |  |  |         for f in cons.fields: | 
					
						
							|  |  |  |             self.visitField(f, cons.name, 2, False) | 
					
						
							|  |  |  |         self.emit("break;", 2) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def visitField(self, field, name, depth, product): | 
					
						
							|  |  |  |         def emit(s, d): | 
					
						
							|  |  |  |             self.emit(s, depth + d) | 
					
						
							|  |  |  |         if product: | 
					
						
							|  |  |  |             value = "o->%s" % field.name | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             value = "o->v.%s.%s" % (name, field.name) | 
					
						
							| 
									
										
										
										
											2006-02-26 19:42:26 +00:00
										 |  |  |         self.set(field, value, depth) | 
					
						
							|  |  |  |         emit("if (!value) goto failed;", 0) | 
					
						
							| 
									
										
										
										
											2020-07-03 11:35:37 +02:00
										 |  |  |         emit("if (PyObject_SetAttr(result, state->%s, value) == -1)" % field.name, 0) | 
					
						
							| 
									
										
										
										
											2006-02-26 19:42:26 +00:00
										 |  |  |         emit("goto failed;", 1) | 
					
						
							|  |  |  |         emit("Py_DECREF(value);", 0) | 
					
						
							| 
									
										
										
										
											2005-10-20 19:59:25 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-02-26 19:42:26 +00:00
										 |  |  |     def set(self, field, value, depth): | 
					
						
							|  |  |  |         if field.seq: | 
					
						
							| 
									
										
										
										
											2021-06-22 19:29:42 +03:00
										 |  |  |             if field.type in self.metadata.simple_sums: | 
					
						
							| 
									
										
										
										
											2006-02-26 20:51:25 +00:00
										 |  |  |                 # While the sequence elements are stored as void*, | 
					
						
							| 
									
										
										
										
											2021-06-22 19:29:42 +03:00
										 |  |  |                 # simple sums expects an enum | 
					
						
							| 
									
										
										
										
											2006-02-26 20:51:25 +00:00
										 |  |  |                 self.emit("{", depth) | 
					
						
							| 
									
										
										
										
											2012-05-15 10:10:27 -07:00
										 |  |  |                 self.emit("Py_ssize_t i, n = asdl_seq_LEN(%s);" % value, depth+1) | 
					
						
							| 
									
										
										
										
											2006-02-26 20:51:25 +00:00
										 |  |  |                 self.emit("value = PyList_New(n);", depth+1) | 
					
						
							|  |  |  |                 self.emit("if (!value) goto failed;", depth+1) | 
					
						
							|  |  |  |                 self.emit("for(i = 0; i < n; i++)", depth+1) | 
					
						
							|  |  |  |                 # This cannot fail, so no need for error handling | 
					
						
							| 
									
										
										
										
											2021-06-22 19:29:42 +03:00
										 |  |  |                 self.emit( | 
					
						
							| 
									
										
										
										
											2023-12-25 09:36:59 -08:00
										 |  |  |                     "PyList_SET_ITEM(value, i, ast2obj_{0}(state, vstate, ({0}_ty)asdl_seq_GET({1}, i)));".format( | 
					
						
							| 
									
										
										
										
											2021-06-22 19:29:42 +03:00
										 |  |  |                         field.type, | 
					
						
							|  |  |  |                         value | 
					
						
							|  |  |  |                     ), | 
					
						
							|  |  |  |                     depth + 2, | 
					
						
							|  |  |  |                     reflow=False, | 
					
						
							|  |  |  |                 ) | 
					
						
							| 
									
										
										
										
											2006-02-26 20:51:25 +00:00
										 |  |  |                 self.emit("}", depth) | 
					
						
							| 
									
										
										
										
											2006-02-26 19:42:26 +00:00
										 |  |  |             else: | 
					
						
							| 
									
										
										
										
											2023-12-25 09:36:59 -08:00
										 |  |  |                 self.emit("value = ast2obj_list(state, vstate, (asdl_seq*)%s, ast2obj_%s);" % (value, field.type), depth) | 
					
						
							| 
									
										
										
										
											2005-10-20 19:59:25 +00:00
										 |  |  |         else: | 
					
						
							| 
									
										
										
										
											2023-12-25 09:36:59 -08:00
										 |  |  |             self.emit("value = ast2obj_%s(state, vstate, %s);" % (field.type, value), depth, reflow=False) | 
					
						
							| 
									
										
										
										
											2005-12-25 23:18:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-10-20 19:59:25 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-02-26 19:42:26 +00:00
										 |  |  | class PartingShots(StaticVisitor): | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     CODE = """
 | 
					
						
							|  |  |  | PyObject* PyAST_mod2obj(mod_ty t) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2020-11-02 22:03:28 +01:00
										 |  |  |     struct ast_state *state = get_ast_state(); | 
					
						
							| 
									
										
										
										
											2020-07-03 20:01:46 +02:00
										 |  |  |     if (state == NULL) { | 
					
						
							| 
									
										
										
										
											2013-07-17 00:17:15 +02:00
										 |  |  |         return NULL; | 
					
						
							| 
									
										
										
										
											2020-07-03 14:15:53 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-07-24 15:58:52 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     int starting_recursion_depth; | 
					
						
							|  |  |  |     /* Be careful here to prevent overflow. */ | 
					
						
							|  |  |  |     PyThreadState *tstate = _PyThreadState_GET(); | 
					
						
							|  |  |  |     if (!tstate) { | 
					
						
							| 
									
										
										
										
											2023-11-16 14:19:54 -05:00
										 |  |  |         return NULL; | 
					
						
							| 
									
										
										
										
											2022-07-24 15:58:52 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2023-12-25 09:36:59 -08:00
										 |  |  |     struct validator vstate; | 
					
						
							| 
									
										
										
										
											2024-01-16 09:32:01 +00:00
										 |  |  |     vstate.recursion_limit = Py_C_RECURSION_LIMIT; | 
					
						
							| 
									
										
										
										
											2023-09-08 11:48:28 +02:00
										 |  |  |     int recursion_depth = Py_C_RECURSION_LIMIT - tstate->c_recursion_remaining; | 
					
						
							| 
									
										
										
										
											2024-01-16 09:32:01 +00:00
										 |  |  |     starting_recursion_depth = recursion_depth; | 
					
						
							| 
									
										
										
										
											2023-12-25 09:36:59 -08:00
										 |  |  |     vstate.recursion_depth = starting_recursion_depth; | 
					
						
							| 
									
										
										
										
											2022-07-24 15:58:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-12-25 09:36:59 -08:00
										 |  |  |     PyObject *result = ast2obj_mod(state, &vstate, t); | 
					
						
							| 
									
										
										
										
											2022-07-24 15:58:52 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /* Check that the recursion depth counting balanced correctly */ | 
					
						
							| 
									
										
										
										
											2023-12-25 09:36:59 -08:00
										 |  |  |     if (result && vstate.recursion_depth != starting_recursion_depth) { | 
					
						
							| 
									
										
										
										
											2022-07-24 15:58:52 +01:00
										 |  |  |         PyErr_Format(PyExc_SystemError, | 
					
						
							|  |  |  |             "AST constructor recursion depth mismatch (before=%d, after=%d)", | 
					
						
							| 
									
										
										
										
											2023-12-25 09:36:59 -08:00
										 |  |  |             starting_recursion_depth, vstate.recursion_depth); | 
					
						
							| 
									
										
										
										
											2023-11-16 14:19:54 -05:00
										 |  |  |         return NULL; | 
					
						
							| 
									
										
										
										
											2022-07-24 15:58:52 +01:00
										 |  |  |     } | 
					
						
							|  |  |  |     return result; | 
					
						
							| 
									
										
										
										
											2006-02-26 19:42:26 +00:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2008-03-30 20:03:44 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-03-31 04:20:05 +00:00
										 |  |  | /* mode is 0 for "exec", 1 for "eval" and 2 for "single" input */ | 
					
						
							|  |  |  | mod_ty PyAST_obj2mod(PyObject* ast, PyArena* arena, int mode) | 
					
						
							| 
									
										
										
										
											2008-03-30 20:03:44 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2019-09-10 13:02:30 +03:00
										 |  |  |     const char * const req_name[] = {"Module", "Expression", "Interactive"}; | 
					
						
							| 
									
										
										
										
											2009-12-13 01:23:39 +00:00
										 |  |  |     int isinstance; | 
					
						
							| 
									
										
										
										
											2014-02-10 22:19:02 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-23 08:45:22 -07:00
										 |  |  |     if (PySys_Audit("compile", "OO", ast, Py_None) < 0) { | 
					
						
							|  |  |  |         return NULL; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-02 22:03:28 +01:00
										 |  |  |     struct ast_state *state = get_ast_state(); | 
					
						
							|  |  |  |     if (state == NULL) { | 
					
						
							|  |  |  |         return NULL; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-03 11:35:37 +02:00
										 |  |  |     PyObject *req_type[3]; | 
					
						
							|  |  |  |     req_type[0] = state->Module_type; | 
					
						
							|  |  |  |     req_type[1] = state->Expression_type; | 
					
						
							|  |  |  |     req_type[2] = state->Interactive_type; | 
					
						
							| 
									
										
										
										
											2014-02-10 22:19:02 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-01 11:37:34 -08:00
										 |  |  |     assert(0 <= mode && mode <= 2); | 
					
						
							| 
									
										
										
										
											2008-03-31 04:20:05 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-12-13 01:23:39 +00:00
										 |  |  |     isinstance = PyObject_IsInstance(ast, req_type[mode]); | 
					
						
							|  |  |  |     if (isinstance == -1) | 
					
						
							|  |  |  |         return NULL; | 
					
						
							|  |  |  |     if (!isinstance) { | 
					
						
							| 
									
										
										
										
											2008-03-31 04:20:05 +00:00
										 |  |  |         PyErr_Format(PyExc_TypeError, "expected %s node, got %.400s", | 
					
						
							| 
									
										
										
										
											2019-09-11 10:16:34 -07:00
										 |  |  |                      req_name[mode], _PyType_Name(Py_TYPE(ast))); | 
					
						
							| 
									
										
										
										
											2008-03-30 20:03:44 +00:00
										 |  |  |         return NULL; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-10-10 16:41:26 +09:00
										 |  |  | 
 | 
					
						
							|  |  |  |     mod_ty res = NULL; | 
					
						
							| 
									
										
										
										
											2020-07-03 11:35:37 +02:00
										 |  |  |     if (obj2ast_mod(state, ast, &res, arena) != 0) | 
					
						
							| 
									
										
										
										
											2008-03-30 20:03:44 +00:00
										 |  |  |         return NULL; | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  |         return res; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int PyAST_Check(PyObject* obj) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2020-11-02 22:03:28 +01:00
										 |  |  |     struct ast_state *state = get_ast_state(); | 
					
						
							| 
									
										
										
										
											2020-07-03 20:01:46 +02:00
										 |  |  |     if (state == NULL) { | 
					
						
							| 
									
										
										
										
											2013-07-17 00:17:15 +02:00
										 |  |  |         return -1; | 
					
						
							| 
									
										
										
										
											2020-07-03 14:15:53 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-07-03 11:35:37 +02:00
										 |  |  |     return PyObject_IsInstance(obj, state->AST_type); | 
					
						
							| 
									
										
										
										
											2008-03-30 20:03:44 +00:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2006-02-26 19:42:26 +00:00
										 |  |  | """
 | 
					
						
							| 
									
										
										
										
											2005-10-20 19:59:25 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | class ChainOfVisitors: | 
					
						
							| 
									
										
										
										
											2021-06-22 19:29:42 +03:00
										 |  |  |     def __init__(self, *visitors, metadata = None): | 
					
						
							| 
									
										
										
										
											2005-10-20 19:59:25 +00:00
										 |  |  |         self.visitors = visitors | 
					
						
							| 
									
										
										
										
											2021-06-22 19:29:42 +03:00
										 |  |  |         self.metadata = metadata | 
					
						
							| 
									
										
										
										
											2005-10-20 19:59:25 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def visit(self, object): | 
					
						
							|  |  |  |         for v in self.visitors: | 
					
						
							| 
									
										
										
										
											2021-06-22 19:29:42 +03:00
										 |  |  |             v.metadata = self.metadata | 
					
						
							| 
									
										
										
										
											2005-10-20 19:59:25 +00:00
										 |  |  |             v.visit(object) | 
					
						
							| 
									
										
										
										
											2005-11-13 19:14:20 +00:00
										 |  |  |             v.emit("", 0) | 
					
						
							| 
									
										
										
										
											2005-10-20 19:59:25 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-11 10:16:34 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-02 22:03:28 +01:00
										 |  |  | def generate_ast_state(module_state, f): | 
					
						
							|  |  |  |     f.write('struct ast_state {\n') | 
					
						
							| 
									
										
										
										
											2023-11-16 14:19:54 -05:00
										 |  |  |     f.write('    _PyOnceFlag once;\n') | 
					
						
							|  |  |  |     f.write('    int finalized;\n') | 
					
						
							| 
									
										
										
										
											2020-11-02 22:03:28 +01:00
										 |  |  |     for s in module_state: | 
					
						
							|  |  |  |         f.write('    PyObject *' + s + ';\n') | 
					
						
							|  |  |  |     f.write('};') | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def generate_ast_fini(module_state, f): | 
					
						
							| 
									
										
										
										
											2020-11-03 18:07:15 +01:00
										 |  |  |     f.write(textwrap.dedent("""
 | 
					
						
							|  |  |  |             void _PyAST_Fini(PyInterpreterState *interp) | 
					
						
							|  |  |  |             { | 
					
						
							|  |  |  |                 struct ast_state *state = &interp->ast; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     """))
 | 
					
						
							| 
									
										
										
										
											2020-11-02 22:03:28 +01:00
										 |  |  |     for s in module_state: | 
					
						
							|  |  |  |         f.write("    Py_CLEAR(state->" + s + ');\n') | 
					
						
							| 
									
										
										
										
											2020-11-03 18:07:15 +01:00
										 |  |  |     f.write(textwrap.dedent("""
 | 
					
						
							| 
									
										
										
										
											2023-11-16 14:19:54 -05:00
										 |  |  |                 state->finalized = 1; | 
					
						
							|  |  |  |                 state->once = (_PyOnceFlag){0}; | 
					
						
							| 
									
										
										
										
											2020-11-03 18:07:15 +01:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2020-11-02 22:03:28 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-03 18:07:15 +01:00
										 |  |  |     """))
 | 
					
						
							| 
									
										
										
										
											2020-11-02 22:03:28 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-24 15:16:00 +03:00
										 |  |  | def generate_module_def(mod, metadata, f, internal_h): | 
					
						
							| 
									
										
										
										
											2019-09-11 10:16:34 -07:00
										 |  |  |     # Gather all the data needed for ModuleSpec | 
					
						
							| 
									
										
										
										
											2020-03-16 11:12:53 +03:00
										 |  |  |     state_strings = { | 
					
						
							| 
									
										
										
										
											2020-03-22 20:33:34 +02:00
										 |  |  |         "ast", | 
					
						
							| 
									
										
										
										
											2020-03-16 11:12:53 +03:00
										 |  |  |         "_fields", | 
					
						
							| 
									
										
										
										
											2021-02-26 14:51:55 -08:00
										 |  |  |         "__match_args__", | 
					
						
							| 
									
										
										
										
											2020-03-16 11:12:53 +03:00
										 |  |  |         "__doc__", | 
					
						
							|  |  |  |         "__dict__", | 
					
						
							|  |  |  |         "__module__", | 
					
						
							|  |  |  |         "_attributes", | 
					
						
							| 
									
										
										
										
											2021-06-24 15:16:00 +03:00
										 |  |  |         *metadata.identifiers | 
					
						
							| 
									
										
										
										
											2020-03-16 11:12:53 +03:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-06-24 15:16:00 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-16 11:12:53 +03:00
										 |  |  |     module_state = state_strings.copy() | 
					
						
							| 
									
										
										
										
											2021-06-24 15:16:00 +03:00
										 |  |  |     module_state.update( | 
					
						
							|  |  |  |         "%s_singleton" % singleton | 
					
						
							|  |  |  |         for singleton in metadata.singletons | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  |     module_state.update( | 
					
						
							|  |  |  |         "%s_type" % type | 
					
						
							|  |  |  |         for type in metadata.types | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-11 10:16:34 -07:00
										 |  |  |     state_strings = sorted(state_strings) | 
					
						
							|  |  |  |     module_state = sorted(module_state) | 
					
						
							| 
									
										
										
										
											2020-11-02 22:03:28 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     generate_ast_state(module_state, internal_h) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-07 13:01:09 +02:00
										 |  |  |     print(textwrap.dedent("""
 | 
					
						
							|  |  |  |         #include "Python.h" | 
					
						
							|  |  |  |         #include "pycore_ast.h" | 
					
						
							| 
									
										
										
										
											2022-05-04 13:30:23 +02:00
										 |  |  |         #include "pycore_ast_state.h"     // struct ast_state | 
					
						
							|  |  |  |         #include "pycore_ceval.h"         // _Py_EnterRecursiveCall | 
					
						
							| 
									
										
										
										
											2023-11-16 14:19:54 -05:00
										 |  |  |         #include "pycore_lock.h"          // _PyOnceFlag | 
					
						
							| 
									
										
										
										
											2022-05-04 13:30:23 +02:00
										 |  |  |         #include "pycore_interp.h"        // _PyInterpreterState.ast | 
					
						
							| 
									
										
										
										
											2024-07-04 05:10:54 +02:00
										 |  |  |         #include "pycore_modsupport.h"    // _PyArg_NoPositional() | 
					
						
							| 
									
										
										
										
											2022-05-04 13:30:23 +02:00
										 |  |  |         #include "pycore_pystate.h"       // _PyInterpreterState_GET() | 
					
						
							| 
									
										
										
										
											2024-07-04 05:10:54 +02:00
										 |  |  |         #include "pycore_setobject.h"     // _PySet_NextEntry(), _PySet_Update() | 
					
						
							| 
									
										
										
										
											2024-02-27 18:13:03 -08:00
										 |  |  |         #include "pycore_unionobject.h"   // _Py_union_type_or | 
					
						
							|  |  |  |         #include "structmember.h" | 
					
						
							| 
									
										
										
										
											2021-04-07 13:01:09 +02:00
										 |  |  |         #include <stddef.h> | 
					
						
							| 
									
										
										
										
											2020-09-15 18:03:34 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-12-25 09:36:59 -08:00
										 |  |  |         struct validator { | 
					
						
							|  |  |  |             int recursion_depth;            /* current recursion depth */ | 
					
						
							|  |  |  |             int recursion_limit;            /* recursion limit */ | 
					
						
							|  |  |  |         }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-07 13:01:09 +02:00
										 |  |  |         // Forward declaration | 
					
						
							| 
									
										
										
										
											2024-10-04 11:59:08 +02:00
										 |  |  |         static int init_types(void *arg); | 
					
						
							| 
									
										
										
										
											2019-09-11 10:16:34 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-07 13:01:09 +02:00
										 |  |  |         static struct ast_state* | 
					
						
							|  |  |  |         get_ast_state(void) | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             PyInterpreterState *interp = _PyInterpreterState_GET(); | 
					
						
							|  |  |  |             struct ast_state *state = &interp->ast; | 
					
						
							| 
									
										
										
										
											2023-11-16 14:19:54 -05:00
										 |  |  |             assert(!state->finalized); | 
					
						
							|  |  |  |             if (_PyOnceFlag_CallOnce(&state->once, (_Py_once_fn_t *)&init_types, state) < 0) { | 
					
						
							| 
									
										
										
										
											2021-04-07 13:01:09 +02:00
										 |  |  |                 return NULL; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             return state; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     """).strip(), file=f)
 | 
					
						
							| 
									
										
										
										
											2020-11-02 22:03:28 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     generate_ast_fini(module_state, f) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     f.write('static int init_identifiers(struct ast_state *state)\n') | 
					
						
							| 
									
										
										
										
											2019-09-11 10:16:34 -07:00
										 |  |  |     f.write('{\n') | 
					
						
							|  |  |  |     for identifier in state_strings: | 
					
						
							|  |  |  |         f.write('    if ((state->' + identifier) | 
					
						
							|  |  |  |         f.write(' = PyUnicode_InternFromString("') | 
					
						
							| 
									
										
										
										
											2023-11-16 14:19:54 -05:00
										 |  |  |         f.write(identifier + '")) == NULL) return -1;\n') | 
					
						
							|  |  |  |     f.write('    return 0;\n') | 
					
						
							| 
									
										
										
										
											2019-09-11 10:16:34 -07:00
										 |  |  |     f.write('};\n\n') | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-22 19:29:42 +03:00
										 |  |  | def write_header(mod, metadata, f): | 
					
						
							| 
									
										
										
										
											2021-03-23 20:47:40 +01:00
										 |  |  |     f.write(textwrap.dedent("""
 | 
					
						
							|  |  |  |         #ifndef Py_INTERNAL_AST_H | 
					
						
							|  |  |  |         #define Py_INTERNAL_AST_H | 
					
						
							|  |  |  |         #ifdef __cplusplus | 
					
						
							|  |  |  |         extern "C" { | 
					
						
							|  |  |  |         #endif | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         #ifndef Py_BUILD_CORE | 
					
						
							|  |  |  |         #  error "this header requires Py_BUILD_CORE define" | 
					
						
							|  |  |  |         #endif | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-21 20:05:59 +02:00
										 |  |  |         #include "pycore_asdl.h"          // _ASDL_SEQ_HEAD | 
					
						
							| 
									
										
										
										
											2021-03-23 20:47:40 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     """).lstrip())
 | 
					
						
							| 
									
										
										
										
											2021-06-22 19:29:42 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  |     c = ChainOfVisitors( | 
					
						
							|  |  |  |         TypeDefVisitor(f), | 
					
						
							|  |  |  |         SequenceDefVisitor(f), | 
					
						
							|  |  |  |         StructVisitor(f), | 
					
						
							|  |  |  |         metadata=metadata | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2020-05-18 20:42:10 +03:00
										 |  |  |     c.visit(mod) | 
					
						
							| 
									
										
										
										
											2021-06-22 19:29:42 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-18 20:42:10 +03:00
										 |  |  |     f.write("// Note: these macros affect function definitions, not only call sites.\n") | 
					
						
							| 
									
										
										
										
											2021-06-22 19:29:42 +03:00
										 |  |  |     prototype_visitor = PrototypeVisitor(f, metadata=metadata) | 
					
						
							|  |  |  |     prototype_visitor.visit(mod) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-23 20:47:40 +01:00
										 |  |  |     f.write(textwrap.dedent("""
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         PyObject* PyAST_mod2obj(mod_ty t); | 
					
						
							|  |  |  |         mod_ty PyAST_obj2mod(PyObject* ast, PyArena* arena, int mode); | 
					
						
							|  |  |  |         int PyAST_Check(PyObject* obj); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         extern int _PyAST_Validate(mod_ty); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         /* _PyAST_ExprAsUnicode is defined in ast_unparse.c */ | 
					
						
							|  |  |  |         extern PyObject* _PyAST_ExprAsUnicode(expr_ty); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         /* Return the borrowed reference to the first literal string in the | 
					
						
							|  |  |  |            sequence of statements or NULL if it doesn't start from a literal string. | 
					
						
							|  |  |  |            Doesn't set exception. */ | 
					
						
							|  |  |  |         extern PyObject* _PyAST_GetDocString(asdl_stmt_seq *); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         #ifdef __cplusplus | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         #endif | 
					
						
							|  |  |  |         #endif /* !Py_INTERNAL_AST_H */ | 
					
						
							|  |  |  |     """))
 | 
					
						
							| 
									
										
										
										
											2020-05-18 20:42:10 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-02 22:03:28 +01:00
										 |  |  | def write_internal_h_header(mod, f): | 
					
						
							|  |  |  |     print(textwrap.dedent("""
 | 
					
						
							| 
									
										
										
										
											2021-03-17 23:11:03 +01:00
										 |  |  |         #ifndef Py_INTERNAL_AST_STATE_H | 
					
						
							|  |  |  |         #define Py_INTERNAL_AST_STATE_H | 
					
						
							| 
									
										
										
										
											2023-11-16 14:19:54 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |         #include "pycore_lock.h"    // _PyOnceFlag | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-02 22:03:28 +01:00
										 |  |  |         #ifdef __cplusplus | 
					
						
							|  |  |  |         extern "C" { | 
					
						
							|  |  |  |         #endif | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         #ifndef Py_BUILD_CORE | 
					
						
							|  |  |  |         #  error "this header requires Py_BUILD_CORE define" | 
					
						
							|  |  |  |         #endif | 
					
						
							|  |  |  |     """).lstrip(), file=f)
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def write_internal_h_footer(mod, f): | 
					
						
							|  |  |  |     print(textwrap.dedent("""
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         #ifdef __cplusplus | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         #endif | 
					
						
							| 
									
										
										
										
											2021-03-17 23:11:03 +01:00
										 |  |  |         #endif /* !Py_INTERNAL_AST_STATE_H */ | 
					
						
							| 
									
										
										
										
											2020-11-02 22:03:28 +01:00
										 |  |  |     """), file=f)
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-22 19:29:42 +03:00
										 |  |  | def write_source(mod, metadata, f, internal_h_file): | 
					
						
							| 
									
										
										
										
											2021-06-24 15:16:00 +03:00
										 |  |  |     generate_module_def(mod, metadata, f, internal_h_file) | 
					
						
							| 
									
										
										
										
											2020-05-18 20:42:10 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  |     v = ChainOfVisitors( | 
					
						
							| 
									
										
										
										
											2020-09-16 19:42:00 +01:00
										 |  |  |         SequenceConstructorVisitor(f), | 
					
						
							| 
									
										
										
										
											2020-05-18 20:42:10 +03:00
										 |  |  |         PyTypesDeclareVisitor(f), | 
					
						
							| 
									
										
										
										
											2024-02-27 18:13:03 -08:00
										 |  |  |         AnnotationsVisitor(f), | 
					
						
							| 
									
										
										
										
											2020-05-18 20:42:10 +03:00
										 |  |  |         PyTypesVisitor(f), | 
					
						
							|  |  |  |         Obj2ModPrototypeVisitor(f), | 
					
						
							|  |  |  |         FunctionVisitor(f), | 
					
						
							|  |  |  |         ObjVisitor(f), | 
					
						
							|  |  |  |         Obj2ModVisitor(f), | 
					
						
							|  |  |  |         ASTModuleVisitor(f), | 
					
						
							|  |  |  |         PartingShots(f), | 
					
						
							| 
									
										
										
										
											2021-06-22 19:29:42 +03:00
										 |  |  |         metadata=metadata | 
					
						
							| 
									
										
										
										
											2020-05-18 20:42:10 +03:00
										 |  |  |     ) | 
					
						
							|  |  |  |     v.visit(mod) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-02 22:03:28 +01:00
										 |  |  | def main(input_filename, c_filename, h_filename, internal_h_filename, dump_module=False): | 
					
						
							| 
									
										
										
										
											2020-05-18 20:42:10 +03:00
										 |  |  |     auto_gen_msg = AUTOGEN_MESSAGE.format("/".join(Path(__file__).parts[-2:])) | 
					
						
							| 
									
										
										
										
											2020-11-02 22:03:28 +01:00
										 |  |  |     mod = asdl.parse(input_filename) | 
					
						
							| 
									
										
										
										
											2014-05-09 17:58:22 -07:00
										 |  |  |     if dump_module: | 
					
						
							|  |  |  |         print('Parsed Module:') | 
					
						
							|  |  |  |         print(mod) | 
					
						
							| 
									
										
										
										
											2005-10-20 19:59:25 +00:00
										 |  |  |     if not asdl.check(mod): | 
					
						
							|  |  |  |         sys.exit(1) | 
					
						
							| 
									
										
										
										
											2020-11-02 22:03:28 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-22 19:29:42 +03:00
										 |  |  |     metadata_visitor = MetadataVisitor() | 
					
						
							|  |  |  |     metadata_visitor.visit(mod) | 
					
						
							|  |  |  |     metadata = metadata_visitor.metadata | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-02 22:03:28 +01:00
										 |  |  |     with c_filename.open("w") as c_file, \ | 
					
						
							|  |  |  |          h_filename.open("w") as h_file, \ | 
					
						
							|  |  |  |          internal_h_filename.open("w") as internal_h_file: | 
					
						
							|  |  |  |         c_file.write(auto_gen_msg) | 
					
						
							|  |  |  |         h_file.write(auto_gen_msg) | 
					
						
							|  |  |  |         internal_h_file.write(auto_gen_msg) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         write_internal_h_header(mod, internal_h_file) | 
					
						
							| 
									
										
										
										
											2021-06-22 19:29:42 +03:00
										 |  |  |         write_source(mod, metadata, c_file, internal_h_file) | 
					
						
							|  |  |  |         write_header(mod, metadata, h_file) | 
					
						
							| 
									
										
										
										
											2020-11-02 22:03:28 +01:00
										 |  |  |         write_internal_h_footer(mod, internal_h_file) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     print(f"{c_filename}, {h_filename}, {internal_h_filename} regenerated.") | 
					
						
							| 
									
										
										
										
											2005-10-20 19:59:25 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | if __name__ == "__main__": | 
					
						
							| 
									
										
										
										
											2020-05-18 20:42:10 +03:00
										 |  |  |     parser = ArgumentParser() | 
					
						
							|  |  |  |     parser.add_argument("input_file", type=Path) | 
					
						
							| 
									
										
										
										
											2020-11-02 22:03:28 +01:00
										 |  |  |     parser.add_argument("-C", "--c-file", type=Path, required=True) | 
					
						
							|  |  |  |     parser.add_argument("-H", "--h-file", type=Path, required=True) | 
					
						
							|  |  |  |     parser.add_argument("-I", "--internal-h-file", type=Path, required=True) | 
					
						
							| 
									
										
										
										
											2020-05-18 20:42:10 +03:00
										 |  |  |     parser.add_argument("-d", "--dump-module", action="store_true") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-02 22:03:28 +01:00
										 |  |  |     args = parser.parse_args() | 
					
						
							|  |  |  |     main(args.input_file, args.c_file, args.h_file, | 
					
						
							|  |  |  |          args.internal_h_file, args.dump_module) |