- fix for python refcounting crashes, remember PyDict_GetItem and PyDict_GetItemString borrow a ref.
- the namespace dictionary wasn't being de-allocated for each run. - clear every error after printing it to avoid stale PyObjects hanging about.
This commit is contained in:
		@@ -179,7 +179,7 @@ int BPY_run_python_script( bContext *C, const char *fn, struct Text *text )
 | 
			
		||||
			MEM_freeN( buf );
 | 
			
		||||
 | 
			
		||||
			if( PyErr_Occurred(  ) ) {
 | 
			
		||||
				PyErr_Print();
 | 
			
		||||
				PyErr_Print(); PyErr_Clear();
 | 
			
		||||
				BPY_free_compiled_text( text );
 | 
			
		||||
				PyGILState_Release(gilstate);
 | 
			
		||||
				return 0;
 | 
			
		||||
@@ -195,10 +195,12 @@ int BPY_run_python_script( bContext *C, const char *fn, struct Text *text )
 | 
			
		||||
	}
 | 
			
		||||
	
 | 
			
		||||
	if (!py_result) {
 | 
			
		||||
		PyErr_Print();
 | 
			
		||||
		PyErr_Print(); PyErr_Clear();
 | 
			
		||||
	} else {
 | 
			
		||||
		Py_DECREF( py_result );
 | 
			
		||||
	}
 | 
			
		||||
	
 | 
			
		||||
	Py_DECREF(py_dict);
 | 
			
		||||
	PyGILState_Release(gilstate);
 | 
			
		||||
	
 | 
			
		||||
	//BPY_end_python();
 | 
			
		||||
@@ -220,7 +222,7 @@ static void exit_pydraw( SpaceScript * sc, short err )
 | 
			
		||||
	script = sc->script;
 | 
			
		||||
 | 
			
		||||
	if( err ) {
 | 
			
		||||
		PyErr_Print(  );
 | 
			
		||||
		PyErr_Print(); PyErr_Clear();
 | 
			
		||||
		script->flags = 0;	/* mark script struct for deletion */
 | 
			
		||||
		SCRIPT_SET_NULL(script);
 | 
			
		||||
		script->scriptname[0] = '\0';
 | 
			
		||||
@@ -327,13 +329,14 @@ int BPY_run_python_script_space(const char *modulename, const char *func)
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	
 | 
			
		||||
	if (!py_result)
 | 
			
		||||
		PyErr_Print();
 | 
			
		||||
	else
 | 
			
		||||
	if (!py_result) {
 | 
			
		||||
		PyErr_Print(); PyErr_Clear();
 | 
			
		||||
	} else
 | 
			
		||||
		Py_DECREF( py_result );
 | 
			
		||||
	
 | 
			
		||||
	Py_XDECREF(module);
 | 
			
		||||
	
 | 
			
		||||
	Py_DECREF(py_dict);
 | 
			
		||||
	
 | 
			
		||||
	PyGILState_Release(gilstate);
 | 
			
		||||
	return 1;
 | 
			
		||||
@@ -406,7 +409,7 @@ void BPY_run_ui_scripts(bContext *C, int reload)
 | 
			
		||||
			if(mod) {
 | 
			
		||||
				Py_DECREF(mod); /* could be NULL from reloading */
 | 
			
		||||
			} else {
 | 
			
		||||
				PyErr_Print();
 | 
			
		||||
				PyErr_Print(); PyErr_Clear();
 | 
			
		||||
				fprintf(stderr, "unable to import \"%s\"  %s/%s\n", path, dirname, de->d_name);
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
@@ -527,7 +530,7 @@ static float pydriver_error(ChannelDriver *driver)
 | 
			
		||||
	driver->flag |= DRIVER_FLAG_INVALID; /* py expression failed */
 | 
			
		||||
	fprintf(stderr, "\nError in Driver: The following Python expression failed:\n\t'%s'\n\n", driver->expression);
 | 
			
		||||
	
 | 
			
		||||
	PyErr_Print();
 | 
			
		||||
	PyErr_Print(); PyErr_Clear();
 | 
			
		||||
 | 
			
		||||
	return 0.0f;
 | 
			
		||||
}
 | 
			
		||||
@@ -586,7 +589,7 @@ float BPY_pydriver_eval (ChannelDriver *driver)
 | 
			
		||||
			}
 | 
			
		||||
			
 | 
			
		||||
			fprintf(stderr, "\tBPY_pydriver_eval() - couldn't add variable '%s' to namespace \n", dtar->name);
 | 
			
		||||
			PyErr_Print();
 | 
			
		||||
			PyErr_Print(); PyErr_Clear();
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	
 | 
			
		||||
 
 | 
			
		||||
@@ -1144,7 +1144,7 @@ static PyObject * pyrna_func_call(PyObject * self, PyObject *args, PyObject *kw)
 | 
			
		||||
			i++;
 | 
			
		||||
		}
 | 
			
		||||
		else if (kw != NULL)
 | 
			
		||||
			item= PyDict_GetItemString(kw, pid);
 | 
			
		||||
			item= PyDict_GetItemString(kw, pid);  /* borrow ref */
 | 
			
		||||
 | 
			
		||||
		if (item==NULL) {
 | 
			
		||||
			if(flag & PROP_REQUIRED) {
 | 
			
		||||
@@ -1957,12 +1957,10 @@ PyObject *pyrna_basetype_register(PyObject *self, PyObject *args)
 | 
			
		||||
	}
 | 
			
		||||
	
 | 
			
		||||
	/* get the context, so register callback can do necessary refreshes */
 | 
			
		||||
	item= PyDict_GetItemString(PyEval_GetGlobals(), "__bpy_context__");
 | 
			
		||||
	item= PyDict_GetItemString(PyEval_GetGlobals(), "__bpy_context__");  /* borrow ref */
 | 
			
		||||
 | 
			
		||||
	if(item) {
 | 
			
		||||
	if(item)
 | 
			
		||||
		C= (bContext*)PyCObject_AsVoidPtr(item);
 | 
			
		||||
		Py_DECREF(item);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	/* call the register callback */
 | 
			
		||||
	BKE_reports_init(&reports, RPT_PRINT);
 | 
			
		||||
@@ -1998,18 +1996,14 @@ PyObject *pyrna_basetype_unregister(PyObject *self, PyObject *args)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	/* check we got an __rna__ attribute */
 | 
			
		||||
	item= PyDict_GetItemString(((PyTypeObject*)py_class)->tp_dict, "__rna__");
 | 
			
		||||
	item= PyDict_GetItemString(((PyTypeObject*)py_class)->tp_dict, "__rna__");  /* borrow ref */
 | 
			
		||||
 | 
			
		||||
	if(!item || !BPy_StructRNA_Check(item)) {
 | 
			
		||||
		if(item) {
 | 
			
		||||
			Py_DECREF(item);
 | 
			
		||||
		}
 | 
			
		||||
		PyErr_SetString(PyExc_AttributeError, "expected a Type subclassed from a registerable rna type (no __rna__ property).");
 | 
			
		||||
		return NULL;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	/* check the __rna__ attribute has the right type */
 | 
			
		||||
	Py_DECREF(item);
 | 
			
		||||
	py_srna= (BPy_StructRNA*)item;
 | 
			
		||||
 | 
			
		||||
	if(py_srna->ptr.type != &RNA_Struct) {
 | 
			
		||||
@@ -2026,12 +2020,10 @@ PyObject *pyrna_basetype_unregister(PyObject *self, PyObject *args)
 | 
			
		||||
	}
 | 
			
		||||
	
 | 
			
		||||
	/* get the context, so register callback can do necessary refreshes */
 | 
			
		||||
	item= PyDict_GetItemString(PyEval_GetGlobals(), "__bpy_context__");
 | 
			
		||||
	item= PyDict_GetItemString(PyEval_GetGlobals(), "__bpy_context__");  /* borrow ref */
 | 
			
		||||
 | 
			
		||||
	if(item) {
 | 
			
		||||
	if(item)
 | 
			
		||||
		C= (bContext*)PyCObject_AsVoidPtr(item);
 | 
			
		||||
		Py_DECREF(item);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	/* call unregister */
 | 
			
		||||
	unreg(C, py_srna->ptr.data);
 | 
			
		||||
 
 | 
			
		||||
@@ -344,7 +344,7 @@ static PyObject *Method_registerKey( PyObject * self, PyObject * args )
 | 
			
		||||
static bContext *get_py_context__internal(void)
 | 
			
		||||
{
 | 
			
		||||
	PyObject *globals = PyEval_GetGlobals();
 | 
			
		||||
	PyObject *val= PyDict_GetItemString(globals, "__bpy_context__");
 | 
			
		||||
	PyObject *val= PyDict_GetItemString(globals, "__bpy_context__"); /* borrow ref */
 | 
			
		||||
	return PyCObject_AsVoidPtr(val);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user