| 
									
										
										
										
											2011-02-23 10:52:22 +00:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2008-12-28 08:41:49 +00:00
										 |  |  |  * $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, | 
					
						
							| 
									
										
										
										
											2010-02-12 13:34:04 +00:00
										 |  |  |  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | 
					
						
							| 
									
										
										
										
											2008-12-28 08:41:49 +00:00
										 |  |  |  * | 
					
						
							|  |  |  |  * Contributor(s): Campbell Barton | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * ***** END GPL LICENSE BLOCK ***** | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-14 04:15:25 +00:00
										 |  |  | #include <Python.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-12-28 08:41:49 +00:00
										 |  |  | #include "bpy_util.h"
 | 
					
						
							|  |  |  | #include "BLI_dynstr.h"
 | 
					
						
							|  |  |  | #include "MEM_guardedalloc.h"
 | 
					
						
							| 
									
										
										
										
											2009-04-19 13:37:59 +00:00
										 |  |  | #include "BKE_report.h"
 | 
					
						
							| 
									
										
										
										
											2010-09-01 14:13:48 +00:00
										 |  |  | #include "BKE_context.h"
 | 
					
						
							| 
									
										
										
										
											2008-12-28 08:41:49 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-09-01 14:13:48 +00:00
										 |  |  | #include "../generic/py_capi_utils.h"
 | 
					
						
							| 
									
										
										
										
											2009-05-25 13:48:44 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-13 10:52:18 +00:00
										 |  |  | static bContext*	__py_context = NULL; | 
					
						
							| 
									
										
										
										
											2010-12-03 01:52:28 +00:00
										 |  |  | bContext*	BPy_GetContext(void) { return __py_context; } | 
					
						
							|  |  |  | void		BPy_SetContext(bContext *C) { __py_context= C; } | 
					
						
							| 
									
										
										
										
											2009-05-25 13:48:44 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-04-01 12:43:07 +00:00
										 |  |  | char *BPy_enum_as_string(EnumPropertyItem *item) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	DynStr *dynstr= BLI_dynstr_new(); | 
					
						
							|  |  |  | 	EnumPropertyItem *e; | 
					
						
							|  |  |  | 	char *cstring; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for (e= item; item->identifier; item++) { | 
					
						
							| 
									
										
										
											
												RNA
* Enums can now be dynamically created in the _itemf callback,
  using RNA_enum_item(s)_add, RNA_enum_item_end. All places asking
  for enum items now need to potentially free the items.
* This callback now also gets context, this was added specifically
  for operators. This doesn't fit design well at all, needed to do
  some ugly hacks, but can't find a good solution at the moment.
* All enums must have a default list of items too, even with an
  _itemf callback, for docs and fallback in case there is no context.
* Used by MESH_OT_merge, MESH_OT_select_similar, TFM_OT_select_orientation.
* Also changes some operator properties that were enums to booleas
  (unselected, deselect), to make them consistent with other ops.
											
										 
											2009-07-10 19:56:13 +00:00
										 |  |  | 		if(item->identifier[0]) | 
					
						
							|  |  |  | 			BLI_dynstr_appendf(dynstr, (e==item)?"'%s'":", '%s'", item->identifier); | 
					
						
							| 
									
										
										
										
											2009-04-01 12:43:07 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	cstring = BLI_dynstr_get_cstring(dynstr); | 
					
						
							|  |  |  | 	BLI_dynstr_free(dynstr); | 
					
						
							|  |  |  | 	return cstring; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2009-04-19 13:37:59 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-12-31 05:40:30 +00:00
										 |  |  | short BPy_reports_to_error(ReportList *reports, const short clear) | 
					
						
							| 
									
										
										
										
											2009-04-19 13:37:59 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	char *report_str; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	report_str= BKE_reports_string(reports, RPT_ERROR); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-12-31 05:40:30 +00:00
										 |  |  | 	if(clear) { | 
					
						
							|  |  |  | 		BKE_reports_clear(reports); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-04-19 13:37:59 +00:00
										 |  |  | 	if(report_str) { | 
					
						
							| 
									
										
										
										
											2011-02-18 06:04:05 +00:00
										 |  |  | 		PyErr_SetString(PyExc_RuntimeError, report_str); | 
					
						
							| 
									
										
										
										
											2009-04-19 13:37:59 +00:00
										 |  |  | 		MEM_freeN(report_str); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return (report_str != NULL); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-06-14 12:53:47 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-12-31 05:40:30 +00:00
										 |  |  | short BPy_errors_to_report(ReportList *reports) | 
					
						
							| 
									
										
										
										
											2009-06-14 12:53:47 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	PyObject *pystring; | 
					
						
							| 
									
										
										
										
											2009-11-23 17:36:44 +00:00
										 |  |  | 	PyObject *pystring_format= NULL; // workaround, see below
 | 
					
						
							| 
									
										
										
										
											2009-06-14 12:53:47 +00:00
										 |  |  | 	char *cstring; | 
					
						
							| 
									
										
										
										
											2009-11-04 22:36:46 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-08-14 05:33:20 +00:00
										 |  |  | 	const char *filename; | 
					
						
							| 
									
										
										
										
											2009-11-04 22:36:46 +00:00
										 |  |  | 	int lineno; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-06-14 12:53:47 +00:00
										 |  |  | 	if (!PyErr_Occurred()) | 
					
						
							|  |  |  | 		return 1; | 
					
						
							|  |  |  | 	 | 
					
						
							|  |  |  | 	/* less hassle if we allow NULL */ | 
					
						
							|  |  |  | 	if(reports==NULL) { | 
					
						
							|  |  |  | 		PyErr_Print(); | 
					
						
							|  |  |  | 		PyErr_Clear(); | 
					
						
							|  |  |  | 		return 1; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	 | 
					
						
							| 
									
										
										
										
											2010-09-01 14:13:48 +00:00
										 |  |  | 	pystring= PyC_ExceptionBuffer(); | 
					
						
							| 
									
										
										
										
											2009-06-14 12:53:47 +00:00
										 |  |  | 	 | 
					
						
							|  |  |  | 	if(pystring==NULL) { | 
					
						
							|  |  |  | 		BKE_report(reports, RPT_ERROR, "unknown py-exception, could not convert"); | 
					
						
							|  |  |  | 		return 0; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	 | 
					
						
							| 
									
										
										
										
											2010-09-01 14:13:48 +00:00
										 |  |  | 	PyC_FileAndNum(&filename, &lineno); | 
					
						
							| 
									
										
										
										
											2009-11-23 17:36:44 +00:00
										 |  |  | 	if(filename==NULL) | 
					
						
							|  |  |  | 		filename= "<unknown location>"; | 
					
						
							| 
									
										
										
										
											2009-11-04 22:36:46 +00:00
										 |  |  | 	 | 
					
						
							| 
									
										
										
										
											2009-06-14 12:53:47 +00:00
										 |  |  | 	cstring= _PyUnicode_AsString(pystring); | 
					
						
							| 
									
										
										
										
											2009-11-23 17:36:44 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | #if 0 // ARG!. workaround for a bug in blenders use of vsnprintf
 | 
					
						
							| 
									
										
										
										
											2009-11-04 22:36:46 +00:00
										 |  |  | 	BKE_reportf(reports, RPT_ERROR, "%s\nlocation:%s:%d\n", cstring, filename, lineno); | 
					
						
							| 
									
										
										
										
											2009-11-23 17:36:44 +00:00
										 |  |  | #else
 | 
					
						
							|  |  |  | 	pystring_format= PyUnicode_FromFormat("%s\nlocation:%s:%d\n", cstring, filename, lineno); | 
					
						
							|  |  |  | 	cstring= _PyUnicode_AsString(pystring_format); | 
					
						
							|  |  |  | 	BKE_report(reports, RPT_ERROR, cstring); | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2009-11-04 22:36:46 +00:00
										 |  |  | 	 | 
					
						
							|  |  |  | 	fprintf(stderr, "%s\nlocation:%s:%d\n", cstring, filename, lineno); // not exactly needed. just for testing
 | 
					
						
							|  |  |  | 	 | 
					
						
							| 
									
										
										
										
											2009-06-14 12:53:47 +00:00
										 |  |  | 	Py_DECREF(pystring); | 
					
						
							| 
									
										
										
										
											2009-11-23 17:36:44 +00:00
										 |  |  | 	Py_DECREF(pystring_format); // workaround
 | 
					
						
							| 
									
										
										
										
											2009-06-14 12:53:47 +00:00
										 |  |  | 	return 1; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2009-11-04 22:36:46 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-02-01 10:51:34 +00:00
										 |  |  | /* array utility function */ | 
					
						
							| 
									
										
										
										
											2010-12-03 17:05:21 +00:00
										 |  |  | int PyC_AsArray(void *array, PyObject *value, int length, PyTypeObject *type, const char *error_prefix) | 
					
						
							| 
									
										
										
										
											2010-02-01 10:51:34 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	PyObject *value_fast; | 
					
						
							|  |  |  | 	int value_len; | 
					
						
							|  |  |  | 	int i; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if(!(value_fast=PySequence_Fast(value, error_prefix))) { | 
					
						
							|  |  |  | 		return -1; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	value_len= PySequence_Fast_GET_SIZE(value_fast); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if(value_len != length) { | 
					
						
							|  |  |  | 		Py_DECREF(value); | 
					
						
							| 
									
										
										
										
											2010-11-23 16:45:17 +00:00
										 |  |  | 		PyErr_Format(PyExc_TypeError, "%.200s: invalid sequence length. expected %d, got %d", error_prefix, length, value_len); | 
					
						
							| 
									
										
										
										
											2010-02-01 10:51:34 +00:00
										 |  |  | 		return -1; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* for each type */ | 
					
						
							|  |  |  | 	if(type == &PyFloat_Type) { | 
					
						
							|  |  |  | 		float *array_float= array; | 
					
						
							|  |  |  | 		for(i=0; i<length; i++) { | 
					
						
							|  |  |  | 			array_float[i] = PyFloat_AsDouble(PySequence_Fast_GET_ITEM(value_fast, i)); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	else if(type == &PyLong_Type) { | 
					
						
							|  |  |  | 		int *array_int= array; | 
					
						
							|  |  |  | 		for(i=0; i<length; i++) { | 
					
						
							|  |  |  | 			array_int[i] = PyLong_AsSsize_t(PySequence_Fast_GET_ITEM(value_fast, i)); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	else if(type == &PyBool_Type) { | 
					
						
							|  |  |  | 		int *array_bool= array; | 
					
						
							|  |  |  | 		for(i=0; i<length; i++) { | 
					
						
							|  |  |  | 			array_bool[i] = (PyLong_AsSsize_t(PySequence_Fast_GET_ITEM(value_fast, i)) != 0); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	else { | 
					
						
							|  |  |  | 		Py_DECREF(value_fast); | 
					
						
							| 
									
										
										
										
											2010-11-23 16:45:17 +00:00
										 |  |  | 		PyErr_Format(PyExc_TypeError, "%s: internal error %s is invalid", error_prefix, type->tp_name); | 
					
						
							| 
									
										
										
										
											2010-02-01 10:51:34 +00:00
										 |  |  | 		return -1; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	Py_DECREF(value_fast); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if(PyErr_Occurred()) { | 
					
						
							| 
									
										
										
										
											2010-11-23 16:45:17 +00:00
										 |  |  | 		PyErr_Format(PyExc_TypeError, "%s: one or more items could not be used as a %s", error_prefix, type->tp_name); | 
					
						
							| 
									
										
										
										
											2010-02-01 10:51:34 +00:00
										 |  |  | 		return -1; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return 0; | 
					
						
							|  |  |  | } |