|
3 | 3 | #include "dtype.h"
|
4 | 4 | #include "static_string.h"
|
5 | 5 |
|
| 6 | +void |
| 7 | +gil_error(PyObject *type, const char *msg) |
| 8 | +{ |
| 9 | + PyGILState_STATE gstate; |
| 10 | + gstate = PyGILState_Ensure(); |
| 11 | + PyErr_SetString(type, msg); |
| 12 | + PyGILState_Release(gstate); |
| 13 | +} |
| 14 | + |
6 | 15 | static NPY_CASTING
|
7 | 16 | string_to_string_resolve_descriptors(PyObject *NPY_UNUSED(self),
|
8 | 17 | PyArray_DTypeMeta *NPY_UNUSED(dtypes[2]),
|
@@ -45,6 +54,10 @@ string_to_string(PyArrayMethod_Context *context, char *const data[],
|
45 | 54 |
|
46 | 55 | while (N--) {
|
47 | 56 | out[0] = ssdup(in[0]);
|
| 57 | + if (out[0] == NULL) { |
| 58 | + gil_error(PyExc_MemoryError, "ssdup failed"); |
| 59 | + return -1; |
| 60 | + } |
48 | 61 | in += in_stride;
|
49 | 62 | out += out_stride;
|
50 | 63 | }
|
@@ -201,15 +214,13 @@ unicode_to_string(PyArrayMethod_Context *context, char *const data[],
|
201 | 214 | size_t num_codepoints = 0;
|
202 | 215 | if (utf8_size(in, max_in_size, &num_codepoints, &out_num_bytes) ==
|
203 | 216 | -1) {
|
204 |
| - // invalid codepoint found so acquire GIL, set error, return |
205 |
| - PyGILState_STATE gstate; |
206 |
| - gstate = PyGILState_Ensure(); |
207 |
| - PyErr_SetString(PyExc_TypeError, |
208 |
| - "Invalid unicode code point found"); |
209 |
| - PyGILState_Release(gstate); |
| 217 | + gil_error(PyExc_TypeError, "Invalid unicode code point found"); |
210 | 218 | return -1;
|
211 | 219 | }
|
212 | 220 | ss *out_ss = ssnewempty(out_num_bytes);
|
| 221 | + if (out_ss == NULL) { |
| 222 | + gil_error(PyExc_MemoryError, "ssnewempty failed"); |
| 223 | + } |
213 | 224 | char *out_buf = out_ss->buf;
|
214 | 225 | for (int i = 0; i < num_codepoints; i++) {
|
215 | 226 | // get code point
|
|
0 commit comments