-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
/
dtypemeta.c
998 lines (901 loc) · 32.7 KB
/
dtypemeta.c
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
/* Array Descr Object */
#define NPY_NO_DEPRECATED_API NPY_API_VERSION
#define _MULTIARRAYMODULE
#define PY_SSIZE_T_CLEAN
#include <Python.h>
#include <structmember.h>
#include <numpy/ndarraytypes.h>
#include <numpy/arrayscalars.h>
#include "npy_pycompat.h"
#include "common.h"
#include "dtypemeta.h"
#include "descriptor.h"
#include "_datetime.h"
#include "array_coercion.h"
#include "scalartypes.h"
#include "convert_datatype.h"
#include "usertypes.h"
#include "conversion_utils.h"
#include "templ_common.h"
#include <assert.h>
static void
dtypemeta_dealloc(PyArray_DTypeMeta *self) {
/* Do not accidentally delete a statically defined DType: */
assert(((PyTypeObject *)self)->tp_flags & Py_TPFLAGS_HEAPTYPE);
Py_XDECREF(self->scalar_type);
Py_XDECREF(self->singleton);
Py_XDECREF(NPY_DT_SLOTS(self)->castingimpls);
PyMem_Free(self->dt_slots);
PyType_Type.tp_dealloc((PyObject *) self);
}
static PyObject *
dtypemeta_alloc(PyTypeObject *NPY_UNUSED(type), Py_ssize_t NPY_UNUSED(items))
{
PyErr_SetString(PyExc_TypeError,
"DTypes can only be created using the NumPy API.");
return NULL;
}
static PyObject *
dtypemeta_new(PyTypeObject *NPY_UNUSED(type),
PyObject *NPY_UNUSED(args), PyObject *NPY_UNUSED(kwds))
{
PyErr_SetString(PyExc_TypeError,
"Preliminary-API: Cannot subclass DType.");
return NULL;
}
static int
dtypemeta_init(PyTypeObject *NPY_UNUSED(type),
PyObject *NPY_UNUSED(args), PyObject *NPY_UNUSED(kwds))
{
PyErr_SetString(PyExc_TypeError,
"Preliminary-API: Cannot __init__ DType class.");
return -1;
}
/**
* tp_is_gc slot of Python types. This is implemented only for documentation
* purposes to indicate and document the subtleties involved.
*
* Python Type objects are either statically created (typical C-Extension type)
* or HeapTypes (typically created in Python).
* HeapTypes have the Py_TPFLAGS_HEAPTYPE flag and are garbage collected.
* Our DTypeMeta instances (`np.dtype` and its subclasses) *may* be HeapTypes
* if the Py_TPFLAGS_HEAPTYPE flag is set (they are created from Python).
* They are not for legacy DTypes or np.dtype itself.
*
* @param self
* @return nonzero if the object is garbage collected
*/
static inline int
dtypemeta_is_gc(PyObject *dtype_class)
{
return PyType_Type.tp_is_gc(dtype_class);
}
static int
dtypemeta_traverse(PyArray_DTypeMeta *type, visitproc visit, void *arg)
{
/*
* We have to traverse the base class (if it is a HeapType).
* PyType_Type will handle this logic for us.
* This function is currently not used, but will probably be necessary
* in the future when we implement HeapTypes (python/dynamically
* defined types). It should be revised at that time.
*/
assert(0);
assert(!NPY_DT_is_legacy(type) && (PyTypeObject *)type != &PyArrayDescr_Type);
Py_VISIT(type->singleton);
Py_VISIT(type->scalar_type);
return PyType_Type.tp_traverse((PyObject *)type, visit, arg);
}
static PyObject *
legacy_dtype_default_new(PyArray_DTypeMeta *self,
PyObject *args, PyObject *kwargs)
{
/* TODO: This should allow endianness and possibly metadata */
if (NPY_DT_is_parametric(self)) {
/* reject parametric ones since we would need to get unit, etc. info */
PyErr_Format(PyExc_TypeError,
"Preliminary-API: Flexible/Parametric legacy DType '%S' can "
"only be instantiated using `np.dtype(...)`", self);
return NULL;
}
if (PyTuple_GET_SIZE(args) != 0 ||
(kwargs != NULL && PyDict_Size(kwargs))) {
PyErr_Format(PyExc_TypeError,
"currently only the no-argument instantiation is supported; "
"use `np.dtype` instead.");
return NULL;
}
Py_INCREF(self->singleton);
return (PyObject *)self->singleton;
}
static PyObject *
string_unicode_new(PyArray_DTypeMeta *self, PyObject *args, PyObject *kwargs)
{
npy_intp size;
static char *kwlist[] = {"", NULL};
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&", kwlist,
PyArray_IntpFromPyIntConverter, &size)) {
return NULL;
}
if (size < 0) {
PyErr_Format(PyExc_ValueError,
"Strings cannot have a negative size but a size of "
"%"NPY_INTP_FMT" was given", size);
return NULL;
}
PyArray_Descr *res = PyArray_DescrNewFromType(self->type_num);
if (res == NULL) {
return NULL;
}
if (self->type_num == NPY_UNICODE) {
// unicode strings are 4 bytes per character
if (npy_mul_sizes_with_overflow(&size, size, 4)) {
PyErr_SetString(
PyExc_TypeError,
"Strings too large to store inside array.");
return NULL;
}
}
if (size > NPY_MAX_INT) {
PyErr_SetString(PyExc_TypeError,
"Strings too large to store inside array.");
return NULL;
}
res->elsize = (int)size;
return (PyObject *)res;
}
static PyArray_Descr *
nonparametric_discover_descr_from_pyobject(
PyArray_DTypeMeta *cls, PyObject *obj)
{
/* If the object is of the correct scalar type return our singleton */
assert(!NPY_DT_is_parametric(cls));
Py_INCREF(cls->singleton);
return cls->singleton;
}
static PyArray_Descr *
string_discover_descr_from_pyobject(
PyArray_DTypeMeta *cls, PyObject *obj)
{
npy_intp itemsize = -1;
if (PyBytes_Check(obj)) {
itemsize = PyBytes_Size(obj);
}
else if (PyUnicode_Check(obj)) {
itemsize = PyUnicode_GetLength(obj);
}
if (itemsize != -1) {
if (cls->type_num == NPY_UNICODE) {
itemsize *= 4;
}
if (itemsize > NPY_MAX_INT) {
PyErr_SetString(PyExc_TypeError,
"string too large to store inside array.");
}
PyArray_Descr *res = PyArray_DescrNewFromType(cls->type_num);
if (res == NULL) {
return NULL;
}
res->elsize = (int)itemsize;
return res;
}
return PyArray_DTypeFromObjectStringDiscovery(obj, NULL, cls->type_num);
}
static PyArray_Descr *
void_discover_descr_from_pyobject(
PyArray_DTypeMeta *NPY_UNUSED(cls), PyObject *obj)
{
if (PyArray_IsScalar(obj, Void)) {
PyVoidScalarObject *void_obj = (PyVoidScalarObject *)obj;
Py_INCREF(void_obj->descr);
return void_obj->descr;
}
if (PyBytes_Check(obj)) {
PyArray_Descr *descr = PyArray_DescrNewFromType(NPY_VOID);
if (descr == NULL) {
return NULL;
}
Py_ssize_t itemsize = PyBytes_Size(obj);
if (itemsize > NPY_MAX_INT) {
PyErr_SetString(PyExc_TypeError,
"byte-like to large to store inside array.");
Py_DECREF(descr);
return NULL;
}
descr->elsize = (int)itemsize;
return descr;
}
PyErr_Format(PyExc_TypeError,
"A bytes-like object is required, not '%s'", Py_TYPE(obj)->tp_name);
return NULL;
}
static PyArray_Descr *
discover_datetime_and_timedelta_from_pyobject(
PyArray_DTypeMeta *cls, PyObject *obj) {
if (PyArray_IsScalar(obj, Datetime) ||
PyArray_IsScalar(obj, Timedelta)) {
PyArray_DatetimeMetaData *meta;
PyArray_Descr *descr = PyArray_DescrFromScalar(obj);
meta = get_datetime_metadata_from_dtype(descr);
if (meta == NULL) {
return NULL;
}
PyArray_Descr *new_descr = create_datetime_dtype(cls->type_num, meta);
Py_DECREF(descr);
return new_descr;
}
else {
return find_object_datetime_type(obj, cls->type_num);
}
}
static PyArray_Descr *
nonparametric_default_descr(PyArray_DTypeMeta *cls)
{
Py_INCREF(cls->singleton);
return cls->singleton;
}
/*
* For most builtin (and legacy) dtypes, the canonical property means to
* ensure native byte-order. (We do not care about metadata here.)
*/
static PyArray_Descr *
ensure_native_byteorder(PyArray_Descr *descr)
{
if (PyArray_ISNBO(descr->byteorder)) {
Py_INCREF(descr);
return descr;
}
else {
return PyArray_DescrNewByteorder(descr, NPY_NATIVE);
}
}
/* Ensure a copy of the singleton (just in case we do adapt it somewhere) */
static PyArray_Descr *
datetime_and_timedelta_default_descr(PyArray_DTypeMeta *cls)
{
return PyArray_DescrNew(cls->singleton);
}
static PyArray_Descr *
void_default_descr(PyArray_DTypeMeta *cls)
{
PyArray_Descr *res = PyArray_DescrNew(cls->singleton);
if (res == NULL) {
return NULL;
}
/*
* The legacy behaviour for `np.array([], dtype="V")` is to use "V8".
* This is because `[]` uses `float64` as dtype, and then that is used
* for the size of the requested void.
*/
res->elsize = 8;
return res;
}
static PyArray_Descr *
string_and_unicode_default_descr(PyArray_DTypeMeta *cls)
{
PyArray_Descr *res = PyArray_DescrNewFromType(cls->type_num);
if (res == NULL) {
return NULL;
}
res->elsize = 1;
if (cls->type_num == NPY_UNICODE) {
res->elsize *= 4;
}
return res;
}
static PyArray_Descr *
string_unicode_common_instance(PyArray_Descr *descr1, PyArray_Descr *descr2)
{
if (descr1->elsize >= descr2->elsize) {
return NPY_DT_CALL_ensure_canonical(descr1);
}
else {
return NPY_DT_CALL_ensure_canonical(descr2);
}
}
static PyArray_Descr *
void_ensure_canonical(PyArray_Descr *self)
{
if (self->subarray != NULL) {
PyArray_Descr *new_base = NPY_DT_CALL_ensure_canonical(
self->subarray->base);
if (new_base == NULL) {
return NULL;
}
if (new_base == self->subarray->base) {
/* just return self, no need to modify */
Py_DECREF(new_base);
Py_INCREF(self);
return self;
}
PyArray_Descr *new = PyArray_DescrNew(self);
if (new == NULL) {
return NULL;
}
Py_SETREF(new->subarray->base, new_base);
return new;
}
else if (self->names != NULL) {
/*
* This branch is fairly complex, since it needs to build a new
* descriptor that is in canonical form. This means that the new
* descriptor should be an aligned struct if the old one was, and
* otherwise it should be an unaligned struct.
* Any unnecessary empty space is stripped from the struct.
*
* TODO: In principle we could/should try to provide the identity when
* no change is necessary. (Simple if we add a flag.)
*/
Py_ssize_t field_num = PyTuple_GET_SIZE(self->names);
PyArray_Descr *new = PyArray_DescrNew(self);
if (new == NULL) {
return NULL;
}
Py_SETREF(new->fields, PyDict_New());
if (new->fields == NULL) {
Py_DECREF(new);
return NULL;
}
int aligned = PyDataType_FLAGCHK(new, NPY_ALIGNED_STRUCT);
new->flags = new->flags & ~NPY_FROM_FIELDS;
new->flags |= NPY_NEEDS_PYAPI; /* always needed for field access */
int totalsize = 0;
int maxalign = 1;
for (Py_ssize_t i = 0; i < field_num; i++) {
PyObject *name = PyTuple_GET_ITEM(self->names, i);
PyObject *tuple = PyDict_GetItem(self->fields, name);
PyObject *new_tuple = PyTuple_New(PyTuple_GET_SIZE(tuple));
PyArray_Descr *field_descr = NPY_DT_CALL_ensure_canonical(
(PyArray_Descr *)PyTuple_GET_ITEM(tuple, 0));
if (field_descr == NULL) {
Py_DECREF(new_tuple);
Py_DECREF(new);
return NULL;
}
new->flags |= field_descr->flags & NPY_FROM_FIELDS;
PyTuple_SET_ITEM(new_tuple, 0, (PyObject *)field_descr);
if (aligned) {
totalsize = NPY_NEXT_ALIGNED_OFFSET(
totalsize, field_descr->alignment);
maxalign = PyArray_MAX(maxalign, field_descr->alignment);
}
PyObject *offset_obj = PyLong_FromLong(totalsize);
if (offset_obj == NULL) {
Py_DECREF(new_tuple);
Py_DECREF(new);
return NULL;
}
PyTuple_SET_ITEM(new_tuple, 1, (PyObject *)offset_obj);
if (PyTuple_GET_SIZE(tuple) == 3) {
/* Be sure to set all items in the tuple before using it */
PyObject *title = PyTuple_GET_ITEM(tuple, 2);
Py_INCREF(title);
PyTuple_SET_ITEM(new_tuple, 2, title);
if (PyDict_SetItem(new->fields, title, new_tuple) < 0) {
Py_DECREF(new_tuple);
Py_DECREF(new);
return NULL;
}
}
if (PyDict_SetItem(new->fields, name, new_tuple) < 0) {
Py_DECREF(new_tuple);
Py_DECREF(new);
return NULL;
}
Py_DECREF(new_tuple); /* Reference now owned by new->fields */
totalsize += field_descr->elsize;
}
totalsize = NPY_NEXT_ALIGNED_OFFSET(totalsize, maxalign);
new->elsize = totalsize;
new->alignment = maxalign;
return new;
}
else {
/* unstructured voids are always canonical. */
Py_INCREF(self);
return self;
}
}
static PyArray_Descr *
void_common_instance(PyArray_Descr *descr1, PyArray_Descr *descr2)
{
if (descr1->subarray == NULL && descr1->names == NULL &&
descr2->subarray == NULL && descr2->names == NULL) {
if (descr1->elsize != descr2->elsize) {
PyErr_SetString(npy_DTypePromotionError,
"Invalid type promotion with void datatypes of different "
"lengths. Use the `np.bytes_` datatype instead to pad the "
"shorter value with trailing zero bytes.");
return NULL;
}
Py_INCREF(descr1);
return descr1;
}
if (descr1->names != NULL && descr2->names != NULL) {
/* If both have fields promoting individual fields may be possible */
static PyObject *promote_fields_func = NULL;
npy_cache_import("numpy.core._internal", "_promote_fields",
&promote_fields_func);
if (promote_fields_func == NULL) {
return NULL;
}
PyObject *result = PyObject_CallFunctionObjArgs(promote_fields_func,
descr1, descr2, NULL);
if (result == NULL) {
return NULL;
}
if (!PyObject_TypeCheck(result, Py_TYPE(descr1))) {
PyErr_SetString(PyExc_RuntimeError,
"Internal NumPy error: `_promote_fields` did not return "
"a valid descriptor object.");
Py_DECREF(result);
return NULL;
}
return (PyArray_Descr *)result;
}
else if (descr1->subarray != NULL && descr2->subarray != NULL) {
int cmp = PyObject_RichCompareBool(
descr1->subarray->shape, descr2->subarray->shape, Py_EQ);
if (error_converting(cmp)) {
return NULL;
}
if (!cmp) {
PyErr_SetString(npy_DTypePromotionError,
"invalid type promotion with subarray datatypes "
"(shape mismatch).");
return NULL;
}
PyArray_Descr *new_base = PyArray_PromoteTypes(
descr1->subarray->base, descr2->subarray->base);
if (new_base == NULL) {
return NULL;
}
/*
* If it is the same dtype and the container did not change, we might
* as well preserve identity and metadata. This could probably be
* changed.
*/
if (descr1 == descr2 && new_base == descr1->subarray->base) {
Py_DECREF(new_base);
Py_INCREF(descr1);
return descr1;
}
PyArray_Descr *new_descr = PyArray_DescrNew(descr1);
if (new_descr == NULL) {
Py_DECREF(new_base);
return NULL;
}
Py_SETREF(new_descr->subarray->base, new_base);
return new_descr;
}
PyErr_SetString(npy_DTypePromotionError,
"invalid type promotion with structured datatype(s).");
return NULL;
}
NPY_NO_EXPORT int
python_builtins_are_known_scalar_types(
PyArray_DTypeMeta *NPY_UNUSED(cls), PyTypeObject *pytype)
{
/*
* Always accept the common Python types, this ensures that we do not
* convert pyfloat->float64->integers. Subclasses are hopefully rejected
* as being discovered.
* This is necessary only for python scalar classes which we discover
* as valid DTypes.
*/
if (pytype == &PyFloat_Type) {
return 1;
}
if (pytype == &PyLong_Type) {
return 1;
}
if (pytype == &PyBool_Type) {
return 1;
}
if (pytype == &PyComplex_Type) {
return 1;
}
if (pytype == &PyUnicode_Type) {
return 1;
}
if (pytype == &PyBytes_Type) {
return 1;
}
return 0;
}
static int
signed_integers_is_known_scalar_types(
PyArray_DTypeMeta *cls, PyTypeObject *pytype)
{
if (python_builtins_are_known_scalar_types(cls, pytype)) {
return 1;
}
/* Convert our scalars (raise on too large unsigned and NaN, etc.) */
return PyType_IsSubtype(pytype, &PyGenericArrType_Type);
}
static int
datetime_known_scalar_types(
PyArray_DTypeMeta *cls, PyTypeObject *pytype)
{
if (python_builtins_are_known_scalar_types(cls, pytype)) {
return 1;
}
/*
* To be able to identify the descriptor from e.g. any string, datetime
* must take charge. Otherwise we would attempt casting which does not
* truly support this. Only object arrays are special cased in this way.
*/
return (PyType_IsSubtype(pytype, &PyBytes_Type) ||
PyType_IsSubtype(pytype, &PyUnicode_Type));
}
static int
string_known_scalar_types(
PyArray_DTypeMeta *cls, PyTypeObject *pytype) {
if (python_builtins_are_known_scalar_types(cls, pytype)) {
return 1;
}
if (PyType_IsSubtype(pytype, &PyDatetimeArrType_Type)) {
/*
* TODO: This should likely be deprecated or otherwise resolved.
* Deprecation has to occur in `String->setitem` unfortunately.
*
* Datetime currently do not cast to shorter strings, but string
* coercion for arbitrary values uses `str(obj)[:len]` so it works.
* This means `np.array(np.datetime64("2020-01-01"), "U9")`
* and `np.array(np.datetime64("2020-01-01")).astype("U9")` behave
* differently.
*/
return 1;
}
return 0;
}
/*
* The following set of functions define the common dtype operator for
* the builtin types.
*/
static PyArray_DTypeMeta *
default_builtin_common_dtype(PyArray_DTypeMeta *cls, PyArray_DTypeMeta *other)
{
assert(cls->type_num < NPY_NTYPES);
if (!NPY_DT_is_legacy(other) || other->type_num > cls->type_num) {
/*
* Let the more generic (larger type number) DType handle this
* (note that half is after all others, which works out here.)
*/
Py_INCREF(Py_NotImplemented);
return (PyArray_DTypeMeta *)Py_NotImplemented;
}
/*
* Note: The use of the promotion table should probably be revised at
* some point. It may be most useful to remove it entirely and then
* consider adding a fast path/cache `PyArray_CommonDType()` itself.
*/
int common_num = _npy_type_promotion_table[cls->type_num][other->type_num];
if (common_num < 0) {
Py_INCREF(Py_NotImplemented);
return (PyArray_DTypeMeta *)Py_NotImplemented;
}
return PyArray_DTypeFromTypeNum(common_num);
}
static PyArray_DTypeMeta *
string_unicode_common_dtype(PyArray_DTypeMeta *cls, PyArray_DTypeMeta *other)
{
assert(cls->type_num < NPY_NTYPES && cls != other);
if (!NPY_DT_is_legacy(other) || (!PyTypeNum_ISNUMBER(other->type_num) &&
/* Not numeric so defer unless cls is unicode and other is string */
!(cls->type_num == NPY_UNICODE && other->type_num == NPY_STRING))) {
Py_INCREF(Py_NotImplemented);
return (PyArray_DTypeMeta *)Py_NotImplemented;
}
/*
* The builtin types are ordered by complexity (aside from object) here.
* Arguably, we should not consider numbers and strings "common", but
* we currently do.
*/
Py_INCREF(cls);
return cls;
}
static PyArray_DTypeMeta *
datetime_common_dtype(PyArray_DTypeMeta *cls, PyArray_DTypeMeta *other)
{
/*
* Timedelta/datetime shouldn't actually promote at all. That they
* currently do means that we need additional hacks in the comparison
* type resolver. For comparisons we have to make sure we reject it
* nicely in order to return an array of True/False values.
*/
if (cls->type_num == NPY_DATETIME && other->type_num == NPY_TIMEDELTA) {
/*
* TODO: We actually currently do allow promotion here. This is
* currently relied on within `np.add(datetime, timedelta)`,
* while for concatenation the cast step will fail.
*/
Py_INCREF(cls);
return cls;
}
return default_builtin_common_dtype(cls, other);
}
static PyArray_DTypeMeta *
object_common_dtype(
PyArray_DTypeMeta *cls, PyArray_DTypeMeta *NPY_UNUSED(other))
{
/*
* The object DType is special in that it can represent everything,
* including all potential user DTypes.
* One reason to defer (or error) here might be if the other DType
* does not support scalars so that e.g. `arr1d[0]` returns a 0-D array
* and `arr.astype(object)` would fail. But object casts are special.
*/
Py_INCREF(cls);
return cls;
}
int
object_fill_zero_value(PyArrayObject *arr)
{
PyObject *zero = PyLong_FromLong(0);
PyArray_FillObjectArray(arr, zero);
Py_DECREF(zero);
if (PyErr_Occurred()) {
return -1;
}
return 0;
}
int
void_fill_zero_value(PyArrayObject *arr)
{
if (PyDataType_REFCHK(PyArray_DESCR(arr))) {
if (object_fill_zero_value(arr) < 0) {
return -1;
}
}
return 0;
}
/**
* This function takes a PyArray_Descr and replaces its base class with
* a newly created dtype subclass (DTypeMeta instances).
* There are some subtleties that need to be remembered when doing this,
* first for the class objects itself it could be either a HeapType or not.
* Since we are defining the DType from C, we will not make it a HeapType,
* thus making it identical to a typical *static* type (except that we
* malloc it). We could do it the other way, but there seems no reason to
* do so.
*
* The DType instances (the actual dtypes or descriptors), are based on
* prototypes which are passed in. These should not be garbage collected
* and thus Py_TPFLAGS_HAVE_GC is not set. (We could allow this, but than
* would have to allocate a new object, since the GC needs information before
* the actual struct).
*
* The above is the reason why we should works exactly like we would for a
* static type here.
* Otherwise, we blurry the lines between C-defined extension classes
* and Python subclasses. e.g. `class MyInt(int): pass` is very different
* from our `class Float64(np.dtype): pass`, because the latter should not
* be a HeapType and its instances should be exact PyArray_Descr structs.
*
* @param descr The descriptor that should be wrapped.
* @param name The name for the DType, if NULL the type character is used.
*
* @returns 0 on success, -1 on failure.
*/
NPY_NO_EXPORT int
dtypemeta_wrap_legacy_descriptor(PyArray_Descr *descr)
{
int has_type_set = Py_TYPE(descr) == &PyArrayDescr_Type;
if (!has_type_set) {
/* Accept if the type was filled in from an existing builtin dtype */
for (int i = 0; i < NPY_NTYPES; i++) {
PyArray_Descr *builtin = PyArray_DescrFromType(i);
has_type_set = Py_TYPE(descr) == Py_TYPE(builtin);
Py_DECREF(builtin);
if (has_type_set) {
break;
}
}
}
if (!has_type_set) {
PyErr_Format(PyExc_RuntimeError,
"During creation/wrapping of legacy DType, the original class "
"was not of PyArrayDescr_Type (it is replaced in this step). "
"The extension creating a custom DType for type %S must be "
"modified to ensure `Py_TYPE(descr) == &PyArrayDescr_Type` or "
"that of an existing dtype (with the assumption it is just "
"copied over and can be replaced).",
descr->typeobj, Py_TYPE(descr));
return -1;
}
/*
* Note: we have no intention of freeing the memory again since this
* behaves identically to static type definition (see comment above).
* This is seems cleaner for the legacy API, in the new API both static
* and heap types are possible (some difficulty arises from the fact that
* these are instances of DTypeMeta and not type).
* In particular our own DTypes can be true static declarations.
* However, this function remains necessary for legacy user dtypes.
*/
const char *scalar_name = descr->typeobj->tp_name;
/*
* We have to take only the name, and ignore the module to get
* a reasonable __name__, since static types are limited in this regard
* (this is not ideal, but not a big issue in practice).
* This is what Python does to print __name__ for static types.
*/
const char *dot = strrchr(scalar_name, '.');
if (dot) {
scalar_name = dot + 1;
}
Py_ssize_t name_length = strlen(scalar_name) + 14;
char *tp_name = PyMem_Malloc(name_length);
if (tp_name == NULL) {
PyErr_NoMemory();
return -1;
}
snprintf(tp_name, name_length, "numpy.dtype[%s]", scalar_name);
NPY_DType_Slots *dt_slots = PyMem_Malloc(sizeof(NPY_DType_Slots));
if (dt_slots == NULL) {
PyMem_Free(tp_name);
return -1;
}
memset(dt_slots, '\0', sizeof(NPY_DType_Slots));
PyArray_DTypeMeta *dtype_class = PyMem_Malloc(sizeof(PyArray_DTypeMeta));
if (dtype_class == NULL) {
PyMem_Free(tp_name);
PyMem_Free(dt_slots);
return -1;
}
/*
* Initialize the struct fields identically to static code by copying
* a prototype instances for everything except our own fields which
* vary between the DTypes.
* In particular any Object initialization must be strictly copied from
* the untouched prototype to avoid complexities (e.g. with PyPy).
* Any Type slots need to be fixed before PyType_Ready, although most
* will be inherited automatically there.
*/
static PyArray_DTypeMeta prototype = {
{{
PyVarObject_HEAD_INIT(&PyArrayDTypeMeta_Type, 0)
.tp_name = NULL, /* set below */
.tp_basicsize = sizeof(PyArray_Descr),
.tp_flags = Py_TPFLAGS_DEFAULT,
.tp_base = &PyArrayDescr_Type,
.tp_new = (newfunc)legacy_dtype_default_new,
},},
.flags = NPY_DT_LEGACY,
/* Further fields are not common between DTypes */
};
memcpy(dtype_class, &prototype, sizeof(PyArray_DTypeMeta));
/* Fix name of the Type*/
((PyTypeObject *)dtype_class)->tp_name = tp_name;
dtype_class->dt_slots = dt_slots;
/* Let python finish the initialization (probably unnecessary) */
if (PyType_Ready((PyTypeObject *)dtype_class) < 0) {
Py_DECREF(dtype_class);
return -1;
}
dt_slots->castingimpls = PyDict_New();
if (dt_slots->castingimpls == NULL) {
Py_DECREF(dtype_class);
return -1;
}
/*
* Fill DTypeMeta information that varies between DTypes, any variable
* type information would need to be set before PyType_Ready().
*/
dtype_class->singleton = descr;
Py_INCREF(descr->typeobj);
dtype_class->scalar_type = descr->typeobj;
dtype_class->type_num = descr->type_num;
dt_slots->f = *(descr->f);
/* Set default functions (correct for most dtypes, override below) */
dt_slots->default_descr = nonparametric_default_descr;
dt_slots->discover_descr_from_pyobject = (
nonparametric_discover_descr_from_pyobject);
dt_slots->is_known_scalar_type = python_builtins_are_known_scalar_types;
dt_slots->common_dtype = default_builtin_common_dtype;
dt_slots->common_instance = NULL;
dt_slots->ensure_canonical = ensure_native_byteorder;
dt_slots->fill_zero_value = NULL;
if (PyTypeNum_ISSIGNED(dtype_class->type_num)) {
/* Convert our scalars (raise on too large unsigned and NaN, etc.) */
dt_slots->is_known_scalar_type = signed_integers_is_known_scalar_types;
}
if (PyTypeNum_ISUSERDEF(descr->type_num)) {
dt_slots->common_dtype = legacy_userdtype_common_dtype_function;
}
else if (descr->type_num == NPY_OBJECT) {
dt_slots->common_dtype = object_common_dtype;
dt_slots->fill_zero_value = object_fill_zero_value;
}
else if (PyTypeNum_ISDATETIME(descr->type_num)) {
/* Datetimes are flexible, but were not considered previously */
dtype_class->flags |= NPY_DT_PARAMETRIC;
dt_slots->default_descr = datetime_and_timedelta_default_descr;
dt_slots->discover_descr_from_pyobject = (
discover_datetime_and_timedelta_from_pyobject);
dt_slots->common_dtype = datetime_common_dtype;
dt_slots->common_instance = datetime_type_promotion;
if (descr->type_num == NPY_DATETIME) {
dt_slots->is_known_scalar_type = datetime_known_scalar_types;
}
}
else if (PyTypeNum_ISFLEXIBLE(descr->type_num)) {
dtype_class->flags |= NPY_DT_PARAMETRIC;
if (descr->type_num == NPY_VOID) {
dt_slots->default_descr = void_default_descr;
dt_slots->discover_descr_from_pyobject = (
void_discover_descr_from_pyobject);
dt_slots->common_instance = void_common_instance;
dt_slots->ensure_canonical = void_ensure_canonical;
dt_slots->fill_zero_value = void_fill_zero_value;
}
else {
dt_slots->default_descr = string_and_unicode_default_descr;
dt_slots->is_known_scalar_type = string_known_scalar_types;
dt_slots->discover_descr_from_pyobject = (
string_discover_descr_from_pyobject);
dt_slots->common_dtype = string_unicode_common_dtype;
dt_slots->common_instance = string_unicode_common_instance;
((PyTypeObject*)dtype_class)->tp_new = (newfunc)string_unicode_new;
}
}
if (PyTypeNum_ISNUMBER(descr->type_num)) {
dtype_class->flags |= NPY_DT_NUMERIC;
}
if (_PyArray_MapPyTypeToDType(dtype_class, descr->typeobj,
PyTypeNum_ISUSERDEF(dtype_class->type_num)) < 0) {
Py_DECREF(dtype_class);
return -1;
}
/* Finally, replace the current class of the descr */
Py_SET_TYPE(descr, (PyTypeObject *)dtype_class);
return 0;
}
static PyObject *
dtypemeta_get_abstract(PyArray_DTypeMeta *self) {
return PyBool_FromLong(NPY_DT_is_abstract(self));
}
static PyObject *
dtypemeta_get_legacy(PyArray_DTypeMeta *self) {
return PyBool_FromLong(NPY_DT_is_legacy(self));
}
static PyObject *
dtypemeta_get_parametric(PyArray_DTypeMeta *self) {
return PyBool_FromLong(NPY_DT_is_parametric(self));
}
static PyObject *
dtypemeta_get_is_numeric(PyArray_DTypeMeta *self) {
return PyBool_FromLong(NPY_DT_is_numeric(self));
}
/*
* Simple exposed information, defined for each DType (class).
*/
static PyGetSetDef dtypemeta_getset[] = {
{"_abstract", (getter)dtypemeta_get_abstract, NULL, NULL, NULL},
{"_legacy", (getter)dtypemeta_get_legacy, NULL, NULL, NULL},
{"_parametric", (getter)dtypemeta_get_parametric, NULL, NULL, NULL},
{"_is_numeric", (getter)dtypemeta_get_is_numeric, NULL, NULL, NULL},
{NULL, NULL, NULL, NULL, NULL}
};
static PyMemberDef dtypemeta_members[] = {
{"type",
T_OBJECT, offsetof(PyArray_DTypeMeta, scalar_type), READONLY, NULL},
{NULL, 0, 0, 0, NULL},
};
NPY_NO_EXPORT PyTypeObject PyArrayDTypeMeta_Type = {
PyVarObject_HEAD_INIT(NULL, 0)
.tp_name = "numpy._DTypeMeta",
.tp_basicsize = sizeof(PyArray_DTypeMeta),
.tp_dealloc = (destructor)dtypemeta_dealloc,
/* Types are garbage collected (see dtypemeta_is_gc documentation) */
.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,
.tp_doc = "Preliminary NumPy API: The Type of NumPy DTypes (metaclass)",
.tp_traverse = (traverseproc)dtypemeta_traverse,
.tp_members = dtypemeta_members,
.tp_getset = dtypemeta_getset,
.tp_base = NULL, /* set to PyType_Type at import time */
.tp_init = (initproc)dtypemeta_init,
.tp_alloc = dtypemeta_alloc,
.tp_new = dtypemeta_new,
.tp_is_gc = dtypemeta_is_gc,
};