-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy path_internal.c
823 lines (728 loc) · 27.6 KB
/
_internal.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
#include "_internal.h"
#include "Python.h"
/* A small object that handles deallocation of some of a PyUFunc's fields */
typedef struct {
PyObject_HEAD
/* Borrowed reference */
PyUFuncObject *ufunc;
/* Owned reference to ancillary object */
PyObject *object;
} PyUFuncCleaner;
PyTypeObject PyUFuncCleaner_Type;
static PyObject *
cleaner_new(PyUFuncObject *ufunc, PyObject *object)
{
PyUFuncCleaner *obj = PyObject_New(PyUFuncCleaner, &PyUFuncCleaner_Type);
if (obj != NULL) {
obj->ufunc = ufunc;
Py_XINCREF(object);
obj->object = object;
}
return (PyObject *) obj;
}
/* Deallocate the PyArray_malloc calls */
static void
cleaner_dealloc(PyUFuncCleaner *self)
{
PyUFuncObject *ufunc = self->ufunc;
Py_XDECREF(self->object);
if (ufunc->functions)
PyArray_free(ufunc->functions);
if (ufunc->types)
PyArray_free((void *)ufunc->types);
if (ufunc->data)
PyArray_free((void *)ufunc->data);
PyObject_Del(self);
}
PyTypeObject PyUFuncCleaner_Type = {
PyVarObject_HEAD_INIT(NULL, 0)
"numba._UFuncCleaner", /* tp_name*/
sizeof(PyUFuncCleaner), /* tp_basicsize*/
0, /* tp_itemsize */
(destructor) cleaner_dealloc, /* tp_dealloc */
0, /* tp_vectorcall_offset */
0, /* tp_getattr */
0, /* tp_setattr */
0, /* tp_as_async */
0, /* tp_repr */
0, /* tp_as_number */
0, /* tp_as_sequence */
0, /* tp_as_mapping */
0, /* tp_hash */
0, /* tp_call */
0, /* tp_str */
0, /* tp_getattro */
0, /* tp_setattro */
0, /* tp_as_buffer */
Py_TPFLAGS_DEFAULT, /* tp_flags */
0, /* tp_doc */
0, /* tp_traverse */
0, /* tp_clear */
0, /* tp_richcompare */
0, /* tp_weaklistoffset */
0, /* tp_iter */
0, /* tp_iternext */
0, /* tp_methods */
0, /* tp_members */
0, /* tp_getset */
0, /* tp_base */
0, /* tp_dict */
0, /* tp_descr_get */
0, /* tp_descr_set */
0, /* tp_dictoffset */
0, /* tp_init */
0, /* tp_alloc */
0, /* tp_new */
0, /* tp_free */
0, /* tp_is_gc */
0, /* tp_bases */
0, /* tp_mro */
0, /* tp_cache */
0, /* tp_subclasses */
0, /* tp_weaklist */
0, /* tp_del */
0, /* tp_version_tag */
0, /* tp_finalize */
0, /* tp_vectorcall */
#if (PY_MAJOR_VERSION == 3) && (PY_MINOR_VERSION == 12)
/* This was introduced first in 3.12
* https://github.com/python/cpython/issues/91051
*/
0, /* tp_watched */
#endif
/* WARNING: Do not remove this, only modify it! It is a version guard to
* act as a reminder to update this struct on Python version update! */
#if (PY_MAJOR_VERSION == 3)
#if ! (NB_SUPPORTED_PYTHON_MINOR)
#error "Python minor version is not supported."
#endif
#else
#error "Python major version is not supported."
#endif
/* END WARNING*/
};
/* ______________________________________________________________________
* DUFunc: A call-time (hence dynamic) specializable ufunc.
*/
typedef struct {
PyObject_HEAD
PyObject * dispatcher;
PyUFuncObject * ufunc;
PyObject * keepalive;
int frozen;
} PyDUFuncObject;
static void
dufunc_dealloc(PyDUFuncObject *self)
{
/* Note: There is no need to call PyArray_free() on
self->ufunc->ptr, since ufunc_dealloc() will do it for us. */
Py_XDECREF(self->ufunc);
Py_XDECREF(self->dispatcher);
Py_XDECREF(self->keepalive);
Py_TYPE(self)->tp_free((PyObject *)self);
}
static PyObject *
dufunc_repr(PyDUFuncObject *dufunc)
{
return PyString_FromFormat("<numba._DUFunc '%s'>", dufunc->ufunc->name);
}
static PyObject *
dufunc_call(PyDUFuncObject *self, PyObject *args, PyObject *kws)
{
PyObject *result=NULL, *method=NULL;
result = PyUFunc_Type.tp_call((PyObject *)self->ufunc, args, kws);
if ((!self->frozen) &&
(result == NULL) &&
(PyErr_Occurred()) &&
(PyErr_ExceptionMatches(PyExc_TypeError))) {
/* Break back into Python when we fail at dispatch. */
PyErr_Clear();
method = PyObject_GetAttrString((PyObject*)self, "_compile_for_args");
if (method) {
result = PyObject_Call(method, args, kws);
if (result) {
Py_DECREF(result);
result = PyUFunc_Type.tp_call((PyObject *)self->ufunc, args,
kws);
}
}
Py_XDECREF(method);
}
return result;
}
static Py_ssize_t
_get_nin(PyObject * py_func_obj)
{
int result = -1;
PyObject *inspect=NULL, *getargspec=NULL, *argspec=NULL, *args=NULL;
inspect = PyImport_ImportModule("inspect");
if (!inspect) goto _get_nin_cleanup;
getargspec = PyObject_GetAttrString(inspect, "getfullargspec");
if (!getargspec) goto _get_nin_cleanup;
argspec = PyObject_CallFunctionObjArgs(getargspec, py_func_obj, NULL);
if (!argspec) goto _get_nin_cleanup;
args = PyObject_GetAttrString(argspec, "args");
if (!args) goto _get_nin_cleanup;
result = PyList_Size(args);
_get_nin_cleanup:
Py_XDECREF(args);
Py_XDECREF(argspec);
Py_XDECREF(getargspec);
Py_XDECREF(inspect);
return result;
}
static int
dufunc_init(PyDUFuncObject *self, PyObject *args, PyObject *kws)
{
PyObject *dispatcher=NULL, *keepalive=NULL, *py_func_obj=NULL, *tmp;
PyUFuncObject *ufunc=NULL;
int identity=PyUFunc_None;
int nin=-1, nout=1, idx;
const char *name=NULL, *doc=NULL;
static char * kwlist[] = {"dispatcher", "identity", "_keepalive", "nin",
"nout", NULL};
if (!PyArg_ParseTupleAndKeywords(args, kws, "O|iO!nn", kwlist,
&dispatcher, &identity,
&PyList_Type, &keepalive, &nin, &nout)) {
return -1;
}
py_func_obj = PyObject_GetAttrString(dispatcher, "py_func");
if (!py_func_obj) {
return -1;
}
if (nin < 0) {
nin = (int)_get_nin(py_func_obj);
if ((nin < 0) || (PyErr_Occurred())) {
Py_XDECREF(py_func_obj);
return -1;
}
}
/* Construct the UFunc. */
tmp = PyObject_GetAttrString(py_func_obj, "__name__");
if (tmp) {
name = PyString_AsString(tmp);
}
Py_XDECREF(tmp);
tmp = PyObject_GetAttrString(py_func_obj, "__doc__");
if (tmp && (tmp != Py_None)) {
doc = PyString_AsString(tmp);
}
Py_XDECREF(tmp);
tmp = NULL;
Py_XDECREF(py_func_obj);
py_func_obj = NULL;
if (!name) {
return -1;
}
ufunc = (PyUFuncObject *)PyUFunc_FromFuncAndData(NULL, NULL, NULL, 0,
nin, nout, identity,
name, doc, 0);
if (!ufunc) {
return -1;
}
for (idx = 0; idx < nout; idx++) {
ufunc->op_flags[idx + nin] |= NPY_ITER_READWRITE | NPY_ITER_UPDATEIFCOPY | NPY_ITER_ALLOCATE;
}
/* Construct a keepalive list if none was given. */
if (!keepalive) {
keepalive = PyList_New(0);
if (!keepalive) {
Py_XDECREF(ufunc);
return -1;
}
} else {
Py_INCREF(keepalive);
}
tmp = self->dispatcher;
Py_INCREF(dispatcher);
self->dispatcher = dispatcher;
Py_XDECREF(tmp);
tmp = (PyObject*)self->ufunc;
self->ufunc = ufunc;
Py_XDECREF(tmp);
tmp = self->keepalive;
/* Already incref'ed, either by PyList_New(), or else clause, both above. */
self->keepalive = keepalive;
Py_XDECREF(tmp);
self->frozen = 0;
return 0;
}
static PyMemberDef dufunc_members[] = {
{"_dispatcher", T_OBJECT_EX, offsetof(PyDUFuncObject, dispatcher), 0,
"Dispatcher object for the core Python function."},
{"ufunc", T_OBJECT_EX, offsetof(PyDUFuncObject, ufunc), 0,
"Numpy Ufunc for the dynamic ufunc."},
{"_keepalive", T_OBJECT_EX, offsetof(PyDUFuncObject, keepalive), 0,
"List of objects to keep alive during life of dufunc."},
{NULL}
};
/* ____________________________________________________________
* Shims to expose ufunc methods.
*/
static struct _ufunc_dispatch {
/* Note that the following may also hold `_PyCFunctionFastWithKeywords` */
PyCFunctionWithKeywords ufunc_reduce;
PyCFunctionWithKeywords ufunc_accumulate;
PyCFunctionWithKeywords ufunc_reduceat;
PyCFunctionWithKeywords ufunc_outer;
PyCFunction ufunc_at;
} ufunc_dispatch;
static int
init_ufunc_dispatch(int *numpy_uses_fastcall)
{
int result = 0;
PyMethodDef * crnt = PyUFunc_Type.tp_methods;
const char * crnt_name = NULL;
for (; crnt->ml_name != NULL; crnt++) {
crnt_name = crnt->ml_name;
switch(crnt_name[0]) {
case 'a':
if (strncmp(crnt_name, "accumulate", 11) == 0) {
ufunc_dispatch.ufunc_accumulate =
(PyCFunctionWithKeywords)crnt->ml_meth;
} else if (strncmp(crnt_name, "at", 3) == 0) {
ufunc_dispatch.ufunc_at = crnt->ml_meth;
} else {
result = -1;
}
break;
case 'o':
if (strncmp(crnt_name, "outer", 6) == 0) {
ufunc_dispatch.ufunc_outer =
(PyCFunctionWithKeywords)crnt->ml_meth;
} else {
result = -1;
}
break;
case 'r':
if (strncmp(crnt_name, "reduce", 7) == 0) {
ufunc_dispatch.ufunc_reduce =
(PyCFunctionWithKeywords)crnt->ml_meth;
} else if (strncmp(crnt_name, "reduceat", 9) == 0) {
ufunc_dispatch.ufunc_reduceat =
(PyCFunctionWithKeywords)crnt->ml_meth;
} else if (strncmp(crnt_name, "resolve_dtypes", 15) == 0) {
/* Ignored */
} else {
result = -1;
}
break;
case '_':
// We ignore private methods
break;
default:
result = -1; /* Unknown method */
}
if (result < 0) break;
/* Check whether NumPy uses fastcall (ufunc.at never uses it) */
if (strncmp(crnt_name, "at", 3) != 0) {
if (*numpy_uses_fastcall == -1) {
*numpy_uses_fastcall = crnt->ml_flags & METH_FASTCALL;
}
else if (*numpy_uses_fastcall != (crnt->ml_flags & METH_FASTCALL)) {
PyErr_Format(PyExc_RuntimeError,
"ufunc.%s() flags do not match numpy_uses_fastcall",
crnt_name);
return -1;
}
}
}
if (result == 0) {
/* Sanity check. */
result = ((ufunc_dispatch.ufunc_reduce != NULL)
&& (ufunc_dispatch.ufunc_accumulate != NULL)
&& (ufunc_dispatch.ufunc_reduceat != NULL)
&& (ufunc_dispatch.ufunc_outer != NULL)
&& (ufunc_dispatch.ufunc_at != NULL)
);
} else {
char const * const fmt = "Unexpected ufunc method %s()";
PyErr_Format(PyExc_RuntimeError, fmt, crnt_name);
}
return result;
}
static PyObject *
dufunc_reduce(PyDUFuncObject * self, PyObject * args, PyObject *kws)
{
return ufunc_dispatch.ufunc_reduce((PyObject*)self->ufunc, args, kws);
}
static PyObject *
dufunc_accumulate(PyDUFuncObject * self, PyObject * args, PyObject *kws)
{
return ufunc_dispatch.ufunc_accumulate((PyObject*)self->ufunc, args, kws);
}
static PyObject *
dufunc_reduceat(PyDUFuncObject * self, PyObject * args, PyObject *kws)
{
return ufunc_dispatch.ufunc_reduceat((PyObject*)self->ufunc, args, kws);
}
static PyObject *
dufunc_outer(PyDUFuncObject * self, PyObject * args, PyObject *kws)
{
return ufunc_dispatch.ufunc_outer((PyObject*)self->ufunc, args, kws);
}
/*
* The following are the vectorcall versions of the above, since NumPy
* uses the FASTCALL/Vectorcall protocol starting with version 1.21.
* The only NumPy versions supporting vectorcall use Python 3.7 or higher.
*/
static PyObject *
dufunc_reduce_fast(PyDUFuncObject * self,
PyObject *const *args, Py_ssize_t len_args, PyObject *kwnames)
{
return ((_PyCFunctionFastWithKeywords)ufunc_dispatch.ufunc_reduce)(
(PyObject*)self->ufunc, args, len_args, kwnames);
}
static PyObject *
dufunc_reduceat_fast(PyDUFuncObject * self,
PyObject *const *args, Py_ssize_t len_args, PyObject *kwnames)
{
return ((_PyCFunctionFastWithKeywords)ufunc_dispatch.ufunc_reduceat)(
(PyObject*)self->ufunc, args, len_args, kwnames);
}
static PyObject *
dufunc_accumulate_fast(PyDUFuncObject * self,
PyObject *const *args, Py_ssize_t len_args, PyObject *kwnames)
{
return ((_PyCFunctionFastWithKeywords)ufunc_dispatch.ufunc_accumulate)(
(PyObject*)self->ufunc, args, len_args, kwnames);
}
static PyObject *
dufunc_outer_fast(PyDUFuncObject * self,
PyObject *const *args, Py_ssize_t len_args, PyObject *kwnames)
{
return ((_PyCFunctionFastWithKeywords)ufunc_dispatch.ufunc_outer)(
(PyObject*)self->ufunc, args, len_args, kwnames);
}
static PyObject *
dufunc_at(PyDUFuncObject * self, PyObject * args)
{
return ufunc_dispatch.ufunc_at((PyObject*)self->ufunc, args);
}
static PyObject *
dufunc__compile_for_args(PyDUFuncObject * self, PyObject * args,
PyObject * kws)
{
PyErr_SetString(PyExc_NotImplementedError,
"Abstract method _DUFunc._compile_for_args() called!");
return NULL;
}
static int *
_build_arg_types_array(PyObject * type_list, Py_ssize_t nargs)
{
int *arg_types_array=NULL;
Py_ssize_t idx, arg_types_size = PyList_Size(type_list);
if (arg_types_size != nargs) {
PyErr_SetString(
PyExc_ValueError,
"argument type list size does not equal ufunc argument count");
return NULL;
}
arg_types_array = PyArray_malloc(sizeof(int) * nargs);
if (!arg_types_array) {
PyErr_NoMemory();
return NULL;
}
for (idx = 0; idx < nargs; idx++) {
arg_types_array[idx] = (int)PyLong_AsLong(PyList_GET_ITEM(type_list,
idx));
}
if (PyErr_Occurred()) {
PyArray_free(arg_types_array);
arg_types_array = NULL;
}
return arg_types_array;
}
static PyObject *
dufunc__add_loop(PyDUFuncObject * self, PyObject * args)
{
PyUFuncObject * ufunc=self->ufunc;
void *loop_ptr=NULL, *data_ptr=NULL;
int idx=-1, usertype=NPY_VOID;
int *arg_types_arr=NULL;
PyObject *arg_types=NULL, *loop_obj=NULL, *data_obj=NULL;
if (self->frozen) {
PyErr_SetString(PyExc_ValueError,
"_DUFunc._add_loop() called for frozen dufunc");
return NULL;
}
if (!PyArg_ParseTuple(args, "O!O!|O!",
&PyLong_Type, &loop_obj, &PyList_Type, &arg_types,
&PyLong_Type, &data_obj)) {
return NULL;
}
loop_ptr = PyLong_AsVoidPtr(loop_obj);
if (PyErr_Occurred()) {
return NULL;
}
if (data_obj) {
data_ptr = PyLong_AsVoidPtr(data_obj);
if (PyErr_Occurred()) {
return NULL;
}
}
arg_types_arr = _build_arg_types_array(arg_types, (Py_ssize_t)ufunc->nargs);
if (!arg_types_arr) goto _dufunc__add_loop_fail;
/* Check to see if any of the input types are user defined dtypes.
If they are, we should use PyUFunc_RegisterLoopForType() since
dispatch on a user defined dtype uses a Python dictionary
keyed by usertype (and not the functions array).
For more information, see how the usertype argument is used in
PyUFunc_RegisterLoopForType(), defined by Numpy at
.../numpy/core/src/umath/ufunc_object.c
*/
for (idx = 0; idx < ufunc->nargs; idx++) {
if (arg_types_arr[idx] >= NPY_USERDEF) {
usertype = arg_types_arr[idx];
}
}
if (usertype != NPY_VOID) {
if (PyUFunc_RegisterLoopForType(ufunc, usertype,
(PyUFuncGenericFunction)loop_ptr,
arg_types_arr, data_ptr) < 0) {
goto _dufunc__add_loop_fail;
}
} else {
/* The following is an attempt to loosely follow the allocation
code in Numpy. See ufunc_frompyfunc() in
.../numpy/core/src/umath/umathmodule.c.
The primary goal is to allocate a single chunk of memory to
hold the functions, data, and types loop arrays:
ptr == |<- functions ->|<- data ->|<- types ->|
*/
int ntypes=ufunc->ntypes + 1;
PyUFuncGenericFunction *functions=NULL;
void **data=NULL;
char *types=NULL;
void *newptr=NULL, *oldptr=NULL;
size_t functions_size=sizeof(PyUFuncGenericFunction) * ntypes;
size_t data_size=sizeof(void *) * ntypes;
size_t type_ofs=sizeof(char) * ufunc->ntypes * ufunc->nargs;
size_t newsize=(functions_size + data_size +
(sizeof(char) * ntypes * ufunc->nargs));
oldptr = ufunc->ptr;
newptr = PyArray_malloc(newsize);
if (!newptr) {
PyErr_NoMemory();
goto _dufunc__add_loop_fail;
}
functions = (PyUFuncGenericFunction*)newptr;
memcpy(functions, ufunc->functions,
sizeof(PyUFuncGenericFunction) * ufunc->ntypes);
functions[ntypes - 1] = (PyUFuncGenericFunction)loop_ptr;
data = (void **)((char *)functions + functions_size);
memcpy(data, ufunc->data, sizeof(void *) * ufunc->ntypes);
data[ntypes - 1] = data_ptr;
types = (char *)data + data_size;
memcpy(types, ufunc->types, sizeof(char) * ufunc->ntypes *
ufunc->nargs);
for (idx = 0; idx < ufunc->nargs; idx++) {
types[idx + type_ofs] = (char)arg_types_arr[idx];
}
ufunc->ntypes = ntypes;
ufunc->functions = functions;
ufunc->types = types;
ufunc->data = data;
ufunc->ptr = newptr;
PyArray_free(oldptr);
}
PyArray_free(arg_types_arr);
Py_INCREF(Py_None);
return Py_None;
_dufunc__add_loop_fail:
PyArray_free(arg_types_arr);
return NULL;
}
static struct PyMethodDef dufunc_methods[] = {
{"reduce",
(PyCFunction)dufunc_reduce,
METH_VARARGS | METH_KEYWORDS, NULL },
{"accumulate",
(PyCFunction)dufunc_accumulate,
METH_VARARGS | METH_KEYWORDS, NULL },
{"reduceat",
(PyCFunction)dufunc_reduceat,
METH_VARARGS | METH_KEYWORDS, NULL },
{"outer",
(PyCFunction)dufunc_outer,
METH_VARARGS | METH_KEYWORDS, NULL},
{"at",
(PyCFunction)dufunc_at,
METH_VARARGS, NULL},
{"_compile_for_args",
(PyCFunction)dufunc__compile_for_args,
METH_VARARGS | METH_KEYWORDS,
"Abstract method: subclasses should overload _compile_for_args() to compile the ufunc at the given arguments' types."},
{"_add_loop",
(PyCFunction)dufunc__add_loop,
METH_VARARGS,
NULL},
{NULL, NULL, 0, NULL} /* sentinel */
};
/*
* If Python is new enough, NumPy may use fastcall. In that case we have to
* also use fastcall for simplicity and speed.
*/
static struct PyMethodDef dufunc_methods_fast[] = {
{"reduce",
(PyCFunction)dufunc_reduce_fast,
METH_FASTCALL | METH_KEYWORDS, NULL },
{"accumulate",
(PyCFunction)dufunc_accumulate_fast,
METH_FASTCALL | METH_KEYWORDS, NULL },
{"reduceat",
(PyCFunction)dufunc_reduceat_fast,
METH_FASTCALL | METH_KEYWORDS, NULL },
{"outer",
(PyCFunction)dufunc_outer_fast,
METH_FASTCALL | METH_KEYWORDS, NULL},
{"at",
(PyCFunction)dufunc_at,
METH_VARARGS, NULL},
{"_compile_for_args",
(PyCFunction)dufunc__compile_for_args,
METH_VARARGS | METH_KEYWORDS,
"Abstract method: subclasses should overload _compile_for_args() to compile the ufunc at the given arguments' types."},
{"_add_loop",
(PyCFunction)dufunc__add_loop,
METH_VARARGS,
NULL},
{NULL, NULL, 0, NULL} /* sentinel */
};
static PyObject *
dufunc_getfrozen(PyDUFuncObject * self, void * closure)
{
PyObject *result=(self->frozen) ? Py_True : Py_False;
Py_INCREF(result);
return result;
}
static int
dufunc_setfrozen(PyDUFuncObject * self, PyObject * value, void * closure)
{
int result=0;
if (PyObject_IsTrue(value)) {
self->frozen = 1;
} else {
PyErr_SetString(PyExc_ValueError,
"cannot clear the _DUFunc.frozen flag");
result = -1;
}
return result;
}
static PyGetSetDef dufunc_getsets[] = {
{"_frozen",
(getter)dufunc_getfrozen, (setter)dufunc_setfrozen,
"flag indicating call-time compilation has been disabled",
NULL},
{NULL} /* Sentinel */
};
PyTypeObject PyDUFunc_Type = {
PyVarObject_HEAD_INIT(NULL, 0)
"numba._DUFunc", /* tp_name*/
sizeof(PyDUFuncObject), /* tp_basicsize*/
0, /* tp_itemsize */
(destructor) dufunc_dealloc, /* tp_dealloc */
0, /* tp_vectorcall_offset */
0, /* tp_getattr */
0, /* tp_setattr */
0, /* tp_as_async */
(reprfunc) dufunc_repr, /* tp_repr */
0, /* tp_as_number */
0, /* tp_as_sequence */
0, /* tp_as_mapping */
0, /* tp_hash */
(ternaryfunc) dufunc_call, /* tp_call */
(reprfunc) dufunc_repr, /* tp_str */
0, /* tp_getattro */
0, /* tp_setattro */
0, /* tp_as_buffer */
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
0, /* tp_doc */
0, /* tp_traverse */
0, /* tp_clear */
0, /* tp_richcompare */
0, /* tp_weaklistoffset */
0, /* tp_iter */
0, /* tp_iternext */
dufunc_methods, /* tp_methods */
dufunc_members, /* tp_members */
dufunc_getsets, /* tp_getset */
0, /* tp_base */
0, /* tp_dict */
0, /* tp_descr_get */
0, /* tp_descr_set */
0, /* tp_dictoffset */
(initproc) dufunc_init, /* tp_init */
0, /* tp_alloc */
0, /* tp_new */
0, /* tp_free */
0, /* tp_is_gc */
0, /* tp_bases */
0, /* tp_mro */
0, /* tp_cache */
0, /* tp_subclasses */
0, /* tp_weaklist */
0, /* tp_del */
0, /* tp_version_tag */
0, /* tp_finalize */
0, /* tp_vectorcall */
#if (PY_MAJOR_VERSION == 3) && (PY_MINOR_VERSION == 12)
/* This was introduced first in 3.12
* https://github.com/python/cpython/issues/91051
*/
0, /* tp_watched */
#endif
/* WARNING: Do not remove this, only modify it! It is a version guard to
* act as a reminder to update this struct on Python version update! */
#if (PY_MAJOR_VERSION == 3)
#if ! (NB_SUPPORTED_PYTHON_MINOR)
#error "Python minor version is not supported."
#endif
#else
#error "Python major version is not supported."
#endif
/* END WARNING*/
};
/* ______________________________________________________________________
* Module initialization boilerplate follows.
*/
static PyMethodDef ext_methods[] = {
{"fromfunc", (PyCFunction) ufunc_fromfunc, METH_VARARGS, NULL},
{ NULL }
};
/* Don't remove this marker, it is used for inserting licensing code */
/*MARK1*/
MOD_INIT(_internal)
{
PyObject *m;
/* Don't remove this marker, it is used for inserting licensing code */
/*MARK2*/
import_array();
import_umath();
MOD_DEF(m, "_internal", "No docs",
ext_methods)
if (m == NULL)
return MOD_ERROR_VAL;
if (PyType_Ready(&PyUFuncCleaner_Type) < 0)
return MOD_ERROR_VAL;
PyDUFunc_Type.tp_new = PyType_GenericNew;
int numpy_uses_fastcall = -1;
if (init_ufunc_dispatch(&numpy_uses_fastcall) <= 0)
return MOD_ERROR_VAL;
if (numpy_uses_fastcall) {
PyDUFunc_Type.tp_methods = dufunc_methods_fast;
}
if (PyType_Ready(&PyDUFunc_Type) < 0)
return MOD_ERROR_VAL;
Py_INCREF(&PyDUFunc_Type);
if (PyModule_AddObject(m, "_DUFunc", (PyObject *)&PyDUFunc_Type) < 0)
return MOD_ERROR_VAL;
if (PyModule_AddIntMacro(m, PyUFunc_One)
|| PyModule_AddIntMacro(m, PyUFunc_Zero)
|| PyModule_AddIntMacro(m, PyUFunc_None)
|| PyModule_AddIntMacro(m, PyUFunc_ReorderableNone)
)
return MOD_ERROR_VAL;
return MOD_SUCCESS_VAL(m);
}
#include "_ufunc.c"