/* * Run scripts * Return non zero on failure */ int runScripts() { unsigned char *data; char buf[_MAX_PATH]; int rc = 0; TOC * ptoc = f_tocbuff; PyObject *__main__ = PI_PyImport_AddModule("__main__"); PyObject *__file__; VS("Running scripts\n"); /* * Now that the startup is complete, we can reset the _MEIPASS2 env * so that if the program invokes another PyInstaller one-file program * as subprocess, this subprocess will not fooled into thinking that it * is already unpacked. */ unsetenv("_MEIPASS2"); /* Iterate through toc looking for scripts (type 's') */ while (ptoc < f_tocend) { if (ptoc->typcd == 's') { /* Get data out of the archive. */ data = extract(ptoc); /* Set the __file__ attribute within the __main__ module, for full compatibility with normal execution. */ strcpy(buf, ptoc->name); strcat(buf, ".py"); __file__ = PI_PyString_FromStringAndSize(buf, strlen(buf)); PI_PyObject_SetAttrString(__main__, "__file__", __file__); Py_DECREF(__file__); /* Run it */ rc = PI_PyRun_SimpleString(data); /* log errors and abort */ if (rc != 0) { VS("RC: %d from %s\n", rc, ptoc->name); return rc; } free(data); } ptoc = incrementTocPtr(ptoc); } return 0; }
/* * call a simple "int func(void)" entry point. Assumes such a function * exists in the main namespace. * Return non zero on failure, with -2 if the specific error is * that the function does not exist in the namespace. */ int callSimpleEntryPoint(char *name, int *presult) { int rc = -1; /* Objects with no ref. */ PyObject *mod, *dict; /* Objects with refs to kill. */ PyObject *func = NULL, *pyresult = NULL; mod = PI_PyImport_AddModule("__main__"); /* NO ref added */ if (!mod) { VS("No __main__\n"); goto done; } dict = PI_PyModule_GetDict(mod); /* NO ref added */ if (!mod) { VS("No __dict__\n"); goto done; } func = PI_PyDict_GetItemString(dict, name); if (func == NULL) { /* should explicitly check KeyError */ VS("CallSimpleEntryPoint can't find the function name\n"); rc = -2; goto done; } pyresult = PI_PyObject_CallFunction(func, ""); if (pyresult==NULL) goto done; PI_PyErr_Clear(); *presult = PI_PyInt_AsLong(pyresult); rc = PI_PyErr_Occurred() ? -1 : 0; VS( rc ? "Finished with failure\n" : "Finished OK\n"); /* all done! */ done: Py_XDECREF(func); Py_XDECREF(pyresult); /* can't leave Python error set, else it may cause failures in later async code */ if (rc) /* But we will print them 'cos they may be useful */ PI_PyErr_Print(); PI_PyErr_Clear(); return rc; }
/* * Run scripts * Return non zero on failure */ int pyi_pylib_run_scripts(ARCHIVE_STATUS *status) { unsigned char *data; char buf[PATH_MAX]; int rc = 0; TOC * ptoc = status->tocbuff; PyObject *__main__ = PI_PyImport_AddModule("__main__"); PyObject *__file__; VS("LOADER: Running scripts\n"); /* Iterate through toc looking for scripts (type 's') */ while (ptoc < status->tocend) { if (ptoc->typcd == ARCHIVE_ITEM_PYSOURCE) { /* Get data out of the archive. */ data = pyi_arch_extract(status, ptoc); /* Set the __file__ attribute within the __main__ module, for full compatibility with normal execution. */ strcpy(buf, ptoc->name); strcat(buf, ".py"); VS("LOADER: Running %s\n", buf); __file__ = PI_PyString_FromStringAndSize(buf, strlen(buf)); PI_PyObject_SetAttrString(__main__, "__file__", __file__); Py_DECREF(__file__); /* Run it */ rc = PI_PyRun_SimpleString((char *) data); /* log errors and abort */ if (rc != 0) { VS("LOADER: RC: %d from %s\n", rc, ptoc->name); return rc; } free(data); } ptoc = pyi_arch_increment_toc_ptr(status, ptoc); } return 0; }
/* * Run scripts * Return non zero on failure */ int pyi_launch_run_scripts(ARCHIVE_STATUS *status) { unsigned char *data; char buf[PATH_MAX]; size_t namelen; TOC * ptoc = status->tocbuff; PyObject *__main__; PyObject *__file__; PyObject *main_dict; PyObject *code, *retval; __main__ = PI_PyImport_AddModule("__main__"); if (!__main__) { FATALERROR("Could not get __main__ module."); return -1; } main_dict = PI_PyModule_GetDict(__main__); if (!main_dict) { FATALERROR("Could not get __main__ module's dict."); return -1; } /* Iterate through toc looking for scripts (type 's') */ while (ptoc < status->tocend) { if (ptoc->typcd == ARCHIVE_ITEM_PYSOURCE) { /* Get data out of the archive. */ data = pyi_arch_extract(status, ptoc); /* Set the __file__ attribute within the __main__ module, * for full compatibility with normal execution. */ namelen = strnlen(ptoc->name, PATH_MAX); if (namelen >= PATH_MAX-strlen(".py")-1) { FATALERROR("Name exceeds PATH_MAX\n"); return -1; } strcpy(buf, ptoc->name); strcat(buf, ".py"); VS("LOADER: Running %s\n", buf); if (is_py2) { __file__ = PI_PyString_FromString(buf); } else { __file__ = PI_PyUnicode_FromString(buf); }; PI_PyObject_SetAttrString(__main__, "__file__", __file__); Py_DECREF(__file__); /* Unmarshall code object */ code = PI_PyMarshal_ReadObjectFromString((const char *) data, ntohl(ptoc->ulen)); if (!code) { FATALERROR("Failed to unmarshal code object for %s\n", ptoc->name); PI_PyErr_Print(); return -1; } /* Run it */ retval = PI_PyEval_EvalCode(code, main_dict, main_dict); /* If retval is NULL, an error occured. Otherwise, it is a Python object. * (Since we evaluate module-level code, which is not allowed to return an * object, the Python object returned is always None.) */ if (!retval) { PI_PyErr_Print(); /* If the error was SystemExit, PyErr_Print calls exit() without * returning. So don't print "Failed to execute" on SystemExit. */ FATALERROR("Failed to execute script %s\n", ptoc->name); return -1; } free(data); } ptoc = pyi_arch_increment_toc_ptr(status, ptoc); } return 0; }