3 * ***** BEGIN GPL LICENSE BLOCK *****
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software Foundation,
17 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
20 * All rights reserved.
22 * This is a new part of Blender.
24 * Contributor(s): Willian P. Germano
26 * ***** END GPL LICENSE BLOCK *****
29 #include "bpy_internal_import.h"
30 #include "DNA_text_types.h"
33 #include "BKE_global.h"
34 #include "MEM_guardedalloc.h"
35 #include "BKE_text.h" /* txt_to_buf */
38 static Main *bpy_import_main= NULL;
40 static void free_compiled_text(Text *text)
43 Py_DECREF(( PyObject * )text->compiled);
48 struct Main *bpy_import_main_get(void)
50 return bpy_import_main;
53 void bpy_import_main_set(struct Main *maggie)
55 bpy_import_main= maggie;
59 PyObject *importText( char *name, int *found )
62 char txtname[22]; /* 21+NULL */
64 int namelen = strlen( name );
65 Main *maggie= bpy_import_main ? bpy_import_main:G.main;
69 if (namelen>21-3) return NULL; /* we know this cant be importable, the name is too long for blender! */
71 memcpy( txtname, name, namelen );
72 memcpy( &txtname[namelen], ".py", 4 );
74 for(text = maggie->text.first; text; text = text->id.next) {
75 if( !strcmp( txtname, text->id.name+2 ) )
84 if( !text->compiled ) {
85 buf = txt_to_buf( text );
86 text->compiled = Py_CompileString( buf, text->id.name+2, Py_file_input );
89 if( PyErr_Occurred( ) ) {
92 PySys_SetObject("last_traceback", NULL);
93 free_compiled_text( text );
98 return PyImport_ExecCodeModule( name, text->compiled );
103 * find in-memory module and recompile
106 PyObject *reimportText( PyObject *module, int *found )
112 Main *maggie= bpy_import_main ? bpy_import_main:G.main;
116 /* get name, filename from the module itself */
118 txtname = PyModule_GetFilename( module );
119 name = PyModule_GetName( module );
120 if( !txtname || !name)
123 /* look up the text object */
124 text = ( Text * ) & ( maggie->text.first );
126 if( !strcmp( txtname, text->id.name+2 ) )
128 text = text->id.next;
131 /* uh-oh.... didn't find it */
137 /* if previously compiled, free the object */
138 /* (can't see how could be NULL, but check just in case) */
139 if( text->compiled ){
140 Py_DECREF( (PyObject *)text->compiled );
143 /* compile the buffer */
144 buf = txt_to_buf( text );
145 text->compiled = Py_CompileString( buf, text->id.name+2, Py_file_input );
148 /* if compile failed.... return this error */
149 if( PyErr_Occurred( ) ) {
152 PySys_SetObject("last_traceback", NULL);
153 free_compiled_text( text );
157 /* make into a module */
158 return PyImport_ExecCodeModule( name, text->compiled );
162 static PyObject *blender_import( PyObject * self, PyObject * args, PyObject * kw)
164 PyObject *exception, *err, *tb;
167 PyObject *globals = NULL, *locals = NULL, *fromlist = NULL;
170 //PyObject_Print(args, stderr, 0);
171 #if (PY_VERSION_HEX >= 0x02060000)
172 int dummy_val; /* what does this do?*/
173 static char *kwlist[] = {"name", "globals", "locals", "fromlist", "level", 0};
175 if( !PyArg_ParseTupleAndKeywords( args, kw, "s|OOOi:bpy_import", kwlist,
176 &name, &globals, &locals, &fromlist, &dummy_val) )
179 static char *kwlist[] = {"name", "globals", "locals", "fromlist", 0};
181 if( !PyArg_ParseTupleAndKeywords( args, kw, "s|OOO:bpy_import", kwlist,
182 &name, &globals, &locals, &fromlist ) )
186 /* import existing builtin modules or modules that have been imported alredy */
187 newmodule = PyImport_ImportModuleEx( name, globals, locals, fromlist );
192 PyErr_Fetch( &exception, &err, &tb ); /* get the python error incase we cant import as blender text either */
194 /* importing from existing modules failed, see if we have this module as blender text */
195 newmodule = importText( name, &found );
197 if( newmodule ) {/* found module as blender text, ignore above exception */
199 Py_XDECREF( exception );
202 /* printf( "imported from text buffer...\n" ); */
204 else if (found==1) { /* blender text module failed to execute but was found, use its error message */
205 Py_XDECREF( exception );
211 /* no blender text was found that could import the module
212 * rause the original error from PyImport_ImportModuleEx */
213 PyErr_Restore( exception, err, tb );
220 * our reload() module, to handle reloading in-memory scripts
223 static PyObject *blender_reload( PyObject * self, PyObject * args )
225 PyObject *exception, *err, *tb;
226 PyObject *module = NULL;
227 PyObject *newmodule = NULL;
230 /* check for a module arg */
231 if( !PyArg_ParseTuple( args, "O:bpy_reload", &module ) )
234 /* try reimporting from file */
235 newmodule = PyImport_ReloadModule( module );
239 /* no file, try importing from memory */
240 PyErr_Fetch( &exception, &err, &tb ); /*restore for probable later use */
242 newmodule = reimportText( module, &found );
243 if( newmodule ) {/* found module as blender text, ignore above exception */
245 Py_XDECREF( exception );
248 /* printf( "imported from text buffer...\n" ); */
250 else if (found==1) { /* blender text module failed to execute but was found, use its error message */
251 Py_XDECREF( exception );
257 /* no blender text was found that could import the module
258 * rause the original error from PyImport_ImportModuleEx */
259 PyErr_Restore( exception, err, tb );
265 PyMethodDef bpy_import[] = { {"bpy_import", blender_import, METH_KEYWORDS, "blenders import"} };
266 PyMethodDef bpy_reload[] = { {"bpy_reload", blender_reload, METH_VARARGS, "blenders reload"} };
269 /* Clear user modules.
270 * This is to clear any modules that could be defined from running scripts in blender.
272 * Its also needed for the BGE Python api so imported scripts are not used between levels
274 * This clears every modules that has a __file__ attribute (is not a builtin)
275 * and is a filename only (no path). since pythons bultins include a full path even for win32.
276 * even if we remove a python module a reimport will bring it back again.
280 #if defined(WIN32) || defined(WIN64)
287 void importClearUserModules(void)
289 PyObject *modules= PySys_GetObject("modules");
292 char *file_extension;
294 /* looping over the dict */
295 PyObject *key, *value;
299 PyObject *list= PyList_New(0);
301 /* go over sys.modules and remove anything with a
302 * sys.modukes[x].__file__ thats ends with a .py and has no path
304 while (PyDict_Next(modules, &pos, &key, &value)) {
305 fname= PyModule_GetFilename(value);
307 if ((strstr(fname, SEPSTR))==0) { /* no path ? */
308 file_extension = strstr(fname, ".py");
309 if(file_extension && *(file_extension + 3) == '\0') { /* .py extension ? */
310 /* now we can be fairly sure its a python import from the blendfile */
311 PyList_Append(list, key); /* free'd with the list */
320 /* remove all our modules */
321 for(pos=0; pos < PyList_Size(list); pos++) {
322 /* PyObject_Print(key, stderr, 0); */
323 key= PyList_GET_ITEM(list, pos);
324 PyDict_DelItem(modules, key);
327 Py_DECREF(list); /* removes all references from append */