PyObject * PyRun_StringFlags(const char *str, int start, PyObject *globals, PyObject *locals, PyCompilerFlags *flags) { PyObject *ret = NULL; mod_ty mod; PyArena *arena = PyArena_New(); if (arena == NULL) return NULL; mod = PyParser_ASTFromString(str, "<string>", start, flags, arena); if (mod != NULL) ret = run_mod(mod, "<string>", globals, locals, flags, arena); PyArena_Free(arena); return ret; }
struct symtable * Py_SymtableString(const char *str, const char *filename, int start) { struct symtable *st; mod_ty mod; PyArena *arena = PyArena_New(); if (arena == NULL) return NULL; mod = PyParser_ASTFromString(str, filename, start, NULL, arena); if (mod == NULL) { PyArena_Free(arena); return NULL; } st = PySymtable_Build(mod, filename, 0); PyArena_Free(arena); return st; }
// This function is expanded out from the Python library function // PyRun_SimpleStringFlags(). We do this so we can tell the difference, // and fail in the case of a parse error in the code text. The unit tests // do contain code which should fail, but never any code that fails because // it won't parse. int CyPyRun_SimpleString(const char * command, PyObject * exception) { PyCompilerFlags *flags = NULL; PyObject * m = PyImport_AddModule("__main__"); if (m == NULL) return -1; PyObject * d = PyModule_GetDict(m); // v = PyRun_StringFlags(command, Py_file_input, d, d, flags); PyArena *arena = PyArena_New(); if (arena == NULL) return -1; mod_ty mod = PyParser_ASTFromString(command, "<string>", Py_file_input, flags, arena); if (mod == NULL) { PyArena_Free(arena); PyErr_Print(); return -2; } PyObject *ret = run_mod(mod, "<string>", d, d, flags, arena); PyArena_Free(arena); if (ret == NULL) { int errcode = -1; if (exception != 0) { if (PyErr_ExceptionMatches(exception)) { errcode = -3; } } PyErr_Print(); return errcode; } Py_DECREF(ret); if (Py_FlushLine()) PyErr_Clear(); return 0; }
PyObject * Py_CompileStringFlags(const char *str, const char *filename, int start, PyCompilerFlags *flags) { PyCodeObject *co; mod_ty mod; PyArena *arena = PyArena_New(); if (arena == NULL) return NULL; mod = PyParser_ASTFromString(str, filename, start, flags, arena); if (mod == NULL) { PyArena_Free(arena); return NULL; } if (flags && (flags->cf_flags & PyCF_ONLY_AST)) { PyObject *result = PyAST_mod2obj(mod); PyArena_Free(arena); return result; } co = PyAST_Compile(mod, filename, flags, arena); PyArena_Free(arena); return (PyObject *)co; }