/* Preferred access to parser is through AST. */ mod_ty PyParser_ASTFromStringObject(const char *s, PyObject *filename, int start, PyCompilerFlags *flags, PyArena *arena) { mod_ty mod; PyCompilerFlags localflags; perrdetail err; int iflags = PARSER_FLAGS(flags); node *n = PyParser_ParseStringObject(s, filename, &_PyParser_Grammar, start, &err, &iflags); if (flags == NULL) { localflags.cf_flags = 0; flags = &localflags; } if (n) { flags->cf_flags |= iflags & PyCF_MASK; mod = PyAST_FromNodeObject(n, flags, filename, arena); PyNode_Free(n); } else { err_input(&err); mod = NULL; } err_free(&err); return mod; }
mod_ty PyParser_ASTFromFileObject(FILE *fp, PyObject *filename, const char* enc, int start, const char *ps1, const char *ps2, PyCompilerFlags *flags, int *errcode, PyArena *arena) { mod_ty mod; PyCompilerFlags localflags; perrdetail err; int iflags = PARSER_FLAGS(flags); node *n = PyParser_ParseFileObject(fp, filename, enc, &_PyParser_Grammar, start, ps1, ps2, &err, &iflags); if (flags == NULL) { localflags.cf_flags = 0; flags = &localflags; } if (n) { flags->cf_flags |= iflags & PyCF_MASK; mod = PyAST_FromNodeObject(n, flags, filename, arena); PyNode_Free(n); } else { err_input(&err); if (errcode) *errcode = err.error; mod = NULL; } err_free(&err); return mod; }
/* parser_compilest(PyObject* self, PyObject* args) * * This function creates code objects from the parse tree represented by * the passed-in data object. An optional file name is passed in as well. * */ static PyObject* parser_compilest(PyST_Object *self, PyObject *args, PyObject *kw) { PyObject* res = NULL; PyArena* arena = NULL; mod_ty mod; PyObject* filename = NULL; int ok; static char *keywords[] = {"st", "filename", NULL}; if (self == NULL || PyModule_Check(self)) ok = PyArg_ParseTupleAndKeywords(args, kw, "O!|O&:compilest", keywords, &PyST_Type, &self, PyUnicode_FSDecoder, &filename); else ok = PyArg_ParseTupleAndKeywords(args, kw, "|O&:compile", &keywords[1], PyUnicode_FSDecoder, &filename); if (!ok) goto error; if (filename == NULL) { filename = PyUnicode_FromString("<syntax-tree>"); if (filename == NULL) goto error; } arena = PyArena_New(); if (!arena) goto error; mod = PyAST_FromNodeObject(self->st_node, &self->st_flags, filename, arena); if (!mod) goto error; res = (PyObject *)PyAST_CompileObject(mod, filename, &self->st_flags, -1, arena); error: Py_XDECREF(filename); if (arena != NULL) PyArena_Free(arena); return res; }