| 
									
										
										
										
											2011-02-23 10:52:22 +00:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2008-12-28 08:41:49 +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. | 
					
						
							| 
									
										
										
										
											2008-12-28 08:41:49 +00:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-18 08:08:12 +11:00
										 |  |  | /** \file
 | 
					
						
							|  |  |  |  * \ingroup pythonintern | 
					
						
							| 
									
										
										
										
											2011-11-05 08:21:12 +00:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2017-11-29 21:11:29 +11:00
										 |  |  |  * This file contains Blender/Python utility functions to help implementing API's. | 
					
						
							|  |  |  |  * This is not related to a particular module. | 
					
						
							| 
									
										
										
										
											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 "BLI_dynstr.h"
 | 
					
						
							| 
									
										
										
										
											2020-04-03 17:38:58 +02:00
										 |  |  | #include "BLI_listbase.h"
 | 
					
						
							| 
									
										
										
										
											2020-03-19 09:33:03 +01:00
										 |  |  | #include "BLI_utildefines.h"
 | 
					
						
							| 
									
										
										
										
											2013-01-07 05:26:12 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-29 21:11:29 +11:00
										 |  |  | #include "bpy_capi_utils.h"
 | 
					
						
							| 
									
										
										
										
											2013-01-07 05:26:12 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-12-28 08:41:49 +00:00
										 |  |  | #include "MEM_guardedalloc.h"
 | 
					
						
							| 
									
										
										
										
											2013-01-07 05:26:12 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-09-01 14:13:48 +00:00
										 |  |  | #include "BKE_context.h"
 | 
					
						
							| 
									
										
										
										
											2020-03-19 09:33:03 +01:00
										 |  |  | #include "BKE_report.h"
 | 
					
						
							| 
									
										
										
										
											2008-12-28 08:41:49 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-16 17:32:01 +10:00
										 |  |  | #include "BLT_translation.h"
 | 
					
						
							| 
									
										
										
										
											2012-10-14 15:29:09 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-09-01 14:13:48 +00:00
										 |  |  | #include "../generic/py_capi_utils.h"
 | 
					
						
							| 
									
										
										
										
											2009-05-25 13:48:44 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-18 15:07:26 +11:00
										 |  |  | char *BPy_enum_as_string(const EnumPropertyItem *item) | 
					
						
							| 
									
										
										
										
											2009-04-01 12:43:07 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  |   DynStr *dynstr = BLI_dynstr_new(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-01 16:32:11 +10:00
										 |  |  |   /* We can't compare with the first element in the array
 | 
					
						
							|  |  |  |    * since it may be a category (without an identifier). */ | 
					
						
							|  |  |  |   for (bool is_first = true; item->identifier; item++) { | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  |     if (item->identifier[0]) { | 
					
						
							| 
									
										
										
										
											2020-09-01 16:32:11 +10:00
										 |  |  |       BLI_dynstr_appendf(dynstr, is_first ? "'%s'" : ", '%s'", item->identifier); | 
					
						
							|  |  |  |       is_first = false; | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-01 16:32:11 +10:00
										 |  |  |   char *cstring = BLI_dynstr_get_cstring(dynstr); | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  |   BLI_dynstr_free(dynstr); | 
					
						
							|  |  |  |   return cstring; | 
					
						
							| 
									
										
										
										
											2009-04-01 12:43:07 +00:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2009-04-19 13:37:59 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-01-07 05:26:12 +00:00
										 |  |  | short BPy_reports_to_error(ReportList *reports, PyObject *exception, const bool clear) | 
					
						
							| 
									
										
										
										
											2009-04-19 13:37:59 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  |   char *report_str; | 
					
						
							| 
									
										
										
										
											2009-04-19 13:37:59 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  |   report_str = BKE_reports_string(reports, RPT_ERROR); | 
					
						
							| 
									
										
										
										
											2009-04-19 13:37:59 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  |   if (clear == true) { | 
					
						
							|  |  |  |     BKE_reports_clear(reports); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2010-12-31 05:40:30 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  |   if (report_str) { | 
					
						
							|  |  |  |     PyErr_SetString(exception, report_str); | 
					
						
							|  |  |  |     MEM_freeN(report_str); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2009-04-19 13:37:59 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  |   return (report_str == NULL) ? 0 : -1; | 
					
						
							| 
									
										
										
										
											2009-04-19 13:37:59 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-27 14:01:25 +11:00
										 |  |  | /**
 | 
					
						
							|  |  |  |  * A version of #BKE_report_write_file_fp that uses Python's stdout. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | void BPy_reports_write_stdout(const ReportList *reports, const char *header) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  |   if (header) { | 
					
						
							|  |  |  |     PySys_WriteStdout("%s\n", header); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2019-03-27 14:01:25 +11:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-03 19:15:01 +02:00
										 |  |  |   LISTBASE_FOREACH (const Report *, report, &reports->list) { | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  |     PySys_WriteStdout("%s: %s\n", report->typestr, report->message); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2019-03-27 14:01:25 +11:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2009-06-14 12:53:47 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-27 13:46:58 +10:00
										 |  |  | bool BPy_errors_to_report_ex(ReportList *reports, | 
					
						
							|  |  |  |                              const char *error_prefix, | 
					
						
							|  |  |  |                              const bool use_full, | 
					
						
							|  |  |  |                              const bool use_location) | 
					
						
							| 
									
										
										
										
											2009-06-14 12:53:47 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  |   PyObject *pystring; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (!PyErr_Occurred()) { | 
					
						
							|  |  |  |     return 1; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /* less hassle if we allow NULL */ | 
					
						
							|  |  |  |   if (reports == NULL) { | 
					
						
							|  |  |  |     PyErr_Print(); | 
					
						
							|  |  |  |     PyErr_Clear(); | 
					
						
							|  |  |  |     return 1; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (use_full) { | 
					
						
							|  |  |  |     pystring = PyC_ExceptionBuffer(); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   else { | 
					
						
							|  |  |  |     pystring = PyC_ExceptionBuffer_Simple(); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (pystring == NULL) { | 
					
						
							|  |  |  |     BKE_report(reports, RPT_ERROR, "Unknown py-exception, could not convert"); | 
					
						
							|  |  |  |     return 0; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-27 13:46:58 +10:00
										 |  |  |   if (error_prefix == NULL) { | 
					
						
							|  |  |  |     /* Not very helpful, better than nothing. */ | 
					
						
							|  |  |  |     error_prefix = "Python"; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  |   if (use_location) { | 
					
						
							|  |  |  |     const char *filename; | 
					
						
							|  |  |  |     int lineno; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     PyC_FileAndNum(&filename, &lineno); | 
					
						
							|  |  |  |     if (filename == NULL) { | 
					
						
							|  |  |  |       filename = "<unknown location>"; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-05-18 09:12:26 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-17 08:24:14 +02:00
										 |  |  |     BKE_reportf(reports, | 
					
						
							|  |  |  |                 RPT_ERROR, | 
					
						
							| 
									
										
										
										
											2020-07-27 13:46:58 +10:00
										 |  |  |                 TIP_("%s: %s\nlocation: %s:%d\n"), | 
					
						
							|  |  |  |                 error_prefix, | 
					
						
							| 
									
										
										
										
											2019-04-17 08:24:14 +02:00
										 |  |  |                 _PyUnicode_AsString(pystring), | 
					
						
							|  |  |  |                 filename, | 
					
						
							|  |  |  |                 lineno); | 
					
						
							| 
									
										
										
										
											2015-06-03 12:36:32 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-27 13:02:33 +10:00
										 |  |  |     /* Not exactly needed. Useful for developers tracking down issues. */ | 
					
						
							| 
									
										
										
										
											2020-07-27 13:46:58 +10:00
										 |  |  |     fprintf(stderr, | 
					
						
							|  |  |  |             TIP_("%s: %s\nlocation: %s:%d\n"), | 
					
						
							|  |  |  |             error_prefix, | 
					
						
							|  |  |  |             _PyUnicode_AsString(pystring), | 
					
						
							|  |  |  |             filename, | 
					
						
							|  |  |  |             lineno); | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  |   } | 
					
						
							|  |  |  |   else { | 
					
						
							| 
									
										
										
										
											2020-07-27 13:46:58 +10:00
										 |  |  |     BKE_reportf(reports, RPT_ERROR, "%s: %s", error_prefix, _PyUnicode_AsString(pystring)); | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2012-10-15 09:11:17 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  |   Py_DECREF(pystring); | 
					
						
							|  |  |  |   return 1; | 
					
						
							| 
									
										
										
										
											2009-06-14 12:53:47 +00:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2015-05-18 09:12:26 +10:00
										 |  |  | 
 | 
					
						
							|  |  |  | bool BPy_errors_to_report(ReportList *reports) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2020-07-27 13:46:58 +10:00
										 |  |  |   return BPy_errors_to_report_ex(reports, NULL, true, true); | 
					
						
							| 
									
										
										
										
											2016-05-16 00:48:02 +02:00
										 |  |  | } |