Welcome to mirror list, hosted at ThFree Co, Russian Federation.

bpy_internal_import.c « api2_2x « python « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: d944c24d928d30d0647f607bc88d6178e3e471fc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/* 
 * $Id$
 * ***** 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,
 * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * 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 *****
*/

#include "bpy_internal_import.h"
#include "DNA_text_types.h"
#include "DNA_ID.h"

#include "BKE_global.h"
#include "MEM_guardedalloc.h"
#include "BKE_text.h" /* txt_to_buf */	
#include "BKE_main.h"

static Main *bpy_import_main= NULL;

static void free_compiled_text(Text *text)
{
	if(text->compiled) {
		Py_DECREF(( PyObject * )text->compiled);
	}
	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;
}


PyObject *importText( char *name, int *found )
{
	Text *text;
	char txtname[22]; /* 21+NULL */
	char *buf = NULL;
	int namelen = strlen( name );
	Main *maggie= bpy_import_main ? bpy_import_main:G.main;
	
	*found= 0;
	
	if (namelen>21-3) return NULL; /* we know this cant be importable, the name is too long for blender! */
	
	memcpy( txtname, name, namelen );
	memcpy( &txtname[namelen], ".py", 4 );

	for(text = maggie->text.first; text; text = text->id.next) {
		if( !strcmp( txtname, text->id.name+2 ) )
			break;
	}

	if( !text )
		return NULL;
	else
		*found = 1;
	
	if( !text->compiled ) {
		buf = txt_to_buf( text );
		text->compiled = Py_CompileString( buf, text->id.name+2, Py_file_input );
		MEM_freeN( buf );

		if( PyErr_Occurred(  ) ) {
			PyErr_Print(  );
			PyErr_Clear(  );
			PySys_SetObject("last_traceback", NULL);
			free_compiled_text( text );
			return NULL;
		}
	}

	return PyImport_ExecCodeModule( name, text->compiled );
}


/*
 * find in-memory module and recompile
 */

PyObject *reimportText( PyObject *module, int *found )
{
	Text *text;
	char *txtname;
	char *name;
	char *buf = NULL;
	Main *maggie= bpy_import_main ? bpy_import_main:G.main;
	
	*found= 0;
	
	/* get name, filename from the module itself */

	txtname = PyModule_GetFilename( module );
	name = PyModule_GetName( module );
	if( !txtname || !name)
		return NULL;

	/* look up the text object */
	text = ( Text * ) & ( maggie->text.first );
	while( text ) {
		if( !strcmp( txtname, text->id.name+2 ) )
			break;
		text = text->id.next;
	}

	/* uh-oh.... didn't find it */
	if( !text )
		return NULL;
	else
		*found = 1;

	/* if previously compiled, free the object */
	/* (can't see how could be NULL, but check just in case) */ 
	if( text->compiled ){
		Py_DECREF( (PyObject *)text->compiled );
	}

	/* compile the buffer */
	buf = txt_to_buf( text );
	text->compiled = Py_CompileString( buf, text->id.name+2, Py_file_input );
	MEM_freeN( buf );

	/* if compile failed.... return this error */
	if( PyErr_Occurred(  ) ) {
		PyErr_Print(  );
		PyErr_Clear(  );
		PySys_SetObject("last_traceback", NULL);
		free_compiled_text( text );
		return NULL;
	}

	/* make into a module */
	return PyImport_ExecCodeModule( name, text->compiled );
}


static PyObject *blender_import( PyObject * self, PyObject * args,  PyObject * kw)
{
	PyObject *exception, *err, *tb;
	char *name;
	int found= 0;
	PyObject *globals = NULL, *locals = NULL, *fromlist = NULL;
	PyObject *newmodule;
	
	//PyObject_Print(args, stderr, 0);
#if (PY_VERSION_HEX >= 0x02060000)
	int dummy_val; /* what does this do?*/
	static char *kwlist[] = {"name", "globals", "locals", "fromlist", "level", 0};
	
	if( !PyArg_ParseTupleAndKeywords( args, kw, "s|OOOi:bpy_import", kwlist,
			       &name, &globals, &locals, &fromlist, &dummy_val) )
		return NULL;
#else
	static char *kwlist[] = {"name", "globals", "locals", "fromlist", 0};
	
	if( !PyArg_ParseTupleAndKeywords( args, kw, "s|OOO:bpy_import", kwlist,
			       &name, &globals, &locals, &fromlist ) )
		return NULL;
#endif

	/* import existing builtin modules or modules that have been imported alredy */
	newmodule = PyImport_ImportModuleEx( name, globals, locals, fromlist );
	
	if(newmodule)
		return newmodule;
	
	PyErr_Fetch( &exception, &err, &tb );	/* get the python error incase we cant import as blender text either */
	
	/* importing from existing modules failed, see if we have this module as blender text */
	newmodule = importText( name, &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" ); */
	}
	else if (found==1) { /* blender text module failed to execute but was found, use its error message */
		Py_XDECREF( exception );
		Py_XDECREF( err );
		Py_XDECREF( tb );
		return NULL;
	}
	else {
		/* no blender text was found that could import the module
		 * rause the original error from PyImport_ImportModuleEx */
		PyErr_Restore( exception, err, tb );
	}
	return newmodule;
}


/*
 * our reload() module, to handle reloading in-memory scripts
 */

static PyObject *blender_reload( PyObject * self, PyObject * args )
{
	PyObject *exception, *err, *tb;
	PyObject *module = NULL;
	PyObject *newmodule = NULL;
	int found= 0;
	
	/* check for a module arg */
	if( !PyArg_ParseTuple( args, "O:bpy_reload", &module ) )
		return NULL;

	/* try reimporting from file */
	newmodule = PyImport_ReloadModule( module );
	if( newmodule )
		return newmodule;

	/* no file, try importing from memory */
	PyErr_Fetch( &exception, &err, &tb );	/*restore for probable later use */

	newmodule = reimportText( 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" ); */
	}
	else if (found==1) { /* blender text module failed to execute but was found, use its error message */
		Py_XDECREF( exception );
		Py_XDECREF( err );
		Py_XDECREF( tb );
		return NULL;
	}
	else {
		/* no blender text was found that could import the module
		 * rause the original error from PyImport_ImportModuleEx */
		PyErr_Restore( exception, err, tb );
	}

	return newmodule;
}

PyMethodDef bpy_import[] = { {"bpy_import", blender_import, METH_KEYWORDS, "blenders import"} };
PyMethodDef bpy_reload[] = { {"bpy_reload", blender_reload, METH_VARARGS, "blenders reload"} };