Skip to content
Snippets Groups Projects
Commit c31de002 authored by Willian Padovani Germano's avatar Willian Padovani Germano
Browse files

Script menus:

-- trying a different approach to see if the crash on Windows goes away.

Running a script with PyRun_File -- a Python/C API function -- was probably
the cause for crashes on Windows, because it uses a pointer to a FILE
struct and on windows this struct can be "different and incompatible"
depending on which libc was used to build the program.  This is mentioned in
the Python/C API Ref Manual, chapter 2.

Now we're loading the file contents to a string buffer and using PyRun_String.
parent e581b5c2
No related branches found
No related tags found
No related merge requests found
...@@ -473,13 +473,15 @@ int BPY_txt_do_python(struct SpaceText* st) ...@@ -473,13 +473,15 @@ int BPY_txt_do_python(struct SpaceText* st)
/*****************************************************************************/ /*****************************************************************************/
int BPY_menu_do_python(short menutype, int event) int BPY_menu_do_python(short menutype, int event)
{ {
PyObject *py_dict, *py_result, *pyarg = NULL; PyObject *py_dict, *py_res, *pyarg = NULL;
BPy_constant *info; BPy_constant *info;
BPyMenu *pym; BPyMenu *pym;
BPySubMenu *pysm; BPySubMenu *pysm;
FILE *fp = NULL; FILE *fp = NULL;
char *buffer;
char filestr[FILE_MAXDIR+FILE_MAXFILE]; char filestr[FILE_MAXDIR+FILE_MAXFILE];
Script *script = G.main->script.first; Script *script = G.main->script.first;
int len;
if ((menutype < 0) || (menutype > PYMENU_TOTAL) || (event < 0)) if ((menutype < 0) || (menutype > PYMENU_TOTAL) || (event < 0))
return 0; return 0;
...@@ -555,11 +557,30 @@ int BPY_menu_do_python(short menutype, int event) ...@@ -555,11 +557,30 @@ int BPY_menu_do_python(short menutype, int event)
clearScriptLinks (); clearScriptLinks ();
py_result = PyRun_File(fp, pym->filename, Py_file_input, py_dict, py_dict); /* Previously we used PyRun_File to run directly the code on a FILE object,
* but as written in the Python/C API Ref Manual, chapter 2,
* 'FILE structs for different C libraries can be different and incompatible'
*
* So now we load the script file data to a buffer */
fseek(fp, 0L, SEEK_END);
len = ftell(fp);
fseek(fp, 0L, SEEK_SET);
buffer = MEM_mallocN(len+1, "pyfilebuf"); /* len+1 to add '\0' */
len = fread(buffer, 1, len, fp);
buffer[len] = '\0';
fclose(fp); fclose(fp);
if (!py_result) { /* Failed execution of the script */ /* run the string buffer */
py_res = PyRun_String(buffer, Py_file_input, py_dict, py_dict);
MEM_freeN(buffer);
if (!py_res) { /* Failed execution of the script */
BPY_Err_Handle(script->id.name+2); BPY_Err_Handle(script->id.name+2);
PyErr_Print(); PyErr_Print();
...@@ -572,7 +593,7 @@ int BPY_menu_do_python(short menutype, int event) ...@@ -572,7 +593,7 @@ int BPY_menu_do_python(short menutype, int event)
return 0; return 0;
} }
else { else {
Py_DECREF (py_result); Py_DECREF (py_res);
script->flags &=~SCRIPT_RUNNING; script->flags &=~SCRIPT_RUNNING;
if (!script->flags) { if (!script->flags) {
ReleaseGlobalDictionary(py_dict); ReleaseGlobalDictionary(py_dict);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment