Example #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;}
Example #2
0
/*
 * Install a zlib from a toc entry.
 *
 * Must be called after Py_Initialize (i.e. after pyi_pylib_start_python)
 *
 * The installation is done by adding an entry like
 *    absolute_path/dist/hello_world/hello_world?123456
 * to sys.path. The end number is the offset where the
 * Python bootstrap code should read the zip data.
 * Return non zero on failure.
 * NB: This entry is removed from sys.path by the bootstrap scripts.
 */
int pyi_pylib_install_zlib(ARCHIVE_STATUS *status, TOC *ptoc)
{
	int rc = 0;
	int zlibpos = status->pkgstart + ntohl(ptoc->pos);
	PyObject * sys_path, *zlib_entry, *archivename_obj;
	char *archivename;

	/* Note that sys.path contains PyString on py2, and PyUnicode on py3. Ensure
	 * that filenames are encoded or decoded correctly.
	 */
	if(is_py2) {
#ifdef _WIN32
		/* Must be MBCS encoded. Use SFN if possible.
		 *
		 * We could instead pass the UTF-8 encoded form and modify FrozenImporter to
		 * decode it on Windows, but this breaks the convention that `sys.path`
		 * entries on Windows are MBCS encoded, and may interfere with any code
		 * that inspects `sys.path`
		 *
		 * We could also pass the zlib path through a channel other than `sys.path`
		 * to sidestep that requirement, but there's not much benefit as this only
		 * improves non-codepage/non-SFN compatibility for the zlib and not any other
		 * importable modules.
		 */

		archivename = pyi_win32_utf8_to_mbs_sfn(NULL, status->archivename, 0);
		if(NULL == archivename) {
		    FATALERROR("Failed to convert %s to ShortFileName\n", status->archivename);
			return -1;
		}
#else
		/* Use system-provided path. No encoding required. */
		archivename = status->archivename;
#endif
		zlib_entry = PI_PyString_FromFormat("%s?%d", archivename, zlibpos);
		if(archivename != status->archivename) free(archivename);

	} else {
#ifdef _WIN32
		/* Decode UTF-8 to PyUnicode */
		archivename_obj = PI_PyUnicode_Decode(status->archivename,
											  strlen(status->archivename),
											  "utf-8",
											  "strict");
#else
		/* Decode locale-encoded filename to PyUnicode object using Python's
		 * preferred decoding method for filenames.
		 */
		archivename_obj = PI_PyUnicode_DecodeFSDefault(status->archivename);
#endif
		zlib_entry = PI_PyUnicode_FromFormat("%U?%d", archivename_obj, zlibpos);
		PI_Py_DecRef(archivename_obj);
	}

	sys_path = PI_PySys_GetObject("path");
	if(NULL == sys_path) {
		FATALERROR("Installing PYZ: Could not get sys.path\n");
		PI_Py_DecRef(zlib_entry);
		return -1;
	}

	rc = PI_PyList_Append(sys_path, zlib_entry);
	if(rc) {
		FATALERROR("Failed to append to sys.path\n");
	}

	return rc;
}
Example #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;
}
Example #4
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;
}