static int py_run(lua_State *L, int eval) { const char *s; char *buffer = NULL; PyObject *m, *d, *o; int ret = 0; int len; s = luaL_checkstring(L, 1); if (!s) return 0; if (!eval) { len = strlen(s)+1; buffer = (char *) malloc(len+1); if (!buffer) { luaL_error(L, "Failed allocating buffer for execution"); return 0; } strcpy(buffer, s); buffer[len-1] = '\n'; buffer[len] = '\0'; s = buffer; } m = PyImport_AddModule("__main__"); if (!m) { free(buffer); luaL_error(L, "Can't get __main__ module"); return 0; } d = PyModule_GetDict(m); o = PyRun_StringFlags(s, eval ? Py_eval_input : Py_single_input, d, d, NULL); free(buffer); if (!o) { PyErr_Print(); return 0; } if (py_convert(L, o, 0)) ret = 1; Py_DECREF(o); if (Py_FlushLine()) PyErr_Clear(); return ret; }
int PyRun_SimpleStringFlags(const char *command, PyCompilerFlags *flags) { PyObject *m, *d, *v; m = PyImport_AddModule("__main__"); if (m == NULL) return -1; d = PyModule_GetDict(m); v = PyRun_StringFlags(command, Py_file_input, d, d, flags); if (v == NULL) { PyErr_Print(); return -1; } Py_DECREF(v); return 0; }
static int py_run(lua_State *L, int eval) { const char *s; char *buffer = NULL; PyObject *m, *d, *o; Conversion ret; size_t len; s = luaL_check_string(L, 1); if (!s) return 0; if (!eval) { len = strlen(s) + 1; buffer = (char *) malloc(len + 1); if (!buffer) { lua_error(L, "Failed allocating buffer for execution"); } strcpy(buffer, s); buffer[len - 1] = '\n'; buffer[len] = '\0'; s = buffer; } m = PyImport_AddModule("__main__"); if (!m) { free(buffer); lua_error(L, "Can't get __main__ module"); } d = PyModule_GetDict(m); o = PyRun_StringFlags(s, eval ? Py_eval_input : Py_single_input, d, d, NULL); free(buffer); if (!o) { lua_new_error(L, "run custom code"); return 0; } if ((ret = py_convert(L, o)) == CONVERTED) { Py_DECREF(o); } #if PY_MAJOR_VERSION < 3 if (Py_FlushLine()) #endif PyErr_Clear(); return ret; }
rpmRC rpmpythonRun(rpmpython python, const char * str, const char ** resultp) { rpmRC rc = RPMRC_FAIL; if (_rpmpython_debug) fprintf(stderr, "==> %s(%p,%s,%p)\n", __FUNCTION__, python, str, resultp); if (python == NULL) python = rpmpythonI(); if (str != NULL) { const char * val = rpmpythonSlurp(str); #if defined(WITH_PYTHONEMBED) PyCompilerFlags cf = { .cf_flags = 0 }; PyObject * m = PyImport_AddModule("__main__"); PyObject * d = (m ? PyModule_GetDict(m) : NULL); PyObject * v = (m ? PyRun_StringFlags(val, Py_file_input, d, d, &cf) : NULL); if (v == NULL) { PyErr_Print(); } else { if (resultp != NULL) { PyObject * sys_stdout = PySys_GetObject("stdout"); if (sys_stdout != NULL && PycStringIO_OutputCheck(sys_stdout)) { PyObject * o = (*PycStringIO->cgetvalue)(sys_stdout); *resultp = (PyString_Check(o) ? PyString_AsString(o) : ""); } else *resultp = ""; } Py_DECREF(v); if (Py_FlushLine()) PyErr_Clear(); rc = RPMRC_OK; } #endif val = _free(val); } return rc; }
/** * Run a string in the Python interpreter, in __main_%d__. * * :param command: Python command to execute */ void interpreter_run(const char *command) { char modname[1024] = "__main__"; PyObject *m, *d, *v = NULL; PyCompilerFlags flags; flags.cf_flags = CO_FUTURE_DIVISION; m = PyImport_AddModule(modname); if (m == NULL) goto error; d = PyModule_GetDict(m); v = PyRun_StringFlags(command, Py_file_input, d, d, &flags); if (v == NULL) goto error; Py_DECREF(v); return; error: if (v) { Py_DECREF(v); } PyErr_Print(); mexErrMsgTxt("Python error"); return; }
static int eval_python_command (const char *command) { PyObject *m, *d, *v; m = PyImport_AddModule ("__main__"); if (m == NULL) return -1; d = PyModule_GetDict (m); if (d == NULL) return -1; v = PyRun_StringFlags (command, Py_single_input, d, d, NULL); if (v == NULL) return -1; Py_DECREF (v); #ifndef IS_PY3K if (Py_FlushLine ()) PyErr_Clear (); #endif return 0; }
static PyObject* __Pyx_PyExec3(PyObject* o, PyObject* globals, PyObject* locals) { PyObject* result; PyObject* s = 0; char *code = 0; if (!globals || globals == Py_None) { globals = PyModule_GetDict($module_cname); if (!globals) goto bad; } else if (!PyDict_Check(globals)) { PyErr_Format(PyExc_TypeError, "exec() arg 2 must be a dict, not %.100s", globals->ob_type->tp_name); goto bad; } if (!locals || locals == Py_None) { locals = globals; } if (PyDict_GetItemString(globals, "__builtins__") == NULL) { PyDict_SetItemString(globals, "__builtins__", PyEval_GetBuiltins()); } if (PyCode_Check(o)) { if (PyCode_GetNumFree((PyCodeObject *)o) > 0) { PyErr_SetString(PyExc_TypeError, "code object passed to exec() may not contain free variables"); goto bad; } #if PY_VERSION_HEX < 0x030200B1 result = PyEval_EvalCode((PyCodeObject *)o, globals, locals); #else result = PyEval_EvalCode(o, globals, locals); #endif } else { PyCompilerFlags cf; cf.cf_flags = 0; if (PyUnicode_Check(o)) { cf.cf_flags = PyCF_SOURCE_IS_UTF8; s = PyUnicode_AsUTF8String(o); if (!s) goto bad; o = s; #if PY_MAJOR_VERSION >= 3 } else if (!PyBytes_Check(o)) { #else } else if (!PyString_Check(o)) { #endif PyErr_SetString(PyExc_TypeError, "exec: arg 1 must be string, bytes or code object"); goto bad; } #if PY_MAJOR_VERSION >= 3 code = PyBytes_AS_STRING(o); #else code = PyString_AS_STRING(o); #endif if (PyEval_MergeCompilerFlags(&cf)) { result = PyRun_StringFlags(code, Py_file_input, globals, locals, &cf); } else { result = PyRun_String(code, Py_file_input, globals, locals); } Py_XDECREF(s); } return result; bad: Py_XDECREF(s); return 0; }