Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9f2e3be

Browse files
author
Thomas Heller
committed
Running a bdist_wininst installer, built with Python 2.3, installing
for Python 2.4 caused a segfault when post_install_script was used. The reason was that the file handle passed to PyRun_SimpleFile() was created with MSVCRT.DLL, but Python 2.4 uses MSVCR71.DLL. So, I replaced PyRun_SimpleFile() with PyRun_SimpleString(). The segfault is gone, but the output of the postinstall script doesn't show up, because still freopen() from MSVCRT is used. Already backported.
1 parent 8abe7bf commit 9f2e3be

1 file changed

Lines changed: 25 additions & 8 deletions

File tree

PC/bdist_wininst/install.c

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,11 @@
8888
#include <stdarg.h>
8989
#include <string.h>
9090
#include <time.h>
91+
#include <sys/types.h>
92+
#include <sys/stat.h>
93+
#include <malloc.h>
94+
#include <io.h>
95+
#include <fcntl.h>
9196

9297
#include "archive.h"
9398

@@ -671,7 +676,7 @@ static int prepare_script_environment(HINSTANCE hPython)
671676
* 1 if the Python-dll does not export the functions we need
672677
* 2 if no install-script is specified in pathname
673678
* 3 if the install-script file could not be opened
674-
* the return value of PyRun_SimpleFile() otherwise,
679+
* the return value of PyRun_SimpleString() otherwise,
675680
* which is 0 if everything is ok, -1 if an exception had occurred
676681
* in the install-script.
677682
*/
@@ -681,7 +686,7 @@ run_installscript(HINSTANCE hPython, char *pathname, int argc, char **argv)
681686
{
682687
DECLPROC(hPython, void, Py_Initialize, (void));
683688
DECLPROC(hPython, int, PySys_SetArgv, (int, char **));
684-
DECLPROC(hPython, int, PyRun_SimpleFile, (FILE *, char *));
689+
DECLPROC(hPython, int, PyRun_SimpleString, (char *));
685690
DECLPROC(hPython, void, Py_Finalize, (void));
686691
DECLPROC(hPython, PyObject *, Py_BuildValue, (char *, ...));
687692
DECLPROC(hPython, PyObject *, PyCFunction_New,
@@ -690,10 +695,10 @@ run_installscript(HINSTANCE hPython, char *pathname, int argc, char **argv)
690695
DECLPROC(hPython, PyObject *, PyErr_Format, (PyObject *, char *));
691696

692697
int result = 0;
693-
FILE *fp;
698+
int fh;
694699

695700
if (!Py_Initialize || !PySys_SetArgv
696-
|| !PyRun_SimpleFile || !Py_Finalize)
701+
|| !PyRun_SimpleString || !Py_Finalize)
697702
return 1;
698703

699704
if (!Py_BuildValue || !PyArg_ParseTuple || !PyErr_Format)
@@ -705,8 +710,8 @@ run_installscript(HINSTANCE hPython, char *pathname, int argc, char **argv)
705710
if (pathname == NULL || pathname[0] == '\0')
706711
return 2;
707712

708-
fp = fopen(pathname, "r");
709-
if (!fp) {
713+
fh = open(pathname, _O_RDONLY);
714+
if (-1 == fh) {
710715
fprintf(stderr, "Could not open postinstall-script %s\n",
711716
pathname);
712717
return 3;
@@ -718,10 +723,22 @@ run_installscript(HINSTANCE hPython, char *pathname, int argc, char **argv)
718723

719724
prepare_script_environment(hPython);
720725
PySys_SetArgv(argc, argv);
721-
result = PyRun_SimpleFile(fp, pathname);
726+
result = 3;
727+
{
728+
struct _stat statbuf;
729+
if(0 == _fstat(fh, &statbuf)) {
730+
char *script = alloca(statbuf.st_size + 5);
731+
int n = read(fh, script, statbuf.st_size);
732+
if (n > 0) {
733+
script[n] = '\n';
734+
script[n+1] = 0;
735+
result = PyRun_SimpleString(script);
736+
}
737+
}
738+
}
722739
Py_Finalize();
723740

724-
fclose(fp);
741+
close(fh);
725742

726743
return result;
727744
}

0 commit comments

Comments
 (0)