summaryrefslogtreecommitdiffstats
path: root/graphics/Blender/patch-2.74-use-python35.diff
blob: b10942bf38093193086739f4adce4de1a952c388 (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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
--- source/blender/python/generic/py_capi_utils.h.orig	2015-03-25 21:01:17.000000000 +1000
+++ source/blender/python/generic/py_capi_utils.h	2016-01-07 23:41:12.118573813 +1000
@@ -32,13 +32,18 @@
 void			PyC_LineSpit(void);
 void			PyC_StackSpit(void);
 PyObject *		PyC_ExceptionBuffer(void);
+PyObject *		PyC_ExceptionBuffer_Simple(void);
 PyObject *		PyC_Object_GetAttrStringArgs(PyObject *o, Py_ssize_t n, ...);
 PyObject *		PyC_FrozenSetFromStrings(const char **strings);
 PyObject *		PyC_Err_Format_Prefix(PyObject *exception_type_prefix, const char *format, ...);
 void			PyC_FileAndNum(const char **filename, int *lineno);
 void			PyC_FileAndNum_Safe(const char **filename, int *lineno); /* checks python is running */
-int				PyC_AsArray(void *array, PyObject *value, const Py_ssize_t length,
-                            const PyTypeObject *type, const bool is_double, const char *error_prefix);
+int             PyC_AsArray_FAST(
+        void *array, PyObject *value_fast, const Py_ssize_t length,
+        const PyTypeObject *type, const bool is_double, const char *error_prefix);
+int             PyC_AsArray(
+        void *array, PyObject *value, const Py_ssize_t length,
+        const PyTypeObject *type, const bool is_double, const char *error_prefix);
 PyObject *      PyC_FromArray(const void *array, int length, const PyTypeObject *type,
                               const bool is_double, const char *error_prefix);
 void            PyC_Tuple_Fill(PyObject *tuple, PyObject *value);
@@ -74,6 +79,8 @@
 int       PyC_FlagSet_ToBitfield(PyC_FlagSet *items, PyObject *value, int *r_value, const char *error_prefix);
 PyObject *PyC_FlagSet_FromBitfield(PyC_FlagSet *items, int flag);
 
-int PyC_RunString_AsNumber(const char *expr, double *value, const char *filename);
+bool PyC_RunString_AsNumber(const char *expr, double *value, const char *filename);
+
+int PyC_ParseBool(PyObject *o, void *p);
 
 #endif  /* __PY_CAPI_UTILS_H__ */
--- source/blender/python/generic/py_capi_utils.c.orig	2015-03-25 21:01:17.000000000 +1000
+++ source/blender/python/generic/py_capi_utils.c	2016-01-07 23:41:12.114573720 +1000
@@ -29,7 +29,6 @@
  * BLI_string_utf8() for unicode conversion.
  */
 
-
 #include <Python.h>
 #include <frameobject.h>
 
@@ -37,10 +36,12 @@
 
 #include "py_capi_utils.h"
 
-#include "../generic/python_utildefines.h"
+#include "python_utildefines.h"
 
+#ifndef MATH_STANDALONE
 /* only for BLI_strncpy_wchar_from_utf8, should replace with py funcs but too late in release now */
 #include "BLI_string_utf8.h"
+#endif
 
 #ifdef _WIN32
 #include "BLI_path_util.h"  /* BLI_setenv() */
@@ -48,21 +49,17 @@
 #endif
 
 /* array utility function */
-int PyC_AsArray(void *array, PyObject *value, const Py_ssize_t length,
-                const PyTypeObject *type, const bool is_double, const char *error_prefix)
+int PyC_AsArray_FAST(
+        void *array, PyObject *value_fast, const Py_ssize_t length,
+        const PyTypeObject *type, const bool is_double, const char *error_prefix)
 {
-	PyObject *value_fast;
-	Py_ssize_t value_len;
+	const Py_ssize_t value_len = PySequence_Fast_GET_SIZE(value_fast);
+	PyObject **value_fast_items = PySequence_Fast_ITEMS(value_fast);
 	Py_ssize_t i;
 
-	if (!(value_fast = PySequence_Fast(value, error_prefix))) {
-		return -1;
-	}
-
-	value_len = PySequence_Fast_GET_SIZE(value_fast);
+	BLI_assert(PyList_Check(value_fast) || PyTuple_Check(value_fast));
 
 	if (value_len != length) {
-		Py_DECREF(value);
 		PyErr_Format(PyExc_TypeError,
 		             "%.200s: invalid sequence length. expected %d, got %d",
 		             error_prefix, length, value_len);
@@ -74,13 +71,13 @@
 		if (is_double) {
 			double *array_double = array;
 			for (i = 0; i < length; i++) {
-				array_double[i] = PyFloat_AsDouble(PySequence_Fast_GET_ITEM(value_fast, i));
+				array_double[i] = PyFloat_AsDouble(value_fast_items[i]);
 			}
 		}
 		else {
 			float *array_float = array;
 			for (i = 0; i < length; i++) {
-				array_float[i] = PyFloat_AsDouble(PySequence_Fast_GET_ITEM(value_fast, i));
+				array_float[i] = PyFloat_AsDouble(value_fast_items[i]);
 			}
 		}
 	}
@@ -88,25 +85,22 @@
 		/* could use is_double for 'long int' but no use now */
 		int *array_int = array;
 		for (i = 0; i < length; i++) {
-			array_int[i] = PyLong_AsLong(PySequence_Fast_GET_ITEM(value_fast, i));
+			array_int[i] = PyLong_AsLong(value_fast_items[i]);
 		}
 	}
 	else if (type == &PyBool_Type) {
 		int *array_bool = array;
 		for (i = 0; i < length; i++) {
-			array_bool[i] = (PyLong_AsLong(PySequence_Fast_GET_ITEM(value_fast, i)) != 0);
+			array_bool[i] = (PyLong_AsLong(value_fast_items[i]) != 0);
 		}
 	}
 	else {
-		Py_DECREF(value_fast);
 		PyErr_Format(PyExc_TypeError,
 		             "%s: internal error %s is invalid",
 		             error_prefix, type->tp_name);
 		return -1;
 	}
 
-	Py_DECREF(value_fast);
-
 	if (PyErr_Occurred()) {
 		PyErr_Format(PyExc_TypeError,
 		             "%s: one or more items could not be used as a %s",
@@ -117,6 +111,22 @@
 	return 0;
 }
 
+int PyC_AsArray(
+        void *array, PyObject *value, const Py_ssize_t length,
+        const PyTypeObject *type, const bool is_double, const char *error_prefix)
+{
+	PyObject *value_fast;
+	int ret;
+
+	if (!(value_fast = PySequence_Fast(value, error_prefix))) {
+		return -1;
+	}
+
+	ret = PyC_AsArray_FAST(array, value_fast, length, type, is_double, error_prefix);
+	Py_DECREF(value_fast);
+	return ret;
+}
+
 /* array utility function */
 PyObject *PyC_FromArray(const void *array, int length, const PyTypeObject *type,
                         const bool is_double, const char *error_prefix)
@@ -191,6 +201,27 @@
 	}
 }
 
+/**
+ * Use with PyArg_ParseTuple's "O&" formatting.
+ */
+int PyC_ParseBool(PyObject *o, void *p)
+{
+	bool *bool_p = p;
+	long value;
+	if (((value = PyLong_AsLong(o)) == -1) || !ELEM(value, 0, 1)) {
+		PyErr_Format(PyExc_ValueError,
+		             "expected a bool or int (0/1), got %s",
+		             Py_TYPE(o)->tp_name);
+		return 0;
+	}
+
+	*bool_p = value ? true : false;
+	return 1;
+}
+
+
+#ifndef MATH_STANDALONE
+
 /* for debugging */
 void PyC_ObSpit(const char *name, PyObject *var)
 {
@@ -478,6 +509,34 @@
 }
 #endif
 
+PyObject *PyC_ExceptionBuffer_Simple(void)
+{
+	PyObject *string_io_buf;
+
+	PyObject *error_type, *error_value, *error_traceback;
+
+	if (!PyErr_Occurred())
+		return NULL;
+
+	PyErr_Fetch(&error_type, &error_value, &error_traceback);
+
+	if (error_value == NULL) {
+		return NULL;
+	}
+
+	string_io_buf = PyObject_Str(error_value);
+	/* Python does this too */
+	if (UNLIKELY(string_io_buf == NULL)) {
+		string_io_buf = PyUnicode_FromFormat(
+		        "<unprintable %s object>", Py_TYPE(error_value)->tp_name);
+	}
+
+	PyErr_Restore(error_type, error_value, error_traceback);
+
+	PyErr_Print();
+	PyErr_Clear();
+	return string_io_buf;
+}
 
 /* string conversion, escape non-unicode chars, coerce must be set to NULL */
 const char *PyC_UnicodeAsByte(PyObject *py_str, PyObject **coerce)
@@ -497,15 +556,6 @@
 		if (PyBytes_Check(py_str)) {
 			return PyBytes_AS_STRING(py_str);
 		}
-#ifdef WIN32
-		/* bug [#31856] oddly enough, Python3.2 --> 3.3 on Windows will throw an
-		 * exception here this needs to be fixed in python:
-		 * see: bugs.python.org/issue15859 */
-		else if (!PyUnicode_Check(py_str)) {
-			PyErr_BadArgument();
-			return NULL;
-		}
-#endif
 		else if ((*coerce = PyUnicode_EncodeFSDefault(py_str))) {
 			return PyBytes_AS_STRING(*coerce);
 		}
@@ -629,7 +679,8 @@
 
 bool PyC_IsInterpreterActive(void)
 {
-	return (((PyThreadState *)_Py_atomic_load_relaxed(&_PyThreadState_Current)) != NULL);
+	/* instead of PyThreadState_Get, which calls Py_FatalError */
+	return (PyThreadState_GetDict() != NULL);
 }
 
 /* Would be nice if python had this built in
@@ -673,7 +724,7 @@
 			}
 
 			if (ret == NULL) {
-				printf("PyC_InlineRun error, line:%d\n", __LINE__);
+				printf("%s error, line:%d\n", __func__, __LINE__);
 				PyErr_Print();
 				PyErr_Clear();
 
@@ -747,7 +798,7 @@
 						Py_DECREF(ret);
 					}
 					else {
-						printf("PyC_InlineRun error on arg '%d', line:%d\n", i, __LINE__);
+						printf("%s error on arg '%d', line:%d\n", __func__, i, __LINE__);
 						PyC_ObSpit("failed converting:", item_new);
 						PyErr_Print();
 						PyErr_Clear();
@@ -758,11 +809,11 @@
 				va_end(vargs);
 			}
 			else {
-				printf("PyC_InlineRun error, 'values' not a list, line:%d\n", __LINE__);
+				printf("%s error, 'values' not a list, line:%d\n", __func__, __LINE__);
 			}
 		}
 		else {
-			printf("PyC_InlineRun error line:%d\n", __LINE__);
+			printf("%s error line:%d\n", __func__, __LINE__);
 			PyErr_Print();
 			PyErr_Clear();
 		}
@@ -920,14 +971,14 @@
 
 
 /**
- * \return -1 on error, else 0
+ * \return success
  *
  * \note it is caller's responsibility to acquire & release GIL!
  */
-int PyC_RunString_AsNumber(const char *expr, double *value, const char *filename)
+bool PyC_RunString_AsNumber(const char *expr, double *value, const char *filename)
 {
 	PyObject *py_dict, *mod, *retval;
-	int error_ret = 0;
+	bool ok = true;
 	PyObject *main_mod = NULL;
 
 	PyC_MainModule_Backup(&main_mod);
@@ -947,7 +998,7 @@
 	retval = PyRun_String(expr, Py_eval_input, py_dict, py_dict);
 
 	if (retval == NULL) {
-		error_ret = -1;
+		ok = false;
 	}
 	else {
 		double val;
@@ -973,7 +1024,7 @@
 		Py_DECREF(retval);
 
 		if (val == -1 && PyErr_Occurred()) {
-			error_ret = -1;
+			ok = false;
 		}
 		else if (!finite(val)) {
 			*value = 0.0;
@@ -985,5 +1036,7 @@
 
 	PyC_MainModule_Restore(main_mod);
 
-	return error_ret;
+	return ok;
 }
+
+#endif  /* #ifndef MATH_STANDALONE */