static ERL_NIF_TERM pynerl_exec(ErlNifEnv* env, int arc, const ERL_NIF_TERM argv[]) { Py_Initialize(); char buff[BUFF_SIZE]; ERL_NIF_TERM eResult; PyObject *pdict, *pResult; // TODO: error checking enif_get_string(env, argv[0], buff, BUFF_SIZE, ERL_NIF_LATIN1); pdict = PyDict_New(); PyDict_SetItemString(pdict, "__builtins__", PyEval_GetBuiltins()); PyDict_SetItemString(pdict, "__name__", PyUnicode_FromString("__main__")); pResult = PyRun_FileEx(fopen(buff, "r"), buff, Py_file_input, pdict, pdict, 1); if (pResult == NULL) { eResult = pynerl_make_error(env, "exception", "Exception while running file"); } else { eResult = enif_make_atom(env, "ok"); Py_DECREF(pResult); } Py_DECREF(pdict); Py_Finalize(); return eResult; }
int PyRun_SimpleFileEx(FILE *fp, char *filename, int closeit) { PyObject *m, *d, *v; char *ext; m = PyImport_AddModule("__main__"); if (m == NULL) return -1; d = PyModule_GetDict(m); ext = filename + strlen(filename) - 4; if (strcmp(ext, ".pyc") == 0 || strcmp(ext, ".pyo") == 0 #ifdef macintosh /* On a mac, we also assume a pyc file for types 'PYC ' and 'APPL' */ || PyMac_getfiletype(filename) == 'PYC ' || PyMac_getfiletype(filename) == 'APPL' #endif /* macintosh */ ) { /* Try to run a pyc file. First, re-open in binary */ if (closeit) fclose(fp); if( (fp = fopen(filename, "rb")) == NULL ) { fprintf(stderr, "python: Can't reopen .pyc file\n"); return -1; } /* Turn on optimization if a .pyo file is given */ if (strcmp(ext, ".pyo") == 0) Py_OptimizeFlag = 1; v = run_pyc_file(fp, filename, d, d); } else { v = PyRun_FileEx(fp, filename, Py_file_input, d, d, closeit); } if (v == NULL) { PyErr_Print(); return -1; } Py_DECREF(v); if (Py_FlushLine()) PyErr_Clear(); return 0; }
KValueRef PythonInterpreter::EvaluateFile(const char* filepath, KObjectRef context) { PyLockGIL lock; PyObject* globals = ContextToPyGlobals(context); FILE* file = fopen(filepath, "r"); PyObject* pyResult = PyRun_FileEx(file, filepath, Py_file_input, globals, globals, 1); if (pyResult == NULL) { Py_DECREF(globals); std::string err = PythonUtils::PythonErrorToString(); PyErr_Clear(); throw ValueException::FromString(err); } MergePyGlobalsWithContext(globals, context); Py_DECREF(globals); KValueRef result = PythonUtils::ToKrollValue(pyResult); Py_DECREF(pyResult); return result; }
// Execute python source code from file filename. // global and local are the global and local scopes respectively, // used during execution. object BOOST_PYTHON_DECL exec_file(str filename, object global, object local) { // Set suitable default values for global and local dicts. if (global.is_none()) { if (PyObject *g = PyEval_GetGlobals()) global = object(detail::borrowed_reference(g)); else global = dict(); } if (local.is_none()) local = global; // should be 'char const *' but older python versions don't use 'const' yet. char *f = python::extract<char *>(filename); // Let python open the file to avoid potential binary incompatibilities. #if PY_VERSION_HEX >= 0x03040000 FILE *fs = _Py_fopen(f, "r"); #elif PY_VERSION_HEX >= 0x03000000 PyObject *fo = Py_BuildValue("s", f); FILE *fs = _Py_fopen(fo, "r"); Py_DECREF(fo); #else PyObject *pyfile = PyFile_FromString(f, const_cast<char*>("r")); if (!pyfile) throw std::invalid_argument(std::string(f) + " : no such file"); python::handle<> file(pyfile); FILE *fs = PyFile_AsFile(file.get()); #endif int closeit = 1; // Close file before PyRun returns PyObject* result = PyRun_FileEx(fs, f, Py_file_input, global.ptr(), local.ptr(), closeit); if (!result) throw_error_already_set(); return object(detail::new_reference(result)); }
PyObject * PyRun_File(FILE *fp, char *filename, int start, PyObject *globals, PyObject *locals) { return PyRun_FileEx(fp, filename, start, globals, locals, 0); }