-
-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathcasts.c
More file actions
1510 lines (1323 loc) · 60.4 KB
/
Copy pathcasts.c
File metadata and controls
1510 lines (1323 loc) · 60.4 KB
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <Python.h>
#define PY_ARRAY_UNIQUE_SYMBOL stringdtype_ARRAY_API
#define PY_UFUNC_UNIQUE_SYMBOL stringdtype_UFUNC_API
#define NPY_NO_DEPRECATED_API NPY_2_0_API_VERSION
#define NPY_TARGET_VERSION NPY_2_0_API_VERSION
#define NO_IMPORT_ARRAY
#define NO_IMPORT_UFUNC
#include "numpy/ndarraytypes.h"
#include "numpy/arrayobject.h"
#include "numpy/ufuncobject.h"
#include "numpy/dtype_api.h"
#include "numpy/halffloat.h"
#include "numpy/npy_math.h"
#include "casts.h"
#include "dtype.h"
#include "static_string.h"
#define ANY_TO_STRING_RESOLVE_DESCRIPTORS(safety) \
static NPY_CASTING any_to_string_##safety##_resolve_descriptors( \
PyObject *NPY_UNUSED(self), \
PyArray_DTypeMeta *NPY_UNUSED(dtypes[2]), \
PyArray_Descr *given_descrs[2], PyArray_Descr *loop_descrs[2], \
npy_intp *NPY_UNUSED(view_offset)) \
{ \
if (given_descrs[1] == NULL) { \
PyArray_Descr *new = \
(PyArray_Descr *)new_stringdtype_instance(NULL, 1); \
if (new == NULL) { \
return (NPY_CASTING)-1; \
} \
loop_descrs[1] = new; \
} \
else { \
Py_INCREF(given_descrs[1]); \
loop_descrs[1] = given_descrs[1]; \
} \
\
Py_INCREF(given_descrs[0]); \
loop_descrs[0] = given_descrs[0]; \
\
return NPY_##safety##_CASTING; \
}
ANY_TO_STRING_RESOLVE_DESCRIPTORS(SAFE)
ANY_TO_STRING_RESOLVE_DESCRIPTORS(UNSAFE)
// string to string
static NPY_CASTING
string_to_string_resolve_descriptors(PyObject *NPY_UNUSED(self),
PyArray_DTypeMeta *NPY_UNUSED(dtypes[2]),
PyArray_Descr *given_descrs[2],
PyArray_Descr *loop_descrs[2],
npy_intp *view_offset)
{
if (given_descrs[1] == NULL) {
loop_descrs[1] = stringdtype_finalize_descr(given_descrs[0]);
}
else {
Py_INCREF(given_descrs[1]);
loop_descrs[1] = given_descrs[1];
}
Py_INCREF(given_descrs[0]);
loop_descrs[0] = given_descrs[0];
StringDTypeObject *descr0 = (StringDTypeObject *)loop_descrs[0];
StringDTypeObject *descr1 = (StringDTypeObject *)loop_descrs[1];
if ((descr0->na_object != NULL) && (descr1->na_object == NULL)) {
// cast from a dtype with an NA to one without, so it's a lossy
// unsafe cast
return NPY_UNSAFE_CASTING;
}
*view_offset = 0;
return NPY_NO_CASTING;
}
static int
string_to_string(PyArrayMethod_Context *context, char *const data[],
npy_intp const dimensions[], npy_intp const strides[],
NpyAuxData *NPY_UNUSED(auxdata))
{
StringDTypeObject *idescr = (StringDTypeObject *)context->descriptors[0];
StringDTypeObject *odescr = (StringDTypeObject *)context->descriptors[1];
int in_hasnull = idescr->na_object != NULL;
int out_hasnull = odescr->na_object != NULL;
const _npy_static_string *in_na_name = &idescr->na_name;
npy_intp N = dimensions[0];
char *in = data[0];
char *out = data[1];
npy_intp in_stride = strides[0];
npy_intp out_stride = strides[1];
npy_string_allocator *iallocator = NULL;
npy_string_allocator *oallocator = NULL;
_NpyString_acquire_allocator2(idescr, odescr, &iallocator, &oallocator);
while (N--) {
const npy_packed_static_string *s = (npy_packed_static_string *)in;
npy_packed_static_string *os = (npy_packed_static_string *)out;
if (in != out) {
if (in_hasnull && !out_hasnull && _NpyString_isnull(s)) {
// lossy but this is an unsafe cast so this is OK
if (_NpyString_pack(odescr->allocator, os, in_na_name->buf,
in_na_name->size) < 0) {
gil_error(PyExc_MemoryError,
"Failed to pack string in string to string "
"cast.");
goto fail;
}
}
else if (free_and_copy(idescr->allocator, odescr->allocator, s, os,
"string to string cast") == -1) {
goto fail;
}
}
in += in_stride;
out += out_stride;
}
_NpyString_release_allocator2(odescr, idescr);
return 0;
fail:
_NpyString_release_allocator2(odescr, idescr);
return -1;
}
static PyType_Slot s2s_slots[] = {
{NPY_METH_resolve_descriptors, &string_to_string_resolve_descriptors},
{NPY_METH_strided_loop, &string_to_string},
{NPY_METH_unaligned_strided_loop, &string_to_string},
{0, NULL}};
static char *s2s_name = "cast_StringDType_to_StringDType";
// unicode to string
// Find the number of bytes, *utf8_bytes*, needed to store the string
// represented by *codepoints* in UTF-8. The array of *codepoints* is
// *max_length* long, but may be padded with null codepoints. *num_codepoints*
// is the number of codepoints that are not trailing null codepoints. Returns
// 0 on success and -1 when an invalid code point is found.
static int
utf8_size(const Py_UCS4 *codepoints, long max_length, size_t *num_codepoints,
size_t *utf8_bytes)
{
size_t ucs4len = max_length;
while (ucs4len > 0 && codepoints[ucs4len - 1] == 0) {
ucs4len--;
}
// ucs4len is now the number of codepoints that aren't trailing nulls.
size_t num_bytes = 0;
for (size_t i = 0; i < ucs4len; i++) {
Py_UCS4 code = codepoints[i];
if (code <= 0x7F) {
num_bytes += 1;
}
else if (code <= 0x07FF) {
num_bytes += 2;
}
else if (code <= 0xFFFF) {
if ((code >= 0xD800) && (code <= 0xDFFF)) {
// surrogates are invalid UCS4 code points
return -1;
}
num_bytes += 3;
}
else if (code <= 0x10FFFF) {
num_bytes += 4;
}
else {
// codepoint is outside the valid unicode range
return -1;
}
}
*num_codepoints = ucs4len;
*utf8_bytes = num_bytes;
return 0;
}
// Converts UCS4 code point *code* to 4-byte character array *c*. Assumes *c*
// is a zero-filled 4 byte array and *code* is a valid codepoint and does not
// do any error checking! Returns the number of bytes in the UTF-8 character.
static size_t
ucs4_code_to_utf8_char(const Py_UCS4 code, char *c)
{
if (code <= 0x7F) {
// 0zzzzzzz -> 0zzzzzzz
c[0] = (char)code;
return 1;
}
else if (code <= 0x07FF) {
// 00000yyy yyzzzzzz -> 110yyyyy 10zzzzzz
c[0] = (0xC0 | (code >> 6));
c[1] = (0x80 | (code & 0x3F));
return 2;
}
else if (code <= 0xFFFF) {
// xxxxyyyy yyzzzzzz -> 110yyyyy 10zzzzzz
c[0] = (0xe0 | (code >> 12));
c[1] = (0x80 | ((code >> 6) & 0x3f));
c[2] = (0x80 | (code & 0x3f));
return 3;
}
else {
// 00wwwxx xxxxyyyy yyzzzzzz -> 11110www 10xxxxxx 10yyyyyy 10zzzzzz
c[0] = (0xf0 | (code >> 18));
c[1] = (0x80 | ((code >> 12) & 0x3f));
c[2] = (0x80 | ((code >> 6) & 0x3f));
c[3] = (0x80 | (code & 0x3f));
return 4;
}
}
static int
unicode_to_string(PyArrayMethod_Context *context, char *const data[],
npy_intp const dimensions[], npy_intp const strides[],
NpyAuxData *NPY_UNUSED(auxdata))
{
PyArray_Descr *const *descrs = context->descriptors;
StringDTypeObject *sdescr = (StringDTypeObject *)descrs[1];
npy_string_allocator *allocator = _NpyString_acquire_allocator(sdescr);
long max_in_size = (descrs[0]->elsize) / 4;
npy_intp N = dimensions[0];
Py_UCS4 *in = (Py_UCS4 *)data[0];
char *out = data[1];
// 4 bytes per UCS4 character
npy_intp in_stride = strides[0] / 4;
npy_intp out_stride = strides[1];
while (N--) {
size_t out_num_bytes = 0;
size_t num_codepoints = 0;
if (utf8_size(in, max_in_size, &num_codepoints, &out_num_bytes) ==
-1) {
gil_error(PyExc_TypeError, "Invalid unicode code point found");
goto fail;
}
npy_packed_static_string *out_pss = (npy_packed_static_string *)out;
if (_NpyString_free(out_pss, allocator) < 0) {
gil_error(PyExc_MemoryError,
"Failed to deallocate string in unicode to string cast");
goto fail;
}
if (_NpyString_newemptysize(out_num_bytes, out_pss, allocator) < 0) {
gil_error(PyExc_MemoryError,
"Failed to allocate string in unicode to string cast");
goto fail;
}
_npy_static_string out_ss = {0, NULL};
int is_null = _NpyString_load(allocator, out_pss, &out_ss);
if (is_null == -1) {
gil_error(PyExc_MemoryError,
"Failed to load string in unicode to string cast");
goto fail;
}
// ignores const to fill in the buffer
char *out_buf = (char *)out_ss.buf;
for (size_t i = 0; i < num_codepoints; i++) {
// get code point
Py_UCS4 code = in[i];
// will be filled with UTF-8 bytes
char utf8_c[4] = {0};
// we already checked for invalid code points above,
// so no need to do error checking here
size_t num_bytes = ucs4_code_to_utf8_char(code, utf8_c);
// copy utf8_c into out_buf
strncpy(out_buf, utf8_c, num_bytes);
// increment out_buf by the size of the character
out_buf += num_bytes;
}
// reset out_buf to the beginning of the string
out_buf -= out_num_bytes;
in += in_stride;
out += out_stride;
}
_NpyString_release_allocator(sdescr);
return 0;
fail:
_NpyString_release_allocator(sdescr);
return -1;
}
static PyType_Slot u2s_slots[] = {{NPY_METH_resolve_descriptors,
&any_to_string_SAFE_resolve_descriptors},
{NPY_METH_strided_loop, &unicode_to_string},
{0, NULL}};
static char *u2s_name = "cast_Unicode_to_StringDType";
// string to unicode
static NPY_CASTING
string_to_unicode_resolve_descriptors(PyObject *NPY_UNUSED(self),
PyArray_DTypeMeta *NPY_UNUSED(dtypes[2]),
PyArray_Descr *given_descrs[2],
PyArray_Descr *loop_descrs[2],
npy_intp *NPY_UNUSED(view_offset))
{
if (given_descrs[1] == NULL) {
// currently there's no way to determine the correct output
// size, so set an error and bail
PyErr_SetString(
PyExc_TypeError,
"Casting from StringDType to a fixed-width dtype with an "
"unspecified size is not currently supported, specify "
"an explicit size for the output dtype instead.");
return (NPY_CASTING)-1;
}
else {
Py_INCREF(given_descrs[1]);
loop_descrs[1] = given_descrs[1];
}
Py_INCREF(given_descrs[0]);
loop_descrs[0] = given_descrs[0];
return NPY_UNSAFE_CASTING;
}
// Given UTF-8 bytes in *c*, sets *code* to the corresponding unicode
// codepoint for the next character, returning the size of the character in
// bytes. Does not do any validation or error checking: assumes *c* is valid
// utf-8
size_t
utf8_char_to_ucs4_code(unsigned char *c, size_t len, Py_UCS4 *code)
{
if (len == 0) {
*code = (Py_UCS4)0;
return 0;
}
if (c[0] <= 0x7F) {
// 0zzzzzzz -> 0zzzzzzz
*code = (Py_UCS4)(c[0]);
return 1;
}
else if (c[0] <= 0xDF) {
// 110yyyyy 10zzzzzz -> 00000yyy yyzzzzzz
*code = (Py_UCS4)(((c[0] << 6) + c[1]) - ((0xC0 << 6) + 0x80));
return 2;
}
else if (c[0] <= 0xEF) {
// 1110xxxx 10yyyyyy 10zzzzzz -> xxxxyyyy yyzzzzzz
*code = (Py_UCS4)(((c[0] << 12) + (c[1] << 6) + c[2]) -
((0xE0 << 12) + (0x80 << 6) + 0x80));
return 3;
}
else {
// 11110www 10xxxxxx 10yyyyyy 10zzzzzz -> 000wwwxx xxxxyyyy yyzzzzzz
*code = (Py_UCS4)(((c[0] << 18) + (c[1] << 12) + (c[2] << 6) + c[3]) -
((0xF0 << 18) + (0x80 << 12) + (0x80 << 6) + 0x80));
return 4;
}
}
static int
string_to_unicode(PyArrayMethod_Context *context, char *const data[],
npy_intp const dimensions[], npy_intp const strides[],
NpyAuxData *NPY_UNUSED(auxdata))
{
StringDTypeObject *descr = (StringDTypeObject *)context->descriptors[0];
npy_string_allocator *allocator = _NpyString_acquire_allocator(descr);
int has_null = descr->na_object != NULL;
int has_string_na = descr->has_string_na;
const _npy_static_string *default_string = &descr->default_string;
const _npy_static_string *na_name = &descr->na_name;
npy_intp N = dimensions[0];
char *in = data[0];
Py_UCS4 *out = (Py_UCS4 *)data[1];
npy_intp in_stride = strides[0];
// 4 bytes per UCS4 character
npy_intp out_stride = strides[1] / 4;
// max number of 4 byte UCS4 characters that can fit in the output
long max_out_size = (context->descriptors[1]->elsize) / 4;
while (N--) {
const npy_packed_static_string *ps = (npy_packed_static_string *)in;
_npy_static_string s = {0, NULL};
_npy_static_string name = {0, NULL};
unsigned char *this_string = NULL;
size_t n_bytes;
int is_null = _NpyString_load(allocator, ps, &s);
if (is_null == -1) {
gil_error(PyExc_MemoryError,
"Failed to load string in unicode to string cast");
goto fail;
}
else if (is_null) {
if (has_null && !has_string_na) {
// lossy but not much else we can do
name = *na_name;
}
else {
name = *default_string;
}
}
else {
name = s;
}
this_string = (unsigned char *)(name.buf);
n_bytes = name.size;
size_t tot_n_bytes = 0;
for (int i = 0; i < max_out_size; i++) {
Py_UCS4 code;
// code point for character this_string is currently pointing at
size_t num_bytes =
utf8_char_to_ucs4_code(this_string, n_bytes, &code);
// move to next character
this_string += num_bytes;
tot_n_bytes += num_bytes;
// set output codepoint
out[i] = code;
// stop if we've exhausted the input string
if (tot_n_bytes >= n_bytes) {
break;
}
}
in += in_stride;
out += out_stride;
}
_NpyString_release_allocator(descr);
return 0;
fail:
_NpyString_release_allocator(descr);
return -1;
}
static PyType_Slot s2u_slots[] = {
{NPY_METH_resolve_descriptors, &string_to_unicode_resolve_descriptors},
{NPY_METH_strided_loop, &string_to_unicode},
{0, NULL}};
static char *s2u_name = "cast_StringDType_to_Unicode";
// string to bool
static NPY_CASTING
string_to_bool_resolve_descriptors(PyObject *NPY_UNUSED(self),
PyArray_DTypeMeta *NPY_UNUSED(dtypes[2]),
PyArray_Descr *given_descrs[2],
PyArray_Descr *loop_descrs[2],
npy_intp *NPY_UNUSED(view_offset))
{
if (given_descrs[1] == NULL) {
loop_descrs[1] = PyArray_DescrNewFromType(NPY_BOOL);
}
else {
Py_INCREF(given_descrs[1]);
loop_descrs[1] = given_descrs[1];
}
Py_INCREF(given_descrs[0]);
loop_descrs[0] = given_descrs[0];
return NPY_UNSAFE_CASTING;
}
static int
string_to_bool(PyArrayMethod_Context *context, char *const data[],
npy_intp const dimensions[], npy_intp const strides[],
NpyAuxData *NPY_UNUSED(auxdata))
{
StringDTypeObject *descr = (StringDTypeObject *)context->descriptors[0];
npy_string_allocator *allocator = _NpyString_acquire_allocator(descr);
int has_null = descr->na_object != NULL;
int has_string_na = descr->has_string_na;
const _npy_static_string *default_string = &descr->default_string;
npy_intp N = dimensions[0];
char *in = data[0];
char *out = data[1];
npy_intp in_stride = strides[0];
npy_intp out_stride = strides[1];
while (N--) {
const npy_packed_static_string *ps = (npy_packed_static_string *)in;
_npy_static_string s = {0, NULL};
int is_null = _NpyString_load(allocator, ps, &s);
if (is_null == -1) {
gil_error(PyExc_MemoryError,
"Failed to load string in unicode to string cast");
goto fail;
}
else if (is_null) {
if (has_null && !has_string_na) {
// numpy treats NaN as truthy, following python
*out = (npy_bool)1;
}
else {
*out = (npy_bool)(default_string->size == 0);
}
}
else if (s.size == 0) {
*out = (npy_bool)0;
}
else {
*out = (npy_bool)1;
}
in += in_stride;
out += out_stride;
}
_NpyString_release_allocator(descr);
return 0;
fail:
_NpyString_release_allocator(descr);
return -1;
}
static PyType_Slot s2b_slots[] = {
{NPY_METH_resolve_descriptors, &string_to_bool_resolve_descriptors},
{NPY_METH_strided_loop, &string_to_bool},
{0, NULL}};
static char *s2b_name = "cast_StringDType_to_Bool";
// bool to string
static int
bool_to_string(PyArrayMethod_Context *context, char *const data[],
npy_intp const dimensions[], npy_intp const strides[],
NpyAuxData *NPY_UNUSED(auxdata))
{
npy_intp N = dimensions[0];
char *in = data[0];
char *out = data[1];
npy_intp in_stride = strides[0];
npy_intp out_stride = strides[1];
StringDTypeObject *descr = (StringDTypeObject *)context->descriptors[1];
npy_string_allocator *allocator = _NpyString_acquire_allocator(descr);
while (N--) {
npy_packed_static_string *out_pss = (npy_packed_static_string *)out;
char *ret_val = NULL;
size_t size = 0;
if ((npy_bool)(*in) == 1) {
ret_val = "True";
size = 4;
}
else if ((npy_bool)(*in) == 0) {
ret_val = "False";
size = 5;
}
else {
gil_error(PyExc_RuntimeError,
"invalid value encountered in bool to string cast");
goto fail;
}
if (_NpyString_pack(allocator, out_pss, ret_val, size) < 0) {
gil_error(PyExc_MemoryError,
"Failed to pack string in bool to string cast");
goto fail;
}
in += in_stride;
out += out_stride;
}
_NpyString_release_allocator(descr);
return 0;
fail:
_NpyString_release_allocator(descr);
return -1;
}
static PyType_Slot b2s_slots[] = {{NPY_METH_resolve_descriptors,
&any_to_string_SAFE_resolve_descriptors},
{NPY_METH_strided_loop, &bool_to_string},
{0, NULL}};
static char *b2s_name = "cast_Bool_to_StringDType";
// casts between string and (u)int dtypes
static PyObject *
string_to_pylong(char *in, int hasnull,
const _npy_static_string *default_string,
npy_string_allocator *allocator)
{
const npy_packed_static_string *ps = (npy_packed_static_string *)in;
_npy_static_string s = {0, NULL};
int isnull = _NpyString_load(allocator, ps, &s);
if (isnull == -1) {
PyErr_SetString(PyExc_MemoryError,
"Failed to load string converting string to int");
return NULL;
}
else if (isnull) {
if (hasnull) {
PyErr_SetString(PyExc_ValueError,
"Arrays with missing data cannot be converted to "
"integers");
return NULL;
}
s = *default_string;
}
PyObject *val_obj = PyUnicode_FromStringAndSize(s.buf, s.size);
if (val_obj == NULL) {
return NULL;
}
// interpret as an integer in base 10
PyObject *pylong_value = PyLong_FromUnicodeObject(val_obj, 10);
Py_DECREF(val_obj);
return pylong_value;
}
static npy_longlong
string_to_uint(char *in, npy_ulonglong *value, int hasnull,
const _npy_static_string *default_string,
npy_string_allocator *allocator)
{
PyObject *pylong_value =
string_to_pylong(in, hasnull, default_string, allocator);
if (pylong_value == NULL) {
return -1;
}
*value = PyLong_AsUnsignedLongLong(pylong_value);
if (*value == (unsigned long long)-1 && PyErr_Occurred()) {
Py_DECREF(pylong_value);
return -1;
}
Py_DECREF(pylong_value);
return 0;
}
static npy_longlong
string_to_int(char *in, npy_longlong *value, int hasnull,
const _npy_static_string *default_string,
npy_string_allocator *allocator)
{
PyObject *pylong_value =
string_to_pylong(in, hasnull, default_string, allocator);
if (pylong_value == NULL) {
return -1;
}
*value = PyLong_AsLongLong(pylong_value);
if (*value == -1 && PyErr_Occurred()) {
Py_DECREF(pylong_value);
return -1;
}
Py_DECREF(pylong_value);
return 0;
}
static int
pyobj_to_string(PyObject *obj, char *out, npy_string_allocator *allocator)
{
if (obj == NULL) {
return -1;
}
PyObject *pystr_val = PyObject_Str(obj);
Py_DECREF(obj);
if (pystr_val == NULL) {
return -1;
}
Py_ssize_t length;
const char *cstr_val = PyUnicode_AsUTF8AndSize(pystr_val, &length);
if (cstr_val == NULL) {
Py_DECREF(pystr_val);
return -1;
}
npy_packed_static_string *out_ss = (npy_packed_static_string *)out;
if (_NpyString_pack(allocator, out_ss, cstr_val, length) < 0) {
gil_error(PyExc_MemoryError,
"Failed to pack string while converting from python "
"string");
Py_DECREF(pystr_val);
return -1;
}
// implicitly deallocates cstr_val as well
Py_DECREF(pystr_val);
return 0;
}
static int
int_to_string(long long in, char *out, npy_string_allocator *allocator)
{
PyObject *pylong_val = PyLong_FromLongLong(in);
return pyobj_to_string(pylong_val, out, allocator);
}
static int
uint_to_string(unsigned long long in, char *out,
npy_string_allocator *allocator)
{
PyObject *pylong_val = PyLong_FromUnsignedLongLong(in);
return pyobj_to_string(pylong_val, out, allocator);
}
#define STRING_INT_CASTS(typename, typekind, shortname, numpy_tag, \
printf_code, npy_longtype, longtype) \
static NPY_CASTING string_to_##typename##_resolve_descriptors( \
PyObject *NPY_UNUSED(self), \
PyArray_DTypeMeta *NPY_UNUSED(dtypes[2]), \
PyArray_Descr *given_descrs[2], PyArray_Descr *loop_descrs[2], \
npy_intp *NPY_UNUSED(view_offset)) \
{ \
if (given_descrs[1] == NULL) { \
loop_descrs[1] = PyArray_DescrNewFromType(numpy_tag); \
} \
else { \
Py_INCREF(given_descrs[1]); \
loop_descrs[1] = given_descrs[1]; \
} \
\
Py_INCREF(given_descrs[0]); \
loop_descrs[0] = given_descrs[0]; \
\
return NPY_UNSAFE_CASTING; \
} \
\
static int string_to_## \
typename(PyArrayMethod_Context * context, char *const data[], \
npy_intp const dimensions[], npy_intp const strides[], \
NpyAuxData *NPY_UNUSED(auxdata)) \
{ \
StringDTypeObject *descr = \
((StringDTypeObject *)context->descriptors[0]); \
npy_string_allocator *allocator = _NpyString_acquire_allocator(descr); \
int hasnull = descr->na_object != NULL; \
const _npy_static_string *default_string = &descr->default_string; \
\
npy_intp N = dimensions[0]; \
char *in = data[0]; \
npy_##typename *out = (npy_##typename *)data[1]; \
\
npy_intp in_stride = strides[0]; \
npy_intp out_stride = strides[1] / sizeof(npy_##typename); \
\
while (N--) { \
npy_longtype value; \
if (string_to_##typekind(in, &value, hasnull, default_string, \
allocator) != 0) { \
goto fail; \
} \
*out = (npy_##typename)value; \
if (*out != value) { \
/* out of bounds, raise error following NEP 50 behavior */ \
char message[200]; \
snprintf(message, sizeof(message), \
"Integer %" #printf_code \
" is out of bounds " \
"for " #typename, \
value); \
gil_error(PyExc_OverflowError, message); \
goto fail; \
} \
in += in_stride; \
out += out_stride; \
} \
\
_NpyString_release_allocator(descr); \
return 0; \
\
fail: \
_NpyString_release_allocator(descr); \
return -1; \
} \
\
static PyType_Slot s2##shortname##_slots[] = { \
{NPY_METH_resolve_descriptors, \
&string_to_##typename##_resolve_descriptors}, \
{NPY_METH_strided_loop, &string_to_##typename}, \
{0, NULL}}; \
\
static char *s2##shortname##_name = "cast_StringDType_to_" #typename; \
\
static int typename##_to_string( \
PyArrayMethod_Context *context, char *const data[], \
npy_intp const dimensions[], npy_intp const strides[], \
NpyAuxData *NPY_UNUSED(auxdata)) \
{ \
npy_intp N = dimensions[0]; \
npy_##typename *in = (npy_##typename *)data[0]; \
char *out = data[1]; \
\
npy_intp in_stride = strides[0] / sizeof(npy_##typename); \
npy_intp out_stride = strides[1]; \
\
StringDTypeObject *descr = \
(StringDTypeObject *)context->descriptors[1]; \
npy_string_allocator *allocator = _NpyString_acquire_allocator(descr); \
\
while (N--) { \
if (typekind##_to_string((longtype)*in, out, allocator) != 0) { \
goto fail; \
} \
\
in += in_stride; \
out += out_stride; \
} \
\
_NpyString_release_allocator(descr); \
return 0; \
\
fail: \
_NpyString_release_allocator(descr); \
return -1; \
} \
\
static PyType_Slot shortname##2s_slots [] = { \
{NPY_METH_resolve_descriptors, \
&any_to_string_UNSAFE_resolve_descriptors}, \
{NPY_METH_strided_loop, &typename##_to_string}, \
{0, NULL}}; \
\
static char *shortname##2s_name = "cast_" #typename "_to_StringDType";
#define DTYPES_AND_CAST_SPEC(shortname, typename) \
PyArray_DTypeMeta **s2##shortname##_dtypes = get_dtypes( \
(PyArray_DTypeMeta *)&StringDType, &PyArray_##typename##DType); \
\
PyArrayMethod_Spec *StringTo##typename##CastSpec = \
get_cast_spec(s2##shortname##_name, NPY_UNSAFE_CASTING, \
NPY_METH_REQUIRES_PYAPI, s2##shortname##_dtypes, \
s2##shortname##_slots); \
\
PyArray_DTypeMeta **shortname##2s_dtypes = get_dtypes( \
&PyArray_##typename##DType, (PyArray_DTypeMeta *)&StringDType); \
\
PyArrayMethod_Spec *typename##ToStringCastSpec = get_cast_spec( \
shortname##2s_name, NPY_UNSAFE_CASTING, NPY_METH_REQUIRES_PYAPI, \
shortname##2s_dtypes, shortname##2s_slots);
STRING_INT_CASTS(int8, int, i8, NPY_INT8, lli, npy_longlong, long long)
STRING_INT_CASTS(int16, int, i16, NPY_INT16, lli, npy_longlong, long long)
STRING_INT_CASTS(int32, int, i32, NPY_INT32, lli, npy_longlong, long long)
STRING_INT_CASTS(int64, int, i64, NPY_INT64, lli, npy_longlong, long long)
STRING_INT_CASTS(uint8, uint, u8, NPY_UINT8, llu, npy_ulonglong,
unsigned long long)
STRING_INT_CASTS(uint16, uint, u16, NPY_UINT16, llu, npy_ulonglong,
unsigned long long)
STRING_INT_CASTS(uint32, uint, u32, NPY_UINT32, llu, npy_ulonglong,
unsigned long long)
STRING_INT_CASTS(uint64, uint, u64, NPY_UINT64, llu, npy_ulonglong,
unsigned long long)
#if NPY_SIZEOF_BYTE == NPY_SIZEOF_SHORT
// byte doesn't have a bitsized alias
STRING_INT_CASTS(byte, int, byte, NPY_BYTE, lli, npy_longlong, long long)
STRING_INT_CASTS(ubyte, uint, ubyte, NPY_UBYTE, llu, npy_ulonglong,
unsigned long long)
#endif
#if NPY_SIZEOF_SHORT == NPY_SIZEOF_INT
// short doesn't have a bitsized alias
STRING_INT_CASTS(short, int, short, NPY_SHORT, lli, npy_longlong, long long)
STRING_INT_CASTS(ushort, uint, ushort, NPY_USHORT, llu, npy_ulonglong,
unsigned long long)
#endif
#if NPY_SIZEOF_INT == NPY_SIZEOF_LONG
// int doesn't have a bitsized alias
STRING_INT_CASTS(int, int, int, NPY_INT, lli, npy_longlong, long long)
STRING_INT_CASTS(uint, uint, uint, NPY_UINT, llu, npy_longlong, long long)
#endif
#if NPY_SIZEOF_LONGLONG == NPY_SIZEOF_LONG
// long long doesn't have a bitsized alias
STRING_INT_CASTS(longlong, int, longlong, NPY_LONGLONG, lli, npy_longlong,
long long)
STRING_INT_CASTS(ulonglong, uint, ulonglong, NPY_ULONGLONG, llu, npy_ulonglong,
unsigned long long)
#endif
static PyObject *
string_to_pyfloat(char *in, int hasnull,
const _npy_static_string *default_string,
npy_string_allocator *allocator)
{
const npy_packed_static_string *ps = (npy_packed_static_string *)in;
_npy_static_string s = {0, NULL};
int isnull = _NpyString_load(allocator, ps, &s);
if (isnull == -1) {
PyErr_SetString(
PyExc_MemoryError,
"Failed to load string while converting string to float");
return NULL;
}
if (isnull) {
if (hasnull) {
PyErr_SetString(PyExc_ValueError,
"Arrays with missing data cannot be converted to "
"integers");
return NULL;
}
s = *default_string;
}
PyObject *val_obj = PyUnicode_FromStringAndSize(s.buf, s.size);
if (val_obj == NULL) {
return NULL;
}
PyObject *pyfloat_value = PyFloat_FromString(val_obj);
Py_DECREF(val_obj);
return pyfloat_value;
}
#define STRING_TO_FLOAT_CAST(typename, shortname, isinf_name, \
double_to_float) \
static int string_to_## \
typename(PyArrayMethod_Context * context, char *const data[], \
npy_intp const dimensions[], npy_intp const strides[], \
NpyAuxData *NPY_UNUSED(auxdata)) \
{ \
StringDTypeObject *descr = \
(StringDTypeObject *)context->descriptors[0]; \
npy_string_allocator *allocator = _NpyString_acquire_allocator(descr); \
int hasnull = (descr->na_object != NULL); \
const _npy_static_string *default_string = &descr->default_string; \
\
npy_intp N = dimensions[0]; \
char *in = data[0]; \
npy_##typename *out = (npy_##typename *)data[1]; \
\
npy_intp in_stride = strides[0]; \
npy_intp out_stride = strides[1] / sizeof(npy_##typename); \
\
while (N--) { \
PyObject *pyfloat_value = string_to_pyfloat( \
in, hasnull, default_string, allocator); \
if (pyfloat_value == NULL) { \
goto fail; \
} \
double dval = PyFloat_AS_DOUBLE(pyfloat_value); \
npy_##typename fval = (double_to_float)(dval); \
\
if (NPY_UNLIKELY(isinf_name(fval) && !(npy_isinf(dval)))) { \
if (PyUFunc_GiveFloatingpointErrors("cast", \
NPY_FPE_OVERFLOW) < 0) { \
goto fail; \
} \
} \
\
*out = fval; \
\
in += in_stride; \
out += out_stride; \
} \
\
_NpyString_release_allocator(descr); \
return 0; \
fail: \
_NpyString_release_allocator(descr); \
return -1; \
} \
\
static PyType_Slot s2##shortname##_slots[] = { \
{NPY_METH_resolve_descriptors, \