[Python-checkins] r69098 - in python/branches/py3k: Lib/distutils/command/wininst-9.0-amd64.exe Lib/distutils/command/wininst-9.0.exe PC/bdist_wininst/install.c

mark.hammond python-checkins at python.org
Thu Jan 29 14:08:02 CET 2009


Author: mark.hammond
Date: Thu Jan 29 14:08:01 2009
New Revision: 69098

Log:
Fix issue5076: bdist_wininst fails on py3k


Modified:
   python/branches/py3k/Lib/distutils/command/wininst-9.0-amd64.exe
   python/branches/py3k/Lib/distutils/command/wininst-9.0.exe
   python/branches/py3k/PC/bdist_wininst/install.c

Modified: python/branches/py3k/Lib/distutils/command/wininst-9.0-amd64.exe
==============================================================================
Binary files. No diff available.

Modified: python/branches/py3k/Lib/distutils/command/wininst-9.0.exe
==============================================================================
Binary files. No diff available.

Modified: python/branches/py3k/PC/bdist_wininst/install.c
==============================================================================
--- python/branches/py3k/PC/bdist_wininst/install.c	(original)
+++ python/branches/py3k/PC/bdist_wininst/install.c	Thu Jan 29 14:08:01 2009
@@ -114,6 +114,7 @@
 FILE *logfile;
 
 char modulename[MAX_PATH];
+wchar_t wmodulename[MAX_PATH];
 
 HWND hwndMain;
 HWND hDialog;
@@ -299,6 +300,27 @@
 
 typedef void PyObject;
 
+// Convert a "char *" string to "whcar_t *", or NULL on error.
+// Result string must be free'd
+wchar_t *widen_string(char *src)
+{
+	wchar_t *result;
+	DWORD dest_cch;
+	int src_len = strlen(src) + 1; // include NULL term in all ops
+	/* use MultiByteToWideChar() to see how much we need. */
+	/* NOTE: this will include the null-term in the length */
+	dest_cch = MultiByteToWideChar(CP_ACP, 0, src, src_len, NULL, 0);
+	// alloc the buffer
+	result = (wchar_t *)malloc(dest_cch * sizeof(wchar_t));
+	if (result==NULL)
+		return NULL;
+	/* do the conversion */
+	if (0==MultiByteToWideChar(CP_ACP, 0, src, src_len, result, dest_cch)) {
+		free(result);
+		return NULL;
+	}
+	return result;
+}
 
 /*
  * Returns number of files which failed to compile,
@@ -307,7 +329,7 @@
 static int compile_filelist(HINSTANCE hPython, BOOL optimize_flag)
 {
 	DECLPROC(hPython, void, Py_Initialize, (void));
-	DECLPROC(hPython, void, Py_SetProgramName, (char *));
+	DECLPROC(hPython, void, Py_SetProgramName, (wchar_t *));
 	DECLPROC(hPython, void, Py_Finalize, (void));
 	DECLPROC(hPython, int, PyRun_SimpleString, (char *));
 	DECLPROC(hPython, PyObject *, PySys_GetObject, (char *));
@@ -326,7 +348,7 @@
 		return -1;
 
 	*Py_OptimizeFlag = optimize_flag ? 1 : 0;
-	Py_SetProgramName(modulename);
+	Py_SetProgramName(wmodulename);
 	Py_Initialize();
 
 	errors += do_compile_files(PyRun_SimpleString, optimize_flag);
@@ -696,9 +718,10 @@
 static int
 do_run_installscript(HINSTANCE hPython, char *pathname, int argc, char **argv)
 {
-	int fh, result;
+	int fh, result, i;
+	static wchar_t *wargv[256];
 	DECLPROC(hPython, void, Py_Initialize, (void));
-	DECLPROC(hPython, int, PySys_SetArgv, (int, char **));
+	DECLPROC(hPython, int, PySys_SetArgv, (int, wchar_t **));
 	DECLPROC(hPython, int, PyRun_SimpleString, (char *));
 	DECLPROC(hPython, void, Py_Finalize, (void));
 	DECLPROC(hPython, PyObject *, Py_BuildValue, (char *, ...));
@@ -732,7 +755,16 @@
 	Py_Initialize();
 
 	prepare_script_environment(hPython);
-	PySys_SetArgv(argc, argv);
+	// widen the argv array for py3k.
+	memset(wargv, 0, sizeof(wargv));
+	for (i=0;i<argc;i++)
+		wargv[i] = argv[i] ? widen_string(argv[i]) : NULL;
+	PySys_SetArgv(argc, wargv);
+	// free the strings we just widened.
+	for (i=0;i<argc;i++)
+		if (wargv[i])
+			free(wargv[i]);
+
 	result = 3;
 	{
 		struct _stat statbuf;
@@ -807,7 +839,7 @@
 {
 	int rc;
 	DECLPROC(hPython, void, Py_Initialize, (void));
-	DECLPROC(hPython, void, Py_SetProgramName, (char *));
+	DECLPROC(hPython, void, Py_SetProgramName, (wchar_t *));
 	DECLPROC(hPython, void, Py_Finalize, (void));
 	DECLPROC(hPython, int, PyRun_SimpleString, (char *));
 	DECLPROC(hPython, void, PyErr_Print, (void));
@@ -816,7 +848,7 @@
 	    !PyRun_SimpleString || !PyErr_Print)
 		return -1;
 
-	Py_SetProgramName(modulename);
+	Py_SetProgramName(wmodulename);
 	Py_Initialize();
 	prepare_script_environment(hPython);
 	rc = PyRun_SimpleString(script);
@@ -2618,6 +2650,7 @@
 	char *basename;
 
 	GetModuleFileName(NULL, modulename, sizeof(modulename));
+	GetModuleFileNameW(NULL, wmodulename, sizeof(wmodulename)/sizeof(wmodulename[0]));
 
 	/* Map the executable file to memory */
 	arc_data = MapExistingFile(modulename, &arc_size);


More information about the Python-checkins mailing list