| 
									
										
										
										
											2012-11-06 11:43:19 +00:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  |  * 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. | 
					
						
							| 
									
										
										
										
											2011-10-23 17:52:20 +00:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-18 08:08:12 +11:00
										 |  |  | /** \file
 | 
					
						
							|  |  |  |  * \ingroup pygen | 
					
						
							| 
									
										
										
										
											2011-11-05 08:40:07 +00:00
										 |  |  |  * | 
					
						
							|  |  |  |  * This file defines replacements for pythons '__import__' and 'imp.reload' | 
					
						
							|  |  |  |  * functions which can import from blender textblocks. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * \note | 
					
						
							|  |  |  |  * This should eventually be replaced by import hooks (pep 302). | 
					
						
							| 
									
										
										
										
											2011-02-27 20:10:08 +00:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-27 06:05:22 +00:00
										 |  |  | #include <Python.h>
 | 
					
						
							| 
									
										
										
										
											2011-01-07 18:36:47 +00:00
										 |  |  | #include <stddef.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #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"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-07 15:37:31 +01:00
										 |  |  | #include "BKE_main.h"
 | 
					
						
							| 
									
										
										
										
											2012-03-26 06:55:09 +00:00
										 |  |  | /* UNUSED */ | 
					
						
							| 
									
										
										
										
											2012-07-19 09:23:31 +00:00
										 |  |  | #include "BKE_text.h"  /* txt_to_buf */
 | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-04-30 23:43:01 +10:00
										 |  |  | #include "py_capi_utils.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-14 22:02:03 +11:00
										 |  |  | #include "bpy_internal_import.h"  /* own include */
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-26 12:26:11 +00:00
										 |  |  | static Main *bpy_import_main = NULL; | 
					
						
							| 
									
										
										
										
											2012-06-21 05:41:06 +00:00
										 |  |  | static ListBase bpy_import_main_list; | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-07-19 09:23:31 +00:00
										 |  |  | static PyMethodDef bpy_import_meth; | 
					
						
							|  |  |  | static PyMethodDef bpy_reload_meth; | 
					
						
							| 
									
										
										
										
											2012-11-06 11:43:19 +00:00
										 |  |  | static PyObject   *imp_reload_orig = NULL; | 
					
						
							| 
									
										
										
										
											2012-07-19 09:23:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-29 16:12:25 +00:00
										 |  |  | /* 'builtins' is most likely PyEval_GetBuiltins() */ | 
					
						
							| 
									
										
										
										
											2012-11-18 01:22:31 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | /**
 | 
					
						
							|  |  |  |  * \note to the discerning developer, yes - this is nasty | 
					
						
							|  |  |  |  * monkey-patching our own import into Python's builtin 'imp' module. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * However Python's alternative is to use import hooks, | 
					
						
							|  |  |  |  * which are implemented in a way that we can't use our own importer as a | 
					
						
							| 
									
										
										
										
											2013-02-19 02:30:02 +00:00
										 |  |  |  * fall-back (instead we must try and fail - raise an exception every time). | 
					
						
							| 
									
										
										
										
											2012-11-18 01:22:31 +00:00
										 |  |  |  * Since importing from blenders text-blocks is not the common case | 
					
						
							|  |  |  |  * I prefer to use Pythons import by default and fall-back to | 
					
						
							|  |  |  |  * Blenders - which we can only do by intercepting import calls I'm afraid. | 
					
						
							|  |  |  |  * - Campbell | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2011-03-29 16:12:25 +00:00
										 |  |  | void bpy_import_init(PyObject *builtins) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	PyObject *item; | 
					
						
							|  |  |  | 	PyObject *mod; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-26 12:26:11 +00:00
										 |  |  | 	PyDict_SetItemString(builtins, "__import__", item = PyCFunction_New(&bpy_import_meth, NULL)); Py_DECREF(item); | 
					
						
							| 
									
										
										
										
											2011-03-29 16:12:25 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	/* move reload here
 | 
					
						
							|  |  |  | 	 * XXX, use import hooks */ | 
					
						
							| 
									
										
										
										
											2015-01-23 16:34:08 +11:00
										 |  |  | 	mod = PyImport_ImportModuleLevel("importlib", NULL, NULL, NULL, 0); | 
					
						
							| 
									
										
										
										
											2011-10-13 01:29:08 +00:00
										 |  |  | 	if (mod) { | 
					
						
							| 
									
										
										
										
											2012-11-06 11:43:19 +00:00
										 |  |  | 		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); | 
					
						
							| 
									
										
										
										
											2011-03-29 16:12:25 +00:00
										 |  |  | 		Py_DECREF(mod); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	else { | 
					
						
							| 
									
										
										
										
											2015-01-23 16:34:08 +11:00
										 |  |  | 		BLI_assert(!"unable to load 'importlib' module."); | 
					
						
							| 
									
										
										
										
											2011-03-29 16:12:25 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | static void free_compiled_text(Text *text) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2011-10-13 01:29:08 +00:00
										 |  |  | 	if (text->compiled) { | 
					
						
							| 
									
										
										
										
											2011-03-19 11:12:48 +00:00
										 |  |  | 		Py_DECREF((PyObject *)text->compiled); | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2011-12-26 12:26:11 +00:00
										 |  |  | 	text->compiled = NULL; | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | struct Main *bpy_import_main_get(void) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	return bpy_import_main; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void bpy_import_main_set(struct Main *maggie) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2011-12-26 12:26:11 +00:00
										 |  |  | 	bpy_import_main = maggie; | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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-12-26 12:26:11 +00:00
										 |  |  | 	BLI_snprintf(fn, fn_len, "%s%c%s", ID_BLEND_PATH(bpy_import_main, &text->id), SEP, text->id.name + 2); | 
					
						
							| 
									
										
										
										
											2010-06-02 14:40:58 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-14 22:02:03 +11:00
										 |  |  | bool bpy_text_compile(Text *text) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	char fn_dummy[FILE_MAX]; | 
					
						
							| 
									
										
										
										
											2014-04-30 23:43:01 +10:00
										 |  |  | 	PyObject *fn_dummy_py; | 
					
						
							| 
									
										
										
										
											2014-02-14 22:02:03 +11:00
										 |  |  | 	char *buf; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	bpy_text_filename_get(fn_dummy, sizeof(fn_dummy), text); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* if previously compiled, free the object */ | 
					
						
							|  |  |  | 	free_compiled_text(text); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-04-30 23:43:01 +10:00
										 |  |  | 	fn_dummy_py = PyC_UnicodeFromByte(fn_dummy); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-14 22:02:03 +11:00
										 |  |  | 	buf = txt_to_buf(text); | 
					
						
							| 
									
										
										
										
											2014-04-30 23:43:01 +10:00
										 |  |  | 	text->compiled = Py_CompileStringObject(buf, fn_dummy_py, Py_file_input, NULL, -1); | 
					
						
							| 
									
										
										
										
											2014-02-14 22:02:03 +11:00
										 |  |  | 	MEM_freeN(buf); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-04-30 23:43:01 +10:00
										 |  |  | 	Py_DECREF(fn_dummy_py); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-14 22:02:03 +11:00
										 |  |  | 	if (PyErr_Occurred()) { | 
					
						
							|  |  |  | 		PyErr_Print(); | 
					
						
							|  |  |  | 		PyErr_Clear(); | 
					
						
							|  |  |  | 		PySys_SetObject("last_traceback", NULL); | 
					
						
							|  |  |  | 		free_compiled_text(text); | 
					
						
							|  |  |  | 		return false; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	else { | 
					
						
							|  |  |  | 		return true; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-27 06:05:22 +00:00
										 |  |  | PyObject *bpy_text_import(Text *text) | 
					
						
							| 
									
										
										
										
											2009-11-20 15:01:09 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2012-03-16 21:39:56 +00:00
										 |  |  | 	char modulename[MAX_ID_NAME + 2]; | 
					
						
							| 
									
										
										
										
											2009-11-20 15:01:09 +00:00
										 |  |  | 	int len; | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-13 01:29:08 +00:00
										 |  |  | 	if (!text->compiled) { | 
					
						
							| 
									
										
										
										
											2014-02-14 22:02:03 +11:00
										 |  |  | 		if (bpy_text_compile(text) == false) { | 
					
						
							| 
									
										
										
										
											2009-11-20 15:01:09 +00:00
										 |  |  | 			return NULL; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-26 12:26:11 +00:00
										 |  |  | 	len = strlen(text->id.name + 2); | 
					
						
							|  |  |  | 	BLI_strncpy(modulename, text->id.name + 2, len); | 
					
						
							|  |  |  | 	modulename[len - 3] = '\0'; /* remove .py */ | 
					
						
							| 
									
										
										
										
											2009-11-20 15:01:09 +00:00
										 |  |  | 	return PyImport_ExecCodeModule(modulename, text->compiled); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-27 14:41:26 +00:00
										 |  |  | PyObject *bpy_text_import_name(const char *name, int *found) | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	Text *text; | 
					
						
							| 
									
										
										
										
											2011-12-26 12:26:11 +00:00
										 |  |  | 	char txtname[MAX_ID_NAME - 2]; | 
					
						
							|  |  |  | 	int namelen = strlen(name); | 
					
						
							| 
									
										
										
										
											2018-06-22 11:37:47 +02:00
										 |  |  | //XXX	Main *maggie = bpy_import_main ? bpy_import_main : G_MAIN;
 | 
					
						
							| 
									
										
										
										
											2011-12-26 12:26:11 +00:00
										 |  |  | 	Main *maggie = bpy_import_main; | 
					
						
							| 
									
										
										
										
											2018-06-04 08:54:17 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-26 12:26:11 +00:00
										 |  |  | 	*found = 0; | 
					
						
							| 
									
										
										
										
											2009-07-19 17:44:44 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-13 01:29:08 +00:00
										 |  |  | 	if (!maggie) { | 
					
						
							| 
									
										
										
										
											2009-07-20 11:30:27 +00:00
										 |  |  | 		printf("ERROR: bpy_import_main_set() was not called before running python. this is a bug.\n"); | 
					
						
							|  |  |  | 		return NULL; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2011-12-24 02:37:42 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	/* we know this cant be importable, the name is too long for blender! */ | 
					
						
							| 
									
										
										
										
											2012-07-22 18:40:50 +00:00
										 |  |  | 	if (namelen >= (MAX_ID_NAME - 2) - 3) | 
					
						
							|  |  |  | 		return NULL; | 
					
						
							| 
									
										
										
										
											2011-12-24 02:37:42 +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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-26 12:26:11 +00:00
										 |  |  | 	text = BLI_findstring(&maggie->text, txtname, offsetof(ID, name) + 2); | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-06-21 05:41:06 +00:00
										 |  |  | 	if (text) { | 
					
						
							|  |  |  | 		*found = 1; | 
					
						
							|  |  |  | 		return bpy_text_import(text); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* If we still haven't found the module try additional modules form bpy_import_main_list */ | 
					
						
							|  |  |  | 	maggie = bpy_import_main_list.first; | 
					
						
							|  |  |  | 	while (maggie && !text) { | 
					
						
							|  |  |  | 		text = BLI_findstring(&maggie->text, txtname, offsetof(ID, name) + 2); | 
					
						
							|  |  |  | 		maggie = maggie->next; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-13 01:29:08 +00:00
										 |  |  | 	if (!text) | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | 		return NULL; | 
					
						
							|  |  |  | 	else | 
					
						
							| 
									
										
										
										
											2011-12-26 12:26:11 +00:00
										 |  |  | 		*found = 1; | 
					
						
							| 
									
										
										
										
											2018-06-04 08:54:17 +02: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; | 
					
						
							| 
									
										
										
										
											2014-04-27 00:21:23 +10:00
										 |  |  | 	const char *filepath; | 
					
						
							| 
									
										
										
										
											2018-06-22 11:37:47 +02:00
										 |  |  | //XXX	Main *maggie = bpy_import_main ? bpy_import_main : G_MAIN;
 | 
					
						
							| 
									
										
										
										
											2011-12-26 12:26:11 +00:00
										 |  |  | 	Main *maggie = bpy_import_main; | 
					
						
							| 
									
										
										
										
											2018-06-04 08:54:17 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-13 01:29:08 +00:00
										 |  |  | 	if (!maggie) { | 
					
						
							| 
									
										
										
										
											2009-07-20 11:30:27 +00:00
										 |  |  | 		printf("ERROR: bpy_import_main_set() was not called before running python. this is a bug.\n"); | 
					
						
							|  |  |  | 		return NULL; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-06-04 08:54:17 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-26 12:26:11 +00:00
										 |  |  | 	*found = 0; | 
					
						
							| 
									
										
										
										
											2018-06-04 08:54:17 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | 	/* get name, filename from the module itself */ | 
					
						
							| 
									
										
										
										
											2011-12-26 12:26:11 +00:00
										 |  |  | 	if ((name = PyModule_GetName(module)) == NULL) | 
					
						
							| 
									
										
										
										
											2010-06-05 21:19:59 +00:00
										 |  |  | 		return NULL; | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-17 12:44:03 +10:00
										 |  |  | 	{ | 
					
						
							|  |  |  | 		PyObject *module_file = PyModule_GetFilenameObject(module); | 
					
						
							|  |  |  | 		if (module_file == NULL) { | 
					
						
							|  |  |  | 			return NULL; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2017-10-04 16:44:45 +11:00
										 |  |  | 		filepath = _PyUnicode_AsString(module_file); | 
					
						
							| 
									
										
										
										
											2017-07-17 12:44:03 +10:00
										 |  |  | 		Py_DECREF(module_file); | 
					
						
							|  |  |  | 		if (filepath == NULL) { | 
					
						
							|  |  |  | 			return NULL; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	/* look up the text object */ | 
					
						
							| 
									
										
										
										
											2011-12-26 12:26:11 +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-10-13 01:29:08 +00:00
										 |  |  | 	if (!text) | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | 		return NULL; | 
					
						
							|  |  |  | 	else | 
					
						
							| 
									
										
										
										
											2011-12-26 12:26:11 +00:00
										 |  |  | 		*found = 1; | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-14 22:02:03 +11:00
										 |  |  | 	if (bpy_text_compile(text) == false) { | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | 		return NULL; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* make into a module */ | 
					
						
							| 
									
										
										
										
											2015-01-01 23:26:03 +11:00
										 |  |  | 	return PyImport_ExecCodeModule(name, text->compiled); | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-06-02 08:29:16 +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; | 
					
						
							| 
									
										
										
										
											2014-04-27 00:21:23 +10:00
										 |  |  | 	const char *name; | 
					
						
							| 
									
										
										
										
											2011-12-26 12:26:11 +00:00
										 |  |  | 	int found = 0; | 
					
						
							|  |  |  | 	PyObject *globals = NULL, *locals = NULL, *fromlist = NULL; | 
					
						
							| 
									
										
										
										
											2012-05-21 07:00:23 +00:00
										 |  |  | 	int level = 0; /* relative imports */ | 
					
						
							| 
									
										
										
										
											2010-10-18 11:37:53 +00:00
										 |  |  | 	PyObject *newmodule; | 
					
						
							| 
									
										
										
										
											2017-10-04 15:54:41 +11:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	static const char *_keywords[] = {"name", "globals", "locals", "fromlist", "level", NULL}; | 
					
						
							|  |  |  | 	static _PyArg_Parser _parser = {"s|OOOi:bpy_import_meth", _keywords, 0}; | 
					
						
							|  |  |  | 	if (!_PyArg_ParseTupleAndKeywordsFast( | 
					
						
							|  |  |  | 	        args, kw, &_parser, | 
					
						
							|  |  |  | 	        &name, &globals, &locals, &fromlist, &level)) | 
					
						
							| 
									
										
										
										
											2011-12-26 12:26:11 +00:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | 		return NULL; | 
					
						
							| 
									
										
										
										
											2011-12-26 12:26:11 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-07-17 18:08:14 +00:00
										 |  |  | 	/* import existing builtin modules or modules that have been imported already */ | 
					
						
							| 
									
										
										
										
											2012-11-06 13:29:00 +00:00
										 |  |  | 	newmodule = PyImport_ImportModuleLevel(name, globals, locals, fromlist, level); | 
					
						
							| 
									
										
										
										
											2018-06-04 08:54:17 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-13 01:29:08 +00:00
										 |  |  | 	if (newmodule) | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | 		return newmodule; | 
					
						
							| 
									
										
										
										
											2018-06-04 08:54:17 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-03-26 06:55:09 +00:00
										 |  |  | 	PyErr_Fetch(&exception, &err, &tb); /* get the python error in case we cant import as blender text either */ | 
					
						
							| 
									
										
										
										
											2018-06-04 08:54:17 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | 	/* importing from existing modules failed, see if we have this module as blender text */ | 
					
						
							| 
									
										
										
										
											2012-11-06 13:29:00 +00:00
										 |  |  | 	newmodule = bpy_text_import_name(name, &found); | 
					
						
							| 
									
										
										
										
											2018-06-04 08:54:17 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-03-26 06:55:09 +00:00
										 |  |  | 	if (newmodule) { /* found module as blender text, ignore above exception */ | 
					
						
							| 
									
										
										
										
											2011-03-19 11:12:48 +00:00
										 |  |  | 		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
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2011-12-26 12:26:11 +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
 | 
					
						
							| 
									
										
										
										
											2012-07-16 23:23:33 +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; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*
 | 
					
						
							|  |  |  |  * our reload() module, to handle reloading in-memory scripts | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-06-02 08:29:16 +00:00
										 |  |  | static PyObject *blender_reload(PyObject *UNUSED(self), PyObject *module) | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	PyObject *exception, *err, *tb; | 
					
						
							| 
									
										
										
										
											2011-12-26 12:26:11 +00:00
										 |  |  | 	PyObject *newmodule = NULL; | 
					
						
							|  |  |  | 	int found = 0; | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	/* try reimporting from file */ | 
					
						
							| 
									
										
										
										
											2012-11-06 11:43:19 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	/* 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); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-13 01:29:08 +00:00
										 |  |  | 	if (newmodule) | 
					
						
							| 
									
										
										
										
											2009-06-17 20:33:34 +00:00
										 |  |  | 		return newmodule; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* no file, try importing from memory */ | 
					
						
							| 
									
										
										
										
											2012-03-26 06:55:09 +00:00
										 |  |  | 	PyErr_Fetch(&exception, &err, &tb); /*restore for probable later use */ | 
					
						
							| 
									
										
										
										
											2011-03-19 11:12:48 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-26 12:26:11 +00:00
										 |  |  | 	newmodule = bpy_text_reimport(module, &found); | 
					
						
							| 
									
										
										
										
											2012-03-26 06:55:09 +00:00
										 |  |  | 	if (newmodule) { /* found module as blender text, ignore above exception */ | 
					
						
							| 
									
										
										
										
											2011-03-19 11:12:48 +00:00
										 |  |  | 		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
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2011-12-26 12:26:11 +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; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-07-19 09:23:31 +00:00
										 |  |  | static PyMethodDef bpy_import_meth = {"bpy_import_meth", (PyCFunction)blender_import, METH_VARARGS | METH_KEYWORDS, "blenders import"}; | 
					
						
							|  |  |  | static PyMethodDef bpy_reload_meth = {"bpy_reload_meth", (PyCFunction)blender_reload, METH_O, "blenders reload"}; |