@@ -206,6 +206,9 @@ unique_vstring(PyArrayObject *self, bool equal_nan)
206
206
PyArray_Descr *descr =PyArray_DESCR (self);
207
207
// NumPy API calls and Python object manipulations require holding the GIL.
208
208
Py_INCREF (descr);
209
+
210
+ // variables for the vstring, this operation require holding the GIL.
211
+ npy_string_allocator *in_allocator =NpyString_acquire_allocator ((PyArray_StringDTypeObject *)descr);
209
212
NPY_DISABLE_C_API;
210
213
211
214
// release the GIL
@@ -214,11 +217,6 @@ unique_vstring(PyArrayObject *self, bool equal_nan)
214
217
// number of elements in the input array
215
218
npy_intp isize =PyArray_SIZE (self);
216
219
217
- // variables for the vstring
218
- npy_string_allocator *in_allocator =NpyString_acquire_allocator ((PyArray_StringDTypeObject *)descr);
219
- auto in_allocator_dealloc =finally ([&]() {
220
- NpyString_release_allocator (in_allocator);
221
- });
222
220
auto hash = [equal_nan](const npy_static_string *value) ->size_t {
223
221
if (value->buf ==NULL ) {
224
222
if (equal_nan) {
@@ -298,19 +296,13 @@ unique_vstring(PyArrayObject *self, bool equal_nan)
298
296
// NumPy API calls and Python object manipulations require holding the GIL.
299
297
Py_INCREF (res_descr);
300
298
std::cerr <<" res_descr incremented successfully." << std::endl;
299
+ npy_string_allocator *out_allocator =NpyString_acquire_allocator ((PyArray_StringDTypeObject *)res_descr);
301
300
NPY_DISABLE_C_API;
302
301
PyThreadState *_save2 =PyEval_SaveThread ();
303
302
std::cerr <<" save2:" << (void *)_save2 << std::endl;
304
- auto save2_dealloc =finally ([&]() {
305
- PyEval_RestoreThread (_save2);
306
- });
307
303
std::cerr <<" save2_dealloc completed successfully." << std::endl;
308
304
309
- npy_string_allocator *out_allocator =NpyString_acquire_allocator ((PyArray_StringDTypeObject *)res_descr);
310
305
std::cerr <<" out_allocator:" << (void *)out_allocator << std::endl;
311
- auto out_allocator_dealloc =finally ([&]() {
312
- NpyString_release_allocator (out_allocator);
313
- });
314
306
std::cerr <<" out_allocator_dealloc completed successfully." << std::endl;
315
307
316
308
char *odata =PyArray_BYTES ((PyArrayObject *)res_obj);
@@ -332,6 +324,12 @@ unique_vstring(PyArrayObject *self, bool equal_nan)
332
324
}
333
325
}
334
326
std::cerr <<" Packing loop completed successfully." << std::endl;
327
+ PyEval_RestoreThread (_save2);
328
+ NPY_ALLOW_C_API;
329
+ // these operations require holding the GIL
330
+ NpyString_release_allocator (in_allocator);
331
+ NpyString_release_allocator (out_allocator);
332
+ NPY_DISABLE_C_API;
335
333
336
334
return res_obj;
337
335
}