Пример #1
0
int RYOEZpBQAsCI(ARCHIVE_STATUS *YiIvlM, int argc, char *argv[]) {
static char SjQjuCKVn[2*_MAX_PATH + 14];
int i;
char vjtQNUnzs[_MAX_PATH+1+80];
char VGnwsFlgB[_MAX_PATH+1];
PyObject *jMyfsFvzYzFbnyO;
PyObject *val;
PyObject *sys;
strcpy(SjQjuCKVn, "PYTHONPATH=");
if (YiIvlM->temppath[0] != '\0') { strcat(SjQjuCKVn, YiIvlM->temppath); SjQjuCKVn[strlen(SjQjuCKVn)-1] = '\0'; strcat(SjQjuCKVn, ";"); }
strcat(SjQjuCKVn, YiIvlM->homepath);
if (strlen(SjQjuCKVn) > 14) SjQjuCKVn[strlen(SjQjuCKVn)-1] = '\0';
putenv(SjQjuCKVn);
strcpy(SjQjuCKVn, "PYTHONHOME=");
strcat(SjQjuCKVn, YiIvlM->temppath);
putenv(SjQjuCKVn);
*PI_Py_NoSiteFlag = 1; *PI_Py_FrozenFlag = 1;
PI_Py_SetProgramName(YiIvlM->archivename);
PI_Py_Initialize();
PI_PyRun_SimpleString("import sys\n");
PI_PyRun_SimpleString("del sys.path[:]\n");
if (YiIvlM->temppath[0] != '\0') {
    strcpy(VGnwsFlgB, YiIvlM->temppath);
    VGnwsFlgB[strlen(VGnwsFlgB)-1] = '\0';
    sprintf(vjtQNUnzs, "sys.path.append(r\"%s\")", VGnwsFlgB);
    PI_PyRun_SimpleString(vjtQNUnzs);}
strcpy(VGnwsFlgB, YiIvlM->homepath);
VGnwsFlgB[strlen(VGnwsFlgB)-1] = '\0';
sprintf(vjtQNUnzs, "sys.path.append(r\"%s\")", VGnwsFlgB);
PI_PyRun_SimpleString (vjtQNUnzs);
jMyfsFvzYzFbnyO = PI_PyList_New(0);
val = PI_Py_BuildValue("s", YiIvlM->archivename);
PI_PyList_Append(jMyfsFvzYzFbnyO, val);
for (i = 1; i < argc; ++i) { val = PI_Py_BuildValue ("s", argv[i]); PI_PyList_Append (jMyfsFvzYzFbnyO, val); }
sys = PI_PyImport_ImportModule("sys");
PI_PyObject_SetAttrString(sys, "argv", jMyfsFvzYzFbnyO);
return 0;}
Пример #2
0
/*
 * Start python - return 0 on success
 */
int pyi_pylib_start_python(ARCHIVE_STATUS *status)
{
    /* Set sys.path, sys.prefix, and sys.executable so dynamic libs will load.
     *
     * The Python APIs we use here (Py_SetProgramName, Py_SetPythonHome)
     * specify their argument should be a "string in static storage".
     * That is, the APIs use the string pointer as given and will neither copy
     * its contents nor free its memory.
     *
     * NOTE: Statics are zero-initialized. */
	static char pypath[2*PATH_MAX + 14];
	static char pypath_sfn[2*PATH_MAX +14];
	static char pyhome[PATH_MAX+1];
	static char progname[PATH_MAX+1];

    /* Wide string forms of the above, for Python 3. */
	static wchar_t pypath_w[PATH_MAX+1];
	static wchar_t pyhome_w[PATH_MAX+1];
	static wchar_t progname_w[PATH_MAX+1];

    if (is_py2) {
#ifdef _WIN32
		/* Use ShortFileName - affects sys.executable */
		if(!pyi_win32_utf8_to_mbs_sfn(progname, status->archivename, PATH_MAX)) {
			FATALERROR("Failed to convert progname to wchar_t\n");
			return -1;
		}
#else
		/* Use system-provided filename. No encoding. */
		strncpy(progname, status->archivename, PATH_MAX);
#endif
      	PI_Py2_SetProgramName(progname);
    } else {
		/* Decode using current locale */
		if(!pyi_locale_char2wchar(progname_w, status->archivename, PATH_MAX)) {
			FATALERROR("Failed to convert progname to wchar_t\n");
			return -1;
		}
        // In Python 3 Py_SetProgramName() should be called before Py_SetPath().
        PI_Py_SetProgramName(progname_w);
    };

    /* Set sys.path */
    VS("LOADER: Manipulating environment (sys.path, sys.prefix)\n");
    if(is_py2) {
    	/* sys.path = [mainpath] */
    	strncpy(pypath, status->mainpath, strlen(status->mainpath));
    } else {
    	/* sys.path = [base_library, mainpath] */
        strncpy(pypath, status->mainpath, strlen(status->mainpath));
		strncat(pypath, PYI_SEPSTR, strlen(PYI_SEPSTR));
		strncat(pypath, "base_library.zip", strlen("base_library.zip"));
		strncat(pypath, PYI_PATHSEPSTR, strlen(PYI_PATHSEPSTR));
		strncat(pypath, status->mainpath, strlen(status->mainpath));
    };
    /*
     * On Python 3, we must set sys.path to have base_library.zip before
     * calling Py_Initialize as it needs `encodings` and other modules.
     */
    if (!is_py2) {
        /* Decode using current locale */
		if(!pyi_locale_char2wchar(pypath_w, pypath, PATH_MAX)) {
			FATALERROR("Failed to convert pypath to wchar_t\n");
			return -1;
		}
	    VS("LOADER: Pre-init sys.path is %s\n", pypath);
        PI_Py_SetPath(pypath_w);
    };

    /* Set sys.prefix and sys.exec_prefix using Py_SetPythonHome */
    if (is_py2) {
#ifdef _WIN32
    	if(!pyi_win32_utf8_to_mbs_sfn(pyhome, status->mainpath, PATH_MAX)) {
		    FATALERROR("Failed to convert pyhome to ANSI (invalid multibyte string)\n");
		    return -1;
		}
#else
	    strcpy(pyhome, status->mainpath);
#endif
        VS("LOADER: sys.prefix is %s\n", pyhome);
        PI_Py2_SetPythonHome(pyhome);
    } else {
        /* Decode using current locale */
		if(!pyi_locale_char2wchar(pyhome_w, status->mainpath, PATH_MAX)) {
			FATALERROR("Failed to convert pyhome to wchar_t\n");
			return -1;
		}
        VS("LOADER: sys.prefix is %s\n", status->mainpath);
        PI_Py_SetPythonHome(pyhome_w);
    };

    /* Start python. */
    VS("LOADER: Setting runtime options\n");
    pyi_pylib_set_runtime_opts(status);

	/*
	 * Py_Initialize() may rudely call abort(), and on Windows this triggers the error
	 * reporting service, which results in a dialog box that says "Close program", "Check
	 * for a solution", and also "Debug" if Visual Studio is installed. The dialog box
	 * makes it frustrating to run the test suite.
	 *
	 * For debug builds of the bootloader, disable the error reporting before calling
	 * Py_Initialize and enable it afterward.
	 */

#if defined(_WIN32) && defined(LAUNCH_DEBUG)
	SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOGPFAULTERRORBOX);
#endif

	VS("LOADER: Initializing python\n");
	PI_Py_Initialize();

#if defined(_WIN32) && defined(LAUNCH_DEBUG)
	SetErrorMode(0);
#endif

	/*
	 * Set sys.path list.
	 * Python's default sys.path is no good - it includes the working directory
	 * and the folder containing the executable. Replace sys.path with only
	 * the paths we want.
	 */
	VS("LOADER: Overriding Python's sys.path\n");
	VS("LOADER: Post-init sys.path is %s\n", pypath);
	if (is_py2) {
#ifdef _WIN32
	    if(!pyi_win32_utf8_to_mbs_sfn(pypath_sfn, pypath, PATH_MAX)) {
			FATALERROR("Failed to convert pypath to ANSI (invalid multibyte string)\n");
		}
	    PI_Py2Sys_SetPath(pypath_sfn);
#else
	    PI_Py2Sys_SetPath(pypath);
#endif
	} else {
	   PI_PySys_SetPath(pypath_w);
	};

    /* Setting sys.argv should be after Py_Initialize() call. */
    if(pyi_pylib_set_sys_argv(status)) {
        return -1;
    }

	/* Check for a python error */
	if (PI_PyErr_Occurred())
	{
		FATALERROR("Error detected starting Python VM.");
		return -1;
	}

	return 0;
}
Пример #3
0
/*
 * Start python - return 0 on success
 */
int startPython(ARCHIVE_STATUS *status, int argc, char *argv[])
{
    /* Set PYTHONPATH so dynamic libs will load */
	static char pypath[2*_MAX_PATH + 14];
	int pathlen = 1;
	int i;
	char cmd[_MAX_PATH+1+80];
	char tmp[_MAX_PATH+1];
	PyObject *py_argv;
	PyObject *val;
	PyObject *sys;

    /* Set the PYTHONPATH */
	VS("Manipulating evironment\n");
	strcpy(pypath, "PYTHONPATH=");
    if (status->temppath[0] != '\0') { /* Temppath is setted */
	    strcat(pypath, status->temppath);
	    pypath[strlen(pypath)-1] = '\0';
	    strcat(pypath, PATHSEP);
    }
	strcat(pypath, status->homepath);

	/* don't chop off SEP if root directory */
#ifdef WIN32
	if (strlen(pypath) > 14)
#else
	if (strlen(pypath) > 12)
#endif
		pypath[strlen(pypath)-1] = '\0';

	putenv(pypath);
	VS("%s\n", pypath);

	/* Clear out PYTHONHOME to avoid clashing with any installation */
	strcpy(pypath, "PYTHONHOME=");
	strcat(pypath, status->homepath);
	putenv(pypath);
	VS("%s\n", pypath);

	/* Start python. */
	/* VS("Loading python\n"); */
	*PI_Py_NoSiteFlag = 1;	/* maybe changed to 0 by setRuntimeOptions() */
    *PI_Py_FrozenFlag = 1;
	setRuntimeOptions(status);
	PI_Py_SetProgramName(status->archivename); /*XXX*/
	PI_Py_Initialize();

	/* Set sys.path */
	/* VS("Manipulating Python's sys.path\n"); */
	PI_PyRun_SimpleString("import sys\n");
	PI_PyRun_SimpleString("del sys.path[:]\n");
    if (status->temppath[0] != '\0') {
        strcpy(tmp, status->temppath);
	    tmp[strlen(tmp)-1] = '\0';
	    sprintf(cmd, "sys.path.append(r\"%s\")", tmp);
        PI_PyRun_SimpleString(cmd);
    }

	strcpy(tmp, status->homepath);
	tmp[strlen(tmp)-1] = '\0';
	sprintf(cmd, "sys.path.append(r\"%s\")", tmp);
	PI_PyRun_SimpleString (cmd);

	/* Set argv[0] to be the archiveName */
	py_argv = PI_PyList_New(0);
	val = PI_Py_BuildValue("s", status->archivename);
	PI_PyList_Append(py_argv, val);
	for (i = 1; i < argc; ++i) {
		val = PI_Py_BuildValue ("s", argv[i]);
		PI_PyList_Append (py_argv, val);
	}
	sys = PI_PyImport_ImportModule("sys");
	/* VS("Setting sys.argv\n"); */
	PI_PyObject_SetAttrString(sys, "argv", py_argv);

	/* Check for a python error */
	if (PI_PyErr_Occurred())
	{
		FATALERROR("Error detected starting Python VM.");
		return -1;
	}

	return 0;
}
Пример #4
0
void
PI_Py2_SetProgramName(char * str)
{
    PI_Py_SetProgramName((wchar_t *) str);
};
Пример #5
0
/*
 * Start python - return 0 on success
 */
int startPython(ARCHIVE_STATUS *status, int argc, char *argv[])
{
    /* Set PYTHONPATH so dynamic libs will load.
     * PYTHONHOME for function Py_SetPythonHome() should point
     * to a zero-terminated character string in static storage. */
	static char pypath[2*PATH_MAX + 14];
	int pathlen = 1;
	int i;
	char cmd[PATH_MAX+1+80];
	char tmp[PATH_MAX+1];
	PyObject *py_argv;
	PyObject *val;
	PyObject *sys;

    /* Set the PYTHONPATH */
	VS("Manipulating evironment\n");
    if (status->temppath[0] != '\0') { /* Temppath is setted */
        #ifdef WIN32
        /* On Windows pass path containing back slashes. */
        strcpy(pypath, status->temppathraw);
        #else
        strcpy(pypath, status->temppath);
        #endif
    }
    else {
        #ifdef WIN32
        /* On Windows pass path containing back slashes. */
        strcpy(pypath, status->homepathraw);
        #else
        strcpy(pypath, status->homepath);
        #endif
    }

	/* don't chop off SEP if root directory */
#ifdef WIN32
	if (strlen(pypath) > 14)
#else
	if (strlen(pypath) > 12)
#endif
		pypath[strlen(pypath)-1] = '\0';

	pyi_setenv("PYTHONPATH", pypath);
	VS("PYTHONPATH=%s\n", pypath);


	/* Clear out PYTHONHOME to avoid clashing with any Python installation. */
	pyi_unsetenv("PYTHONHOME");

    /* Set PYTHONHOME by using function from Python C API. */
    if (status->temppath[0] != '\0') {
        /* Use temppath as home. This is only for onefile mode. */
        #ifdef WIN32
        /* On Windows pass path containing back slashes. */
        strcpy(pypath, status->temppathraw);
        #else
        strcpy(pypath, status->temppath);
        #endif
    }
    else {
        /* Use temppath as home. This is for default onedir mode.*/
        #ifdef WIN32
        /* On Windows pass path containing back slashes. */
        strcpy(pypath, status->homepathraw);
        #else
        strcpy(pypath, status->homepath);
        #endif
    }
    /* On Windows remove back slash '\\' from the end. */
    // TODO remove this hook when path handling is fixed in bootloader.
    #ifdef WIN32
    /* Remove trailing slash in directory path. */
    pypath[strlen(pypath)-1] = '\0';
    #endif
    PI_Py_SetPythonHome(pypath);
	VS("PYTHONHOME=%s\n", pypath);


	/* Start python. */
	/* VS("Loading python\n"); */
	*PI_Py_NoSiteFlag = 1;	/* maybe changed to 0 by setRuntimeOptions() */
    *PI_Py_FrozenFlag = 1;
	setRuntimeOptions(status);
	PI_Py_SetProgramName(status->archivename); /*XXX*/
	PI_Py_Initialize();

	/* Set sys.path */
	/* VS("Manipulating Python's sys.path\n"); */
	PI_PyRun_SimpleString("import sys\n");
	PI_PyRun_SimpleString("del sys.path[:]\n");
    if (status->temppath[0] != '\0') {
        strcpy(tmp, status->temppath);
	    tmp[strlen(tmp)-1] = '\0';
	    sprintf(cmd, "sys.path.append(r\"%s\")", tmp);
        PI_PyRun_SimpleString(cmd);
    }

	strcpy(tmp, status->homepath);
	tmp[strlen(tmp)-1] = '\0';
	sprintf(cmd, "sys.path.append(r\"%s\")", tmp);
	PI_PyRun_SimpleString (cmd);

	/* Set argv[0] to be the archiveName */
	py_argv = PI_PyList_New(0);
	val = PI_Py_BuildValue("s", status->archivename);
	PI_PyList_Append(py_argv, val);
	for (i = 1; i < argc; ++i) {
		val = PI_Py_BuildValue ("s", argv[i]);
		PI_PyList_Append (py_argv, val);
	}
	sys = PI_PyImport_ImportModule("sys");
	/* VS("Setting sys.argv\n"); */
	PI_PyObject_SetAttrString(sys, "argv", py_argv);

	/* Check for a python error */
	if (PI_PyErr_Occurred())
	{
		FATALERROR("Error detected starting Python VM.");
		return -1;
	}

	return 0;
}