1
+ #include <Python.h>
2
+ #define PY_ARRAY_UNIQUE_SYMBOL stringdtype_ARRAY_API
3
+ #define NPY_NO_DEPRECATED_API NPY_2_0_API_VERSION
4
+ #define NPY_TARGET_VERSION NPY_2_0_API_VERSION
5
+ #define NO_IMPORT_ARRAY
6
+ #include "numpy/ndarraytypes.h"
7
+ #include "numpy/arrayobject.h"
8
+ #include "numpy/ufuncobject.h"
9
+ #include "numpy/dtype_api.h"
10
+ #include "numpy/halffloat.h"
11
+ #include "numpy/npy_math.h"
12
+
1
13
#include "dtype.h"
2
14
3
15
#include "casts.h"
@@ -24,7 +36,7 @@ new_stringdtype_instance(PyObject *na_object, int coerce)
24
36
char * default_string_buf = NULL ;
25
37
char * na_name_buf = NULL ;
26
38
27
- allocator = NpyString_new_allocator (PyMem_RawMalloc , PyMem_RawFree ,
39
+ allocator = _NpyString_new_allocator (PyMem_RawMalloc , PyMem_RawFree ,
28
40
PyMem_RawRealloc );
29
41
if (allocator == NULL ) {
30
42
PyErr_SetString (PyExc_MemoryError ,
@@ -38,8 +50,8 @@ new_stringdtype_instance(PyObject *na_object, int coerce)
38
50
goto fail ;
39
51
}
40
52
41
- npy_static_string default_string = {0 , NULL };
42
- npy_static_string na_name = {0 , NULL };
53
+ _npy_static_string default_string = {0 , NULL };
54
+ _npy_static_string na_name = {0 , NULL };
43
55
44
56
Py_XINCREF (na_object );
45
57
((StringDTypeObject * )new )-> na_object = na_object ;
@@ -127,7 +139,7 @@ new_stringdtype_instance(PyObject *na_object, int coerce)
127
139
PyMem_RawFree (na_name_buf );
128
140
}
129
141
if (allocator != NULL ) {
130
- NpyString_free_allocator (allocator );
142
+ _NpyString_free_allocator (allocator );
131
143
}
132
144
if (allocator_lock != NULL ) {
133
145
PyThread_free_lock (allocator_lock );
@@ -262,15 +274,15 @@ stringdtype_setitem(StringDTypeObject *descr, PyObject *obj, char **dataptr)
262
274
{
263
275
npy_packed_static_string * sdata = (npy_packed_static_string * )dataptr ;
264
276
265
- npy_string_allocator * allocator = NpyString_acquire_allocator (descr );
277
+ npy_string_allocator * allocator = _NpyString_acquire_allocator (descr );
266
278
267
279
// borrow reference
268
280
PyObject * na_object = descr -> na_object ;
269
281
270
282
// setting NA *must* check pointer equality since NA types might not
271
283
// allow equality
272
284
if (na_object != NULL && obj == na_object ) {
273
- if (NpyString_pack_null (allocator , sdata ) < 0 ) {
285
+ if (_NpyString_pack_null (allocator , sdata ) < 0 ) {
274
286
PyErr_SetString (PyExc_MemoryError ,
275
287
"Failed to pack null string during StringDType "
276
288
"setitem" );
@@ -291,7 +303,7 @@ stringdtype_setitem(StringDTypeObject *descr, PyObject *obj, char **dataptr)
291
303
goto fail ;
292
304
}
293
305
294
- if (NpyString_pack (allocator , sdata , val , length ) < 0 ) {
306
+ if (_NpyString_pack (allocator , sdata , val , length ) < 0 ) {
295
307
PyErr_SetString (PyExc_MemoryError ,
296
308
"Failed to pack string during StringDType "
297
309
"setitem" );
@@ -301,12 +313,12 @@ stringdtype_setitem(StringDTypeObject *descr, PyObject *obj, char **dataptr)
301
313
Py_DECREF (val_obj );
302
314
}
303
315
304
- NpyString_release_allocator (descr );
316
+ _NpyString_release_allocator (descr );
305
317
306
318
return 0 ;
307
319
308
320
fail :
309
- NpyString_release_allocator (descr );
321
+ _NpyString_release_allocator (descr );
310
322
311
323
return -1 ;
312
324
}
@@ -316,10 +328,10 @@ stringdtype_getitem(StringDTypeObject *descr, char **dataptr)
316
328
{
317
329
PyObject * val_obj = NULL ;
318
330
npy_packed_static_string * psdata = (npy_packed_static_string * )dataptr ;
319
- npy_static_string sdata = {0 , NULL };
331
+ _npy_static_string sdata = {0 , NULL };
320
332
int hasnull = descr -> na_object != NULL ;
321
- npy_string_allocator * allocator = NpyString_acquire_allocator (descr );
322
- int is_null = NpyString_load (allocator , psdata , & sdata );
333
+ npy_string_allocator * allocator = _NpyString_acquire_allocator (descr );
334
+ int is_null = _NpyString_load (allocator , psdata , & sdata );
323
335
324
336
if (is_null < 0 ) {
325
337
PyErr_SetString (PyExc_MemoryError ,
@@ -344,7 +356,7 @@ stringdtype_getitem(StringDTypeObject *descr, char **dataptr)
344
356
}
345
357
}
346
358
347
- NpyString_release_allocator (descr );
359
+ _NpyString_release_allocator (descr );
348
360
349
361
/*
350
362
* In principle we should return a StringScalar instance here, but
@@ -361,7 +373,7 @@ stringdtype_getitem(StringDTypeObject *descr, char **dataptr)
361
373
362
374
fail :
363
375
364
- NpyString_release_allocator (descr );
376
+ _NpyString_release_allocator (descr );
365
377
366
378
return NULL ;
367
379
}
@@ -371,7 +383,7 @@ stringdtype_getitem(StringDTypeObject *descr, char **dataptr)
371
383
npy_bool
372
384
nonzero (void * data , void * NPY_UNUSED (arr ))
373
385
{
374
- return NpyString_size ((npy_packed_static_string * )data ) != 0 ;
386
+ return _NpyString_size ((npy_packed_static_string * )data ) != 0 ;
375
387
}
376
388
377
389
// Implementation of PyArray_CompareFunc.
@@ -382,9 +394,9 @@ compare(void *a, void *b, void *arr)
382
394
StringDTypeObject * descr = (StringDTypeObject * )PyArray_DESCR (arr );
383
395
// ignore the allocator returned by this function
384
396
// since _compare needs the descr anyway
385
- NpyString_acquire_allocator (descr );
397
+ _NpyString_acquire_allocator (descr );
386
398
int ret = _compare (a , b , descr , descr );
387
- NpyString_release_allocator (descr );
399
+ _NpyString_release_allocator (descr );
388
400
return ret ;
389
401
}
390
402
@@ -400,13 +412,13 @@ _compare(void *a, void *b, StringDTypeObject *descr_a,
400
412
int hasnull = descr_a -> na_object != NULL ;
401
413
int has_string_na = descr_a -> has_string_na ;
402
414
int has_nan_na = descr_a -> has_nan_na ;
403
- npy_static_string * default_string = & descr_a -> default_string ;
415
+ _npy_static_string * default_string = & descr_a -> default_string ;
404
416
const npy_packed_static_string * ps_a = (npy_packed_static_string * )a ;
405
- npy_static_string s_a = {0 , NULL };
406
- int a_is_null = NpyString_load (allocator_a , ps_a , & s_a );
417
+ _npy_static_string s_a = {0 , NULL };
418
+ int a_is_null = _NpyString_load (allocator_a , ps_a , & s_a );
407
419
const npy_packed_static_string * ps_b = (npy_packed_static_string * )b ;
408
- npy_static_string s_b = {0 , NULL };
409
- int b_is_null = NpyString_load (allocator_b , ps_b , & s_b );
420
+ _npy_static_string s_b = {0 , NULL };
421
+ int b_is_null = _NpyString_load (allocator_b , ps_b , & s_b );
410
422
if (NPY_UNLIKELY (a_is_null == -1 || b_is_null == -1 )) {
411
423
char * msg = "Failed to load string in string comparison" ;
412
424
if (hasnull && !(has_string_na && has_nan_na )) {
@@ -450,7 +462,7 @@ _compare(void *a, void *b, StringDTypeObject *descr_a,
450
462
}
451
463
}
452
464
}
453
- return NpyString_cmp (& s_a , & s_b );
465
+ return _NpyString_cmp (& s_a , & s_b );
454
466
}
455
467
456
468
// PyArray_ArgFunc
@@ -494,25 +506,25 @@ stringdtype_ensure_canonical(StringDTypeObject *self)
494
506
495
507
static int
496
508
stringdtype_clear_loop (void * NPY_UNUSED (traverse_context ),
497
- PyArray_Descr * descr , char * data , npy_intp size ,
509
+ const PyArray_Descr * descr , char * data , npy_intp size ,
498
510
npy_intp stride , NpyAuxData * NPY_UNUSED (auxdata ))
499
511
{
500
512
StringDTypeObject * sdescr = (StringDTypeObject * )descr ;
501
- npy_string_allocator * allocator = NpyString_acquire_allocator (sdescr );
513
+ npy_string_allocator * allocator = _NpyString_acquire_allocator (sdescr );
502
514
while (size -- ) {
503
515
npy_packed_static_string * sdata = (npy_packed_static_string * )data ;
504
- if (data != NULL && NpyString_free (sdata , allocator ) < 0 ) {
516
+ if (data != NULL && _NpyString_free (sdata , allocator ) < 0 ) {
505
517
gil_error (PyExc_MemoryError ,
506
518
"String deallocation failed in clear loop" );
507
519
goto fail ;
508
520
}
509
521
data += stride ;
510
522
}
511
- NpyString_release_allocator (sdescr );
523
+ _NpyString_release_allocator (sdescr );
512
524
return 0 ;
513
525
514
526
fail :
515
- NpyString_release_allocator (sdescr );
527
+ _NpyString_release_allocator (sdescr );
516
528
return -1 ;
517
529
}
518
530
@@ -521,7 +533,7 @@ stringdtype_get_clear_loop(void *NPY_UNUSED(traverse_context),
521
533
PyArray_Descr * NPY_UNUSED (descr ),
522
534
int NPY_UNUSED (aligned ),
523
535
npy_intp NPY_UNUSED (fixed_stride ),
524
- traverse_loop_function * * out_loop ,
536
+ PyArrayMethod_TraverseLoop * * out_loop ,
525
537
NpyAuxData * * NPY_UNUSED (out_auxdata ),
526
538
NPY_ARRAYMETHOD_FLAGS * flags )
527
539
{
@@ -676,7 +688,7 @@ stringdtype_dealloc(StringDTypeObject *self)
676
688
if (self -> allocator != NULL ) {
677
689
// can we assume the destructor for an instance will only get called
678
690
// inside of one C thread?
679
- NpyString_free_allocator (self -> allocator );
691
+ _NpyString_free_allocator (self -> allocator );
680
692
PyThread_free_lock (self -> allocator_lock );
681
693
}
682
694
PyMem_RawFree ((char * )self -> na_name .buf );
@@ -931,14 +943,14 @@ free_and_copy(npy_string_allocator *in_allocator,
931
943
const npy_packed_static_string * in ,
932
944
npy_packed_static_string * out , const char * location )
933
945
{
934
- if (NpyString_free (out , out_allocator ) < 0 ) {
946
+ if (_NpyString_free (out , out_allocator ) < 0 ) {
935
947
char message [200 ];
936
948
snprintf (message , sizeof (message ), "Failed to deallocate string in %s" ,
937
949
location );
938
950
gil_error (PyExc_MemoryError , message );
939
951
return -1 ;
940
952
}
941
- if (NpyString_dup (in , out , in_allocator , out_allocator ) < 0 ) {
953
+ if (_NpyString_dup (in , out , in_allocator , out_allocator ) < 0 ) {
942
954
char message [200 ];
943
955
snprintf (message , sizeof (message ), "Failed to allocate string in %s" ,
944
956
location );
0 commit comments