Mercurial > cpython-lru_cache-weakref
annotate Parser/asdl_c.py @ 54294:dba767bc3155 legacy-trunk
remove mention of old ctypes version
| author | Benjamin Peterson <benjamin@python.org> |
|---|---|
| date | Sat, 23 May 2009 20:59:09 +0000 |
| parents | 2e38ca709b1d |
| children | d79fde1041bd |
| rev | line source |
|---|---|
| 35742 | 1 #! /usr/bin/env python |
| 2 """Generate C code from an ASDL description.""" | |
| 3 | |
| 4 # TO DO | |
| 5 # handle fields that have a type but no name | |
| 6 | |
|
45468
1d84bf5f9478
Patch #2167 from calvin: Remove unused imports
Christian Heimes <christian@cheimes.de>
parents:
40936
diff
changeset
|
7 import os, sys |
| 35742 | 8 |
| 9 import asdl | |
| 10 | |
| 11 TABSIZE = 8 | |
| 12 MAX_COL = 80 | |
| 13 | |
| 14 def get_c_type(name): | |
| 15 """Return a string for the C name of the type. | |
| 16 | |
| 17 This function special cases the default types provided by asdl: | |
| 18 identifier, string, int, bool. | |
| 19 """ | |
| 20 # XXX ack! need to figure out where Id is useful and where string | |
| 21 if isinstance(name, asdl.Id): | |
| 22 name = name.value | |
| 23 if name in asdl.builtin_types: | |
| 24 return name | |
| 25 else: | |
| 26 return "%s_ty" % name | |
| 27 | |
| 28 def reflow_lines(s, depth): | |
| 29 """Reflow the line s indented depth tabs. | |
| 30 | |
| 31 Return a sequence of lines where no line extends beyond MAX_COL | |
| 32 when properly indented. The first line is properly indented based | |
| 33 exclusively on depth * TABSIZE. All following lines -- these are | |
| 34 the reflowed lines generated by this function -- start at the same | |
| 35 column as the first character beyond the opening { in the first | |
| 36 line. | |
| 37 """ | |
| 38 size = MAX_COL - depth * TABSIZE | |
| 39 if len(s) < size: | |
| 40 return [s] | |
| 41 | |
| 42 lines = [] | |
| 43 cur = s | |
| 44 padding = "" | |
| 45 while len(cur) > size: | |
| 46 i = cur.rfind(' ', 0, size) | |
| 47 # XXX this should be fixed for real | |
| 48 if i == -1 and 'GeneratorExp' in cur: | |
| 49 i = size + 3 | |
|
40936
0376f13a1af9
Fix a couple of problems in generating the AST code:
Neal Norwitz <nnorwitz@gmail.com>
parents:
40824
diff
changeset
|
50 assert i != -1, "Impossible line %d to reflow: %r" % (size, s) |
| 35742 | 51 lines.append(padding + cur[:i]) |
| 52 if len(lines) == 1: | |
| 53 # find new size based on brace | |
| 54 j = cur.find('{', 0, i) | |
| 55 if j >= 0: | |
| 56 j += 2 # account for the brace and the space after it | |
| 57 size -= j | |
| 58 padding = " " * j | |
| 59 else: | |
| 60 j = cur.find('(', 0, i) | |
| 61 if j >= 0: | |
| 62 j += 1 # account for the paren (no space after it) | |
| 63 size -= j | |
| 64 padding = " " * j | |
| 65 cur = cur[i+1:] | |
| 66 else: | |
| 67 lines.append(padding + cur) | |
| 68 return lines | |
| 69 | |
| 70 def is_simple(sum): | |
| 71 """Return True if a sum is a simple. | |
| 72 | |
| 73 A sum is simple if its types have no fields, e.g. | |
| 74 unaryop = Invert | Not | UAdd | USub | |
| 75 """ | |
| 76 for t in sum.types: | |
| 77 if t.fields: | |
| 78 return False | |
| 79 return True | |
| 80 | |
|
46162
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
81 |
| 35742 | 82 class EmitVisitor(asdl.VisitorBase): |
| 83 """Visit that emits lines""" | |
| 84 | |
| 85 def __init__(self, file): | |
| 86 self.file = file | |
| 87 super(EmitVisitor, self).__init__() | |
| 88 | |
| 89 def emit(self, s, depth, reflow=1): | |
| 90 # XXX reflow long lines? | |
| 91 if reflow: | |
| 92 lines = reflow_lines(s, depth) | |
| 93 else: | |
| 94 lines = [s] | |
| 95 for line in lines: | |
| 96 line = (" " * TABSIZE * depth) + line + "\n" | |
| 97 self.file.write(line) | |
| 98 | |
|
46162
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
99 |
| 35742 | 100 class TypeDefVisitor(EmitVisitor): |
| 101 def visitModule(self, mod): | |
| 102 for dfn in mod.dfns: | |
| 103 self.visit(dfn) | |
| 104 | |
| 105 def visitType(self, type, depth=0): | |
| 106 self.visit(type.value, type.name, depth) | |
| 107 | |
| 108 def visitSum(self, sum, name, depth): | |
| 109 if is_simple(sum): | |
| 110 self.simple_sum(sum, name, depth) | |
| 111 else: | |
| 112 self.sum_with_constructors(sum, name, depth) | |
| 113 | |
| 114 def simple_sum(self, sum, name, depth): | |
| 115 enum = [] | |
| 116 for i in range(len(sum.types)): | |
| 117 type = sum.types[i] | |
| 118 enum.append("%s=%d" % (type.name, i + 1)) | |
| 119 enums = ", ".join(enum) | |
| 120 ctype = get_c_type(name) | |
| 121 s = "typedef enum _%s { %s } %s;" % (name, enums, ctype) | |
| 122 self.emit(s, depth) | |
| 123 self.emit("", depth) | |
| 124 | |
| 125 def sum_with_constructors(self, sum, name, depth): | |
| 126 ctype = get_c_type(name) | |
| 127 s = "typedef struct _%(name)s *%(ctype)s;" % locals() | |
| 128 self.emit(s, depth) | |
| 129 self.emit("", depth) | |
| 130 | |
| 131 def visitProduct(self, product, name, depth): | |
| 132 ctype = get_c_type(name) | |
| 133 s = "typedef struct _%(name)s *%(ctype)s;" % locals() | |
| 134 self.emit(s, depth) | |
| 135 self.emit("", depth) | |
| 136 | |
|
46162
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
137 |
| 35742 | 138 class StructVisitor(EmitVisitor): |
| 139 """Visitor to generate typdefs for AST.""" | |
| 140 | |
| 141 def visitModule(self, mod): | |
| 142 for dfn in mod.dfns: | |
| 143 self.visit(dfn) | |
| 144 | |
| 145 def visitType(self, type, depth=0): | |
| 146 self.visit(type.value, type.name, depth) | |
| 147 | |
| 148 def visitSum(self, sum, name, depth): | |
| 149 if not is_simple(sum): | |
| 150 self.sum_with_constructors(sum, name, depth) | |
| 151 | |
| 152 def sum_with_constructors(self, sum, name, depth): | |
| 153 def emit(s, depth=depth): | |
| 154 self.emit(s % sys._getframe(1).f_locals, depth) | |
| 155 enum = [] | |
| 156 for i in range(len(sum.types)): | |
| 157 type = sum.types[i] | |
| 158 enum.append("%s_kind=%d" % (type.name, i + 1)) | |
| 159 | |
|
37599
44fb03a6a524
Make _kind types global for C++ compilation.
Martin v. Löwis <martin@v.loewis.de>
parents:
37462
diff
changeset
|
160 emit("enum _%(name)s_kind {" + ", ".join(enum) + "};") |
|
44fb03a6a524
Make _kind types global for C++ compilation.
Martin v. Löwis <martin@v.loewis.de>
parents:
37462
diff
changeset
|
161 |
| 35742 | 162 emit("struct _%(name)s {") |
|
37599
44fb03a6a524
Make _kind types global for C++ compilation.
Martin v. Löwis <martin@v.loewis.de>
parents:
37462
diff
changeset
|
163 emit("enum _%(name)s_kind kind;", depth + 1) |
| 35742 | 164 emit("union {", depth + 1) |
| 165 for t in sum.types: | |
| 166 self.visit(t, depth + 2) | |
| 167 emit("} v;", depth + 1) | |
| 168 for field in sum.attributes: | |
| 169 # rudimentary attribute handling | |
| 170 type = str(field.type) | |
| 171 assert type in asdl.builtin_types, type | |
| 172 emit("%s %s;" % (type, field.name), depth + 1); | |
| 173 emit("};") | |
| 174 emit("") | |
| 175 | |
| 176 def visitConstructor(self, cons, depth): | |
| 177 if cons.fields: | |
| 178 self.emit("struct {", depth) | |
| 179 for f in cons.fields: | |
| 180 self.visit(f, depth + 1) | |
| 181 self.emit("} %s;" % cons.name, depth) | |
| 182 self.emit("", depth) | |
| 183 else: | |
| 184 # XXX not sure what I want here, nothing is probably fine | |
| 185 pass | |
| 186 | |
| 187 def visitField(self, field, depth): | |
| 188 # XXX need to lookup field.type, because it might be something | |
| 189 # like a builtin... | |
| 190 ctype = get_c_type(field.type) | |
| 191 name = field.name | |
| 192 if field.seq: | |
|
37685
5e72b0c74100
Introduce asdl_int_seq, to hold cmpop_ty.
Martin v. Löwis <martin@v.loewis.de>
parents:
37599
diff
changeset
|
193 if field.type.value in ('cmpop',): |
|
5e72b0c74100
Introduce asdl_int_seq, to hold cmpop_ty.
Martin v. Löwis <martin@v.loewis.de>
parents:
37599
diff
changeset
|
194 self.emit("asdl_int_seq *%(name)s;" % locals(), depth) |
|
5e72b0c74100
Introduce asdl_int_seq, to hold cmpop_ty.
Martin v. Löwis <martin@v.loewis.de>
parents:
37599
diff
changeset
|
195 else: |
|
5e72b0c74100
Introduce asdl_int_seq, to hold cmpop_ty.
Martin v. Löwis <martin@v.loewis.de>
parents:
37599
diff
changeset
|
196 self.emit("asdl_seq *%(name)s;" % locals(), depth) |
| 35742 | 197 else: |
| 198 self.emit("%(ctype)s %(name)s;" % locals(), depth) | |
| 199 | |
| 200 def visitProduct(self, product, name, depth): | |
| 201 self.emit("struct _%(name)s {" % locals(), depth) | |
| 202 for f in product.fields: | |
| 203 self.visit(f, depth + 1) | |
| 204 self.emit("};", depth) | |
| 205 self.emit("", depth) | |
| 206 | |
|
46162
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
207 |
| 35742 | 208 class PrototypeVisitor(EmitVisitor): |
| 209 """Generate function prototypes for the .h file""" | |
| 210 | |
| 211 def visitModule(self, mod): | |
| 212 for dfn in mod.dfns: | |
| 213 self.visit(dfn) | |
| 214 | |
| 215 def visitType(self, type): | |
| 216 self.visit(type.value, type.name) | |
| 217 | |
| 218 def visitSum(self, sum, name): | |
| 219 if is_simple(sum): | |
| 220 pass # XXX | |
| 221 else: | |
| 222 for t in sum.types: | |
| 223 self.visit(t, name, sum.attributes) | |
| 224 | |
| 225 def get_args(self, fields): | |
| 226 """Return list of C argument into, one for each field. | |
| 227 | |
| 228 Argument info is 3-tuple of a C type, variable name, and flag | |
| 229 that is true if type can be NULL. | |
| 230 """ | |
| 231 args = [] | |
| 232 unnamed = {} | |
| 233 for f in fields: | |
| 234 if f.name is None: | |
| 235 name = f.type | |
| 236 c = unnamed[name] = unnamed.get(name, 0) + 1 | |
| 237 if c > 1: | |
| 238 name = "name%d" % (c - 1) | |
| 239 else: | |
| 240 name = f.name | |
| 241 # XXX should extend get_c_type() to handle this | |
| 242 if f.seq: | |
|
37685
5e72b0c74100
Introduce asdl_int_seq, to hold cmpop_ty.
Martin v. Löwis <martin@v.loewis.de>
parents:
37599
diff
changeset
|
243 if f.type.value in ('cmpop',): |
|
5e72b0c74100
Introduce asdl_int_seq, to hold cmpop_ty.
Martin v. Löwis <martin@v.loewis.de>
parents:
37599
diff
changeset
|
244 ctype = "asdl_int_seq *" |
|
5e72b0c74100
Introduce asdl_int_seq, to hold cmpop_ty.
Martin v. Löwis <martin@v.loewis.de>
parents:
37599
diff
changeset
|
245 else: |
|
5e72b0c74100
Introduce asdl_int_seq, to hold cmpop_ty.
Martin v. Löwis <martin@v.loewis.de>
parents:
37599
diff
changeset
|
246 ctype = "asdl_seq *" |
| 35742 | 247 else: |
| 248 ctype = get_c_type(f.type) | |
| 249 args.append((ctype, name, f.opt or f.seq)) | |
| 250 return args | |
| 251 | |
| 252 def visitConstructor(self, cons, type, attrs): | |
| 253 args = self.get_args(cons.fields) | |
| 254 attrs = self.get_args(attrs) | |
| 255 ctype = get_c_type(type) | |
| 256 self.emit_function(cons.name, ctype, args, attrs) | |
| 257 | |
| 258 def emit_function(self, name, ctype, args, attrs, union=1): | |
| 259 args = args + attrs | |
| 260 if args: | |
| 261 argstr = ", ".join(["%s %s" % (atype, aname) | |
| 262 for atype, aname, opt in args]) | |
|
36065
ea083efdc96c
Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents:
36023
diff
changeset
|
263 argstr += ", PyArena *arena" |
| 35742 | 264 else: |
|
36065
ea083efdc96c
Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents:
36023
diff
changeset
|
265 argstr = "PyArena *arena" |
|
40668
b051d197f7a4
Prefix AST symbols with _Py_. Fixes #1637022.
Martin v. Löwis <martin@v.loewis.de>
parents:
37712
diff
changeset
|
266 margs = "a0" |
|
b051d197f7a4
Prefix AST symbols with _Py_. Fixes #1637022.
Martin v. Löwis <martin@v.loewis.de>
parents:
37712
diff
changeset
|
267 for i in range(1, len(args)+1): |
|
b051d197f7a4
Prefix AST symbols with _Py_. Fixes #1637022.
Martin v. Löwis <martin@v.loewis.de>
parents:
37712
diff
changeset
|
268 margs += ", a%d" % i |
|
b051d197f7a4
Prefix AST symbols with _Py_. Fixes #1637022.
Martin v. Löwis <martin@v.loewis.de>
parents:
37712
diff
changeset
|
269 self.emit("#define %s(%s) _Py_%s(%s)" % (name, margs, name, margs), 0, |
|
b051d197f7a4
Prefix AST symbols with _Py_. Fixes #1637022.
Martin v. Löwis <martin@v.loewis.de>
parents:
37712
diff
changeset
|
270 reflow = 0) |
|
b051d197f7a4
Prefix AST symbols with _Py_. Fixes #1637022.
Martin v. Löwis <martin@v.loewis.de>
parents:
37712
diff
changeset
|
271 self.emit("%s _Py_%s(%s);" % (ctype, name, argstr), 0) |
| 35742 | 272 |
| 273 def visitProduct(self, prod, name): | |
| 274 self.emit_function(name, get_c_type(name), | |
| 275 self.get_args(prod.fields), [], union=0) | |
| 276 | |
|
46162
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
277 |
| 35742 | 278 class FunctionVisitor(PrototypeVisitor): |
| 279 """Visitor to generate constructor functions for AST.""" | |
| 280 | |
| 281 def emit_function(self, name, ctype, args, attrs, union=1): | |
| 282 def emit(s, depth=0, reflow=1): | |
| 283 self.emit(s, depth, reflow) | |
| 284 argstr = ", ".join(["%s %s" % (atype, aname) | |
| 285 for atype, aname, opt in args + attrs]) | |
|
36065
ea083efdc96c
Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents:
36023
diff
changeset
|
286 if argstr: |
|
ea083efdc96c
Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents:
36023
diff
changeset
|
287 argstr += ", PyArena *arena" |
|
ea083efdc96c
Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents:
36023
diff
changeset
|
288 else: |
|
ea083efdc96c
Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents:
36023
diff
changeset
|
289 argstr = "PyArena *arena" |
| 35742 | 290 self.emit("%s" % ctype, 0) |
| 291 emit("%s(%s)" % (name, argstr)) | |
| 292 emit("{") | |
| 293 emit("%s p;" % ctype, 1) | |
| 294 for argtype, argname, opt in args: | |
| 295 # XXX hack alert: false is allowed for a bool | |
|
37453
16a64e394602
Add lineno, col_offset to excephandler to enable future fix for
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
37412
diff
changeset
|
296 if not opt and not (argtype == "bool" or argtype == "int"): |
| 35742 | 297 emit("if (!%s) {" % argname, 1) |
| 298 emit("PyErr_SetString(PyExc_ValueError,", 2) | |
| 299 msg = "field %s is required for %s" % (argname, name) | |
| 300 emit(' "%s");' % msg, | |
| 301 2, reflow=0) | |
| 302 emit('return NULL;', 2) | |
| 303 emit('}', 1) | |
| 304 | |
|
36065
ea083efdc96c
Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents:
36023
diff
changeset
|
305 emit("p = (%s)PyArena_Malloc(arena, sizeof(*p));" % ctype, 1); |
|
40936
0376f13a1af9
Fix a couple of problems in generating the AST code:
Neal Norwitz <nnorwitz@gmail.com>
parents:
40824
diff
changeset
|
306 emit("if (!p)", 1) |
| 35742 | 307 emit("return NULL;", 2) |
| 308 if union: | |
| 309 self.emit_body_union(name, args, attrs) | |
| 310 else: | |
| 311 self.emit_body_struct(name, args, attrs) | |
| 312 emit("return p;", 1) | |
| 313 emit("}") | |
| 314 emit("") | |
| 315 | |
| 316 def emit_body_union(self, name, args, attrs): | |
| 317 def emit(s, depth=0, reflow=1): | |
| 318 self.emit(s, depth, reflow) | |
| 319 emit("p->kind = %s_kind;" % name, 1) | |
| 320 for argtype, argname, opt in args: | |
| 321 emit("p->v.%s.%s = %s;" % (name, argname, argname), 1) | |
| 322 for argtype, argname, opt in attrs: | |
| 323 emit("p->%s = %s;" % (argname, argname), 1) | |
| 324 | |
| 325 def emit_body_struct(self, name, args, attrs): | |
| 326 def emit(s, depth=0, reflow=1): | |
| 327 self.emit(s, depth, reflow) | |
| 328 for argtype, argname, opt in args: | |
| 329 emit("p->%s = %s;" % (argname, argname), 1) | |
| 330 assert not attrs | |
| 331 | |
|
46162
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
332 |
| 35742 | 333 class PickleVisitor(EmitVisitor): |
| 334 | |
| 335 def visitModule(self, mod): | |
| 336 for dfn in mod.dfns: | |
| 337 self.visit(dfn) | |
| 338 | |
| 339 def visitType(self, type): | |
| 340 self.visit(type.value, type.name) | |
| 341 | |
| 342 def visitSum(self, sum, name): | |
| 343 pass | |
| 344 | |
| 345 def visitProduct(self, sum, name): | |
| 346 pass | |
| 347 | |
| 348 def visitConstructor(self, cons, name): | |
| 349 pass | |
| 350 | |
| 351 def visitField(self, sum): | |
| 352 pass | |
| 353 | |
|
46162
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
354 |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
355 class Obj2ModPrototypeVisitor(PickleVisitor): |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
356 def visitProduct(self, prod, name): |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
357 code = "static int obj2ast_%s(PyObject* obj, %s* out, PyArena* arena);" |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
358 self.emit(code % (name, get_c_type(name)), 0) |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
359 |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
360 visitSum = visitProduct |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
361 |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
362 |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
363 class Obj2ModVisitor(PickleVisitor): |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
364 def funcHeader(self, name): |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
365 ctype = get_c_type(name) |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
366 self.emit("int", 0) |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
367 self.emit("obj2ast_%s(PyObject* obj, %s* out, PyArena* arena)" % (name, ctype), 0) |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
368 self.emit("{", 0) |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
369 self.emit("PyObject* tmp = NULL;", 1) |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
370 self.emit("", 0) |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
371 |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
372 def sumTrailer(self, name): |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
373 self.emit("", 0) |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
374 self.emit("tmp = PyObject_Repr(obj);", 1) |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
375 # there's really nothing more we can do if this fails ... |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
376 self.emit("if (tmp == NULL) goto failed;", 1) |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
377 error = "expected some sort of %s, but got %%.400s" % name |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47294
diff
changeset
|
378 format = "PyErr_Format(PyExc_TypeError, \"%s\", PyString_AS_STRING(tmp));" |
|
46162
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
379 self.emit(format % error, 1, reflow=False) |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
380 self.emit("failed:", 0) |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
381 self.emit("Py_XDECREF(tmp);", 1) |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
382 self.emit("return 1;", 1) |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
383 self.emit("}", 0) |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
384 self.emit("", 0) |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
385 |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
386 def simpleSum(self, sum, name): |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
387 self.funcHeader(name) |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
388 for t in sum.types: |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
389 self.emit("if (PyObject_IsInstance(obj, (PyObject*)%s_type)) {" % t.name, 1) |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
390 self.emit("*out = %s;" % t.name, 2) |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
391 self.emit("return 0;", 2) |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
392 self.emit("}", 1) |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
393 self.sumTrailer(name) |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
394 |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
395 def buildArgs(self, fields): |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
396 return ", ".join(fields + ["arena"]) |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
397 |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
398 def complexSum(self, sum, name): |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
399 self.funcHeader(name) |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
400 for a in sum.attributes: |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
401 self.visitAttributeDeclaration(a, name, sum=sum) |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
402 self.emit("", 0) |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
403 # XXX: should we only do this for 'expr'? |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
404 self.emit("if (obj == Py_None) {", 1) |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
405 self.emit("*out = NULL;", 2) |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
406 self.emit("return 0;", 2) |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
407 self.emit("}", 1) |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
408 for a in sum.attributes: |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
409 self.visitField(a, name, sum=sum, depth=1) |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
410 for t in sum.types: |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
411 self.emit("if (PyObject_IsInstance(obj, (PyObject*)%s_type)) {" % t.name, 1) |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
412 for f in t.fields: |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
413 self.visitFieldDeclaration(f, t.name, sum=sum, depth=2) |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
414 self.emit("", 0) |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
415 for f in t.fields: |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
416 self.visitField(f, t.name, sum=sum, depth=2) |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
417 args = [f.name.value for f in t.fields] + [a.name.value for a in sum.attributes] |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
418 self.emit("*out = %s(%s);" % (t.name, self.buildArgs(args)), 2) |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
419 self.emit("if (*out == NULL) goto failed;", 2) |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
420 self.emit("return 0;", 2) |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
421 self.emit("}", 1) |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
422 self.sumTrailer(name) |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
423 |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
424 def visitAttributeDeclaration(self, a, name, sum=sum): |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
425 ctype = get_c_type(a.type) |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
426 self.emit("%s %s;" % (ctype, a.name), 1) |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
427 |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
428 def visitSum(self, sum, name): |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
429 if is_simple(sum): |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
430 self.simpleSum(sum, name) |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
431 else: |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
432 self.complexSum(sum, name) |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
433 |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
434 def visitProduct(self, prod, name): |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
435 ctype = get_c_type(name) |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
436 self.emit("int", 0) |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
437 self.emit("obj2ast_%s(PyObject* obj, %s* out, PyArena* arena)" % (name, ctype), 0) |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
438 self.emit("{", 0) |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
439 self.emit("PyObject* tmp = NULL;", 1) |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
440 for f in prod.fields: |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
441 self.visitFieldDeclaration(f, name, prod=prod, depth=1) |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
442 self.emit("", 0) |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
443 for f in prod.fields: |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
444 self.visitField(f, name, prod=prod, depth=1) |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
445 args = [f.name.value for f in prod.fields] |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
446 self.emit("*out = %s(%s);" % (name, self.buildArgs(args)), 1) |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
447 self.emit("return 0;", 1) |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
448 self.emit("failed:", 0) |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
449 self.emit("Py_XDECREF(tmp);", 1) |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
450 self.emit("return 1;", 1) |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
451 self.emit("}", 0) |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
452 self.emit("", 0) |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
453 |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
454 def visitFieldDeclaration(self, field, name, sum=None, prod=None, depth=0): |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
455 ctype = get_c_type(field.type) |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
456 if field.seq: |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
457 if self.isSimpleType(field): |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
458 self.emit("asdl_int_seq* %s;" % field.name, depth) |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
459 else: |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
460 self.emit("asdl_seq* %s;" % field.name, depth) |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
461 else: |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
462 ctype = get_c_type(field.type) |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
463 self.emit("%s %s;" % (ctype, field.name), depth) |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
464 |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
465 def isSimpleSum(self, field): |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
466 # XXX can the members of this list be determined automatically? |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
467 return field.type.value in ('expr_context', 'boolop', 'operator', |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
468 'unaryop', 'cmpop') |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
469 |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
470 def isNumeric(self, field): |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
471 return get_c_type(field.type) in ("int", "bool") |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
472 |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
473 def isSimpleType(self, field): |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
474 return self.isSimpleSum(field) or self.isNumeric(field) |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
475 |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
476 def visitField(self, field, name, sum=None, prod=None, depth=0): |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
477 ctype = get_c_type(field.type) |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
478 self.emit("if (PyObject_HasAttrString(obj, \"%s\")) {" % field.name, depth) |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
479 self.emit("int res;", depth+1) |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
480 if field.seq: |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
481 self.emit("Py_ssize_t len;", depth+1) |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
482 self.emit("Py_ssize_t i;", depth+1) |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
483 self.emit("tmp = PyObject_GetAttrString(obj, \"%s\");" % field.name, depth+1) |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
484 self.emit("if (tmp == NULL) goto failed;", depth+1) |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
485 if field.seq: |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
486 self.emit("if (!PyList_Check(tmp)) {", depth+1) |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
487 self.emit("PyErr_Format(PyExc_TypeError, \"%s field \\\"%s\\\" must " |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
488 "be a list, not a %%.200s\", tmp->ob_type->tp_name);" % |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
489 (name, field.name), |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
490 depth+2, reflow=False) |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
491 self.emit("goto failed;", depth+2) |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
492 self.emit("}", depth+1) |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
493 self.emit("len = PyList_GET_SIZE(tmp);", depth+1) |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
494 if self.isSimpleType(field): |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
495 self.emit("%s = asdl_int_seq_new(len, arena);" % field.name, depth+1) |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
496 else: |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
497 self.emit("%s = asdl_seq_new(len, arena);" % field.name, depth+1) |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
498 self.emit("if (%s == NULL) goto failed;" % field.name, depth+1) |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
499 self.emit("for (i = 0; i < len; i++) {", depth+1) |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
500 self.emit("%s value;" % ctype, depth+2) |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
501 self.emit("res = obj2ast_%s(PyList_GET_ITEM(tmp, i), &value, arena);" % |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
502 field.type, depth+2, reflow=False) |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
503 self.emit("if (res != 0) goto failed;", depth+2) |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
504 self.emit("asdl_seq_SET(%s, i, value);" % field.name, depth+2) |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
505 self.emit("}", depth+1) |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
506 else: |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
507 self.emit("res = obj2ast_%s(tmp, &%s, arena);" % |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
508 (field.type, field.name), depth+1) |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
509 self.emit("if (res != 0) goto failed;", depth+1) |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
510 |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
511 self.emit("Py_XDECREF(tmp);", depth+1) |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
512 self.emit("tmp = NULL;", depth+1) |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
513 self.emit("} else {", depth) |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
514 if not field.opt: |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
515 message = "required field \\\"%s\\\" missing from %s" % (field.name, name) |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
516 format = "PyErr_SetString(PyExc_TypeError, \"%s\");" |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
517 self.emit(format % message, depth+1, reflow=False) |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
518 self.emit("return 1;", depth+1) |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
519 else: |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
520 if self.isNumeric(field): |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
521 self.emit("%s = 0;" % field.name, depth+1) |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
522 elif not self.isSimpleType(field): |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
523 self.emit("%s = NULL;" % field.name, depth+1) |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
524 else: |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
525 raise TypeError("could not determine the default value for %s" % field.name) |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
526 self.emit("}", depth) |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
527 |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
528 |
| 35742 | 529 class MarshalPrototypeVisitor(PickleVisitor): |
| 530 | |
| 531 def prototype(self, sum, name): | |
| 532 ctype = get_c_type(name) | |
|
35862
6867995a18a7
Prevent name pollution by making lots of internal functions static.
Neal Norwitz <nnorwitz@gmail.com>
parents:
35782
diff
changeset
|
533 self.emit("static int marshal_write_%s(PyObject **, int *, %s);" |
| 35742 | 534 % (name, ctype), 0) |
| 535 | |
| 536 visitProduct = visitSum = prototype | |
| 537 | |
|
46162
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
538 |
|
36660
3c0b26113df6
Generate code to recursively copy an AST into
Martin v. Löwis <martin@v.loewis.de>
parents:
36121
diff
changeset
|
539 class PyTypesDeclareVisitor(PickleVisitor): |
| 35742 | 540 |
|
36660
3c0b26113df6
Generate code to recursively copy an AST into
Martin v. Löwis <martin@v.loewis.de>
parents:
36121
diff
changeset
|
541 def visitProduct(self, prod, name): |
|
36743
dbf9cc6c36eb
Don't pollute namespace as bad as before. All the types are static now.
Neal Norwitz <nnorwitz@gmail.com>
parents:
36721
diff
changeset
|
542 self.emit("static PyTypeObject *%s_type;" % name, 0) |
|
36660
3c0b26113df6
Generate code to recursively copy an AST into
Martin v. Löwis <martin@v.loewis.de>
parents:
36121
diff
changeset
|
543 self.emit("static PyObject* ast2obj_%s(void*);" % name, 0) |
|
36664
7bfbfb2ef4e2
Stop generating empty arrays.
Martin v. Löwis <martin@v.loewis.de>
parents:
36661
diff
changeset
|
544 if prod.fields: |
|
36743
dbf9cc6c36eb
Don't pollute namespace as bad as before. All the types are static now.
Neal Norwitz <nnorwitz@gmail.com>
parents:
36721
diff
changeset
|
545 self.emit("static char *%s_fields[]={" % name,0) |
|
36664
7bfbfb2ef4e2
Stop generating empty arrays.
Martin v. Löwis <martin@v.loewis.de>
parents:
36661
diff
changeset
|
546 for f in prod.fields: |
|
7bfbfb2ef4e2
Stop generating empty arrays.
Martin v. Löwis <martin@v.loewis.de>
parents:
36661
diff
changeset
|
547 self.emit('"%s",' % f.name, 1) |
|
7bfbfb2ef4e2
Stop generating empty arrays.
Martin v. Löwis <martin@v.loewis.de>
parents:
36661
diff
changeset
|
548 self.emit("};", 0) |
|
36721
f7b4726ada6c
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
36708
diff
changeset
|
549 |
|
36660
3c0b26113df6
Generate code to recursively copy an AST into
Martin v. Löwis <martin@v.loewis.de>
parents:
36121
diff
changeset
|
550 def visitSum(self, sum, name): |
|
36743
dbf9cc6c36eb
Don't pollute namespace as bad as before. All the types are static now.
Neal Norwitz <nnorwitz@gmail.com>
parents:
36721
diff
changeset
|
551 self.emit("static PyTypeObject *%s_type;" % name, 0) |
| 36669 | 552 if sum.attributes: |
|
36743
dbf9cc6c36eb
Don't pollute namespace as bad as before. All the types are static now.
Neal Norwitz <nnorwitz@gmail.com>
parents:
36721
diff
changeset
|
553 self.emit("static char *%s_attributes[] = {" % name, 0) |
| 36669 | 554 for a in sum.attributes: |
| 555 self.emit('"%s",' % a.name, 1) | |
| 556 self.emit("};", 0) | |
|
36660
3c0b26113df6
Generate code to recursively copy an AST into
Martin v. Löwis <martin@v.loewis.de>
parents:
36121
diff
changeset
|
557 ptype = "void*" |
|
3c0b26113df6
Generate code to recursively copy an AST into
Martin v. Löwis <martin@v.loewis.de>
parents:
36121
diff
changeset
|
558 if is_simple(sum): |
|
3c0b26113df6
Generate code to recursively copy an AST into
Martin v. Löwis <martin@v.loewis.de>
parents:
36121
diff
changeset
|
559 ptype = get_c_type(name) |
|
3c0b26113df6
Generate code to recursively copy an AST into
Martin v. Löwis <martin@v.loewis.de>
parents:
36121
diff
changeset
|
560 tnames = [] |
|
3c0b26113df6
Generate code to recursively copy an AST into
Martin v. Löwis <martin@v.loewis.de>
parents:
36121
diff
changeset
|
561 for t in sum.types: |
|
3c0b26113df6
Generate code to recursively copy an AST into
Martin v. Löwis <martin@v.loewis.de>
parents:
36121
diff
changeset
|
562 tnames.append(str(t.name)+"_singleton") |
|
3c0b26113df6
Generate code to recursively copy an AST into
Martin v. Löwis <martin@v.loewis.de>
parents:
36121
diff
changeset
|
563 tnames = ", *".join(tnames) |
|
3c0b26113df6
Generate code to recursively copy an AST into
Martin v. Löwis <martin@v.loewis.de>
parents:
36121
diff
changeset
|
564 self.emit("static PyObject *%s;" % tnames, 0) |
|
3c0b26113df6
Generate code to recursively copy an AST into
Martin v. Löwis <martin@v.loewis.de>
parents:
36121
diff
changeset
|
565 self.emit("static PyObject* ast2obj_%s(%s);" % (name, ptype), 0) |
|
3c0b26113df6
Generate code to recursively copy an AST into
Martin v. Löwis <martin@v.loewis.de>
parents:
36121
diff
changeset
|
566 for t in sum.types: |
|
3c0b26113df6
Generate code to recursively copy an AST into
Martin v. Löwis <martin@v.loewis.de>
parents:
36121
diff
changeset
|
567 self.visitConstructor(t, name) |
|
36721
f7b4726ada6c
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
36708
diff
changeset
|
568 |
|
36660
3c0b26113df6
Generate code to recursively copy an AST into
Martin v. Löwis <martin@v.loewis.de>
parents:
36121
diff
changeset
|
569 def visitConstructor(self, cons, name): |
|
36743
dbf9cc6c36eb
Don't pollute namespace as bad as before. All the types are static now.
Neal Norwitz <nnorwitz@gmail.com>
parents:
36721
diff
changeset
|
570 self.emit("static PyTypeObject *%s_type;" % cons.name, 0) |
|
36664
7bfbfb2ef4e2
Stop generating empty arrays.
Martin v. Löwis <martin@v.loewis.de>
parents:
36661
diff
changeset
|
571 if cons.fields: |
|
36743
dbf9cc6c36eb
Don't pollute namespace as bad as before. All the types are static now.
Neal Norwitz <nnorwitz@gmail.com>
parents:
36721
diff
changeset
|
572 self.emit("static char *%s_fields[]={" % cons.name, 0) |
|
36664
7bfbfb2ef4e2
Stop generating empty arrays.
Martin v. Löwis <martin@v.loewis.de>
parents:
36661
diff
changeset
|
573 for t in cons.fields: |
|
7bfbfb2ef4e2
Stop generating empty arrays.
Martin v. Löwis <martin@v.loewis.de>
parents:
36661
diff
changeset
|
574 self.emit('"%s",' % t.name, 1) |
|
7bfbfb2ef4e2
Stop generating empty arrays.
Martin v. Löwis <martin@v.loewis.de>
parents:
36661
diff
changeset
|
575 self.emit("};",0) |
|
36660
3c0b26113df6
Generate code to recursively copy an AST into
Martin v. Löwis <martin@v.loewis.de>
parents:
36121
diff
changeset
|
576 |
|
3c0b26113df6
Generate code to recursively copy an AST into
Martin v. Löwis <martin@v.loewis.de>
parents:
36121
diff
changeset
|
577 class PyTypesVisitor(PickleVisitor): |
|
3c0b26113df6
Generate code to recursively copy an AST into
Martin v. Löwis <martin@v.loewis.de>
parents:
36121
diff
changeset
|
578 |
|
3c0b26113df6
Generate code to recursively copy an AST into
Martin v. Löwis <martin@v.loewis.de>
parents:
36121
diff
changeset
|
579 def visitModule(self, mod): |
|
3c0b26113df6
Generate code to recursively copy an AST into
Martin v. Löwis <martin@v.loewis.de>
parents:
36121
diff
changeset
|
580 self.emit(""" |
|
46202
42c5d287a852
#2505: allow easier creation of AST nodes.
Georg Brandl <georg@python.org>
parents:
46193
diff
changeset
|
581 static int |
|
42c5d287a852
#2505: allow easier creation of AST nodes.
Georg Brandl <georg@python.org>
parents:
46193
diff
changeset
|
582 ast_type_init(PyObject *self, PyObject *args, PyObject *kw) |
|
42c5d287a852
#2505: allow easier creation of AST nodes.
Georg Brandl <georg@python.org>
parents:
46193
diff
changeset
|
583 { |
|
42c5d287a852
#2505: allow easier creation of AST nodes.
Georg Brandl <georg@python.org>
parents:
46193
diff
changeset
|
584 Py_ssize_t i, numfields = 0; |
|
42c5d287a852
#2505: allow easier creation of AST nodes.
Georg Brandl <georg@python.org>
parents:
46193
diff
changeset
|
585 int res = -1; |
|
42c5d287a852
#2505: allow easier creation of AST nodes.
Georg Brandl <georg@python.org>
parents:
46193
diff
changeset
|
586 PyObject *key, *value, *fields; |
|
42c5d287a852
#2505: allow easier creation of AST nodes.
Georg Brandl <georg@python.org>
parents:
46193
diff
changeset
|
587 fields = PyObject_GetAttrString((PyObject*)Py_TYPE(self), "_fields"); |
|
42c5d287a852
#2505: allow easier creation of AST nodes.
Georg Brandl <georg@python.org>
parents:
46193
diff
changeset
|
588 if (!fields) |
|
42c5d287a852
#2505: allow easier creation of AST nodes.
Georg Brandl <georg@python.org>
parents:
46193
diff
changeset
|
589 PyErr_Clear(); |
|
42c5d287a852
#2505: allow easier creation of AST nodes.
Georg Brandl <georg@python.org>
parents:
46193
diff
changeset
|
590 if (fields) { |
|
42c5d287a852
#2505: allow easier creation of AST nodes.
Georg Brandl <georg@python.org>
parents:
46193
diff
changeset
|
591 numfields = PySequence_Size(fields); |
|
42c5d287a852
#2505: allow easier creation of AST nodes.
Georg Brandl <georg@python.org>
parents:
46193
diff
changeset
|
592 if (numfields == -1) |
|
42c5d287a852
#2505: allow easier creation of AST nodes.
Georg Brandl <georg@python.org>
parents:
46193
diff
changeset
|
593 goto cleanup; |
|
42c5d287a852
#2505: allow easier creation of AST nodes.
Georg Brandl <georg@python.org>
parents:
46193
diff
changeset
|
594 } |
|
42c5d287a852
#2505: allow easier creation of AST nodes.
Georg Brandl <georg@python.org>
parents:
46193
diff
changeset
|
595 res = 0; /* if no error occurs, this stays 0 to the end */ |
|
42c5d287a852
#2505: allow easier creation of AST nodes.
Georg Brandl <georg@python.org>
parents:
46193
diff
changeset
|
596 if (PyTuple_GET_SIZE(args) > 0) { |
|
42c5d287a852
#2505: allow easier creation of AST nodes.
Georg Brandl <georg@python.org>
parents:
46193
diff
changeset
|
597 if (numfields != PyTuple_GET_SIZE(args)) { |
|
46207
59d42e6d1885
Fix error message -- "expects either 0 or 0 arguments"
Georg Brandl <georg@python.org>
parents:
46204
diff
changeset
|
598 PyErr_Format(PyExc_TypeError, "%.400s constructor takes %s" |
|
49140
a4df4903b355
update asdl_c.py from r66377
Benjamin Peterson <benjamin@python.org>
parents:
47527
diff
changeset
|
599 "%zd positional argument%s", |
|
46207
59d42e6d1885
Fix error message -- "expects either 0 or 0 arguments"
Georg Brandl <georg@python.org>
parents:
46204
diff
changeset
|
600 Py_TYPE(self)->tp_name, |
|
59d42e6d1885
Fix error message -- "expects either 0 or 0 arguments"
Georg Brandl <georg@python.org>
parents:
46204
diff
changeset
|
601 numfields == 0 ? "" : "either 0 or ", |
|
46202
42c5d287a852
#2505: allow easier creation of AST nodes.
Georg Brandl <georg@python.org>
parents:
46193
diff
changeset
|
602 numfields, numfields == 1 ? "" : "s"); |
|
42c5d287a852
#2505: allow easier creation of AST nodes.
Georg Brandl <georg@python.org>
parents:
46193
diff
changeset
|
603 res = -1; |
|
42c5d287a852
#2505: allow easier creation of AST nodes.
Georg Brandl <georg@python.org>
parents:
46193
diff
changeset
|
604 goto cleanup; |
|
42c5d287a852
#2505: allow easier creation of AST nodes.
Georg Brandl <georg@python.org>
parents:
46193
diff
changeset
|
605 } |
|
42c5d287a852
#2505: allow easier creation of AST nodes.
Georg Brandl <georg@python.org>
parents:
46193
diff
changeset
|
606 for (i = 0; i < PyTuple_GET_SIZE(args); i++) { |
|
42c5d287a852
#2505: allow easier creation of AST nodes.
Georg Brandl <georg@python.org>
parents:
46193
diff
changeset
|
607 /* cannot be reached when fields is NULL */ |
|
42c5d287a852
#2505: allow easier creation of AST nodes.
Georg Brandl <georg@python.org>
parents:
46193
diff
changeset
|
608 PyObject *name = PySequence_GetItem(fields, i); |
|
42c5d287a852
#2505: allow easier creation of AST nodes.
Georg Brandl <georg@python.org>
parents:
46193
diff
changeset
|
609 if (!name) { |
|
42c5d287a852
#2505: allow easier creation of AST nodes.
Georg Brandl <georg@python.org>
parents:
46193
diff
changeset
|
610 res = -1; |
|
42c5d287a852
#2505: allow easier creation of AST nodes.
Georg Brandl <georg@python.org>
parents:
46193
diff
changeset
|
611 goto cleanup; |
|
42c5d287a852
#2505: allow easier creation of AST nodes.
Georg Brandl <georg@python.org>
parents:
46193
diff
changeset
|
612 } |
|
42c5d287a852
#2505: allow easier creation of AST nodes.
Georg Brandl <georg@python.org>
parents:
46193
diff
changeset
|
613 res = PyObject_SetAttr(self, name, PyTuple_GET_ITEM(args, i)); |
|
42c5d287a852
#2505: allow easier creation of AST nodes.
Georg Brandl <georg@python.org>
parents:
46193
diff
changeset
|
614 Py_DECREF(name); |
|
42c5d287a852
#2505: allow easier creation of AST nodes.
Georg Brandl <georg@python.org>
parents:
46193
diff
changeset
|
615 if (res < 0) |
|
42c5d287a852
#2505: allow easier creation of AST nodes.
Georg Brandl <georg@python.org>
parents:
46193
diff
changeset
|
616 goto cleanup; |
|
42c5d287a852
#2505: allow easier creation of AST nodes.
Georg Brandl <georg@python.org>
parents:
46193
diff
changeset
|
617 } |
|
42c5d287a852
#2505: allow easier creation of AST nodes.
Georg Brandl <georg@python.org>
parents:
46193
diff
changeset
|
618 } |
|
42c5d287a852
#2505: allow easier creation of AST nodes.
Georg Brandl <georg@python.org>
parents:
46193
diff
changeset
|
619 if (kw) { |
|
42c5d287a852
#2505: allow easier creation of AST nodes.
Georg Brandl <georg@python.org>
parents:
46193
diff
changeset
|
620 i = 0; /* needed by PyDict_Next */ |
|
42c5d287a852
#2505: allow easier creation of AST nodes.
Georg Brandl <georg@python.org>
parents:
46193
diff
changeset
|
621 while (PyDict_Next(kw, &i, &key, &value)) { |
|
42c5d287a852
#2505: allow easier creation of AST nodes.
Georg Brandl <georg@python.org>
parents:
46193
diff
changeset
|
622 res = PyObject_SetAttr(self, key, value); |
|
42c5d287a852
#2505: allow easier creation of AST nodes.
Georg Brandl <georg@python.org>
parents:
46193
diff
changeset
|
623 if (res < 0) |
|
42c5d287a852
#2505: allow easier creation of AST nodes.
Georg Brandl <georg@python.org>
parents:
46193
diff
changeset
|
624 goto cleanup; |
|
42c5d287a852
#2505: allow easier creation of AST nodes.
Georg Brandl <georg@python.org>
parents:
46193
diff
changeset
|
625 } |
|
42c5d287a852
#2505: allow easier creation of AST nodes.
Georg Brandl <georg@python.org>
parents:
46193
diff
changeset
|
626 } |
|
42c5d287a852
#2505: allow easier creation of AST nodes.
Georg Brandl <georg@python.org>
parents:
46193
diff
changeset
|
627 cleanup: |
|
42c5d287a852
#2505: allow easier creation of AST nodes.
Georg Brandl <georg@python.org>
parents:
46193
diff
changeset
|
628 Py_XDECREF(fields); |
|
42c5d287a852
#2505: allow easier creation of AST nodes.
Georg Brandl <georg@python.org>
parents:
46193
diff
changeset
|
629 return res; |
|
42c5d287a852
#2505: allow easier creation of AST nodes.
Georg Brandl <georg@python.org>
parents:
46193
diff
changeset
|
630 } |
|
42c5d287a852
#2505: allow easier creation of AST nodes.
Georg Brandl <georg@python.org>
parents:
46193
diff
changeset
|
631 |
|
46212
be18df634405
Make AST nodes pickleable.
Georg Brandl <georg@python.org>
parents:
46207
diff
changeset
|
632 /* Pickling support */ |
|
be18df634405
Make AST nodes pickleable.
Georg Brandl <georg@python.org>
parents:
46207
diff
changeset
|
633 static PyObject * |
|
be18df634405
Make AST nodes pickleable.
Georg Brandl <georg@python.org>
parents:
46207
diff
changeset
|
634 ast_type_reduce(PyObject *self, PyObject *unused) |
|
be18df634405
Make AST nodes pickleable.
Georg Brandl <georg@python.org>
parents:
46207
diff
changeset
|
635 { |
|
be18df634405
Make AST nodes pickleable.
Georg Brandl <georg@python.org>
parents:
46207
diff
changeset
|
636 PyObject *res; |
|
be18df634405
Make AST nodes pickleable.
Georg Brandl <georg@python.org>
parents:
46207
diff
changeset
|
637 PyObject *dict = PyObject_GetAttrString(self, "__dict__"); |
|
be18df634405
Make AST nodes pickleable.
Georg Brandl <georg@python.org>
parents:
46207
diff
changeset
|
638 if (dict == NULL) { |
|
be18df634405
Make AST nodes pickleable.
Georg Brandl <georg@python.org>
parents:
46207
diff
changeset
|
639 if (PyErr_ExceptionMatches(PyExc_AttributeError)) |
|
be18df634405
Make AST nodes pickleable.
Georg Brandl <georg@python.org>
parents:
46207
diff
changeset
|
640 PyErr_Clear(); |
|
be18df634405
Make AST nodes pickleable.
Georg Brandl <georg@python.org>
parents:
46207
diff
changeset
|
641 else |
|
be18df634405
Make AST nodes pickleable.
Georg Brandl <georg@python.org>
parents:
46207
diff
changeset
|
642 return NULL; |
|
be18df634405
Make AST nodes pickleable.
Georg Brandl <georg@python.org>
parents:
46207
diff
changeset
|
643 } |
|
be18df634405
Make AST nodes pickleable.
Georg Brandl <georg@python.org>
parents:
46207
diff
changeset
|
644 if (dict) { |
|
be18df634405
Make AST nodes pickleable.
Georg Brandl <georg@python.org>
parents:
46207
diff
changeset
|
645 res = Py_BuildValue("O()O", Py_TYPE(self), dict); |
|
be18df634405
Make AST nodes pickleable.
Georg Brandl <georg@python.org>
parents:
46207
diff
changeset
|
646 Py_DECREF(dict); |
|
be18df634405
Make AST nodes pickleable.
Georg Brandl <georg@python.org>
parents:
46207
diff
changeset
|
647 return res; |
|
be18df634405
Make AST nodes pickleable.
Georg Brandl <georg@python.org>
parents:
46207
diff
changeset
|
648 } |
|
be18df634405
Make AST nodes pickleable.
Georg Brandl <georg@python.org>
parents:
46207
diff
changeset
|
649 return Py_BuildValue("O()", Py_TYPE(self)); |
|
be18df634405
Make AST nodes pickleable.
Georg Brandl <georg@python.org>
parents:
46207
diff
changeset
|
650 } |
|
be18df634405
Make AST nodes pickleable.
Georg Brandl <georg@python.org>
parents:
46207
diff
changeset
|
651 |
|
be18df634405
Make AST nodes pickleable.
Georg Brandl <georg@python.org>
parents:
46207
diff
changeset
|
652 static PyMethodDef ast_type_methods[] = { |
|
be18df634405
Make AST nodes pickleable.
Georg Brandl <georg@python.org>
parents:
46207
diff
changeset
|
653 {"__reduce__", ast_type_reduce, METH_NOARGS, NULL}, |
|
be18df634405
Make AST nodes pickleable.
Georg Brandl <georg@python.org>
parents:
46207
diff
changeset
|
654 {NULL} |
|
be18df634405
Make AST nodes pickleable.
Georg Brandl <georg@python.org>
parents:
46207
diff
changeset
|
655 }; |
|
be18df634405
Make AST nodes pickleable.
Georg Brandl <georg@python.org>
parents:
46207
diff
changeset
|
656 |
|
46202
42c5d287a852
#2505: allow easier creation of AST nodes.
Georg Brandl <georg@python.org>
parents:
46193
diff
changeset
|
657 static PyTypeObject AST_type = { |
|
42c5d287a852
#2505: allow easier creation of AST nodes.
Georg Brandl <georg@python.org>
parents:
46193
diff
changeset
|
658 PyVarObject_HEAD_INIT(&PyType_Type, 0) |
|
46212
be18df634405
Make AST nodes pickleable.
Georg Brandl <georg@python.org>
parents:
46207
diff
changeset
|
659 "_ast.AST", |
|
46202
42c5d287a852
#2505: allow easier creation of AST nodes.
Georg Brandl <georg@python.org>
parents:
46193
diff
changeset
|
660 sizeof(PyObject), |
|
42c5d287a852
#2505: allow easier creation of AST nodes.
Georg Brandl <georg@python.org>
parents:
46193
diff
changeset
|
661 0, |
|
42c5d287a852
#2505: allow easier creation of AST nodes.
Georg Brandl <georg@python.org>
parents:
46193
diff
changeset
|
662 0, /* tp_dealloc */ |
|
42c5d287a852
#2505: allow easier creation of AST nodes.
Georg Brandl <georg@python.org>
parents:
46193
diff
changeset
|
663 0, /* tp_print */ |
|
42c5d287a852
#2505: allow easier creation of AST nodes.
Georg Brandl <georg@python.org>
parents:
46193
diff
changeset
|
664 0, /* tp_getattr */ |
|
42c5d287a852
#2505: allow easier creation of AST nodes.
Georg Brandl <georg@python.org>
parents:
46193
diff
changeset
|
665 0, /* tp_setattr */ |
|
42c5d287a852
#2505: allow easier creation of AST nodes.
Georg Brandl <georg@python.org>
parents:
46193
diff
changeset
|
666 0, /* tp_compare */ |
|
42c5d287a852
#2505: allow easier creation of AST nodes.
Georg Brandl <georg@python.org>
parents:
46193
diff
changeset
|
667 0, /* tp_repr */ |
|
42c5d287a852
#2505: allow easier creation of AST nodes.
Georg Brandl <georg@python.org>
parents:
46193
diff
changeset
|
668 0, /* tp_as_number */ |
|
42c5d287a852
#2505: allow easier creation of AST nodes.
Georg Brandl <georg@python.org>
parents:
46193
diff
changeset
|
669 0, /* tp_as_sequence */ |
|
42c5d287a852
#2505: allow easier creation of AST nodes.
Georg Brandl <georg@python.org>
parents:
46193
diff
changeset
|
670 0, /* tp_as_mapping */ |
|
42c5d287a852
#2505: allow easier creation of AST nodes.
Georg Brandl <georg@python.org>
parents:
46193
diff
changeset
|
671 0, /* tp_hash */ |
|
42c5d287a852
#2505: allow easier creation of AST nodes.
Georg Brandl <georg@python.org>
parents:
46193
diff
changeset
|
672 0, /* tp_call */ |
|
42c5d287a852
#2505: allow easier creation of AST nodes.
Georg Brandl <georg@python.org>
parents:
46193
diff
changeset
|
673 0, /* tp_str */ |
|
42c5d287a852
#2505: allow easier creation of AST nodes.
Georg Brandl <georg@python.org>
parents:
46193
diff
changeset
|
674 PyObject_GenericGetAttr, /* tp_getattro */ |
|
42c5d287a852
#2505: allow easier creation of AST nodes.
Georg Brandl <georg@python.org>
parents:
46193
diff
changeset
|
675 PyObject_GenericSetAttr, /* tp_setattro */ |
|
42c5d287a852
#2505: allow easier creation of AST nodes.
Georg Brandl <georg@python.org>
parents:
46193
diff
changeset
|
676 0, /* tp_as_buffer */ |
|
42c5d287a852
#2505: allow easier creation of AST nodes.
Georg Brandl <georg@python.org>
parents:
46193
diff
changeset
|
677 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
|
42c5d287a852
#2505: allow easier creation of AST nodes.
Georg Brandl <georg@python.org>
parents:
46193
diff
changeset
|
678 0, /* tp_doc */ |
|
42c5d287a852
#2505: allow easier creation of AST nodes.
Georg Brandl <georg@python.org>
parents:
46193
diff
changeset
|
679 0, /* tp_traverse */ |
|
42c5d287a852
#2505: allow easier creation of AST nodes.
Georg Brandl <georg@python.org>
parents:
46193
diff
changeset
|
680 0, /* tp_clear */ |
|
42c5d287a852
#2505: allow easier creation of AST nodes.
Georg Brandl <georg@python.org>
parents:
46193
diff
changeset
|
681 0, /* tp_richcompare */ |
|
42c5d287a852
#2505: allow easier creation of AST nodes.
Georg Brandl <georg@python.org>
parents:
46193
diff
changeset
|
682 0, /* tp_weaklistoffset */ |
|
42c5d287a852
#2505: allow easier creation of AST nodes.
Georg Brandl <georg@python.org>
parents:
46193
diff
changeset
|
683 0, /* tp_iter */ |
|
42c5d287a852
#2505: allow easier creation of AST nodes.
Georg Brandl <georg@python.org>
parents:
46193
diff
changeset
|
684 0, /* tp_iternext */ |
|
46212
be18df634405
Make AST nodes pickleable.
Georg Brandl <georg@python.org>
parents:
46207
diff
changeset
|
685 ast_type_methods, /* tp_methods */ |
|
46202
42c5d287a852
#2505: allow easier creation of AST nodes.
Georg Brandl <georg@python.org>
parents:
46193
diff
changeset
|
686 0, /* tp_members */ |
|
42c5d287a852
#2505: allow easier creation of AST nodes.
Georg Brandl <georg@python.org>
parents:
46193
diff
changeset
|
687 0, /* tp_getset */ |
|
42c5d287a852
#2505: allow easier creation of AST nodes.
Georg Brandl <georg@python.org>
parents:
46193
diff
changeset
|
688 0, /* tp_base */ |
|
42c5d287a852
#2505: allow easier creation of AST nodes.
Georg Brandl <georg@python.org>
parents:
46193
diff
changeset
|
689 0, /* tp_dict */ |
|
42c5d287a852
#2505: allow easier creation of AST nodes.
Georg Brandl <georg@python.org>
parents:
46193
diff
changeset
|
690 0, /* tp_descr_get */ |
|
42c5d287a852
#2505: allow easier creation of AST nodes.
Georg Brandl <georg@python.org>
parents:
46193
diff
changeset
|
691 0, /* tp_descr_set */ |
|
42c5d287a852
#2505: allow easier creation of AST nodes.
Georg Brandl <georg@python.org>
parents:
46193
diff
changeset
|
692 0, /* tp_dictoffset */ |
|
42c5d287a852
#2505: allow easier creation of AST nodes.
Georg Brandl <georg@python.org>
parents:
46193
diff
changeset
|
693 (initproc)ast_type_init, /* tp_init */ |
|
42c5d287a852
#2505: allow easier creation of AST nodes.
Georg Brandl <georg@python.org>
parents:
46193
diff
changeset
|
694 PyType_GenericAlloc, /* tp_alloc */ |
|
42c5d287a852
#2505: allow easier creation of AST nodes.
Georg Brandl <georg@python.org>
parents:
46193
diff
changeset
|
695 PyType_GenericNew, /* tp_new */ |
|
42c5d287a852
#2505: allow easier creation of AST nodes.
Georg Brandl <georg@python.org>
parents:
46193
diff
changeset
|
696 PyObject_Del, /* tp_free */ |
|
42c5d287a852
#2505: allow easier creation of AST nodes.
Georg Brandl <georg@python.org>
parents:
46193
diff
changeset
|
697 }; |
|
42c5d287a852
#2505: allow easier creation of AST nodes.
Georg Brandl <georg@python.org>
parents:
46193
diff
changeset
|
698 |
|
42c5d287a852
#2505: allow easier creation of AST nodes.
Georg Brandl <georg@python.org>
parents:
46193
diff
changeset
|
699 |
|
36660
3c0b26113df6
Generate code to recursively copy an AST into
Martin v. Löwis <martin@v.loewis.de>
parents:
36121
diff
changeset
|
700 static PyTypeObject* make_type(char *type, PyTypeObject* base, char**fields, int num_fields) |
|
3c0b26113df6
Generate code to recursively copy an AST into
Martin v. Löwis <martin@v.loewis.de>
parents:
36121
diff
changeset
|
701 { |
|
3c0b26113df6
Generate code to recursively copy an AST into
Martin v. Löwis <martin@v.loewis.de>
parents:
36121
diff
changeset
|
702 PyObject *fnames, *result; |
|
3c0b26113df6
Generate code to recursively copy an AST into
Martin v. Löwis <martin@v.loewis.de>
parents:
36121
diff
changeset
|
703 int i; |
|
46204
29ea507fd998
Make _fields attr for no fields consistent with _attributes attr.
Georg Brandl <georg@python.org>
parents:
46202
diff
changeset
|
704 fnames = PyTuple_New(num_fields); |
|
29ea507fd998
Make _fields attr for no fields consistent with _attributes attr.
Georg Brandl <georg@python.org>
parents:
46202
diff
changeset
|
705 if (!fnames) return NULL; |
|
29ea507fd998
Make _fields attr for no fields consistent with _attributes attr.
Georg Brandl <georg@python.org>
parents:
46202
diff
changeset
|
706 for (i = 0; i < num_fields; i++) { |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47294
diff
changeset
|
707 PyObject *field = PyString_FromString(fields[i]); |
|
36660
3c0b26113df6
Generate code to recursively copy an AST into
Martin v. Löwis <martin@v.loewis.de>
parents:
36121
diff
changeset
|
708 if (!field) { |
|
3c0b26113df6
Generate code to recursively copy an AST into
Martin v. Löwis <martin@v.loewis.de>
parents:
36121
diff
changeset
|
709 Py_DECREF(fnames); |
|
3c0b26113df6
Generate code to recursively copy an AST into
Martin v. Löwis <martin@v.loewis.de>
parents:
36121
diff
changeset
|
710 return NULL; |
|
3c0b26113df6
Generate code to recursively copy an AST into
Martin v. Löwis <martin@v.loewis.de>
parents:
36121
diff
changeset
|
711 } |
|
3c0b26113df6
Generate code to recursively copy an AST into
Martin v. Löwis <martin@v.loewis.de>
parents:
36121
diff
changeset
|
712 PyTuple_SET_ITEM(fnames, i, field); |
|
3c0b26113df6
Generate code to recursively copy an AST into
Martin v. Löwis <martin@v.loewis.de>
parents:
36121
diff
changeset
|
713 } |
|
36721
f7b4726ada6c
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
36708
diff
changeset
|
714 result = PyObject_CallFunction((PyObject*)&PyType_Type, "s(O){sOss}", |
| 36669 | 715 type, base, "_fields", fnames, "__module__", "_ast"); |
|
36660
3c0b26113df6
Generate code to recursively copy an AST into
Martin v. Löwis <martin@v.loewis.de>
parents:
36121
diff
changeset
|
716 Py_DECREF(fnames); |
|
3c0b26113df6
Generate code to recursively copy an AST into
Martin v. Löwis <martin@v.loewis.de>
parents:
36121
diff
changeset
|
717 return (PyTypeObject*)result; |
|
3c0b26113df6
Generate code to recursively copy an AST into
Martin v. Löwis <martin@v.loewis.de>
parents:
36121
diff
changeset
|
718 } |
|
3c0b26113df6
Generate code to recursively copy an AST into
Martin v. Löwis <martin@v.loewis.de>
parents:
36121
diff
changeset
|
719 |
| 36669 | 720 static int add_attributes(PyTypeObject* type, char**attrs, int num_fields) |
| 721 { | |
|
37412
dfc33ec7f73a
* Fix a refleak of *_attributes.
Neal Norwitz <nnorwitz@gmail.com>
parents:
36794
diff
changeset
|
722 int i, result; |
|
46202
42c5d287a852
#2505: allow easier creation of AST nodes.
Georg Brandl <georg@python.org>
parents:
46193
diff
changeset
|
723 PyObject *s, *l = PyTuple_New(num_fields); |
| 36669 | 724 if (!l) return 0; |
|
37412
dfc33ec7f73a
* Fix a refleak of *_attributes.
Neal Norwitz <nnorwitz@gmail.com>
parents:
36794
diff
changeset
|
725 for(i = 0; i < num_fields; i++) { |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47294
diff
changeset
|
726 s = PyString_FromString(attrs[i]); |
| 36669 | 727 if (!s) { |
| 728 Py_DECREF(l); | |
| 729 return 0; | |
| 730 } | |
|
46202
42c5d287a852
#2505: allow easier creation of AST nodes.
Georg Brandl <georg@python.org>
parents:
46193
diff
changeset
|
731 PyTuple_SET_ITEM(l, i, s); |
| 36669 | 732 } |
|
37412
dfc33ec7f73a
* Fix a refleak of *_attributes.
Neal Norwitz <nnorwitz@gmail.com>
parents:
36794
diff
changeset
|
733 result = PyObject_SetAttrString((PyObject*)type, "_attributes", l) >= 0; |
|
dfc33ec7f73a
* Fix a refleak of *_attributes.
Neal Norwitz <nnorwitz@gmail.com>
parents:
36794
diff
changeset
|
734 Py_DECREF(l); |
|
dfc33ec7f73a
* Fix a refleak of *_attributes.
Neal Norwitz <nnorwitz@gmail.com>
parents:
36794
diff
changeset
|
735 return result; |
| 36669 | 736 } |
| 737 | |
|
46162
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
738 /* Conversion AST -> Python */ |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
739 |
|
36660
3c0b26113df6
Generate code to recursively copy an AST into
Martin v. Löwis <martin@v.loewis.de>
parents:
36121
diff
changeset
|
740 static PyObject* ast2obj_list(asdl_seq *seq, PyObject* (*func)(void*)) |
|
3c0b26113df6
Generate code to recursively copy an AST into
Martin v. Löwis <martin@v.loewis.de>
parents:
36121
diff
changeset
|
741 { |
|
3c0b26113df6
Generate code to recursively copy an AST into
Martin v. Löwis <martin@v.loewis.de>
parents:
36121
diff
changeset
|
742 int i, n = asdl_seq_LEN(seq); |
|
3c0b26113df6
Generate code to recursively copy an AST into
Martin v. Löwis <martin@v.loewis.de>
parents:
36121
diff
changeset
|
743 PyObject *result = PyList_New(n); |
|
3c0b26113df6
Generate code to recursively copy an AST into
Martin v. Löwis <martin@v.loewis.de>
parents:
36121
diff
changeset
|
744 PyObject *value; |
|
3c0b26113df6
Generate code to recursively copy an AST into
Martin v. Löwis <martin@v.loewis.de>
parents:
36121
diff
changeset
|
745 if (!result) |
|
3c0b26113df6
Generate code to recursively copy an AST into
Martin v. Löwis <martin@v.loewis.de>
parents:
36121
diff
changeset
|
746 return NULL; |
|
3c0b26113df6
Generate code to recursively copy an AST into
Martin v. Löwis <martin@v.loewis.de>
parents:
36121
diff
changeset
|
747 for (i = 0; i < n; i++) { |
|
3c0b26113df6
Generate code to recursively copy an AST into
Martin v. Löwis <martin@v.loewis.de>
parents:
36121
diff
changeset
|
748 value = func(asdl_seq_GET(seq, i)); |
|
3c0b26113df6
Generate code to recursively copy an AST into
Martin v. Löwis <martin@v.loewis.de>
parents:
36121
diff
changeset
|
749 if (!value) { |
|
3c0b26113df6
Generate code to recursively copy an AST into
Martin v. Löwis <martin@v.loewis.de>
parents:
36121
diff
changeset
|
750 Py_DECREF(result); |
|
3c0b26113df6
Generate code to recursively copy an AST into
Martin v. Löwis <martin@v.loewis.de>
parents:
36121
diff
changeset
|
751 return NULL; |
|
3c0b26113df6
Generate code to recursively copy an AST into
Martin v. Löwis <martin@v.loewis.de>
parents:
36121
diff
changeset
|
752 } |
|
3c0b26113df6
Generate code to recursively copy an AST into
Martin v. Löwis <martin@v.loewis.de>
parents:
36121
diff
changeset
|
753 PyList_SET_ITEM(result, i, value); |
|
3c0b26113df6
Generate code to recursively copy an AST into
Martin v. Löwis <martin@v.loewis.de>
parents:
36121
diff
changeset
|
754 } |
|
3c0b26113df6
Generate code to recursively copy an AST into
Martin v. Löwis <martin@v.loewis.de>
parents:
36121
diff
changeset
|
755 return result; |
|
3c0b26113df6
Generate code to recursively copy an AST into
Martin v. Löwis <martin@v.loewis.de>
parents:
36121
diff
changeset
|
756 } |
|
3c0b26113df6
Generate code to recursively copy an AST into
Martin v. Löwis <martin@v.loewis.de>
parents:
36121
diff
changeset
|
757 |
|
3c0b26113df6
Generate code to recursively copy an AST into
Martin v. Löwis <martin@v.loewis.de>
parents:
36121
diff
changeset
|
758 static PyObject* ast2obj_object(void *o) |
|
3c0b26113df6
Generate code to recursively copy an AST into
Martin v. Löwis <martin@v.loewis.de>
parents:
36121
diff
changeset
|
759 { |
|
3c0b26113df6
Generate code to recursively copy an AST into
Martin v. Löwis <martin@v.loewis.de>
parents:
36121
diff
changeset
|
760 if (!o) |
|
3c0b26113df6
Generate code to recursively copy an AST into
Martin v. Löwis <martin@v.loewis.de>
parents:
36121
diff
changeset
|
761 o = Py_None; |
|
3c0b26113df6
Generate code to recursively copy an AST into
Martin v. Löwis <martin@v.loewis.de>
parents:
36121
diff
changeset
|
762 Py_INCREF((PyObject*)o); |
|
3c0b26113df6
Generate code to recursively copy an AST into
Martin v. Löwis <martin@v.loewis.de>
parents:
36121
diff
changeset
|
763 return (PyObject*)o; |
|
3c0b26113df6
Generate code to recursively copy an AST into
Martin v. Löwis <martin@v.loewis.de>
parents:
36121
diff
changeset
|
764 } |
|
3c0b26113df6
Generate code to recursively copy an AST into
Martin v. Löwis <martin@v.loewis.de>
parents:
36121
diff
changeset
|
765 #define ast2obj_identifier ast2obj_object |
|
3c0b26113df6
Generate code to recursively copy an AST into
Martin v. Löwis <martin@v.loewis.de>
parents:
36121
diff
changeset
|
766 #define ast2obj_string ast2obj_object |
|
3c0b26113df6
Generate code to recursively copy an AST into
Martin v. Löwis <martin@v.loewis.de>
parents:
36121
diff
changeset
|
767 static PyObject* ast2obj_bool(bool b) |
|
3c0b26113df6
Generate code to recursively copy an AST into
Martin v. Löwis <martin@v.loewis.de>
parents:
36121
diff
changeset
|
768 { |
|
3c0b26113df6
Generate code to recursively copy an AST into
Martin v. Löwis <martin@v.loewis.de>
parents:
36121
diff
changeset
|
769 return PyBool_FromLong(b); |
|
3c0b26113df6
Generate code to recursively copy an AST into
Martin v. Löwis <martin@v.loewis.de>
parents:
36121
diff
changeset
|
770 } |
| 36669 | 771 |
|
40936
0376f13a1af9
Fix a couple of problems in generating the AST code:
Neal Norwitz <nnorwitz@gmail.com>
parents:
40824
diff
changeset
|
772 static PyObject* ast2obj_int(long b) |
| 36669 | 773 { |
| 774 return PyInt_FromLong(b); | |
| 775 } | |
|
46162
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
776 |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
777 /* Conversion Python -> AST */ |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
778 |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
779 static int obj2ast_object(PyObject* obj, PyObject** out, PyArena* arena) |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
780 { |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
781 if (obj == Py_None) |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
782 obj = NULL; |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
783 if (obj) |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
784 PyArena_AddPyObject(arena, obj); |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
785 Py_XINCREF(obj); |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
786 *out = obj; |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
787 return 0; |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
788 } |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
789 |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
790 #define obj2ast_identifier obj2ast_object |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
791 #define obj2ast_string obj2ast_object |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
792 |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
793 static int obj2ast_int(PyObject* obj, int* out, PyArena* arena) |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
794 { |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
795 int i; |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
796 if (!PyInt_Check(obj) && !PyLong_Check(obj)) { |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
797 PyObject *s = PyObject_Repr(obj); |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
798 if (s == NULL) return 1; |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
799 PyErr_Format(PyExc_ValueError, "invalid integer value: %.400s", |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47294
diff
changeset
|
800 PyString_AS_STRING(s)); |
|
46162
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
801 Py_DECREF(s); |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
802 return 1; |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
803 } |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
804 |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
805 i = (int)PyLong_AsLong(obj); |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
806 if (i == -1 && PyErr_Occurred()) |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
807 return 1; |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
808 *out = i; |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
809 return 0; |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
810 } |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
811 |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
812 static int obj2ast_bool(PyObject* obj, bool* out, PyArena* arena) |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
813 { |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
814 if (!PyBool_Check(obj)) { |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
815 PyObject *s = PyObject_Repr(obj); |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
816 if (s == NULL) return 1; |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
817 PyErr_Format(PyExc_ValueError, "invalid boolean value: %.400s", |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47294
diff
changeset
|
818 PyString_AS_STRING(s)); |
|
46162
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
819 Py_DECREF(s); |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
820 return 1; |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
821 } |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
822 |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
823 *out = (obj == Py_True); |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
824 return 0; |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
825 } |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
826 |
|
49552
2e38ca709b1d
fix compiler warning
Benjamin Peterson <benjamin@python.org>
parents:
49551
diff
changeset
|
827 static int add_ast_fields(void) |
|
49551
dbd54bac0b6f
Fixed #4067 by implementing _attributes and _fields for the AST root node.
Armin Ronacher <armin.ronacher@active-4.com>
parents:
49140
diff
changeset
|
828 { |
|
dbd54bac0b6f
Fixed #4067 by implementing _attributes and _fields for the AST root node.
Armin Ronacher <armin.ronacher@active-4.com>
parents:
49140
diff
changeset
|
829 PyObject *empty_tuple, *d; |
|
dbd54bac0b6f
Fixed #4067 by implementing _attributes and _fields for the AST root node.
Armin Ronacher <armin.ronacher@active-4.com>
parents:
49140
diff
changeset
|
830 if (PyType_Ready(&AST_type) < 0) |
|
dbd54bac0b6f
Fixed #4067 by implementing _attributes and _fields for the AST root node.
Armin Ronacher <armin.ronacher@active-4.com>
parents:
49140
diff
changeset
|
831 return -1; |
|
dbd54bac0b6f
Fixed #4067 by implementing _attributes and _fields for the AST root node.
Armin Ronacher <armin.ronacher@active-4.com>
parents:
49140
diff
changeset
|
832 d = AST_type.tp_dict; |
|
dbd54bac0b6f
Fixed #4067 by implementing _attributes and _fields for the AST root node.
Armin Ronacher <armin.ronacher@active-4.com>
parents:
49140
diff
changeset
|
833 empty_tuple = PyTuple_New(0); |
|
dbd54bac0b6f
Fixed #4067 by implementing _attributes and _fields for the AST root node.
Armin Ronacher <armin.ronacher@active-4.com>
parents:
49140
diff
changeset
|
834 if (!empty_tuple || |
|
dbd54bac0b6f
Fixed #4067 by implementing _attributes and _fields for the AST root node.
Armin Ronacher <armin.ronacher@active-4.com>
parents:
49140
diff
changeset
|
835 PyDict_SetItemString(d, "_fields", empty_tuple) < 0 || |
|
dbd54bac0b6f
Fixed #4067 by implementing _attributes and _fields for the AST root node.
Armin Ronacher <armin.ronacher@active-4.com>
parents:
49140
diff
changeset
|
836 PyDict_SetItemString(d, "_attributes", empty_tuple) < 0) { |
|
dbd54bac0b6f
Fixed #4067 by implementing _attributes and _fields for the AST root node.
Armin Ronacher <armin.ronacher@active-4.com>
parents:
49140
diff
changeset
|
837 Py_XDECREF(empty_tuple); |
|
dbd54bac0b6f
Fixed #4067 by implementing _attributes and _fields for the AST root node.
Armin Ronacher <armin.ronacher@active-4.com>
parents:
49140
diff
changeset
|
838 return -1; |
|
dbd54bac0b6f
Fixed #4067 by implementing _attributes and _fields for the AST root node.
Armin Ronacher <armin.ronacher@active-4.com>
parents:
49140
diff
changeset
|
839 } |
|
dbd54bac0b6f
Fixed #4067 by implementing _attributes and _fields for the AST root node.
Armin Ronacher <armin.ronacher@active-4.com>
parents:
49140
diff
changeset
|
840 Py_DECREF(empty_tuple); |
|
dbd54bac0b6f
Fixed #4067 by implementing _attributes and _fields for the AST root node.
Armin Ronacher <armin.ronacher@active-4.com>
parents:
49140
diff
changeset
|
841 return 0; |
|
dbd54bac0b6f
Fixed #4067 by implementing _attributes and _fields for the AST root node.
Armin Ronacher <armin.ronacher@active-4.com>
parents:
49140
diff
changeset
|
842 } |
|
dbd54bac0b6f
Fixed #4067 by implementing _attributes and _fields for the AST root node.
Armin Ronacher <armin.ronacher@active-4.com>
parents:
49140
diff
changeset
|
843 |
|
36660
3c0b26113df6
Generate code to recursively copy an AST into
Martin v. Löwis <martin@v.loewis.de>
parents:
36121
diff
changeset
|
844 """, 0, reflow=False) |
|
3c0b26113df6
Generate code to recursively copy an AST into
Martin v. Löwis <martin@v.loewis.de>
parents:
36121
diff
changeset
|
845 |
|
3c0b26113df6
Generate code to recursively copy an AST into
Martin v. Löwis <martin@v.loewis.de>
parents:
36121
diff
changeset
|
846 self.emit("static int init_types(void)",0) |
|
3c0b26113df6
Generate code to recursively copy an AST into
Martin v. Löwis <martin@v.loewis.de>
parents:
36121
diff
changeset
|
847 self.emit("{", 0) |
|
37412
dfc33ec7f73a
* Fix a refleak of *_attributes.
Neal Norwitz <nnorwitz@gmail.com>
parents:
36794
diff
changeset
|
848 self.emit("static int initialized;", 1) |
|
36660
3c0b26113df6
Generate code to recursively copy an AST into
Martin v. Löwis <martin@v.loewis.de>
parents:
36121
diff
changeset
|
849 self.emit("if (initialized) return 1;", 1) |
|
49551
dbd54bac0b6f
Fixed #4067 by implementing _attributes and _fields for the AST root node.
Armin Ronacher <armin.ronacher@active-4.com>
parents:
49140
diff
changeset
|
850 self.emit("if (add_ast_fields() < 0) return 0;", 1) |
|
36660
3c0b26113df6
Generate code to recursively copy an AST into
Martin v. Löwis <martin@v.loewis.de>
parents:
36121
diff
changeset
|
851 for dfn in mod.dfns: |
|
3c0b26113df6
Generate code to recursively copy an AST into
Martin v. Löwis <martin@v.loewis.de>
parents:
36121
diff
changeset
|
852 self.visit(dfn) |
| 36669 | 853 self.emit("initialized = 1;", 1) |
| 854 self.emit("return 1;", 1); | |
|
36660
3c0b26113df6
Generate code to recursively copy an AST into
Martin v. Löwis <martin@v.loewis.de>
parents:
36121
diff
changeset
|
855 self.emit("}", 0) |
|
3c0b26113df6
Generate code to recursively copy an AST into
Martin v. Löwis <martin@v.loewis.de>
parents:
36121
diff
changeset
|
856 |
|
3c0b26113df6
Generate code to recursively copy an AST into
Martin v. Löwis <martin@v.loewis.de>
parents:
36121
diff
changeset
|
857 def visitProduct(self, prod, name): |
|
36664
7bfbfb2ef4e2
Stop generating empty arrays.
Martin v. Löwis <martin@v.loewis.de>
parents:
36661
diff
changeset
|
858 if prod.fields: |
|
7bfbfb2ef4e2
Stop generating empty arrays.
Martin v. Löwis <martin@v.loewis.de>
parents:
36661
diff
changeset
|
859 fields = name.value+"_fields" |
|
7bfbfb2ef4e2
Stop generating empty arrays.
Martin v. Löwis <martin@v.loewis.de>
parents:
36661
diff
changeset
|
860 else: |
|
7bfbfb2ef4e2
Stop generating empty arrays.
Martin v. Löwis <martin@v.loewis.de>
parents:
36661
diff
changeset
|
861 fields = "NULL" |
|
46202
42c5d287a852
#2505: allow easier creation of AST nodes.
Georg Brandl <georg@python.org>
parents:
46193
diff
changeset
|
862 self.emit('%s_type = make_type("%s", &AST_type, %s, %d);' % |
|
36664
7bfbfb2ef4e2
Stop generating empty arrays.
Martin v. Löwis <martin@v.loewis.de>
parents:
36661
diff
changeset
|
863 (name, name, fields, len(prod.fields)), 1) |
| 36669 | 864 self.emit("if (!%s_type) return 0;" % name, 1) |
|
36721
f7b4726ada6c
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
36708
diff
changeset
|
865 |
|
36660
3c0b26113df6
Generate code to recursively copy an AST into
Martin v. Löwis <martin@v.loewis.de>
parents:
36121
diff
changeset
|
866 def visitSum(self, sum, name): |
|
46202
42c5d287a852
#2505: allow easier creation of AST nodes.
Georg Brandl <georg@python.org>
parents:
46193
diff
changeset
|
867 self.emit('%s_type = make_type("%s", &AST_type, NULL, 0);' % |
|
42c5d287a852
#2505: allow easier creation of AST nodes.
Georg Brandl <georg@python.org>
parents:
46193
diff
changeset
|
868 (name, name), 1) |
| 36669 | 869 self.emit("if (!%s_type) return 0;" % name, 1) |
| 870 if sum.attributes: | |
|
36721
f7b4726ada6c
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
36708
diff
changeset
|
871 self.emit("if (!add_attributes(%s_type, %s_attributes, %d)) return 0;" % |
| 36669 | 872 (name, name, len(sum.attributes)), 1) |
| 873 else: | |
| 874 self.emit("if (!add_attributes(%s_type, NULL, 0)) return 0;" % name, 1) | |
|
36660
3c0b26113df6
Generate code to recursively copy an AST into
Martin v. Löwis <martin@v.loewis.de>
parents:
36121
diff
changeset
|
875 simple = is_simple(sum) |
|
3c0b26113df6
Generate code to recursively copy an AST into
Martin v. Löwis <martin@v.loewis.de>
parents:
36121
diff
changeset
|
876 for t in sum.types: |
|
3c0b26113df6
Generate code to recursively copy an AST into
Martin v. Löwis <martin@v.loewis.de>
parents:
36121
diff
changeset
|
877 self.visitConstructor(t, name, simple) |
|
36721
f7b4726ada6c
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
36708
diff
changeset
|
878 |
|
36660
3c0b26113df6
Generate code to recursively copy an AST into
Martin v. Löwis <martin@v.loewis.de>
parents:
36121
diff
changeset
|
879 def visitConstructor(self, cons, name, simple): |
|
36664
7bfbfb2ef4e2
Stop generating empty arrays.
Martin v. Löwis <martin@v.loewis.de>
parents:
36661
diff
changeset
|
880 if cons.fields: |
|
7bfbfb2ef4e2
Stop generating empty arrays.
Martin v. Löwis <martin@v.loewis.de>
parents:
36661
diff
changeset
|
881 fields = cons.name.value+"_fields" |
|
7bfbfb2ef4e2
Stop generating empty arrays.
Martin v. Löwis <martin@v.loewis.de>
parents:
36661
diff
changeset
|
882 else: |
|
7bfbfb2ef4e2
Stop generating empty arrays.
Martin v. Löwis <martin@v.loewis.de>
parents:
36661
diff
changeset
|
883 fields = "NULL" |
|
36721
f7b4726ada6c
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
36708
diff
changeset
|
884 self.emit('%s_type = make_type("%s", %s_type, %s, %d);' % |
|
36664
7bfbfb2ef4e2
Stop generating empty arrays.
Martin v. Löwis <martin@v.loewis.de>
parents:
36661
diff
changeset
|
885 (cons.name, cons.name, name, fields, len(cons.fields)), 1) |
| 36669 | 886 self.emit("if (!%s_type) return 0;" % cons.name, 1) |
|
36660
3c0b26113df6
Generate code to recursively copy an AST into
Martin v. Löwis <martin@v.loewis.de>
parents:
36121
diff
changeset
|
887 if simple: |
|
3c0b26113df6
Generate code to recursively copy an AST into
Martin v. Löwis <martin@v.loewis.de>
parents:
36121
diff
changeset
|
888 self.emit("%s_singleton = PyType_GenericNew(%s_type, NULL, NULL);" % |
|
3c0b26113df6
Generate code to recursively copy an AST into
Martin v. Löwis <martin@v.loewis.de>
parents:
36121
diff
changeset
|
889 (cons.name, cons.name), 1) |
| 36669 | 890 self.emit("if (!%s_singleton) return 0;" % cons.name, 1) |
|
36721
f7b4726ada6c
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
36708
diff
changeset
|
891 |
|
46162
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
892 |
|
40824
180d4e899683
Modify Parser/asdl_c.py so that the __version__ number for Python/Python-ast.c
Brett Cannon <bcannon@gmail.com>
parents:
40668
diff
changeset
|
893 def parse_version(mod): |
|
180d4e899683
Modify Parser/asdl_c.py so that the __version__ number for Python/Python-ast.c
Brett Cannon <bcannon@gmail.com>
parents:
40668
diff
changeset
|
894 return mod.version.value[12:-3] |
|
180d4e899683
Modify Parser/asdl_c.py so that the __version__ number for Python/Python-ast.c
Brett Cannon <bcannon@gmail.com>
parents:
40668
diff
changeset
|
895 |
| 36669 | 896 class ASTModuleVisitor(PickleVisitor): |
| 897 | |
| 898 def visitModule(self, mod): | |
| 899 self.emit("PyMODINIT_FUNC", 0) | |
| 900 self.emit("init_ast(void)", 0) | |
| 901 self.emit("{", 0) | |
| 902 self.emit("PyObject *m, *d;", 1) | |
| 903 self.emit("if (!init_types()) return;", 1) | |
| 904 self.emit('m = Py_InitModule3("_ast", NULL, NULL);', 1) | |
| 905 self.emit("if (!m) return;", 1) | |
| 906 self.emit("d = PyModule_GetDict(m);", 1) | |
|
46202
42c5d287a852
#2505: allow easier creation of AST nodes.
Georg Brandl <georg@python.org>
parents:
46193
diff
changeset
|
907 self.emit('if (PyDict_SetItemString(d, "AST", (PyObject*)&AST_type) < 0) return;', 1) |
| 36669 | 908 self.emit('if (PyModule_AddIntConstant(m, "PyCF_ONLY_AST", PyCF_ONLY_AST) < 0)', 1) |
| 909 self.emit("return;", 2) | |
|
36705
8ff28fcf872e
Add generation of the version.
Martin v. Löwis <martin@v.loewis.de>
parents:
36703
diff
changeset
|
910 # Value of version: "$Revision$" |
|
40824
180d4e899683
Modify Parser/asdl_c.py so that the __version__ number for Python/Python-ast.c
Brett Cannon <bcannon@gmail.com>
parents:
40668
diff
changeset
|
911 self.emit('if (PyModule_AddStringConstant(m, "__version__", "%s") < 0)' |
|
180d4e899683
Modify Parser/asdl_c.py so that the __version__ number for Python/Python-ast.c
Brett Cannon <bcannon@gmail.com>
parents:
40668
diff
changeset
|
912 % parse_version(mod), 1) |
|
36708
81be15b84244
Generate return statement.
Martin v. Löwis <martin@v.loewis.de>
parents:
36705
diff
changeset
|
913 self.emit("return;", 2) |
| 36669 | 914 for dfn in mod.dfns: |
| 915 self.visit(dfn) | |
| 916 self.emit("}", 0) | |
| 917 | |
| 918 def visitProduct(self, prod, name): | |
| 919 self.addObj(name) | |
|
36721
f7b4726ada6c
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
36708
diff
changeset
|
920 |
| 36669 | 921 def visitSum(self, sum, name): |
| 922 self.addObj(name) | |
| 923 for t in sum.types: | |
| 924 self.visitConstructor(t, name) | |
|
36721
f7b4726ada6c
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
36708
diff
changeset
|
925 |
| 36669 | 926 def visitConstructor(self, cons, name): |
| 927 self.addObj(cons.name) | |
|
36721
f7b4726ada6c
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
36708
diff
changeset
|
928 |
| 36669 | 929 def addObj(self, name): |
|
37412
dfc33ec7f73a
* Fix a refleak of *_attributes.
Neal Norwitz <nnorwitz@gmail.com>
parents:
36794
diff
changeset
|
930 self.emit('if (PyDict_SetItemString(d, "%s", (PyObject*)%s_type) < 0) return;' % (name, name), 1) |
| 35742 | 931 |
|
46162
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
932 |
| 35742 | 933 _SPECIALIZED_SEQUENCES = ('stmt', 'expr') |
| 934 | |
| 935 def find_sequence(fields, doing_specialization): | |
| 936 """Return True if any field uses a sequence.""" | |
| 937 for f in fields: | |
| 938 if f.seq: | |
| 939 if not doing_specialization: | |
| 940 return True | |
| 941 if str(f.type) not in _SPECIALIZED_SEQUENCES: | |
| 942 return True | |
| 943 return False | |
| 944 | |
| 945 def has_sequence(types, doing_specialization): | |
| 946 for t in types: | |
| 947 if find_sequence(t.fields, doing_specialization): | |
| 948 return True | |
| 949 return False | |
| 950 | |
| 951 | |
| 952 class StaticVisitor(PickleVisitor): | |
|
35866
9f73dc0953d2
Whoops, checkin consistent versions of *all* files to stop polluting
Neal Norwitz <nnorwitz@gmail.com>
parents:
35862
diff
changeset
|
953 CODE = '''Very simple, always emit this static code. Overide CODE''' |
|
9f73dc0953d2
Whoops, checkin consistent versions of *all* files to stop polluting
Neal Norwitz <nnorwitz@gmail.com>
parents:
35862
diff
changeset
|
954 |
|
9f73dc0953d2
Whoops, checkin consistent versions of *all* files to stop polluting
Neal Norwitz <nnorwitz@gmail.com>
parents:
35862
diff
changeset
|
955 def visit(self, object): |
|
9f73dc0953d2
Whoops, checkin consistent versions of *all* files to stop polluting
Neal Norwitz <nnorwitz@gmail.com>
parents:
35862
diff
changeset
|
956 self.emit(self.CODE, 0, reflow=False) |
|
9f73dc0953d2
Whoops, checkin consistent versions of *all* files to stop polluting
Neal Norwitz <nnorwitz@gmail.com>
parents:
35862
diff
changeset
|
957 |
|
46162
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
958 |
|
36660
3c0b26113df6
Generate code to recursively copy an AST into
Martin v. Löwis <martin@v.loewis.de>
parents:
36121
diff
changeset
|
959 class ObjVisitor(PickleVisitor): |
| 35742 | 960 |
| 36669 | 961 def func_begin(self, name): |
| 35742 | 962 ctype = get_c_type(name) |
|
36660
3c0b26113df6
Generate code to recursively copy an AST into
Martin v. Löwis <martin@v.loewis.de>
parents:
36121
diff
changeset
|
963 self.emit("PyObject*", 0) |
|
3c0b26113df6
Generate code to recursively copy an AST into
Martin v. Löwis <martin@v.loewis.de>
parents:
36121
diff
changeset
|
964 self.emit("ast2obj_%s(void* _o)" % (name), 0) |
| 35742 | 965 self.emit("{", 0) |
|
36660
3c0b26113df6
Generate code to recursively copy an AST into
Martin v. Löwis <martin@v.loewis.de>
parents:
36121
diff
changeset
|
966 self.emit("%s o = (%s)_o;" % (ctype, ctype), 1) |
|
3c0b26113df6
Generate code to recursively copy an AST into
Martin v. Löwis <martin@v.loewis.de>
parents:
36121
diff
changeset
|
967 self.emit("PyObject *result = NULL, *value = NULL;", 1) |
|
3c0b26113df6
Generate code to recursively copy an AST into
Martin v. Löwis <martin@v.loewis.de>
parents:
36121
diff
changeset
|
968 self.emit('if (!o) {', 1) |
|
3c0b26113df6
Generate code to recursively copy an AST into
Martin v. Löwis <martin@v.loewis.de>
parents:
36121
diff
changeset
|
969 self.emit("Py_INCREF(Py_None);", 2) |
|
3c0b26113df6
Generate code to recursively copy an AST into
Martin v. Löwis <martin@v.loewis.de>
parents:
36121
diff
changeset
|
970 self.emit('return Py_None;', 2) |
|
3c0b26113df6
Generate code to recursively copy an AST into
Martin v. Löwis <martin@v.loewis.de>
parents:
36121
diff
changeset
|
971 self.emit("}", 1) |
| 35742 | 972 self.emit('', 0) |
| 973 | |
| 36669 | 974 def func_end(self): |
|
36660
3c0b26113df6
Generate code to recursively copy an AST into
Martin v. Löwis <martin@v.loewis.de>
parents:
36121
diff
changeset
|
975 self.emit("return result;", 1) |
|
3c0b26113df6
Generate code to recursively copy an AST into
Martin v. Löwis <martin@v.loewis.de>
parents:
36121
diff
changeset
|
976 self.emit("failed:", 0) |
|
3c0b26113df6
Generate code to recursively copy an AST into
Martin v. Löwis <martin@v.loewis.de>
parents:
36121
diff
changeset
|
977 self.emit("Py_XDECREF(value);", 1) |
|
3c0b26113df6
Generate code to recursively copy an AST into
Martin v. Löwis <martin@v.loewis.de>
parents:
36121
diff
changeset
|
978 self.emit("Py_XDECREF(result);", 1) |
|
3c0b26113df6
Generate code to recursively copy an AST into
Martin v. Löwis <martin@v.loewis.de>
parents:
36121
diff
changeset
|
979 self.emit("return NULL;", 1) |
| 35742 | 980 self.emit("}", 0) |
| 981 self.emit("", 0) | |
| 982 | |
| 983 def visitSum(self, sum, name): | |
|
36660
3c0b26113df6
Generate code to recursively copy an AST into
Martin v. Löwis <martin@v.loewis.de>
parents:
36121
diff
changeset
|
984 if is_simple(sum): |
|
3c0b26113df6
Generate code to recursively copy an AST into
Martin v. Löwis <martin@v.loewis.de>
parents:
36121
diff
changeset
|
985 self.simpleSum(sum, name) |
|
3c0b26113df6
Generate code to recursively copy an AST into
Martin v. Löwis <martin@v.loewis.de>
parents:
36121
diff
changeset
|
986 return |
| 36669 | 987 self.func_begin(name) |
|
36660
3c0b26113df6
Generate code to recursively copy an AST into
Martin v. Löwis <martin@v.loewis.de>
parents:
36121
diff
changeset
|
988 self.emit("switch (o->kind) {", 1) |
|
3c0b26113df6
Generate code to recursively copy an AST into
Martin v. Löwis <martin@v.loewis.de>
parents:
36121
diff
changeset
|
989 for i in range(len(sum.types)): |
|
3c0b26113df6
Generate code to recursively copy an AST into
Martin v. Löwis <martin@v.loewis.de>
parents:
36121
diff
changeset
|
990 t = sum.types[i] |
|
3c0b26113df6
Generate code to recursively copy an AST into
Martin v. Löwis <martin@v.loewis.de>
parents:
36121
diff
changeset
|
991 self.visitConstructor(t, i + 1, name) |
|
3c0b26113df6
Generate code to recursively copy an AST into
Martin v. Löwis <martin@v.loewis.de>
parents:
36121
diff
changeset
|
992 self.emit("}", 1) |
| 36669 | 993 for a in sum.attributes: |
| 994 self.emit("value = ast2obj_%s(o->%s);" % (a.type, a.name), 1) | |
| 995 self.emit("if (!value) goto failed;", 1) | |
|
36794
df843b07f5fe
Fix memory leak on attributes.
Martin v. Löwis <martin@v.loewis.de>
parents:
36743
diff
changeset
|
996 self.emit('if (PyObject_SetAttrString(result, "%s", value) < 0)' % a.name, 1) |
|
df843b07f5fe
Fix memory leak on attributes.
Martin v. Löwis <martin@v.loewis.de>
parents:
36743
diff
changeset
|
997 self.emit('goto failed;', 2) |
|
df843b07f5fe
Fix memory leak on attributes.
Martin v. Löwis <martin@v.loewis.de>
parents:
36743
diff
changeset
|
998 self.emit('Py_DECREF(value);', 1) |
| 36669 | 999 self.func_end() |
|
36721
f7b4726ada6c
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
36708
diff
changeset
|
1000 |
|
36660
3c0b26113df6
Generate code to recursively copy an AST into
Martin v. Löwis <martin@v.loewis.de>
parents:
36121
diff
changeset
|
1001 def simpleSum(self, sum, name): |
|
3c0b26113df6
Generate code to recursively copy an AST into
Martin v. Löwis <martin@v.loewis.de>
parents:
36121
diff
changeset
|
1002 self.emit("PyObject* ast2obj_%s(%s_ty o)" % (name, name), 0) |
|
3c0b26113df6
Generate code to recursively copy an AST into
Martin v. Löwis <martin@v.loewis.de>
parents:
36121
diff
changeset
|
1003 self.emit("{", 0) |
|
3c0b26113df6
Generate code to recursively copy an AST into
Martin v. Löwis <martin@v.loewis.de>
parents:
36121
diff
changeset
|
1004 self.emit("switch(o) {", 1) |
|
3c0b26113df6
Generate code to recursively copy an AST into
Martin v. Löwis <martin@v.loewis.de>
parents:
36121
diff
changeset
|
1005 for t in sum.types: |
|
3c0b26113df6
Generate code to recursively copy an AST into
Martin v. Löwis <martin@v.loewis.de>
parents:
36121
diff
changeset
|
1006 self.emit("case %s:" % t.name, 2) |
|
3c0b26113df6
Generate code to recursively copy an AST into
Martin v. Löwis <martin@v.loewis.de>
parents:
36121
diff
changeset
|
1007 self.emit("Py_INCREF(%s_singleton);" % t.name, 3) |
|
3c0b26113df6
Generate code to recursively copy an AST into
Martin v. Löwis <martin@v.loewis.de>
parents:
36121
diff
changeset
|
1008 self.emit("return %s_singleton;" % t.name, 3) |
|
46162
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
1009 self.emit("default:" % name, 2) |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
1010 self.emit('/* should never happen, but just in case ... */', 3) |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
1011 code = "PyErr_Format(PyExc_SystemError, \"unknown %s found\");" % name |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
1012 self.emit(code, 3, reflow=False) |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
1013 self.emit("return NULL;", 3) |
|
36660
3c0b26113df6
Generate code to recursively copy an AST into
Martin v. Löwis <martin@v.loewis.de>
parents:
36121
diff
changeset
|
1014 self.emit("}", 1) |
|
3c0b26113df6
Generate code to recursively copy an AST into
Martin v. Löwis <martin@v.loewis.de>
parents:
36121
diff
changeset
|
1015 self.emit("}", 0) |
| 35742 | 1016 |
| 1017 def visitProduct(self, prod, name): | |
| 36669 | 1018 self.func_begin(name) |
|
36660
3c0b26113df6
Generate code to recursively copy an AST into
Martin v. Löwis <martin@v.loewis.de>
parents:
36121
diff
changeset
|
1019 self.emit("result = PyType_GenericNew(%s_type, NULL, NULL);" % name, 1); |
|
3c0b26113df6
Generate code to recursively copy an AST into
Martin v. Löwis <martin@v.loewis.de>
parents:
36121
diff
changeset
|
1020 self.emit("if (!result) return NULL;", 1) |
| 35742 | 1021 for field in prod.fields: |
| 1022 self.visitField(field, name, 1, True) | |
| 36669 | 1023 self.func_end() |
|
36121
686306a67e9b
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
36065
diff
changeset
|
1024 |
| 35742 | 1025 def visitConstructor(self, cons, enum, name): |
| 1026 self.emit("case %s_kind:" % cons.name, 1) | |
|
36660
3c0b26113df6
Generate code to recursively copy an AST into
Martin v. Löwis <martin@v.loewis.de>
parents:
36121
diff
changeset
|
1027 self.emit("result = PyType_GenericNew(%s_type, NULL, NULL);" % cons.name, 2); |
|
3c0b26113df6
Generate code to recursively copy an AST into
Martin v. Löwis <martin@v.loewis.de>
parents:
36121
diff
changeset
|
1028 self.emit("if (!result) goto failed;", 2) |
| 35742 | 1029 for f in cons.fields: |
| 1030 self.visitField(f, cons.name, 2, False) | |
| 1031 self.emit("break;", 2) | |
| 1032 | |
| 1033 def visitField(self, field, name, depth, product): | |
| 1034 def emit(s, d): | |
| 1035 self.emit(s, depth + d) | |
| 1036 if product: | |
| 1037 value = "o->%s" % field.name | |
| 1038 else: | |
| 1039 value = "o->v.%s.%s" % (name, field.name) | |
|
36660
3c0b26113df6
Generate code to recursively copy an AST into
Martin v. Löwis <martin@v.loewis.de>
parents:
36121
diff
changeset
|
1040 self.set(field, value, depth) |
|
3c0b26113df6
Generate code to recursively copy an AST into
Martin v. Löwis <martin@v.loewis.de>
parents:
36121
diff
changeset
|
1041 emit("if (!value) goto failed;", 0) |
|
3c0b26113df6
Generate code to recursively copy an AST into
Martin v. Löwis <martin@v.loewis.de>
parents:
36121
diff
changeset
|
1042 emit('if (PyObject_SetAttrString(result, "%s", value) == -1)' % field.name, 0) |
|
3c0b26113df6
Generate code to recursively copy an AST into
Martin v. Löwis <martin@v.loewis.de>
parents:
36121
diff
changeset
|
1043 emit("goto failed;", 1) |
|
3c0b26113df6
Generate code to recursively copy an AST into
Martin v. Löwis <martin@v.loewis.de>
parents:
36121
diff
changeset
|
1044 emit("Py_DECREF(value);", 0) |
| 35742 | 1045 |
| 1046 def emitSeq(self, field, value, depth, emit): | |
|
36660
3c0b26113df6
Generate code to recursively copy an AST into
Martin v. Löwis <martin@v.loewis.de>
parents:
36121
diff
changeset
|
1047 emit("seq = %s;" % value, 0) |
|
3c0b26113df6
Generate code to recursively copy an AST into
Martin v. Löwis <martin@v.loewis.de>
parents:
36121
diff
changeset
|
1048 emit("n = asdl_seq_LEN(seq);", 0) |
|
3c0b26113df6
Generate code to recursively copy an AST into
Martin v. Löwis <martin@v.loewis.de>
parents:
36121
diff
changeset
|
1049 emit("value = PyList_New(n);", 0) |
|
3c0b26113df6
Generate code to recursively copy an AST into
Martin v. Löwis <martin@v.loewis.de>
parents:
36121
diff
changeset
|
1050 emit("if (!value) goto failed;", 0) |
|
3c0b26113df6
Generate code to recursively copy an AST into
Martin v. Löwis <martin@v.loewis.de>
parents:
36121
diff
changeset
|
1051 emit("for (i = 0; i < n; i++) {", 0) |
|
3c0b26113df6
Generate code to recursively copy an AST into
Martin v. Löwis <martin@v.loewis.de>
parents:
36121
diff
changeset
|
1052 self.set("value", field, "asdl_seq_GET(seq, i)", depth + 1) |
|
3c0b26113df6
Generate code to recursively copy an AST into
Martin v. Löwis <martin@v.loewis.de>
parents:
36121
diff
changeset
|
1053 emit("if (!value1) goto failed;", 1) |
|
3c0b26113df6
Generate code to recursively copy an AST into
Martin v. Löwis <martin@v.loewis.de>
parents:
36121
diff
changeset
|
1054 emit("PyList_SET_ITEM(value, i, value1);", 1) |
|
3c0b26113df6
Generate code to recursively copy an AST into
Martin v. Löwis <martin@v.loewis.de>
parents:
36121
diff
changeset
|
1055 emit("value1 = NULL;", 1) |
|
3c0b26113df6
Generate code to recursively copy an AST into
Martin v. Löwis <martin@v.loewis.de>
parents:
36121
diff
changeset
|
1056 emit("}", 0) |
| 35742 | 1057 |
|
36660
3c0b26113df6
Generate code to recursively copy an AST into
Martin v. Löwis <martin@v.loewis.de>
parents:
36121
diff
changeset
|
1058 def set(self, field, value, depth): |
|
3c0b26113df6
Generate code to recursively copy an AST into
Martin v. Löwis <martin@v.loewis.de>
parents:
36121
diff
changeset
|
1059 if field.seq: |
|
36661
710cbdd478a8
Fix iterating over cmpop_ty lists.
Martin v. Löwis <martin@v.loewis.de>
parents:
36660
diff
changeset
|
1060 # XXX should really check for is_simple, but that requires a symbol table |
|
36660
3c0b26113df6
Generate code to recursively copy an AST into
Martin v. Löwis <martin@v.loewis.de>
parents:
36121
diff
changeset
|
1061 if field.type.value == "cmpop": |
|
36661
710cbdd478a8
Fix iterating over cmpop_ty lists.
Martin v. Löwis <martin@v.loewis.de>
parents:
36660
diff
changeset
|
1062 # While the sequence elements are stored as void*, |
|
710cbdd478a8
Fix iterating over cmpop_ty lists.
Martin v. Löwis <martin@v.loewis.de>
parents:
36660
diff
changeset
|
1063 # ast2obj_cmpop expects an enum |
|
710cbdd478a8
Fix iterating over cmpop_ty lists.
Martin v. Löwis <martin@v.loewis.de>
parents:
36660
diff
changeset
|
1064 self.emit("{", depth) |
|
710cbdd478a8
Fix iterating over cmpop_ty lists.
Martin v. Löwis <martin@v.loewis.de>
parents:
36660
diff
changeset
|
1065 self.emit("int i, n = asdl_seq_LEN(%s);" % value, depth+1) |
|
710cbdd478a8
Fix iterating over cmpop_ty lists.
Martin v. Löwis <martin@v.loewis.de>
parents:
36660
diff
changeset
|
1066 self.emit("value = PyList_New(n);", depth+1) |
|
710cbdd478a8
Fix iterating over cmpop_ty lists.
Martin v. Löwis <martin@v.loewis.de>
parents:
36660
diff
changeset
|
1067 self.emit("if (!value) goto failed;", depth+1) |
|
710cbdd478a8
Fix iterating over cmpop_ty lists.
Martin v. Löwis <martin@v.loewis.de>
parents:
36660
diff
changeset
|
1068 self.emit("for(i = 0; i < n; i++)", depth+1) |
|
710cbdd478a8
Fix iterating over cmpop_ty lists.
Martin v. Löwis <martin@v.loewis.de>
parents:
36660
diff
changeset
|
1069 # This cannot fail, so no need for error handling |
|
37685
5e72b0c74100
Introduce asdl_int_seq, to hold cmpop_ty.
Martin v. Löwis <martin@v.loewis.de>
parents:
37599
diff
changeset
|
1070 self.emit("PyList_SET_ITEM(value, i, ast2obj_cmpop((cmpop_ty)asdl_seq_GET(%s, i)));" % value, |
|
37599
44fb03a6a524
Make _kind types global for C++ compilation.
Martin v. Löwis <martin@v.loewis.de>
parents:
37462
diff
changeset
|
1071 depth+2, reflow=False) |
|
36661
710cbdd478a8
Fix iterating over cmpop_ty lists.
Martin v. Löwis <martin@v.loewis.de>
parents:
36660
diff
changeset
|
1072 self.emit("}", depth) |
|
36660
3c0b26113df6
Generate code to recursively copy an AST into
Martin v. Löwis <martin@v.loewis.de>
parents:
36121
diff
changeset
|
1073 else: |
|
36661
710cbdd478a8
Fix iterating over cmpop_ty lists.
Martin v. Löwis <martin@v.loewis.de>
parents:
36660
diff
changeset
|
1074 self.emit("value = ast2obj_list(%s, ast2obj_%s);" % (value, field.type), depth) |
| 35742 | 1075 else: |
| 1076 ctype = get_c_type(field.type) | |
|
36660
3c0b26113df6
Generate code to recursively copy an AST into
Martin v. Löwis <martin@v.loewis.de>
parents:
36121
diff
changeset
|
1077 self.emit("value = ast2obj_%s(%s);" % (field.type, value), depth, reflow=False) |
|
36121
686306a67e9b
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
36065
diff
changeset
|
1078 |
| 35742 | 1079 |
|
36660
3c0b26113df6
Generate code to recursively copy an AST into
Martin v. Löwis <martin@v.loewis.de>
parents:
36121
diff
changeset
|
1080 class PartingShots(StaticVisitor): |
|
3c0b26113df6
Generate code to recursively copy an AST into
Martin v. Löwis <martin@v.loewis.de>
parents:
36121
diff
changeset
|
1081 |
|
3c0b26113df6
Generate code to recursively copy an AST into
Martin v. Löwis <martin@v.loewis.de>
parents:
36121
diff
changeset
|
1082 CODE = """ |
|
3c0b26113df6
Generate code to recursively copy an AST into
Martin v. Löwis <martin@v.loewis.de>
parents:
36121
diff
changeset
|
1083 PyObject* PyAST_mod2obj(mod_ty t) |
|
3c0b26113df6
Generate code to recursively copy an AST into
Martin v. Löwis <martin@v.loewis.de>
parents:
36121
diff
changeset
|
1084 { |
|
3c0b26113df6
Generate code to recursively copy an AST into
Martin v. Löwis <martin@v.loewis.de>
parents:
36121
diff
changeset
|
1085 init_types(); |
|
3c0b26113df6
Generate code to recursively copy an AST into
Martin v. Löwis <martin@v.loewis.de>
parents:
36121
diff
changeset
|
1086 return ast2obj_mod(t); |
|
3c0b26113df6
Generate code to recursively copy an AST into
Martin v. Löwis <martin@v.loewis.de>
parents:
36121
diff
changeset
|
1087 } |
|
46162
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
1088 |
|
46193
b0bf06d49647
Properly check for consistency with the third argument of
Georg Brandl <georg@python.org>
parents:
46169
diff
changeset
|
1089 /* mode is 0 for "exec", 1 for "eval" and 2 for "single" input */ |
|
b0bf06d49647
Properly check for consistency with the third argument of
Georg Brandl <georg@python.org>
parents:
46169
diff
changeset
|
1090 mod_ty PyAST_obj2mod(PyObject* ast, PyArena* arena, int mode) |
|
46162
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
1091 { |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
1092 mod_ty res; |
|
46193
b0bf06d49647
Properly check for consistency with the third argument of
Georg Brandl <georg@python.org>
parents:
46169
diff
changeset
|
1093 PyObject *req_type[] = {(PyObject*)Module_type, (PyObject*)Expression_type, |
|
b0bf06d49647
Properly check for consistency with the third argument of
Georg Brandl <georg@python.org>
parents:
46169
diff
changeset
|
1094 (PyObject*)Interactive_type}; |
|
b0bf06d49647
Properly check for consistency with the third argument of
Georg Brandl <georg@python.org>
parents:
46169
diff
changeset
|
1095 char *req_name[] = {"Module", "Expression", "Interactive"}; |
|
b0bf06d49647
Properly check for consistency with the third argument of
Georg Brandl <georg@python.org>
parents:
46169
diff
changeset
|
1096 assert(0 <= mode && mode <= 2); |
|
b0bf06d49647
Properly check for consistency with the third argument of
Georg Brandl <georg@python.org>
parents:
46169
diff
changeset
|
1097 |
|
46162
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
1098 init_types(); |
|
46193
b0bf06d49647
Properly check for consistency with the third argument of
Georg Brandl <georg@python.org>
parents:
46169
diff
changeset
|
1099 |
|
b0bf06d49647
Properly check for consistency with the third argument of
Georg Brandl <georg@python.org>
parents:
46169
diff
changeset
|
1100 if (!PyObject_IsInstance(ast, req_type[mode])) { |
|
b0bf06d49647
Properly check for consistency with the third argument of
Georg Brandl <georg@python.org>
parents:
46169
diff
changeset
|
1101 PyErr_Format(PyExc_TypeError, "expected %s node, got %.400s", |
|
b0bf06d49647
Properly check for consistency with the third argument of
Georg Brandl <georg@python.org>
parents:
46169
diff
changeset
|
1102 req_name[mode], Py_TYPE(ast)->tp_name); |
|
46162
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
1103 return NULL; |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
1104 } |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
1105 if (obj2ast_mod(ast, &res, arena) != 0) |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
1106 return NULL; |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
1107 else |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
1108 return res; |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
1109 } |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
1110 |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
1111 int PyAST_Check(PyObject* obj) |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
1112 { |
|
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
1113 init_types(); |
|
46202
42c5d287a852
#2505: allow easier creation of AST nodes.
Georg Brandl <georg@python.org>
parents:
46193
diff
changeset
|
1114 return PyObject_IsInstance(obj, (PyObject*)&AST_type); |
|
46162
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
1115 } |
|
36660
3c0b26113df6
Generate code to recursively copy an AST into
Martin v. Löwis <martin@v.loewis.de>
parents:
36121
diff
changeset
|
1116 """ |
| 35742 | 1117 |
| 1118 class ChainOfVisitors: | |
| 1119 def __init__(self, *visitors): | |
| 1120 self.visitors = visitors | |
| 1121 | |
| 1122 def visit(self, object): | |
| 1123 for v in self.visitors: | |
| 1124 v.visit(object) | |
|
35866
9f73dc0953d2
Whoops, checkin consistent versions of *all* files to stop polluting
Neal Norwitz <nnorwitz@gmail.com>
parents:
35862
diff
changeset
|
1125 v.emit("", 0) |
| 35742 | 1126 |
|
46228
e3dc867b6182
Use file.write instead of print to make it easier to merge with 3k.
Neal Norwitz <nnorwitz@gmail.com>
parents:
46212
diff
changeset
|
1127 common_msg = "/* File automatically generated by %s. */\n\n" |
|
40824
180d4e899683
Modify Parser/asdl_c.py so that the __version__ number for Python/Python-ast.c
Brett Cannon <bcannon@gmail.com>
parents:
40668
diff
changeset
|
1128 |
|
180d4e899683
Modify Parser/asdl_c.py so that the __version__ number for Python/Python-ast.c
Brett Cannon <bcannon@gmail.com>
parents:
40668
diff
changeset
|
1129 c_file_msg = """ |
|
180d4e899683
Modify Parser/asdl_c.py so that the __version__ number for Python/Python-ast.c
Brett Cannon <bcannon@gmail.com>
parents:
40668
diff
changeset
|
1130 /* |
|
180d4e899683
Modify Parser/asdl_c.py so that the __version__ number for Python/Python-ast.c
Brett Cannon <bcannon@gmail.com>
parents:
40668
diff
changeset
|
1131 __version__ %s. |
|
180d4e899683
Modify Parser/asdl_c.py so that the __version__ number for Python/Python-ast.c
Brett Cannon <bcannon@gmail.com>
parents:
40668
diff
changeset
|
1132 |
|
180d4e899683
Modify Parser/asdl_c.py so that the __version__ number for Python/Python-ast.c
Brett Cannon <bcannon@gmail.com>
parents:
40668
diff
changeset
|
1133 This module must be committed separately after each AST grammar change; |
|
180d4e899683
Modify Parser/asdl_c.py so that the __version__ number for Python/Python-ast.c
Brett Cannon <bcannon@gmail.com>
parents:
40668
diff
changeset
|
1134 The __version__ number is set to the revision number of the commit |
|
180d4e899683
Modify Parser/asdl_c.py so that the __version__ number for Python/Python-ast.c
Brett Cannon <bcannon@gmail.com>
parents:
40668
diff
changeset
|
1135 containing the grammar change. |
|
180d4e899683
Modify Parser/asdl_c.py so that the __version__ number for Python/Python-ast.c
Brett Cannon <bcannon@gmail.com>
parents:
40668
diff
changeset
|
1136 */ |
|
46228
e3dc867b6182
Use file.write instead of print to make it easier to merge with 3k.
Neal Norwitz <nnorwitz@gmail.com>
parents:
46212
diff
changeset
|
1137 |
|
40824
180d4e899683
Modify Parser/asdl_c.py so that the __version__ number for Python/Python-ast.c
Brett Cannon <bcannon@gmail.com>
parents:
40668
diff
changeset
|
1138 """ |
|
180d4e899683
Modify Parser/asdl_c.py so that the __version__ number for Python/Python-ast.c
Brett Cannon <bcannon@gmail.com>
parents:
40668
diff
changeset
|
1139 |
| 35742 | 1140 def main(srcfile): |
|
36003
52f1d57f237d
SF #1373150, diffs in working copy after a build
Neal Norwitz <nnorwitz@gmail.com>
parents:
35866
diff
changeset
|
1141 argv0 = sys.argv[0] |
|
36023
bec9fac9f77c
When regenerating files like Python-ast.h, take care that the generated
Armin Rigo <arigo@tunes.org>
parents:
36003
diff
changeset
|
1142 components = argv0.split(os.sep) |
|
bec9fac9f77c
When regenerating files like Python-ast.h, take care that the generated
Armin Rigo <arigo@tunes.org>
parents:
36003
diff
changeset
|
1143 argv0 = os.sep.join(components[-2:]) |
|
40824
180d4e899683
Modify Parser/asdl_c.py so that the __version__ number for Python/Python-ast.c
Brett Cannon <bcannon@gmail.com>
parents:
40668
diff
changeset
|
1144 auto_gen_msg = common_msg % argv0 |
| 35742 | 1145 mod = asdl.parse(srcfile) |
| 1146 if not asdl.check(mod): | |
| 1147 sys.exit(1) | |
| 1148 if INC_DIR: | |
| 1149 p = "%s/%s-ast.h" % (INC_DIR, mod.name) | |
|
37712
9bef1debcbaa
Patch #1355883: Build Python-ast.c and Python-ast.h
Martin v. Löwis <martin@v.loewis.de>
parents:
37685
diff
changeset
|
1150 f = open(p, "wb") |
|
46228
e3dc867b6182
Use file.write instead of print to make it easier to merge with 3k.
Neal Norwitz <nnorwitz@gmail.com>
parents:
46212
diff
changeset
|
1151 f.write(auto_gen_msg) |
|
e3dc867b6182
Use file.write instead of print to make it easier to merge with 3k.
Neal Norwitz <nnorwitz@gmail.com>
parents:
46212
diff
changeset
|
1152 f.write('#include "asdl.h"\n\n') |
|
37712
9bef1debcbaa
Patch #1355883: Build Python-ast.c and Python-ast.h
Martin v. Löwis <martin@v.loewis.de>
parents:
37685
diff
changeset
|
1153 c = ChainOfVisitors(TypeDefVisitor(f), |
|
9bef1debcbaa
Patch #1355883: Build Python-ast.c and Python-ast.h
Martin v. Löwis <martin@v.loewis.de>
parents:
37685
diff
changeset
|
1154 StructVisitor(f), |
|
9bef1debcbaa
Patch #1355883: Build Python-ast.c and Python-ast.h
Martin v. Löwis <martin@v.loewis.de>
parents:
37685
diff
changeset
|
1155 PrototypeVisitor(f), |
|
9bef1debcbaa
Patch #1355883: Build Python-ast.c and Python-ast.h
Martin v. Löwis <martin@v.loewis.de>
parents:
37685
diff
changeset
|
1156 ) |
|
9bef1debcbaa
Patch #1355883: Build Python-ast.c and Python-ast.h
Martin v. Löwis <martin@v.loewis.de>
parents:
37685
diff
changeset
|
1157 c.visit(mod) |
|
46228
e3dc867b6182
Use file.write instead of print to make it easier to merge with 3k.
Neal Norwitz <nnorwitz@gmail.com>
parents:
46212
diff
changeset
|
1158 f.write("PyObject* PyAST_mod2obj(mod_ty t);\n") |
|
e3dc867b6182
Use file.write instead of print to make it easier to merge with 3k.
Neal Norwitz <nnorwitz@gmail.com>
parents:
46212
diff
changeset
|
1159 f.write("mod_ty PyAST_obj2mod(PyObject* ast, PyArena* arena, int mode);\n") |
|
e3dc867b6182
Use file.write instead of print to make it easier to merge with 3k.
Neal Norwitz <nnorwitz@gmail.com>
parents:
46212
diff
changeset
|
1160 f.write("int PyAST_Check(PyObject* obj);\n") |
|
37712
9bef1debcbaa
Patch #1355883: Build Python-ast.c and Python-ast.h
Martin v. Löwis <martin@v.loewis.de>
parents:
37685
diff
changeset
|
1161 f.close() |
| 35742 | 1162 |
| 1163 if SRC_DIR: | |
|
37462
b603307292fc
Make path calculation platform independent
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
37453
diff
changeset
|
1164 p = os.path.join(SRC_DIR, str(mod.name) + "-ast.c") |
|
37712
9bef1debcbaa
Patch #1355883: Build Python-ast.c and Python-ast.h
Martin v. Löwis <martin@v.loewis.de>
parents:
37685
diff
changeset
|
1165 f = open(p, "wb") |
|
46228
e3dc867b6182
Use file.write instead of print to make it easier to merge with 3k.
Neal Norwitz <nnorwitz@gmail.com>
parents:
46212
diff
changeset
|
1166 f.write(auto_gen_msg) |
|
e3dc867b6182
Use file.write instead of print to make it easier to merge with 3k.
Neal Norwitz <nnorwitz@gmail.com>
parents:
46212
diff
changeset
|
1167 f.write(c_file_msg % parse_version(mod)) |
|
e3dc867b6182
Use file.write instead of print to make it easier to merge with 3k.
Neal Norwitz <nnorwitz@gmail.com>
parents:
46212
diff
changeset
|
1168 f.write('#include "Python.h"\n') |
|
e3dc867b6182
Use file.write instead of print to make it easier to merge with 3k.
Neal Norwitz <nnorwitz@gmail.com>
parents:
46212
diff
changeset
|
1169 f.write('#include "%s-ast.h"\n' % mod.name) |
|
e3dc867b6182
Use file.write instead of print to make it easier to merge with 3k.
Neal Norwitz <nnorwitz@gmail.com>
parents:
46212
diff
changeset
|
1170 f.write('\n') |
|
e3dc867b6182
Use file.write instead of print to make it easier to merge with 3k.
Neal Norwitz <nnorwitz@gmail.com>
parents:
46212
diff
changeset
|
1171 f.write("static PyTypeObject AST_type;\n") |
|
37712
9bef1debcbaa
Patch #1355883: Build Python-ast.c and Python-ast.h
Martin v. Löwis <martin@v.loewis.de>
parents:
37685
diff
changeset
|
1172 v = ChainOfVisitors( |
|
9bef1debcbaa
Patch #1355883: Build Python-ast.c and Python-ast.h
Martin v. Löwis <martin@v.loewis.de>
parents:
37685
diff
changeset
|
1173 PyTypesDeclareVisitor(f), |
|
9bef1debcbaa
Patch #1355883: Build Python-ast.c and Python-ast.h
Martin v. Löwis <martin@v.loewis.de>
parents:
37685
diff
changeset
|
1174 PyTypesVisitor(f), |
|
46162
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
1175 Obj2ModPrototypeVisitor(f), |
|
37712
9bef1debcbaa
Patch #1355883: Build Python-ast.c and Python-ast.h
Martin v. Löwis <martin@v.loewis.de>
parents:
37685
diff
changeset
|
1176 FunctionVisitor(f), |
|
9bef1debcbaa
Patch #1355883: Build Python-ast.c and Python-ast.h
Martin v. Löwis <martin@v.loewis.de>
parents:
37685
diff
changeset
|
1177 ObjVisitor(f), |
|
46162
ca38f7a4b4c9
Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
1178 Obj2ModVisitor(f), |
|
37712
9bef1debcbaa
Patch #1355883: Build Python-ast.c and Python-ast.h
Martin v. Löwis <martin@v.loewis.de>
parents:
37685
diff
changeset
|
1179 ASTModuleVisitor(f), |
|
9bef1debcbaa
Patch #1355883: Build Python-ast.c and Python-ast.h
Martin v. Löwis <martin@v.loewis.de>
parents:
37685
diff
changeset
|
1180 PartingShots(f), |
|
9bef1debcbaa
Patch #1355883: Build Python-ast.c and Python-ast.h
Martin v. Löwis <martin@v.loewis.de>
parents:
37685
diff
changeset
|
1181 ) |
|
9bef1debcbaa
Patch #1355883: Build Python-ast.c and Python-ast.h
Martin v. Löwis <martin@v.loewis.de>
parents:
37685
diff
changeset
|
1182 v.visit(mod) |
|
9bef1debcbaa
Patch #1355883: Build Python-ast.c and Python-ast.h
Martin v. Löwis <martin@v.loewis.de>
parents:
37685
diff
changeset
|
1183 f.close() |
| 35742 | 1184 |
| 1185 if __name__ == "__main__": | |
| 1186 import sys | |
| 1187 import getopt | |
| 1188 | |
| 1189 INC_DIR = '' | |
| 1190 SRC_DIR = '' | |
| 1191 opts, args = getopt.getopt(sys.argv[1:], "h:c:") | |
|
37712
9bef1debcbaa
Patch #1355883: Build Python-ast.c and Python-ast.h
Martin v. Löwis <martin@v.loewis.de>
parents:
37685
diff
changeset
|
1192 if len(opts) != 1: |
|
9bef1debcbaa
Patch #1355883: Build Python-ast.c and Python-ast.h
Martin v. Löwis <martin@v.loewis.de>
parents:
37685
diff
changeset
|
1193 print "Must specify exactly one output file" |
|
9bef1debcbaa
Patch #1355883: Build Python-ast.c and Python-ast.h
Martin v. Löwis <martin@v.loewis.de>
parents:
37685
diff
changeset
|
1194 sys.exit(1) |
| 35742 | 1195 for o, v in opts: |
| 1196 if o == '-h': | |
| 1197 INC_DIR = v | |
| 1198 if o == '-c': | |
| 1199 SRC_DIR = v | |
| 1200 if len(args) != 1: | |
| 1201 print "Must specify single input file" | |
|
37712
9bef1debcbaa
Patch #1355883: Build Python-ast.c and Python-ast.h
Martin v. Löwis <martin@v.loewis.de>
parents:
37685
diff
changeset
|
1202 sys.exit(1) |
| 35742 | 1203 main(args[0]) |
