Skip to content

Commit daa712e

Browse files
committed
give all the string API functions NpyString names
1 parent 21da3a5 commit daa712e

File tree

6 files changed

+135
-136
lines changed

6 files changed

+135
-136
lines changed

stringdtype/stringdtype/src/casts.c

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,11 @@ string_to_string(PyArrayMethod_Context *context, char *const data[],
8888
const npy_packed_static_string *s = (npy_packed_static_string *)in;
8989
npy_packed_static_string *os = (npy_packed_static_string *)out;
9090
if (in != out) {
91-
if (in_hasnull && !out_hasnull && npy_string_isnull(s)) {
91+
if (in_hasnull && !out_hasnull && NpyString_isnull(s)) {
9292
// lossy but this is an unsafe cast so this is OK
93-
npy_string_free(os, odescr->allocator);
94-
if (npy_string_newsize(in_na_name->buf, in_na_name->size, os,
95-
odescr->allocator) < 0) {
93+
NpyString_free(os, odescr->allocator);
94+
if (NpyString_newsize(in_na_name->buf, in_na_name->size, os,
95+
odescr->allocator) < 0) {
9696
gil_error(PyExc_MemoryError,
9797
"Failed to allocate string in string to string "
9898
"cast.");
@@ -243,18 +243,18 @@ unicode_to_string(PyArrayMethod_Context *context, char *const data[],
243243
goto fail;
244244
}
245245
npy_packed_static_string *out_pss = (npy_packed_static_string *)out;
246-
if (npy_string_free(out_pss, allocator) < 0) {
246+
if (NpyString_free(out_pss, allocator) < 0) {
247247
gil_error(PyExc_MemoryError,
248248
"Failed to deallocate string in unicode to string cast");
249249
goto fail;
250250
}
251-
if (npy_string_newemptysize(out_num_bytes, out_pss, allocator) < 0) {
251+
if (NpyString_newemptysize(out_num_bytes, out_pss, allocator) < 0) {
252252
gil_error(PyExc_MemoryError,
253253
"Failed to allocate string in unicode to string cast");
254254
goto fail;
255255
}
256256
npy_static_string out_ss = {0, NULL};
257-
int is_null = npy_string_load(allocator, out_pss, &out_ss);
257+
int is_null = NpyString_load(allocator, out_pss, &out_ss);
258258
if (is_null == -1) {
259259
gil_error(PyExc_MemoryError,
260260
"Failed to load string in unicode to string cast");
@@ -397,7 +397,7 @@ string_to_unicode(PyArrayMethod_Context *context, char *const data[],
397397
npy_static_string name = {0, NULL};
398398
unsigned char *this_string = NULL;
399399
size_t n_bytes;
400-
int is_null = npy_string_load(allocator, ps, &s);
400+
int is_null = NpyString_load(allocator, ps, &s);
401401
if (is_null == -1) {
402402
gil_error(PyExc_MemoryError,
403403
"Failed to load string in unicode to string cast");
@@ -506,7 +506,7 @@ string_to_bool(PyArrayMethod_Context *context, char *const data[],
506506
while (N--) {
507507
const npy_packed_static_string *ps = (npy_packed_static_string *)in;
508508
npy_static_string s = {0, NULL};
509-
int is_null = npy_string_load(allocator, ps, &s);
509+
int is_null = NpyString_load(allocator, ps, &s);
510510
if (is_null == -1) {
511511
gil_error(PyExc_MemoryError,
512512
"Failed to load string in unicode to string cast");
@@ -570,7 +570,7 @@ bool_to_string(PyArrayMethod_Context *context, char *const data[],
570570

571571
while (N--) {
572572
npy_packed_static_string *out_pss = (npy_packed_static_string *)out;
573-
if (npy_string_free(out_pss, allocator) < 0) {
573+
if (NpyString_free(out_pss, allocator) < 0) {
574574
gil_error(PyExc_MemoryError,
575575
"Failed to deallocate string in bool to string cast");
576576
goto fail;
@@ -590,7 +590,7 @@ bool_to_string(PyArrayMethod_Context *context, char *const data[],
590590
"invalid value encountered in bool to string cast");
591591
goto fail;
592592
}
593-
if (npy_string_newsize(ret_val, size, out_pss, allocator) < 0) {
593+
if (NpyString_newsize(ret_val, size, out_pss, allocator) < 0) {
594594
// execution should never get here because this will be a small
595595
// string on all platforms
596596
gil_error(PyExc_MemoryError,
@@ -628,7 +628,7 @@ string_to_pylong(char *in, int hasnull,
628628
{
629629
const npy_packed_static_string *ps = (npy_packed_static_string *)in;
630630
npy_static_string s = {0, NULL};
631-
int isnull = npy_string_load(allocator, ps, &s);
631+
int isnull = NpyString_load(allocator, ps, &s);
632632
if (isnull == -1) {
633633
PyErr_SetString(PyExc_MemoryError,
634634
"Failed to load string converting string to int");
@@ -709,13 +709,13 @@ pyobj_to_string(PyObject *obj, char *out, npy_string_allocator *allocator)
709709
return -1;
710710
}
711711
npy_packed_static_string *out_ss = (npy_packed_static_string *)out;
712-
if (npy_string_free(out_ss, allocator) < 0) {
712+
if (NpyString_free(out_ss, allocator) < 0) {
713713
gil_error(PyExc_MemoryError,
714714
"Failed to deallocate string when converting from python "
715715
"string");
716716
return -1;
717717
}
718-
if (npy_string_newsize(cstr_val, length, out_ss, allocator) < 0) {
718+
if (NpyString_newsize(cstr_val, length, out_ss, allocator) < 0) {
719719
PyErr_SetString(PyExc_MemoryError,
720720
"Failed to allocate numpy string when converting from "
721721
"python string.");
@@ -925,7 +925,7 @@ string_to_pyfloat(char *in, int hasnull,
925925
{
926926
const npy_packed_static_string *ps = (npy_packed_static_string *)in;
927927
npy_static_string s = {0, NULL};
928-
int isnull = npy_string_load(allocator, ps, &s);
928+
int isnull = NpyString_load(allocator, ps, &s);
929929
if (isnull == -1) {
930930
PyErr_SetString(
931931
PyExc_MemoryError,
@@ -1186,7 +1186,7 @@ string_to_datetime(PyArrayMethod_Context *context, char *const data[],
11861186
while (N--) {
11871187
const npy_packed_static_string *ps = (npy_packed_static_string *)in;
11881188
npy_static_string s = {0, NULL};
1189-
int is_null = npy_string_load(allocator, ps, &s);
1189+
int is_null = NpyString_load(allocator, ps, &s);
11901190
if (is_null == -1) {
11911191
// do we hold the gil in this cast? error handling below seems to
11921192
// think we do
@@ -1260,7 +1260,7 @@ datetime_to_string(PyArrayMethod_Context *context, char *const data[],
12601260

12611261
while (N--) {
12621262
npy_packed_static_string *out_pss = (npy_packed_static_string *)out;
1263-
if (npy_string_free(out_pss, allocator) < 0) {
1263+
if (NpyString_free(out_pss, allocator) < 0) {
12641264
gil_error(
12651265
PyExc_MemoryError,
12661266
"Failed to deallocate string in datetime to string cast");
@@ -1284,8 +1284,8 @@ datetime_to_string(PyArrayMethod_Context *context, char *const data[],
12841284
goto fail;
12851285
}
12861286

1287-
if (npy_string_newsize(datetime_buf, strlen(datetime_buf), out_pss,
1288-
allocator) < 0) {
1287+
if (NpyString_newsize(datetime_buf, strlen(datetime_buf), out_pss,
1288+
allocator) < 0) {
12891289
PyErr_SetString(PyExc_MemoryError,
12901290
"Failed to allocate string when converting "
12911291
"from a datetime.");

stringdtype/stringdtype/src/dtype.c

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ new_stringdtype_instance(PyObject *na_object, int coerce)
2323
return NULL;
2424
}
2525

26-
allocator = npy_string_new_allocator(PyMem_RawMalloc, PyMem_RawFree,
27-
PyMem_RawRealloc);
26+
allocator = NpyString_new_allocator(PyMem_RawMalloc, PyMem_RawFree,
27+
PyMem_RawRealloc);
2828

2929
if (allocator == NULL) {
3030
PyErr_SetString(PyExc_MemoryError,
@@ -49,8 +49,8 @@ new_stringdtype_instance(PyObject *na_object, int coerce)
4949
has_string_na = 1;
5050
Py_ssize_t size = 0;
5151
const char *buf = PyUnicode_AsUTF8AndSize(na_object, &size);
52-
if (npy_string_newsize(buf, (size_t)size, &packed_default_string,
53-
allocator) < 0) {
52+
if (NpyString_newsize(buf, (size_t)size, &packed_default_string,
53+
allocator) < 0) {
5454
PyErr_SetString(PyExc_MemoryError,
5555
"Failed to allocate string while creating "
5656
"StringDType instance.");
@@ -86,8 +86,8 @@ new_stringdtype_instance(PyObject *na_object, int coerce)
8686
Py_DECREF(na_pystr);
8787
goto fail;
8888
}
89-
if (npy_string_newsize(utf8_ptr, (size_t)size, &packed_na_name,
90-
allocator) < 0) {
89+
if (NpyString_newsize(utf8_ptr, (size_t)size, &packed_na_name,
90+
allocator) < 0) {
9191
PyErr_SetString(PyExc_MemoryError,
9292
"Failed to allocate string while creating "
9393
"StringDType instance.");
@@ -109,8 +109,8 @@ new_stringdtype_instance(PyObject *na_object, int coerce)
109109
snew->array_owned = 0;
110110

111111
npy_static_string default_string = {0, NULL};
112-
if (npy_string_load(allocator, &snew->packed_default_string,
113-
&default_string) == -1) {
112+
if (NpyString_load(allocator, &snew->packed_default_string,
113+
&default_string) == -1) {
114114
PyErr_SetString(PyExc_MemoryError,
115115
"Failed to load packed string while "
116116
"creating StringDType instance.");
@@ -119,7 +119,7 @@ new_stringdtype_instance(PyObject *na_object, int coerce)
119119
}
120120

121121
npy_static_string na_name = {0, NULL};
122-
if (npy_string_load(allocator, &snew->packed_na_name, &na_name) == -1) {
122+
if (NpyString_load(allocator, &snew->packed_na_name, &na_name) == -1) {
123123
PyErr_SetString(PyExc_MemoryError,
124124
"Failed to load packed string while "
125125
"creating StringDType instance.");
@@ -150,9 +150,9 @@ new_stringdtype_instance(PyObject *na_object, int coerce)
150150
// this only makes sense if the allocator isn't attached to new yet
151151
Py_DECREF(new);
152152
if (allocator != NULL) {
153-
npy_string_free(&packed_na_name, allocator);
154-
npy_string_free(&packed_default_string, allocator);
155-
npy_string_free_allocator(allocator);
153+
NpyString_free(&packed_na_name, allocator);
154+
NpyString_free(&packed_default_string, allocator);
155+
NpyString_free_allocator(allocator);
156156
}
157157
if (allocator_lock != NULL) {
158158
PyThread_free_lock(allocator_lock);
@@ -291,7 +291,7 @@ stringdtype_setitem(StringDTypeObject *descr, PyObject *obj, char **dataptr)
291291

292292
// free if dataptr holds preexisting string data,
293293
// npy_string_free does a NULL check and checks for small strings
294-
if (npy_string_free(sdata, descr->allocator) < 0) {
294+
if (NpyString_free(sdata, descr->allocator) < 0) {
295295
PyErr_SetString(PyExc_MemoryError,
296296
"String deallocation failed in StringDType setitem");
297297
goto fail;
@@ -319,7 +319,7 @@ stringdtype_setitem(StringDTypeObject *descr, PyObject *obj, char **dataptr)
319319
goto fail;
320320
}
321321

322-
if (npy_string_newsize(val, length, sdata, descr->allocator) < 0) {
322+
if (NpyString_newsize(val, length, sdata, descr->allocator) < 0) {
323323
PyErr_SetString(PyExc_MemoryError,
324324
"Failed to allocate string during StringDType "
325325
"setitem");
@@ -347,7 +347,7 @@ stringdtype_getitem(StringDTypeObject *descr, char **dataptr)
347347
npy_static_string sdata = {0, NULL};
348348
int hasnull = descr->na_object != NULL;
349349
NPY_STRING_ACQUIRE_ALLOCATOR(descr);
350-
int is_null = npy_string_load(descr->allocator, psdata, &sdata);
350+
int is_null = NpyString_load(descr->allocator, psdata, &sdata);
351351

352352
if (is_null < 0) {
353353
PyErr_SetString(PyExc_MemoryError,
@@ -399,7 +399,7 @@ stringdtype_getitem(StringDTypeObject *descr, char **dataptr)
399399
npy_bool
400400
nonzero(void *data, void *NPY_UNUSED(arr))
401401
{
402-
return npy_string_size((npy_packed_static_string *)data) != 0;
402+
return NpyString_size((npy_packed_static_string *)data) != 0;
403403
}
404404

405405
// Implementation of PyArray_CompareFunc.
@@ -429,10 +429,10 @@ _compare(void *a, void *b, StringDTypeObject *descr_a,
429429
npy_static_string *default_string = &descr_a->default_string;
430430
const npy_packed_static_string *ps_a = (npy_packed_static_string *)a;
431431
npy_static_string s_a = {0, NULL};
432-
int a_is_null = npy_string_load(allocator_a, ps_a, &s_a);
432+
int a_is_null = NpyString_load(allocator_a, ps_a, &s_a);
433433
const npy_packed_static_string *ps_b = (npy_packed_static_string *)b;
434434
npy_static_string s_b = {0, NULL};
435-
int b_is_null = npy_string_load(allocator_b, ps_b, &s_b);
435+
int b_is_null = NpyString_load(allocator_b, ps_b, &s_b);
436436
if (NPY_UNLIKELY(a_is_null == -1 || b_is_null == -1)) {
437437
char *msg = "Failed to load string in string comparison";
438438
if (hasnull && !(has_string_na && has_nan_na)) {
@@ -476,7 +476,7 @@ _compare(void *a, void *b, StringDTypeObject *descr_a,
476476
}
477477
}
478478
}
479-
return npy_string_cmp(&s_a, &s_b);
479+
return NpyString_cmp(&s_a, &s_b);
480480
}
481481

482482
// PyArray_ArgFunc
@@ -526,7 +526,7 @@ stringdtype_clear_loop(void *NPY_UNUSED(traverse_context),
526526
while (size--) {
527527
npy_packed_static_string *sdata = (npy_packed_static_string *)data;
528528
if (data != NULL) {
529-
if (npy_string_free(sdata, sdescr->allocator) < 0) {
529+
if (NpyString_free(sdata, sdescr->allocator) < 0) {
530530
gil_error(PyExc_MemoryError,
531531
"String deallocation failed in clear loop");
532532
goto fail;
@@ -703,9 +703,9 @@ stringdtype_dealloc(StringDTypeObject *self)
703703
if (self->allocator != NULL) {
704704
// can we assume the destructor for an instance will only get called
705705
// inside of one C thread?
706-
npy_string_free(&self->packed_default_string, self->allocator);
707-
npy_string_free(&self->packed_na_name, self->allocator);
708-
npy_string_free_allocator(self->allocator);
706+
NpyString_free(&self->packed_default_string, self->allocator);
707+
NpyString_free(&self->packed_na_name, self->allocator);
708+
NpyString_free_allocator(self->allocator);
709709
PyThread_free_lock(self->allocator_lock);
710710
}
711711
PyArrayDescr_Type.tp_dealloc((PyObject *)self);
@@ -958,14 +958,14 @@ free_and_copy(npy_string_allocator *in_allocator,
958958
const npy_packed_static_string *in,
959959
npy_packed_static_string *out, const char *location)
960960
{
961-
if (npy_string_free(out, out_allocator) < 0) {
961+
if (NpyString_free(out, out_allocator) < 0) {
962962
char message[200];
963963
snprintf(message, sizeof(message), "Failed to deallocate string in %s",
964964
location);
965965
gil_error(PyExc_MemoryError, message);
966966
return -1;
967967
}
968-
if (npy_string_dup(in, out, in_allocator, out_allocator) < 0) {
968+
if (NpyString_dup(in, out, in_allocator, out_allocator) < 0) {
969969
char message[200];
970970
snprintf(message, sizeof(message), "Failed to allocate string in %s",
971971
location);

stringdtype/stringdtype/src/main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ _memory_usage(PyObject *NPY_UNUSED(self), PyObject *obj)
5858
npy_intp count = *innersizeptr;
5959

6060
while (count--) {
61-
size_t size = npy_string_size(((npy_packed_static_string *)in));
61+
size_t size = NpyString_size(((npy_packed_static_string *)in));
6262
// FIXME: add a way for a string to report its heap size usage
6363
if (size > (sizeof(npy_static_string) - 1)) {
6464
memory_usage += size;

0 commit comments

Comments
 (0)