[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [51937] trunk/blender/source/blender/ python/generic/bpy_internal_import.c: fix for imp.reload() with Python3.3, function was recursively calling its self.

Campbell Barton ideasman42 at gmail.com
Tue Nov 6 12:43:22 CET 2012


Revision: 51937
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=51937
Author:   campbellbarton
Date:     2012-11-06 11:43:19 +0000 (Tue, 06 Nov 2012)
Log Message:
-----------
fix for imp.reload() with Python3.3, function was recursively calling its self.

Modified Paths:
--------------
    trunk/blender/source/blender/python/generic/bpy_internal_import.c

Modified: trunk/blender/source/blender/python/generic/bpy_internal_import.c
===================================================================
--- trunk/blender/source/blender/python/generic/bpy_internal_import.c	2012-11-06 10:35:02 UTC (rev 51936)
+++ trunk/blender/source/blender/python/generic/bpy_internal_import.c	2012-11-06 11:43:19 UTC (rev 51937)
@@ -1,4 +1,4 @@
-/* 
+/*
  * ***** BEGIN GPL LICENSE BLOCK *****
  *
  * This program is free software; you can redistribute it and/or
@@ -56,6 +56,7 @@
 
 static PyMethodDef bpy_import_meth;
 static PyMethodDef bpy_reload_meth;
+static PyObject   *imp_reload_orig = NULL;
 
 /* 'builtins' is most likely PyEval_GetBuiltins() */
 void bpy_import_init(PyObject *builtins)
@@ -69,7 +70,13 @@
 	 * XXX, use import hooks */
 	mod = PyImport_ImportModuleLevel((char *)"imp", NULL, NULL, NULL, 0);
 	if (mod) {
-		PyDict_SetItemString(PyModule_GetDict(mod), "reload", item = PyCFunction_New(&bpy_reload_meth, NULL)); Py_DECREF(item);
+		PyObject *mod_dict = PyModule_GetDict(mod);
+
+		/* blender owns the function */
+		imp_reload_orig = PyDict_GetItemString(mod_dict, "reload");
+		Py_INCREF(imp_reload_orig);
+
+		PyDict_SetItemString(mod_dict, "reload", item = PyCFunction_New(&bpy_reload_meth, NULL)); Py_DECREF(item);
 		Py_DECREF(mod);
 	}
 	else {
@@ -250,7 +257,8 @@
 static PyObject *blender_import(PyObject *UNUSED(self), PyObject *args, PyObject *kw)
 {
 	PyObject *exception, *err, *tb;
-	char *name;
+	//char *name;
+	PyObject *name;
 	int found = 0;
 	PyObject *globals = NULL, *locals = NULL, *fromlist = NULL;
 	int level = 0; /* relative imports */
@@ -259,14 +267,14 @@
 	//PyObject_Print(args, stderr, 0);
 	static const char *kwlist[] = {"name", "globals", "locals", "fromlist", "level", NULL};
 	
-	if (!PyArg_ParseTupleAndKeywords(args, kw, "s|OOOi:bpy_import_meth", (char **)kwlist,
+	if (!PyArg_ParseTupleAndKeywords(args, kw, "U|OOOi:bpy_import_meth", (char **)kwlist,
 	                                 &name, &globals, &locals, &fromlist, &level))
 	{
 		return NULL;
 	}
 
 	/* import existing builtin modules or modules that have been imported already */
-	newmodule = PyImport_ImportModuleLevel(name, globals, locals, fromlist, level);
+	newmodule = PyImport_ImportModuleLevelObject(name, globals, locals, fromlist, level);
 	
 	if (newmodule)
 		return newmodule;
@@ -274,7 +282,7 @@
 	PyErr_Fetch(&exception, &err, &tb); /* get the python error in case we cant import as blender text either */
 	
 	/* importing from existing modules failed, see if we have this module as blender text */
-	newmodule = bpy_text_import_name(name, &found);
+	newmodule = bpy_text_import_name(_PyUnicode_AsString(name), &found);
 	
 	if (newmodule) { /* found module as blender text, ignore above exception */
 		PyErr_Clear();
@@ -309,7 +317,12 @@
 	int found = 0;
 
 	/* try reimporting from file */
-	newmodule = PyImport_ReloadModule(module);
+
+	/* in Py3.3 this just calls imp.reload() which we overwrite, causing recursive calls */
+	//newmodule = PyImport_ReloadModule(module);
+
+	newmodule = PyObject_CallFunctionObjArgs(imp_reload_orig, module, NULL);
+
 	if (newmodule)
 		return newmodule;
 




More information about the Bf-blender-cvs mailing list