Exemplo n.º 1
0
bool bpy_text_compile(Text *text)
{
	char fn_dummy[FILE_MAX];
	PyObject *fn_dummy_py;
	char *buf;

	bpy_text_filename_get(fn_dummy, sizeof(fn_dummy), text);

	/* if previously compiled, free the object */
	free_compiled_text(text);

	fn_dummy_py = PyC_UnicodeFromByte(fn_dummy);

	buf = txt_to_buf(text);
	text->compiled = Py_CompileStringObject(buf, fn_dummy_py, Py_file_input, NULL, -1);
	MEM_freeN(buf);

	Py_DECREF(fn_dummy_py);

	if (PyErr_Occurred()) {
		PyErr_Print();
		PyErr_Clear();
		PySys_SetObject("last_traceback", NULL);
		free_compiled_text(text);
		return false;
	}
	else {
		return true;
	}
}
Exemplo n.º 2
0
PyObject *bpy_text_import(Text *text)
{
	char *buf = NULL;
	char modulename[MAX_ID_NAME + 2];
	int len;

	if (!text->compiled) {
		char fn_dummy[256];
		bpy_text_filename_get(fn_dummy, sizeof(fn_dummy), text);

		buf = txt_to_buf(text);
		text->compiled = Py_CompileString(buf, fn_dummy, Py_file_input);
		MEM_freeN(buf);

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

	len = strlen(text->id.name + 2);
	BLI_strncpy(modulename, text->id.name + 2, len);
	modulename[len - 3] = '\0'; /* remove .py */
	return PyImport_ExecCodeModule(modulename, text->compiled);
}
Exemplo n.º 3
0
PyObject *bpy_text_reimport(PyObject *module, int *found)
{
	Text *text;
	const char *name;
	char *filepath;
	char *buf = NULL;
//XXX	Main *maggie = bpy_import_main ? bpy_import_main:G.main;
	Main *maggie = bpy_import_main;
	
	if (!maggie) {
		printf("ERROR: bpy_import_main_set() was not called before running python. this is a bug.\n");
		return NULL;
	}
	
	*found = 0;
	
	/* get name, filename from the module itself */
	if ((name = PyModule_GetName(module)) == NULL)
		return NULL;

	if ((filepath = (char *)PyModule_GetFilename(module)) == NULL)
		return NULL;

	/* look up the text object */
	text = BLI_findstring(&maggie->text, BLI_path_basename(filepath), offsetof(ID, name) + 2);

	/* 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((char *)name, text->compiled);
}