Lines Matching +full:- +full:- +full:depth

26 def reflow_lines(s, depth):  argument
27 """Reflow the line s indented depth tabs.
31 exclusively on depth * TABSIZE. All following lines -- these are
32 the reflowed lines generated by this function -- start at the same
36 size = MAX_COL - depth * TABSIZE
46 if i == -1 and 'GeneratorExp' in cur:
48 assert i != -1, "Impossible line %d to reflow: %r" % (size, s)
55 size -= j
61 size -= j
68 def reflow_c_string(s, depth): argument
69 return '"%s"' % s.replace('\n', '\\n"\n%s"' % (' ' * depth * TABSIZE))
117 def emit(self, s, depth, reflow=True): argument
120 lines = reflow_lines(s, depth)
125 line = (" " * TABSIZE * depth) + line
134 def visitType(self, type, depth=0): argument
135 self.visit(type.value, type.name, depth)
137 def visitSum(self, sum, name, depth): argument
139 self.simple_sum(sum, name, depth)
141 self.sum_with_constructors(sum, name, depth)
143 def simple_sum(self, sum, name, depth): argument
151 self.emit(s, depth)
152 self.emit("", depth)
154 def sum_with_constructors(self, sum, name, depth): argument
157 self.emit(s, depth)
158 self.emit("", depth)
160 def visitProduct(self, product, name, depth): argument
163 self.emit(s, depth)
164 self.emit("", depth)
174 def visitType(self, type, depth=0): argument
175 self.visit(type.value, type.name, depth)
177 def visitSum(self, sum, name, depth): argument
179 self.sum_with_constructors(sum, name, depth)
181 def sum_with_constructors(self, sum, name, depth): argument
182 def emit(s, depth=depth): argument
183 self.emit(s % sys._getframe(1).f_locals, depth)
192 emit("enum _%(name)s_kind kind;", depth + 1)
193 emit("union {", depth + 1)
195 self.visit(t, depth + 2)
196 emit("} v;", depth + 1)
201 emit("%s %s;" % (type, field.name), depth + 1);
205 def visitConstructor(self, cons, depth): argument
207 self.emit("struct {", depth)
209 self.visit(f, depth + 1)
210 self.emit("} %s;" % cons.name, depth)
211 self.emit("", depth)
213 def visitField(self, field, depth): argument
220 self.emit("asdl_int_seq *%(name)s;" % locals(), depth)
222 self.emit("asdl_seq *%(name)s;" % locals(), depth)
224 self.emit("%(ctype)s %(name)s;" % locals(), depth)
226 def visitProduct(self, product, name, depth): argument
227 self.emit("struct _%(name)s {" % locals(), depth)
229 self.visit(f, depth + 1)
234 self.emit("%s %s;" % (type, field.name), depth + 1);
235 self.emit("};", depth)
236 self.emit("", depth)
259 Argument info is 3-tuple of a C type, variable name, and flag
269 name = "name%d" % (c - 1)
315 def emit(s, depth=0, reflow=True): argument
316 self.emit(s, depth, reflow)
349 def emit(s, depth=0, reflow=True): argument
350 self.emit(s, depth, reflow)
351 emit("p->kind = %s_kind;" % name, 1)
353 emit("p->v.%s.%s = %s;" % (name, argname, argname), 1)
355 emit("p->%s = %s;" % (argname, argname), 1)
358 def emit(s, depth=0, reflow=True): argument
359 self.emit(s, depth, reflow)
361 emit("p->%s = %s;" % (argname, argname), 1)
363 emit("p->%s = %s;" % (argname, argname), 1)
422 "state->%s_type);")
424 self.emit("if (isinstance == -1) {", 1)
449 self.visitField(a, name, sum=sum, depth=1)
451 self.emit("tp = state->%s_type;" % (t.name,), 1)
453 self.emit("if (isinstance == -1) {", 1)
458 self.visitFieldDeclaration(f, t.name, sum=sum, depth=2)
461 self.visitField(f, t.name, sum=sum, depth=2)
486 self.visitFieldDeclaration(f, name, prod=prod, depth=1)
488 self.visitFieldDeclaration(a, name, prod=prod, depth=1)
491 self.visitField(f, name, prod=prod, depth=1)
493 self.visitField(a, name, prod=prod, depth=1)
504 def visitFieldDeclaration(self, field, name, sum=None, prod=None, depth=0): argument
508 self.emit("asdl_int_seq* %s;" % field.name, depth)
510 self.emit("asdl_seq* %s;" % field.name, depth)
513 self.emit("%s %s;" % (ctype, field.name), depth)
526 def visitField(self, field, name, sum=None, prod=None, depth=0): argument
528 line = "if (_PyObject_LookupAttr(obj, state->%s, &tmp) < 0) {"
529 self.emit(line % field.name, depth)
530 self.emit("return 1;", depth+1)
531 self.emit("}", depth)
533 self.emit("if (tmp == NULL) {", depth)
536 self.emit(format % message, depth+1, reflow=False)
537 self.emit("return 1;", depth+1)
539 self.emit("if (tmp == NULL || tmp == Py_None) {", depth)
540 self.emit("Py_CLEAR(tmp);", depth+1)
542 self.emit("%s = 0;" % field.name, depth+1)
544 self.emit("%s = NULL;" % field.name, depth+1)
547 self.emit("}", depth)
548 self.emit("else {", depth)
550 self.emit("int res;", depth+1)
552 self.emit("Py_ssize_t len;", depth+1)
553 self.emit("Py_ssize_t i;", depth+1)
554 self.emit("if (!PyList_Check(tmp)) {", depth+1)
558 depth+2, reflow=False)
559 self.emit("goto failed;", depth+2)
560 self.emit("}", depth+1)
561 self.emit("len = PyList_GET_SIZE(tmp);", depth+1)
563 self.emit("%s = _Py_asdl_int_seq_new(len, arena);" % field.name, depth+1)
565 self.emit("%s = _Py_asdl_seq_new(len, arena);" % field.name, depth+1)
566 self.emit("if (%s == NULL) goto failed;" % field.name, depth+1)
567 self.emit("for (i = 0; i < len; i++) {", depth+1)
568 self.emit("%s val;" % ctype, depth+2)
569 self.emit("PyObject *tmp2 = PyList_GET_ITEM(tmp, i);", depth+2)
570 self.emit("Py_INCREF(tmp2);", depth+2)
572 field.type, depth+2, reflow=False)
573 self.emit("Py_DECREF(tmp2);", depth+2)
574 self.emit("if (res != 0) goto failed;", depth+2)
575 self.emit("if (len != PyList_GET_SIZE(tmp)) {", depth+2)
579 depth+3, reflow=False)
580 self.emit("goto failed;", depth+3)
581 self.emit("}", depth+2)
582 self.emit("asdl_seq_SET(%s, i, val);" % field.name, depth+2)
583 self.emit("}", depth+1)
586 (field.type, field.name), depth+1)
587 self.emit("if (res != 0) goto failed;", depth+1)
589 self.emit("Py_CLEAR(tmp);", depth+1)
590 self.emit("}", depth)
663 /* bpo-31095: UnTrack is needed before calling any callbacks */
666 Py_CLEAR(self->dict);
677 Py_VISIT(self->dict);
684 Py_CLEAR(self->dict);
693 return -1;
697 int res = -1;
699 if (_PyObject_LookupAttr((PyObject*)Py_TYPE(self), state->_fields, &fields) < 0) {
704 if (numfields == -1) {
715 res = -1;
722 res = -1;
735 if (contains == -1) {
736 res = -1;
740 if (p == -1) {
741 res = -1;
747 Py_TYPE(self)->tp_name, key);
748 res = -1;
773 if (_PyObject_LookupAttr(self, state->__dict__, &dict) < 0) {
839 state->_fields, fnames,
840 state->__module__,
841 state->ast,
842 state->__doc__, doc);
862 result = PyObject_SetAttr(type, state->_attributes, l) >= 0;
867 /* Conversion AST -> Python */
903 /* Conversion Python -> AST */
912 return -1;
924 return -1;
958 if (i == -1 && PyErr_Occurred())
969 PyObject_SetAttrString(state->AST_type, "_fields", empty_tuple) < 0 ||
970 PyObject_SetAttrString(state->AST_type, "_attributes", empty_tuple) < 0) {
972 return -1;
982 self.emit("if (state->initialized) return 1;", 1)
984 self.emit("state->AST_type = PyType_FromSpec(&AST_type_spec);", 1)
985 self.emit("if (!state->AST_type) return 0;", 1)
989 self.emit("state->initialized = 1;", 1)
998 self.emit('state->%s_type = make_type(state, "%s", state->AST_type, %s, %d,' %
1001 self.emit("if (!state->%s_type) return 0;" % name, 1)
1005 self.emit("if (!add_attributes(state, state->%s_type, %s_attributes, %d)) return 0;" %
1008 self.emit("if (!add_attributes(state, state->%s_type, NULL, 0)) return 0;" % name, 1)
1013 self.emit('state->%s_type = make_type(state, "%s", state->AST_type, NULL, 0,' %
1017 self.emit("if (!state->%s_type) return 0;" % name, 1)
1019 self.emit("if (!add_attributes(state, state->%s_type, %s_attributes, %d)) return 0;" %
1022 self.emit("if (!add_attributes(state, state->%s_type, NULL, 0)) return 0;" % name, 1)
1033 self.emit('state->%s_type = make_type(state, "%s", state->%s_type, %s, %d,' %
1036 self.emit("if (!state->%s_type) return 0;" % cons.name, 1)
1040 self.emit("state->%s_singleton = PyType_GenericNew((PyTypeObject *)"
1041 "state->%s_type, NULL, NULL);" %
1043 self.emit("if (!state->%s_singleton) return 0;" % cons.name, 1)
1045 def emit_defaults(self, name, fields, depth): argument
1048 self.emit('if (PyObject_SetAttr(state->%s_type, state->%s, Py_None) == -1)' %
1049 (name, field.name), depth)
1050 self.emit("return 0;", depth+1)
1063 self.emit("return -1;", 2)
1065 self.emit('if (PyModule_AddObject(m, "AST", state->AST_type) < 0) {', 1)
1066 self.emit('return -1;', 2)
1068 self.emit('Py_INCREF(state->AST_type);', 1)
1070 self.emit("return -1;", 2)
1073 self.emit("return -1;", 2)
1076 self.emit("return -1;", 2)
1117 "state->%s_type) < 0) {" % (name, name), 1)
1118 self.emit("return -1;", 2)
1120 self.emit("Py_INCREF(state->%s_type);" % name, 1)
1177 self.emit("switch (o->kind) {", 1)
1183 self.emit("value = ast2obj_%s(state, o->%s);" % (a.type, a.name), 1)
1185 self.emit('if (PyObject_SetAttr(result, state->%s, value) < 0)' % a.name, 1)
1196 self.emit("Py_INCREF(state->%s_singleton);" % t.name, 3)
1197 self.emit("return state->%s_singleton;" % t.name, 3)
1204 self.emit("tp = (PyTypeObject *)state->%s_type;" % name, 1)
1210 self.emit("value = ast2obj_%s(state, o->%s);" % (a.type, a.name), 1)
1212 self.emit("if (PyObject_SetAttr(result, state->%s, value) < 0)" % a.name, 1)
1219 self.emit("tp = (PyTypeObject *)state->%s_type;" % cons.name, 2)
1226 def visitField(self, field, name, depth, product): argument
1228 self.emit(s, depth + d)
1230 value = "o->%s" % field.name
1232 value = "o->v.%s.%s" % (name, field.name)
1233 self.set(field, value, depth)
1235 emit("if (PyObject_SetAttr(result, state->%s, value) == -1)" % field.name, 0)
1239 def emitSeq(self, field, value, depth, emit): argument
1245 self.set("value", field, "asdl_seq_GET(seq, i)", depth + 1)
1251 def set(self, field, value, depth): argument
1257 self.emit("{", depth)
1258 self.emit("Py_ssize_t i, n = asdl_seq_LEN(%s);" % value, depth+1)
1259 self.emit("value = PyList_New(n);", depth+1)
1260 self.emit("if (!value) goto failed;", depth+1)
1261 self.emit("for(i = 0; i < n; i++)", depth+1)
1264 depth+2, reflow=False)
1265 self.emit("}", depth)
1267 … self.emit("value = ast2obj_list(state, %s, ast2obj_%s);" % (value, field.type), depth)
1270 self.emit("value = ast2obj_%s(state, %s);" % (field.type, value), depth, reflow=False)
1297 req_type[0] = state->Module_type;
1298 req_type[1] = state->Expression_type;
1299 req_type[2] = state->Interactive_type;
1304 if (isinstance == -1)
1323 return -1;
1325 return PyObject_IsInstance(obj, state->AST_type);
1378 // bpo-41194, bpo-41261, bpo-41631: The _ast module uses a global state.
1406 f.write(" Py_CLEAR(state->" + s + ');\n')
1408 state->initialized = 0;
1415 f.write(' if ((state->' + identifier)
1453 f.write('#include "%s-ast.h"\n' % mod.name)
1472 auto_gen_msg = AUTOGEN_MESSAGE.format("/".join(Path(__file__).parts[-2:]))
1489 parser.add_argument("-C", "--c-file", type=Path, default=None)
1490 parser.add_argument("-H", "--h-file", type=Path, default=None)
1491 parser.add_argument("-d", "--dump-module", action="store_true")