MCPcopy Index your code
hub / github.com/python/cpython / emit_function

Method emit_function

Parser/asdl_c.py:414–446  ·  view source on GitHub ↗
(self, name, ctype, args, attrs, union=True)

Source from the content-addressed store, hash-verified

412 """Visitor to generate constructor functions for AST."""
413
414 def emit_function(self, name, ctype, args, attrs, union=True):
415 def emit(s, depth=0, reflow=True):
416 self.emit(s, depth, reflow)
417 argstr = ", ".join(["%s %s" % (atype, aname)
418 for atype, aname, opt in args + attrs])
419 if argstr:
420 argstr += ", PyArena *arena"
421 else:
422 argstr = "PyArena *arena"
423 self.emit("%s" % ctype, 0)
424 emit("%s(%s)" % (ast_func_name(name), argstr))
425 emit("{")
426 emit("%s p;" % ctype, 1)
427 for argtype, argname, opt in args:
428 if not opt and argtype != "int":
429 emit("if (!%s) {" % argname, 1)
430 emit("PyErr_SetString(PyExc_ValueError,", 2)
431 msg = "field '%s' is required for %s" % (argname, name)
432 emit(' "%s");' % msg,
433 2, reflow=False)
434 emit('return NULL;', 2)
435 emit('}', 1)
436
437 emit("p = (%s)_PyArena_Malloc(arena, sizeof(*p));" % ctype, 1);
438 emit("if (!p)", 1)
439 emit("return NULL;", 2)
440 if union:
441 self.emit_body_union(name, args, attrs)
442 else:
443 self.emit_body_struct(name, args, attrs)
444 emit("return p;", 1)
445 emit("}")
446 emit("")
447
448 def emit_body_union(self, name, args, attrs):
449 def emit(s, depth=0, reflow=True):

Callers

nothing calls this directly

Calls 5

emitMethod · 0.95
emit_body_unionMethod · 0.95
emit_body_structMethod · 0.95
ast_func_nameFunction · 0.85
joinMethod · 0.45

Tested by

no test coverage detected