| 
									
										
										
										
											2011-02-23 10:52:22 +00:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2008-12-28 08:41:49 +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. | 
					
						
							| 
									
										
										
										
											2008-12-28 08:41:49 +00:00
										 |  |  |  * | 
					
						
							|  |  |  |  * Contributor(s): Campbell Barton | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * ***** END GPL LICENSE BLOCK ***** | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-27 20:10:08 +00:00
										 |  |  | /** \file blender/python/intern/bpy_util.c
 | 
					
						
							|  |  |  |  *  \ingroup pythonintern | 
					
						
							| 
									
										
										
										
											2011-11-05 08:21:12 +00:00
										 |  |  |  * | 
					
						
							|  |  |  |  * This file contains blender/python utility functions for the api's internal | 
					
						
							|  |  |  |  * use (unrelated to 'bpy.utils') | 
					
						
							| 
									
										
										
										
											2011-02-27 20:10:08 +00:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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-03-19 11:12:48 +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++) { | 
					
						
							| 
									
										
										
										
											2011-10-13 01:29:08 +00:00
										 |  |  | 		if (item->identifier[0]) | 
					
						
							| 
									
										
										
											
												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
										 |  |  | 			BLI_dynstr_appendf(dynstr, (e==item)?"'%s'":", '%s'", item->identifier); | 
					
						
							| 
									
										
										
										
											2009-04-01 12:43:07 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-19 11:12:48 +00:00
										 |  |  | 	cstring= BLI_dynstr_get_cstring(dynstr); | 
					
						
							| 
									
										
										
										
											2009-04-01 12:43:07 +00:00
										 |  |  | 	BLI_dynstr_free(dynstr); | 
					
						
							|  |  |  | 	return cstring; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2009-04-19 13:37:59 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-12 15:18:08 +00:00
										 |  |  | short BPy_reports_to_error(ReportList *reports, PyObject *exception, const short clear) | 
					
						
							| 
									
										
										
										
											2009-04-19 13:37:59 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	char *report_str; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	report_str= BKE_reports_string(reports, RPT_ERROR); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-13 01:29:08 +00:00
										 |  |  | 	if (clear) { | 
					
						
							| 
									
										
										
										
											2010-12-31 05:40:30 +00:00
										 |  |  | 		BKE_reports_clear(reports); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-13 01:29:08 +00:00
										 |  |  | 	if (report_str) { | 
					
						
							| 
									
										
										
										
											2011-03-12 15:18:08 +00:00
										 |  |  | 		PyErr_SetString(exception, report_str); | 
					
						
							| 
									
										
										
										
											2009-04-19 13:37:59 +00:00
										 |  |  | 		MEM_freeN(report_str); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-12 15:18:08 +00:00
										 |  |  | 	return (report_str == NULL) ? 0 : -1; | 
					
						
							| 
									
										
										
										
											2009-04-19 13:37:59 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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 */ | 
					
						
							| 
									
										
										
										
											2011-10-13 01:29:08 +00:00
										 |  |  | 	if (reports==NULL) { | 
					
						
							| 
									
										
										
										
											2009-06-14 12:53:47 +00:00
										 |  |  | 		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
										 |  |  | 	 | 
					
						
							| 
									
										
										
										
											2011-10-13 01:29:08 +00:00
										 |  |  | 	if (pystring==NULL) { | 
					
						
							| 
									
										
										
										
											2011-04-21 05:49:47 +00:00
										 |  |  | 		BKE_report(reports, RPT_ERROR, "unknown py-exception, couldn't convert"); | 
					
						
							| 
									
										
										
										
											2009-06-14 12:53:47 +00:00
										 |  |  | 		return 0; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	 | 
					
						
							| 
									
										
										
										
											2010-09-01 14:13:48 +00:00
										 |  |  | 	PyC_FileAndNum(&filename, &lineno); | 
					
						
							| 
									
										
										
										
											2011-10-13 01:29:08 +00:00
										 |  |  | 	if (filename==NULL) | 
					
						
							| 
									
										
										
										
											2009-11-23 17:36:44 +00:00
										 |  |  | 		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; | 
					
						
							|  |  |  | } |