Skip to content

Commit c00213b

Browse files
author
mark.hammond
committed
Fix issue5076: bdist_wininst fails on py3k
git-svn-id: http://svn.python.org/projects/python/branches/py3k@69098 6015fed2-1504-0410-9fe1-9d1591cc4771
1 parent 4446179 commit c00213b

3 files changed

Lines changed: 40 additions & 7 deletions

File tree

512 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.

PC/bdist_wininst/install.c

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@
114114
FILE *logfile;
115115

116116
char modulename[MAX_PATH];
117+
wchar_t wmodulename[MAX_PATH];
117118

118119
HWND hwndMain;
119120
HWND hDialog;
@@ -299,6 +300,27 @@ static int do_compile_files(int (__cdecl * PyRun_SimpleString)(char *),
299300

300301
typedef void PyObject;
301302

303+
// Convert a "char *" string to "whcar_t *", or NULL on error.
304+
// Result string must be free'd
305+
wchar_t *widen_string(char *src)
306+
{
307+
wchar_t *result;
308+
DWORD dest_cch;
309+
int src_len = strlen(src) + 1; // include NULL term in all ops
310+
/* use MultiByteToWideChar() to see how much we need. */
311+
/* NOTE: this will include the null-term in the length */
312+
dest_cch = MultiByteToWideChar(CP_ACP, 0, src, src_len, NULL, 0);
313+
// alloc the buffer
314+
result = (wchar_t *)malloc(dest_cch * sizeof(wchar_t));
315+
if (result==NULL)
316+
return NULL;
317+
/* do the conversion */
318+
if (0==MultiByteToWideChar(CP_ACP, 0, src, src_len, result, dest_cch)) {
319+
free(result);
320+
return NULL;
321+
}
322+
return result;
323+
}
302324

303325
/*
304326
* Returns number of files which failed to compile,
@@ -307,7 +329,7 @@ typedef void PyObject;
307329
static int compile_filelist(HINSTANCE hPython, BOOL optimize_flag)
308330
{
309331
DECLPROC(hPython, void, Py_Initialize, (void));
310-
DECLPROC(hPython, void, Py_SetProgramName, (char *));
332+
DECLPROC(hPython, void, Py_SetProgramName, (wchar_t *));
311333
DECLPROC(hPython, void, Py_Finalize, (void));
312334
DECLPROC(hPython, int, PyRun_SimpleString, (char *));
313335
DECLPROC(hPython, PyObject *, PySys_GetObject, (char *));
@@ -326,7 +348,7 @@ static int compile_filelist(HINSTANCE hPython, BOOL optimize_flag)
326348
return -1;
327349

328350
*Py_OptimizeFlag = optimize_flag ? 1 : 0;
329-
Py_SetProgramName(modulename);
351+
Py_SetProgramName(wmodulename);
330352
Py_Initialize();
331353

332354
errors += do_compile_files(PyRun_SimpleString, optimize_flag);
@@ -696,9 +718,10 @@ static int prepare_script_environment(HINSTANCE hPython)
696718
static int
697719
do_run_installscript(HINSTANCE hPython, char *pathname, int argc, char **argv)
698720
{
699-
int fh, result;
721+
int fh, result, i;
722+
static wchar_t *wargv[256];
700723
DECLPROC(hPython, void, Py_Initialize, (void));
701-
DECLPROC(hPython, int, PySys_SetArgv, (int, char **));
724+
DECLPROC(hPython, int, PySys_SetArgv, (int, wchar_t **));
702725
DECLPROC(hPython, int, PyRun_SimpleString, (char *));
703726
DECLPROC(hPython, void, Py_Finalize, (void));
704727
DECLPROC(hPython, PyObject *, Py_BuildValue, (char *, ...));
@@ -732,7 +755,16 @@ do_run_installscript(HINSTANCE hPython, char *pathname, int argc, char **argv)
732755
Py_Initialize();
733756

734757
prepare_script_environment(hPython);
735-
PySys_SetArgv(argc, argv);
758+
// widen the argv array for py3k.
759+
memset(wargv, 0, sizeof(wargv));
760+
for (i=0;i<argc;i++)
761+
wargv[i] = argv[i] ? widen_string(argv[i]) : NULL;
762+
PySys_SetArgv(argc, wargv);
763+
// free the strings we just widened.
764+
for (i=0;i<argc;i++)
765+
if (wargv[i])
766+
free(wargv[i]);
767+
736768
result = 3;
737769
{
738770
struct _stat statbuf;
@@ -807,7 +839,7 @@ static int do_run_simple_script(HINSTANCE hPython, char *script)
807839
{
808840
int rc;
809841
DECLPROC(hPython, void, Py_Initialize, (void));
810-
DECLPROC(hPython, void, Py_SetProgramName, (char *));
842+
DECLPROC(hPython, void, Py_SetProgramName, (wchar_t *));
811843
DECLPROC(hPython, void, Py_Finalize, (void));
812844
DECLPROC(hPython, int, PyRun_SimpleString, (char *));
813845
DECLPROC(hPython, void, PyErr_Print, (void));
@@ -816,7 +848,7 @@ static int do_run_simple_script(HINSTANCE hPython, char *script)
816848
!PyRun_SimpleString || !PyErr_Print)
817849
return -1;
818850

819-
Py_SetProgramName(modulename);
851+
Py_SetProgramName(wmodulename);
820852
Py_Initialize();
821853
prepare_script_environment(hPython);
822854
rc = PyRun_SimpleString(script);
@@ -2618,6 +2650,7 @@ int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst,
26182650
char *basename;
26192651

26202652
GetModuleFileName(NULL, modulename, sizeof(modulename));
2653+
GetModuleFileNameW(NULL, wmodulename, sizeof(wmodulename)/sizeof(wmodulename[0]));
26212654

26222655
/* Map the executable file to memory */
26232656
arc_data = MapExistingFile(modulename, &arc_size);

0 commit comments

Comments
 (0)