RunPythonFileAction::RunPythonFileAction(const ParameterValueMap ¶meters) : RunPythonAction(parameters) { // Converting to boost::filesystem::path first buys us some useful path expansion and validation const std::string filename(boost::filesystem::path(parameters[PATH]).string()); std::FILE *fp = std::fopen(filename.c_str(), "r"); if (!fp) { throw SimpleException(M_FILE_MESSAGE_DOMAIN, "Unable to open Python file", strerror(errno)); } BOOST_SCOPE_EXIT(fp) { std::fclose(fp); } BOOST_SCOPE_EXIT_END ScopedGILAcquire sga; struct _node *node = PyParser_SimpleParseFile(fp, filename.c_str(), Py_file_input); BOOST_SCOPE_EXIT(node) { PyNode_Free(node); } BOOST_SCOPE_EXIT_END if (!node || !(codeObject = PyNode_Compile(node, filename.c_str()))) { throw PythonException("Python compilation failed"); } }
PyObject *uwsgi_eval_loader(void *arg1) { char *code = (char *) arg1; PyObject *wsgi_eval_module, *wsgi_eval_callable = NULL; struct _node *wsgi_eval_node = NULL; PyObject *wsgi_compiled_node; wsgi_eval_node = PyParser_SimpleParseString(code, Py_file_input); if (!wsgi_eval_node) { PyErr_Print(); uwsgi_log( "failed to parse <eval> code\n"); exit(UWSGI_FAILED_APP_CODE); } wsgi_compiled_node = (PyObject *) PyNode_Compile(wsgi_eval_node, "uwsgi_eval_config"); if (!wsgi_compiled_node) { PyErr_Print(); uwsgi_log( "failed to compile eval code\n"); exit(UWSGI_FAILED_APP_CODE); } wsgi_eval_module = PyImport_ExecCodeModule("uwsgi_eval_config", wsgi_compiled_node); if (!wsgi_eval_module) { PyErr_Print(); exit(UWSGI_FAILED_APP_CODE); } Py_DECREF(wsgi_compiled_node); up.loader_dict = PyModule_GetDict(wsgi_eval_module); if (!up.loader_dict) { PyErr_Print(); exit(UWSGI_FAILED_APP_CODE); } if (up.callable) { wsgi_eval_callable = PyDict_GetItemString(up.loader_dict, up.callable); } else { wsgi_eval_callable = PyDict_GetItemString(up.loader_dict, "application"); } if (wsgi_eval_callable) { if (!PyFunction_Check(wsgi_eval_callable) && !PyCallable_Check(wsgi_eval_callable)) { uwsgi_log( "you must define a callable object in your code\n"); exit(UWSGI_FAILED_APP_CODE); } } return wsgi_eval_callable; }
PyObject * Py_CompileString(char *str, char *filename, int start) { node *n; PyCodeObject *co; n = PyParser_SimpleParseString(str, start); if (n == NULL) return NULL; co = PyNode_Compile(n, filename); PyNode_Free(n); return (PyObject *)co; }
static PyObject * run_node(node *n, char *filename, PyObject *globals, PyObject *locals) { PyCodeObject *co; PyObject *v; co = PyNode_Compile(n, filename); PyNode_Free(n); if (co == NULL) return NULL; v = PyEval_EvalCode(co, globals, locals); Py_DECREF(co); return v; }
int csl_compile(char *str, char *name, char **codeptr, size_t *sizeptr) { PyObject *pCode, *pStr; node *n; size_t size; // compile into a code object n = PyParser_SimpleParseString(str, Py_file_input); if (!n) { log_exception(); return CSL_BADCODE; } pCode = (PyObject *) PyNode_Compile(n, name); PyNode_Free(n); if (!pCode) { log_exception(); return CSL_BADCODE; } // serialize code object #if PY_MINOR_VERSION == 3 pStr = PyMarshal_WriteObjectToString(pCode); #else pStr = PyMarshal_WriteObjectToString(pCode, 0); #endif Py_DECREF(pCode); if (!pStr) { return CSL_NOMEM; } size = PyString_Size(pStr); *codeptr = malloc(size); if (!*codeptr) { Py_DECREF(pStr); return CSL_NOMEM; } memcpy(*codeptr, PyString_AsString(pStr), size); *sizeptr = size; Py_DECREF(pStr); return 0; }
PyObject *uwsgi_pyimport_by_filename(char *name, char *filename) { #ifdef UWSGI_PYPY uwsgi_log("import by filename is currently not supported on PyPy !!!\n"); return NULL; #else FILE *pyfile; struct _node *py_file_node = NULL; PyObject *py_compiled_node, *py_file_module; int is_a_package = 0; struct stat pystat; char *real_filename = filename; if (!uwsgi_check_scheme(filename)) { pyfile = fopen(filename, "r"); if (!pyfile) { uwsgi_log("failed to open python file %s\n", filename); return NULL; } if (fstat(fileno(pyfile), &pystat)) { uwsgi_error("fstat()"); return NULL; } if (S_ISDIR(pystat.st_mode)) { is_a_package = 1; fclose(pyfile); real_filename = uwsgi_concat2(filename, "/__init__.py"); pyfile = fopen(real_filename, "r"); if (!pyfile) { uwsgi_error_open(real_filename); free(real_filename); return NULL; } } py_file_node = PyParser_SimpleParseFile(pyfile, real_filename, Py_file_input); if (!py_file_node) { PyErr_Print(); uwsgi_log("failed to parse file %s\n", real_filename); if (is_a_package) free(real_filename); fclose(pyfile); return NULL; } fclose(pyfile); } else { int pycontent_size = 0; char *pycontent = uwsgi_open_and_read(filename, &pycontent_size, 1, NULL); if (pycontent) { py_file_node = PyParser_SimpleParseString(pycontent, Py_file_input); if (!py_file_node) { PyErr_Print(); uwsgi_log("failed to parse url %s\n", real_filename); return NULL; } } } py_compiled_node = (PyObject *) PyNode_Compile(py_file_node, real_filename); if (!py_compiled_node) { PyErr_Print(); uwsgi_log("failed to compile python file %s\n", real_filename); return NULL; } if (is_a_package) { py_file_module = PyImport_AddModule(name); if (py_file_module) { PyModule_AddObject(py_file_module, "__path__", Py_BuildValue("[O]", PyString_FromString(filename))); } free(real_filename); } py_file_module = PyImport_ExecCodeModule(name, py_compiled_node); if (!py_file_module) { PyErr_Print(); return NULL; } Py_DECREF(py_compiled_node); return py_file_module; #endif }
int csl_execute(char *code, size_t size, const char *func_name, struct pack *pak, char **resptr, int *reslen) { PyObject *pCode, *pModule, *pDict, *pFunc, *pValue, *pStr; PyObject *pArgs, *pkArgs; PyMethodDef *meth; node *n; pModule = PyImport_AddModule("__builtin__"); pDict = PyModule_GetDict(pModule); for (meth = methods; meth->ml_name; meth++) { pCode = PyCFunction_New(meth, NULL); PyDict_SetItemString(pDict, meth->ml_name, pCode); } if (size == 0) { n = PyParser_SimpleParseString(code, Py_file_input); if (!n) { log_exception(); return CSL_BADCODE; } pCode = (PyObject *) PyNode_Compile(n, "lala"); PyNode_Free(n); if (!pCode) { log_exception(); return CSL_BADCODE; } } else { pCode = PyMarshal_ReadObjectFromString(code, size); if (!pCode) { log_exception(); return CSL_BADCODE; } } pModule = PyImport_ExecCodeModule("csl", pCode); Py_DECREF(pCode); if (!pModule || !PyModule_Check(pModule)) { return CSL_BADCODE; } pDict = PyModule_GetDict(pModule); if (!pDict) { Py_DECREF(pModule); return CSL_BADCODE; } pFunc = PyDict_GetItemString(pDict, func_name); if (!pFunc || !PyCallable_Check(pFunc)) { Py_DECREF(pModule); return CSL_NOFUNC; } pArgs = NULL; pkArgs = PyDict_New(); while (pak) { PyObject *p; char *t, *t2; size_t sz; if (pack_get(pak, &t, &sz) == 0) break; if (pack_get(pak, &t2, &sz) == 0) { pArgs = PyTuple_New(1); PyTuple_SetItem(pArgs, 0, PyString_FromString(t)); Py_DECREF(pkArgs); break; } p = PyString_FromStringAndSize(t2, sz); PyDict_SetItemString(pkArgs, t, p); } if (!pArgs) pArgs = PyTuple_New(0); pValue = PyObject_Call(pFunc, pArgs, pkArgs); if (!pValue) { log_exception(); Py_DECREF(pModule); return CSL_FUNCERR; } pStr = PyObject_Str(pValue); Py_DECREF(pValue); Py_DECREF(pModule); // is return value asked? if (resptr == NULL) return 0; *reslen = PyString_Size(pStr); *resptr = malloc((*reslen) + 1); if (!*resptr) { Py_DECREF(pStr); return CSL_NOMEM; } memcpy(*resptr, PyString_AsString(pStr), *reslen); (*resptr)[*reslen] = '\0'; return 0; }