| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | /* 
 | 
					
						
							| 
									
										
										
										
											2009-06-23 00:09:26 +00:00
										 |  |  |  * $Id$ | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  |  * ***** BEGIN GPL LICENSE BLOCK ***** | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This program is free software; you can redistribute it and/or | 
					
						
							|  |  |  |  * modify it under the terms of the GNU General Public License | 
					
						
							|  |  |  |  * as published by the Free Software Foundation; either version 2 | 
					
						
							|  |  |  |  * of the License, or (at your option) any later version. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This program is distributed in the hope that it will be useful, | 
					
						
							|  |  |  |  * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
					
						
							|  |  |  |  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
					
						
							|  |  |  |  * GNU General Public License for more details. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * You should have received a copy of the GNU General Public License | 
					
						
							|  |  |  |  * along with this program; if not, write to the Free Software Foundation, | 
					
						
							| 
									
										
										
										
											2010-02-12 13:34:04 +00:00
										 |  |  |  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  |  * | 
					
						
							|  |  |  |  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV. | 
					
						
							|  |  |  |  * All rights reserved. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This is a new part of Blender. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Contributor(s): Willian P. Germano | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * ***** END GPL LICENSE BLOCK ***** | 
					
						
							|  |  |  | */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-27 20:10:08 +00:00
										 |  |  | /** \file blender/python/generic/bpy_internal_import.c
 | 
					
						
							|  |  |  |  *  \ingroup pygen | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-27 06:05:22 +00:00
										 |  |  | #include <Python.h>
 | 
					
						
							| 
									
										
										
										
											2011-01-07 18:36:47 +00:00
										 |  |  | #include <stddef.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-27 06:05:22 +00:00
										 |  |  | #include "compile.h"	/* for the PyCodeObject */
 | 
					
						
							|  |  |  | #include "eval.h"		/* for PyEval_EvalCode */
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | #include "bpy_internal_import.h"
 | 
					
						
							| 
									
										
										
										
											2010-10-27 06:05:22 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-01-07 18:36:47 +00:00
										 |  |  | #include "MEM_guardedalloc.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | #include "DNA_text_types.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-01-07 18:36:47 +00:00
										 |  |  | #include "BLI_listbase.h"
 | 
					
						
							|  |  |  | #include "BLI_path_util.h"
 | 
					
						
							|  |  |  | #include "BLI_string.h"
 | 
					
						
							|  |  |  | #include "BLI_utildefines.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-01-07 19:18:31 +00:00
										 |  |  |  /* UNUSED */	 | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | #include "BKE_text.h" /* txt_to_buf */	
 | 
					
						
							|  |  |  | #include "BKE_main.h"
 | 
					
						
							| 
									
										
										
										
											2010-10-18 06:41:16 +00:00
										 |  |  | #include "BKE_global.h" /* grr, only for G.main->name */
 | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | static Main *bpy_import_main= NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-29 16:12:25 +00:00
										 |  |  | /* 'builtins' is most likely PyEval_GetBuiltins() */ | 
					
						
							|  |  |  | void bpy_import_init(PyObject *builtins) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	PyObject *item; | 
					
						
							|  |  |  | 	PyObject *mod; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	PyDict_SetItemString(builtins, "__import__",	item=PyCFunction_New(&bpy_import_meth, NULL));	Py_DECREF(item); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* move reload here
 | 
					
						
							|  |  |  | 	 * 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); | 
					
						
							|  |  |  | 		Py_DECREF(mod); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	else { | 
					
						
							|  |  |  | 		BLI_assert(!"unable to load 'imp' module."); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | static void free_compiled_text(Text *text) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	if(text->compiled) { | 
					
						
							| 
									
										
										
										
											2011-03-19 11:12:48 +00:00
										 |  |  | 		Py_DECREF((PyObject *)text->compiled); | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	text->compiled= NULL; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | struct Main *bpy_import_main_get(void) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	return bpy_import_main; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void bpy_import_main_set(struct Main *maggie) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	bpy_import_main= maggie; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-06-02 14:40:58 +00:00
										 |  |  | /* returns a dummy filename for a textblock so we can tell what file a text block comes from */ | 
					
						
							| 
									
										
										
										
											2011-03-07 11:53:40 +00:00
										 |  |  | void bpy_text_filename_get(char *fn, size_t fn_len, Text *text) | 
					
						
							| 
									
										
										
										
											2010-06-02 14:40:58 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2011-03-07 11:53:40 +00:00
										 |  |  | 	BLI_snprintf(fn, fn_len, "%s%c%s", text->id.lib ? text->id.lib->filepath : G.main->name, SEP, text->id.name+2); | 
					
						
							| 
									
										
										
										
											2010-06-02 14:40:58 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-27 06:05:22 +00:00
										 |  |  | PyObject *bpy_text_import(Text *text) | 
					
						
							| 
									
										
										
										
											2009-11-20 15:01:09 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2011-03-19 11:12:48 +00:00
										 |  |  | 	char *buf= NULL; | 
					
						
							| 
									
										
										
										
											2009-11-20 15:01:09 +00:00
										 |  |  | 	char modulename[24]; | 
					
						
							|  |  |  | 	int len; | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-19 11:12:48 +00:00
										 |  |  | 	if(!text->compiled) { | 
					
						
							| 
									
										
										
										
											2010-06-02 14:40:58 +00:00
										 |  |  | 		char fn_dummy[256]; | 
					
						
							| 
									
										
										
										
											2011-03-07 11:53:40 +00:00
										 |  |  | 		bpy_text_filename_get(fn_dummy, sizeof(fn_dummy), text); | 
					
						
							| 
									
										
										
										
											2010-06-02 14:40:58 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-19 11:12:48 +00:00
										 |  |  | 		buf= txt_to_buf(text); | 
					
						
							|  |  |  | 		text->compiled= Py_CompileString(buf, fn_dummy, Py_file_input); | 
					
						
							|  |  |  | 		MEM_freeN(buf); | 
					
						
							| 
									
										
										
										
											2009-11-20 15:01:09 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-19 11:12:48 +00:00
										 |  |  | 		if(PyErr_Occurred()) { | 
					
						
							|  |  |  | 			PyErr_Print(); | 
					
						
							|  |  |  | 			PyErr_Clear(); | 
					
						
							| 
									
										
										
										
											2009-11-20 15:01:09 +00:00
										 |  |  | 			PySys_SetObject("last_traceback", NULL); | 
					
						
							| 
									
										
										
										
											2011-03-19 11:12:48 +00:00
										 |  |  | 			free_compiled_text(text); | 
					
						
							| 
									
										
										
										
											2009-11-20 15:01:09 +00:00
										 |  |  | 			return NULL; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-12-07 14:09:53 +00:00
										 |  |  | 	len= strlen(text->id.name+2); | 
					
						
							| 
									
										
										
										
											2009-11-20 15:01:09 +00:00
										 |  |  | 	strncpy(modulename, text->id.name+2, len); | 
					
						
							| 
									
										
										
										
											2009-12-07 14:09:53 +00:00
										 |  |  | 	modulename[len - 3]= '\0'; /* remove .py */ | 
					
						
							| 
									
										
										
										
											2009-11-20 15:01:09 +00:00
										 |  |  | 	return PyImport_ExecCodeModule(modulename, text->compiled); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-19 11:12:48 +00:00
										 |  |  | PyObject *bpy_text_import_name(char *name, int *found) | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	Text *text; | 
					
						
							| 
									
										
										
										
											2011-04-19 06:59:49 +00:00
										 |  |  | 	char txtname[MAX_ID_NAME-2]; | 
					
						
							| 
									
										
										
										
											2011-03-19 11:12:48 +00:00
										 |  |  | 	int namelen= strlen(name); | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | //XXX	Main *maggie= bpy_import_main ? bpy_import_main:G.main;
 | 
					
						
							|  |  |  | 	Main *maggie= bpy_import_main; | 
					
						
							|  |  |  | 	 | 
					
						
							|  |  |  | 	*found= 0; | 
					
						
							| 
									
										
										
										
											2009-07-19 17:44:44 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-20 11:30:27 +00:00
										 |  |  | 	if(!maggie) { | 
					
						
							|  |  |  | 		printf("ERROR: bpy_import_main_set() was not called before running python. this is a bug.\n"); | 
					
						
							|  |  |  | 		return NULL; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | 	 | 
					
						
							| 
									
										
										
										
											2011-04-19 06:59:49 +00:00
										 |  |  | 	if (namelen >= (MAX_ID_NAME-2) - 3) return NULL; /* we know this cant be importable, the name is too long for blender! */ | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | 	 | 
					
						
							| 
									
										
										
										
											2011-03-19 11:12:48 +00:00
										 |  |  | 	memcpy(txtname, name, namelen); | 
					
						
							|  |  |  | 	memcpy(&txtname[namelen], ".py", 4); | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-12-29 15:40:26 +00:00
										 |  |  | 	text= BLI_findstring(&maggie->text, txtname, offsetof(ID, name) + 2); | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-19 11:12:48 +00:00
										 |  |  | 	if(!text) | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | 		return NULL; | 
					
						
							|  |  |  | 	else | 
					
						
							| 
									
										
										
										
											2011-03-19 11:12:48 +00:00
										 |  |  | 		*found= 1; | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | 	 | 
					
						
							| 
									
										
										
										
											2009-11-20 15:01:09 +00:00
										 |  |  | 	return bpy_text_import(text); | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*
 | 
					
						
							|  |  |  |  * find in-memory module and recompile | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-19 11:12:48 +00:00
										 |  |  | PyObject *bpy_text_reimport(PyObject *module, int *found) | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	Text *text; | 
					
						
							| 
									
										
										
										
											2009-07-31 01:40:15 +00:00
										 |  |  | 	const char *name; | 
					
						
							| 
									
										
										
										
											2010-06-05 21:19:59 +00:00
										 |  |  | 	char *filepath; | 
					
						
							| 
									
										
										
										
											2011-03-19 11:12:48 +00:00
										 |  |  | 	char *buf= NULL; | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | //XXX	Main *maggie= bpy_import_main ? bpy_import_main:G.main;
 | 
					
						
							|  |  |  | 	Main *maggie= bpy_import_main; | 
					
						
							|  |  |  | 	 | 
					
						
							| 
									
										
										
										
											2009-07-20 11:30:27 +00:00
										 |  |  | 	if(!maggie) { | 
					
						
							|  |  |  | 		printf("ERROR: bpy_import_main_set() was not called before running python. this is a bug.\n"); | 
					
						
							|  |  |  | 		return NULL; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	 | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | 	*found= 0; | 
					
						
							|  |  |  | 	 | 
					
						
							|  |  |  | 	/* get name, filename from the module itself */ | 
					
						
							| 
									
										
										
										
											2010-06-05 21:19:59 +00:00
										 |  |  | 	if((name= PyModule_GetName(module)) == NULL) | 
					
						
							|  |  |  | 		return NULL; | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-06-05 21:19:59 +00:00
										 |  |  | 	if((filepath= (char *)PyModule_GetFilename(module)) == NULL) | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | 		return NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* look up the text object */ | 
					
						
							| 
									
										
										
										
											2010-06-05 21:19:59 +00:00
										 |  |  | 	text= BLI_findstring(&maggie->text, BLI_path_basename(filepath), offsetof(ID, name) + 2); | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	/* uh-oh.... didn't find it */ | 
					
						
							| 
									
										
										
										
											2011-03-19 11:12:48 +00:00
										 |  |  | 	if(!text) | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | 		return NULL; | 
					
						
							|  |  |  | 	else | 
					
						
							| 
									
										
										
										
											2011-03-19 11:12:48 +00:00
										 |  |  | 		*found= 1; | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	/* if previously compiled, free the object */ | 
					
						
							|  |  |  | 	/* (can't see how could be NULL, but check just in case) */  | 
					
						
							| 
									
										
										
										
											2011-03-19 11:12:48 +00:00
										 |  |  | 	if(text->compiled){ | 
					
						
							|  |  |  | 		Py_DECREF((PyObject *)text->compiled); | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* compile the buffer */ | 
					
						
							| 
									
										
										
										
											2011-03-19 11:12:48 +00:00
										 |  |  | 	buf= txt_to_buf(text); | 
					
						
							|  |  |  | 	text->compiled= Py_CompileString(buf, text->id.name+2, Py_file_input); | 
					
						
							|  |  |  | 	MEM_freeN(buf); | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	/* if compile failed.... return this error */ | 
					
						
							| 
									
										
										
										
											2011-03-19 11:12:48 +00:00
										 |  |  | 	if(PyErr_Occurred()) { | 
					
						
							|  |  |  | 		PyErr_Print(); | 
					
						
							|  |  |  | 		PyErr_Clear(); | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | 		PySys_SetObject("last_traceback", NULL); | 
					
						
							| 
									
										
										
										
											2011-03-19 11:12:48 +00:00
										 |  |  | 		free_compiled_text(text); | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | 		return NULL; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* make into a module */ | 
					
						
							| 
									
										
										
										
											2011-03-19 11:12:48 +00:00
										 |  |  | 	return PyImport_ExecCodeModule((char *)name, text->compiled); | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-19 11:12:48 +00:00
										 |  |  | static PyObject *blender_import(PyObject *UNUSED(self), PyObject *args, PyObject * kw) | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	PyObject *exception, *err, *tb; | 
					
						
							|  |  |  | 	char *name; | 
					
						
							|  |  |  | 	int found= 0; | 
					
						
							| 
									
										
										
										
											2011-03-19 11:12:48 +00:00
										 |  |  | 	PyObject *globals= NULL, *locals= NULL, *fromlist= NULL; | 
					
						
							| 
									
										
										
										
											2010-10-18 11:37:53 +00:00
										 |  |  | 	int level= -1; /* relative imports */ | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | 	 | 
					
						
							| 
									
										
										
										
											2010-10-18 11:37:53 +00:00
										 |  |  | 	PyObject *newmodule; | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | 	//PyObject_Print(args, stderr, 0);
 | 
					
						
							| 
									
										
										
										
											2011-03-19 11:12:48 +00:00
										 |  |  | 	static const char *kwlist[]= {"name", "globals", "locals", "fromlist", "level", NULL}; | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | 	 | 
					
						
							| 
									
										
										
										
											2011-03-19 11:12:48 +00:00
										 |  |  | 	if(!PyArg_ParseTupleAndKeywords(args, kw, "s|OOOi:bpy_import_meth", (char **)kwlist, | 
					
						
							|  |  |  | 				   &name, &globals, &locals, &fromlist, &level)) | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | 		return NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-07-17 18:08:14 +00:00
										 |  |  | 	/* import existing builtin modules or modules that have been imported already */ | 
					
						
							| 
									
										
										
										
											2010-10-18 11:37:53 +00:00
										 |  |  | 	newmodule= PyImport_ImportModuleLevel(name, globals, locals, fromlist, level); | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | 	 | 
					
						
							|  |  |  | 	if(newmodule) | 
					
						
							|  |  |  | 		return newmodule; | 
					
						
							|  |  |  | 	 | 
					
						
							| 
									
										
										
										
											2011-03-19 11:12:48 +00:00
										 |  |  | 	PyErr_Fetch(&exception, &err, &tb);	/* get the python error incase we cant import as blender text either */ | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | 	 | 
					
						
							|  |  |  | 	/* importing from existing modules failed, see if we have this module as blender text */ | 
					
						
							| 
									
										
										
										
											2011-03-19 11:12:48 +00:00
										 |  |  | 	newmodule= bpy_text_import_name(name, &found); | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | 	 | 
					
						
							| 
									
										
										
										
											2011-03-19 11:12:48 +00:00
										 |  |  | 	if(newmodule) {/* found module as blender text, ignore above exception */ | 
					
						
							|  |  |  | 		PyErr_Clear(); | 
					
						
							|  |  |  | 		Py_XDECREF(exception); | 
					
						
							|  |  |  | 		Py_XDECREF(err); | 
					
						
							|  |  |  | 		Py_XDECREF(tb); | 
					
						
							|  |  |  | 		/* printf("imported from text buffer...\n"); */ | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	else if (found==1) { /* blender text module failed to execute but was found, use its error message */ | 
					
						
							| 
									
										
										
										
											2011-03-19 11:12:48 +00:00
										 |  |  | 		Py_XDECREF(exception); | 
					
						
							|  |  |  | 		Py_XDECREF(err); | 
					
						
							|  |  |  | 		Py_XDECREF(tb); | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | 		return NULL; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	else { | 
					
						
							|  |  |  | 		/* no blender text was found that could import the module
 | 
					
						
							|  |  |  | 		 * rause the original error from PyImport_ImportModuleEx */ | 
					
						
							| 
									
										
										
										
											2011-03-19 11:12:48 +00:00
										 |  |  | 		PyErr_Restore(exception, err, tb); | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	return newmodule; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*
 | 
					
						
							|  |  |  |  * our reload() module, to handle reloading in-memory scripts | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-13 23:25:08 +00:00
										 |  |  | static PyObject *blender_reload(PyObject *UNUSED(self), PyObject * module) | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	PyObject *exception, *err, *tb; | 
					
						
							| 
									
										
										
										
											2011-03-19 11:12:48 +00:00
										 |  |  | 	PyObject *newmodule= NULL; | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | 	int found= 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* try reimporting from file */ | 
					
						
							| 
									
										
										
										
											2011-03-19 11:12:48 +00:00
										 |  |  | 	newmodule= PyImport_ReloadModule(module); | 
					
						
							|  |  |  | 	if(newmodule) | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | 		return newmodule; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* no file, try importing from memory */ | 
					
						
							| 
									
										
										
										
											2011-03-19 11:12:48 +00:00
										 |  |  | 	PyErr_Fetch(&exception, &err, &tb);	/*restore for probable later use */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	newmodule= bpy_text_reimport(module, &found); | 
					
						
							|  |  |  | 	if(newmodule) {/* found module as blender text, ignore above exception */ | 
					
						
							|  |  |  | 		PyErr_Clear(); | 
					
						
							|  |  |  | 		Py_XDECREF(exception); | 
					
						
							|  |  |  | 		Py_XDECREF(err); | 
					
						
							|  |  |  | 		Py_XDECREF(tb); | 
					
						
							|  |  |  | 		/* printf("imported from text buffer...\n"); */ | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	else if (found==1) { /* blender text module failed to execute but was found, use its error message */ | 
					
						
							| 
									
										
										
										
											2011-03-19 11:12:48 +00:00
										 |  |  | 		Py_XDECREF(exception); | 
					
						
							|  |  |  | 		Py_XDECREF(err); | 
					
						
							|  |  |  | 		Py_XDECREF(tb); | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | 		return NULL; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	else { | 
					
						
							|  |  |  | 		/* no blender text was found that could import the module
 | 
					
						
							| 
									
										
										
										
											2011-04-21 05:49:47 +00:00
										 |  |  | 		 * reuse the original error from PyImport_ImportModuleEx */ | 
					
						
							| 
									
										
										
										
											2011-03-19 11:12:48 +00:00
										 |  |  | 		PyErr_Restore(exception, err, tb); | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return newmodule; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-19 11:12:48 +00:00
										 |  |  | PyMethodDef bpy_import_meth= {"bpy_import_meth", (PyCFunction)blender_import, METH_VARARGS | METH_KEYWORDS, "blenders import"}; | 
					
						
							|  |  |  | PyMethodDef bpy_reload_meth= {"bpy_reload_meth", (PyCFunction)blender_reload, METH_O, "blenders reload"}; | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* Clear user modules.
 | 
					
						
							|  |  |  |  * This is to clear any modules that could be defined from running scripts in blender. | 
					
						
							|  |  |  |  *  | 
					
						
							|  |  |  |  * Its also needed for the BGE Python api so imported scripts are not used between levels | 
					
						
							|  |  |  |  *  | 
					
						
							|  |  |  |  * This clears every modules that has a __file__ attribute (is not a builtin) | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Note that clearing external python modules is important for the BGE otherwise | 
					
						
							|  |  |  |  * it wont reload scripts between loading different blend files or while making the game. | 
					
						
							|  |  |  |  * - use 'clear_all' arg in this case. | 
					
						
							|  |  |  |  * | 
					
						
							| 
									
										
										
										
											2011-04-21 05:49:47 +00:00
										 |  |  |  * Since pythons built-ins include a full path even for win32. | 
					
						
							|  |  |  |  * even if we remove a python module a re-import will bring it back again. | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #if 0 // not used anymore but may still come in handy later
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #if defined(WIN32) || defined(WIN64)
 | 
					
						
							|  |  |  | #define SEPSTR "\\"
 | 
					
						
							|  |  |  | #else
 | 
					
						
							|  |  |  | #define SEPSTR "/"
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void bpy_text_clear_modules(int clear_all) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2010-08-14 05:33:20 +00:00
										 |  |  | 	PyObject *modules= PyImport_GetModuleDict(); | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | 	 | 
					
						
							|  |  |  | 	char *fname; | 
					
						
							|  |  |  | 	char *file_extension; | 
					
						
							|  |  |  | 	 | 
					
						
							|  |  |  | 	/* looping over the dict */ | 
					
						
							|  |  |  | 	PyObject *key, *value; | 
					
						
							| 
									
										
										
										
											2011-03-19 11:12:48 +00:00
										 |  |  | 	int pos= 0; | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | 	 | 
					
						
							|  |  |  | 	/* new list */ | 
					
						
							|  |  |  | 	PyObject *list; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (modules==NULL) | 
					
						
							|  |  |  | 		return; /* should never happen but just incase */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	list= PyList_New(0); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* go over sys.modules and remove anything with a 
 | 
					
						
							|  |  |  | 	 * sys.modukes[x].__file__ thats ends with a .py and has no path | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	while (PyDict_Next(modules, &pos, &key, &value)) { | 
					
						
							|  |  |  | 		fname= PyModule_GetFilename(value); | 
					
						
							|  |  |  | 		if(fname) { | 
					
						
							|  |  |  | 			if (clear_all || ((strstr(fname, SEPSTR))==0)) { /* no path ? */ | 
					
						
							| 
									
										
										
										
											2011-03-19 11:12:48 +00:00
										 |  |  | 				file_extension= strstr(fname, ".py"); | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | 				if(file_extension && (*(file_extension + 3) == '\0' || *(file_extension + 4) == '\0')) { /* .py or pyc extension? */ | 
					
						
							|  |  |  | 					/* now we can be fairly sure its a python import from the blendfile */ | 
					
						
							|  |  |  | 					PyList_Append(list, key); /* free'd with the list */ | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		else { | 
					
						
							|  |  |  | 			PyErr_Clear(); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	 | 
					
						
							|  |  |  | 	/* remove all our modules */ | 
					
						
							|  |  |  | 	for(pos=0; pos < PyList_Size(list); pos++) { | 
					
						
							|  |  |  | 		/* PyObject_Print(key, stderr, 0); */ | 
					
						
							|  |  |  | 		key= PyList_GET_ITEM(list, pos); | 
					
						
							|  |  |  | 		PyDict_DelItem(modules, key); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	 | 
					
						
							|  |  |  | 	Py_DECREF(list); /* removes all references from append */ | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | #endif
 |