-
Notifications
You must be signed in to change notification settings - Fork 49
/
_k.c
executable file
·4434 lines (4001 loc) · 111 KB
/
_k.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
999
1000
/* -*- mode: c; c-basic-offset: 4 -*- */
static char __version__[] = "$Revision: 10002$";
/*
K object layout (KXVER < 3, 32 bit):
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6
+-------+---+---+-------+--------
| r | t | u | n | G0 ...
+-------+---+---+-+-----+--------
|g|
+-+-+
| h |
+---+---+
| i |
+-------+-------+
| j |
+-------+ ------+
| e |
+-------+-------+
| f |
+---+---+-------+
| s |
+-------+
| k |
+-------+
K object layout (KXVER >= 3, 64 bit):
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6
+-+-+-+-+-------+---------------+--------
|m|a|t|u| r | n | G0 ...
+-+-+-+-+-------+-+-------------+--------
|g|
+-+-+
| h |
+---+---+
| i |
+-------+-------+
| j |
+-------+ ------+
| e |
+-------+-------+
| f |
+---------------+
| s |
+-------+-------+
| k |
+---------------+
*/
#include "Python.h"
#include "datetime.h"
#include "longintrepr.h"
#include "kx/k.h"
#include <math.h>
#if defined (WIN32) || defined(_WIN32)
#ifndef isfinite
#define isfinite _finite
#endif
double round(double d)
{
return floor(d + 0.5);
}
#endif
#include <stddef.h>
#include <stdlib.h>
#include <float.h>
/* Macrobatics */
#define CAT(x, y) x##y
#define XCAT(x, y) CAT(x, y)
#define SCAT(x, y) (x #y)
#define XSCAT(x, y) SCAT(x, y)
#define SCAT3(x, y, z) (x #y z)
#define XSCAT3(x, y, z) SCAT3(x, y, z)
/* vvv Py3K compatibility vvv */
#if PY_MAJOR_VERSION >= 3
# define PyInt_AsLong PyLong_AsLong
# define PyInt_FromLong PyLong_FromLong
# define PyString_FromStringAndSize PyBytes_FromStringAndSize
# define PyString_AsStringAndSize PyBytes_AsStringAndSize
# define PyString_FromString PyBytes_FromString
# define PY_STR_Check PyUnicode_Check
# define PY_STR_FromString PyUnicode_FromString
# define PY_STR_InternFromString PyUnicode_InternFromString
# define PY_STR_FromFormat PyUnicode_FromFormat
# define PY_STR_Format PyUnicode_Format
# define PY_STR_FromStringAndSize PyUnicode_FromStringAndSize
# define PY_INT_From_LongLong PyLong_FromLongLong
#define PY_SET_SN(var, obj) { \
Py_ssize_t size; \
char *str = PyUnicode_AsUTF8AndSize(obj, &size); \
var = sn(str, (I)size); \
}
static int
PY_STR_AsStringAndSize(PyObject *obj, char **pstr, Py_ssize_t *psize)
{
char *str = PyUnicode_AsUTF8AndSize(obj, psize);
if (str != NULL) {
*pstr = str;
return 0;
}
return -1;
}
# define MOD_ERROR_VAL NULL
# define MOD_SUCCESS_VAL(val) val
# define MOD_INIT(name) PyMODINIT_FUNC PyInit_##name(void)
# define MOD_DEF(ob, name, doc, methods) \
static struct PyModuleDef moduledef = { \
PyModuleDef_HEAD_INIT, name, doc, -1, methods, }; \
ob = PyModule_Create(&moduledef);
#else /* PY_MAJOR_VERSION >= 3 */
# define PY_STR_Check PyString_Check
# define PY_STR_FromString PyString_FromString
# define PY_STR_InternFromString PyString_InternFromString
# define PY_STR_FromFormat PyString_FromFormat
# define PY_STR_Format PyString_Format
# define PY_STR_FromStringAndSize PyString_FromStringAndSize
# define PY_STR_AsStringAndSize PyString_AsStringAndSize
#define PY_SET_SN(var, obj) var = sn(PyString_AS_STRING(obj), \
(I)PyString_GET_SIZE(obj));
# define MOD_ERROR_VAL
# define MOD_SUCCESS_VAL(val)
# define MOD_INIT(name) PyMODINIT_FUNC init##name(void)
# define MOD_DEF(ob, name, doc, methods) \
ob = Py_InitModule3(name, methods, doc);
#if SIZEOF_VOID_P > SIZEOF_LONG /* 64-bit build */
# define PY_INT_From_LongLong PyInt_FromSsize_t
#else
# define PY_INT_From_LongLong(val) \
((val <= (PY_LONG_LONG)LONG_MAX && val >= (PY_LONG_LONG)LONG_MIN) ? \
PyInt_FromLong((long)val) : PyLong_FromLongLong(val))
#endif /* SIZEOF_SIZEOF_VOID_P > SIZEOF_LONG */
#endif /* PY_MAJOR_VERSION >= 3 */
/* ^^^ Py3K compatibility ^^^ */
/* these should be in k.h */
#if KXVER >= 3 && KXVER2 >= 5
K ee(K);
#else
V clr(V);
K1(ee)
{
P(x, x);
x = ka(-128);
xs = "n/a";
clr();
R x;
}
#endif /* ver >= 3.5 */
ZK
km(I i)
{
K x = ka(-KM);
xi = i;
R x;
}
ZK
kuu(I i)
{
K x = ka(-KU);
xi = i;
R x;
}
ZK
kv(I i)
{
K x = ka(-KV);
xi = i;
R x;
}
#include <stdlib.h>
typedef struct {
PyObject_HEAD K x;
} KObject;
static PyTypeObject K_Type;
#define K_Check(op) PyObject_TypeCheck(op, &K_Type)
#define K_CheckExact(op) (Py_TYPE(op) == &K_Type)
#include <stdio.h>
#include <ctype.h>
#ifndef Py_RETURN_NONE
# define Py_RETURN_NONE Py_INCREF(Py_None); return Py_None
#endif
#if PY_VERSION_HEX < 0x2050000
typedef int Py_ssize_t;
typedef Py_ssize_t(*readbufferproc) (PyObject *, Py_ssize_t, void **);
typedef Py_ssize_t(*writebufferproc) (PyObject *, Py_ssize_t, void **);
typedef Py_ssize_t(*segcountproc) (PyObject *, Py_ssize_t *);
#define PyInt_FromSsize_t PyInt_FromLong
#endif
PyDoc_STRVAR(module_doc,
"Low level q interface module.\n"
"\n"
"Provides a K object - python handle to q datatypes.\n"
"\n"
"This module is implemented as a thin layer on top of C API, k.h. Most\n"
"functions described in http://kx.com/q/c/c.txt are\n"
"implemented as methods of the K class. Names of the functions are formed by\n"
"prefixing the names with an _. Note that this module\n"
"is not intended for the end user, but rather as a building block for\n"
"the higher level q module.\n"
"\n"
"Summary of q datatypes\n"
"----------------------\n"
"type: KB KG KH KI KJ KE KF KC KS KD KT KZ 0(nested list)\n"
"code: 1 4 5 6 7 8 9 10 11 14 19 15\n"
">>> KB,KG,KH,KI,KJ,KE,KF,KC,KS,KD,KT,KZ\n"
"(1, 4, 5, 6, 7, 8, 9, 10, 11, 14, 19, 15)\n"
"\n"
"t| size literal q c \n"
"------------------------------------------------\n"
"b 1 0b boolean unsigned char \n"
"x 1 0x0 byte unsigned char \n"
"h 2 0h short short \n"
"i 4 0 int int \n"
"j 8 0j long long long \n"
"e 4 0e real float \n"
"f 8 0.0 float double \n"
"c 1 " " char unsigned char \n"
"s . ` symbol unsigned char* \n"
"p 8 dateDtime timestamp long long \n"
"m 4 2000.01m month int \n"
"d 4 2000.01.01 date int \n"
"z 8 dateTtime datetime double \n"
"n 8 1D02:03 timespan long long \n"
"u 4 00:00 minute int \n"
"v 4 00:00:00 second int \n"
"t 4 00:00:00.000 time int \n"
"* 4 `s$` enum \n");
#define ENUMS_END 77
Z I debug;
/* K objects */
ZK k_none;
ZK k_nil;
ZK k_noargs;
static PyObject *ErrorObject;
/* always consumes x reference */
static PyObject *
KObject_FromK(PyTypeObject * type, K x)
{
KObject *self;
if (!type)
type = &K_Type;
if (x == NULL) {
PyErr_SetString(PyExc_SystemError, "attempted to create null K object");
R NULL;
}
if (xt == -128) {
PyErr_SetString(ErrorObject, xs ? xs : (S) "not set");
R r0(x), NULL;
}
self = (KObject *) type->tp_alloc(type, 0);
if (self)
self->x = x;
else
r0(x);
R (PyObject *) self;
}
/* converter function */
static int
getK(PyObject *arg, void *addr)
{
K r;
if (!K_Check(arg)) {
return PyErr_BadArgument();
}
r = ((KObject *) arg)->x;
/*
if (!r) {
return PyErr_BadArgument();
}
*/
*(K *) addr = r;
return 1;
}
static void
K_dealloc(KObject * self)
{
if (self->x) {
r0(self->x);
}
Py_TYPE(self)->tp_free(self);
}
static PyObject *
K_dot(KObject * self, KObject *args)
{
if (K_Check(args)) {
K x;
Py_BEGIN_ALLOW_THREADS
#if KXVER >= 3 && KXVER2 >= 5
x = dot(self->x, args->x);
if (!x)
x = ee(x);
#else
x = k(0, ".", r1(self->x), r1(args->x), (K)0);
#endif /* ver >= 3.5 */
Py_END_ALLOW_THREADS
return KObject_FromK(Py_TYPE(self), x);
}
return PyErr_Format(PyExc_TypeError, "expected a K object, not %.200s",
Py_TYPE(args)->tp_name);
}
ZK get_backtrace_dl;
Z K2(get_backtrace)
{
K r = ka(-127);
r->k = knk(2, r1(x), r1(y));
R r;
}
static PyObject *
K_trp(KObject * self, KObject *args)
{
if (K_Check(args)) {
K x = knk(3, r1(self->x), r1(args->x),
r1(get_backtrace_dl));
Py_BEGIN_ALLOW_THREADS
x = k(0, "-105!", x, (K)0);
Py_END_ALLOW_THREADS
if (xt != -127) {
return KObject_FromK(Py_TYPE(self), x);
}
else {
K x0, x1;
x0 = kK(xk)[0];
x1 = kK(xk)[1];
PyObject *exc_value, *message, *traceback;
message = PY_STR_FromStringAndSize((S)kG(x0), (Py_ssize_t)x0->n);
traceback = KObject_FromK(Py_TYPE(self), r1(x1));
r0(x);
exc_value = PyObject_CallFunctionObjArgs(ErrorObject, message, traceback, NULL);
Py_DECREF(message);
Py_DECREF(traceback);
PyErr_SetObject(ErrorObject, exc_value);
return NULL;
}
}
return PyErr_Format(PyExc_TypeError, "expected a K object, not %.200s",
Py_TYPE(args)->tp_name);
}
/* extern K a1(K,K); */
static PyObject *
K_a0(KObject * self)
{
K x = self->x;
if (xt < 100) {
Py_INCREF(self);
return (PyObject *)self;
}
Py_BEGIN_ALLOW_THREADS
#if KXVER >= 3 && KXVER2 >= 5
x = dot(x, k_noargs);
if (!x)
x = ee(x);
#else
x = k(0, "@", r1(x), r1(k_none), (K) 0);
#endif /* ver >= 3.5 */
Py_END_ALLOW_THREADS
return KObject_FromK(Py_TYPE(self), x);
}
static PyObject *
K_a1(KObject * self, KObject *arg)
{
if (K_Check(arg)) {
K x, y;
Py_BEGIN_ALLOW_THREADS
#if KXVER >= 3 && KXVER2 >= 5
y = knk(1, r1(arg->x));
x = dot(self->x, y);
r0(y);
if (!x)
x = ee(x);
#else
y = arg->x;
x = k(0, "@", r1(self->x), r1(y), (K)0);
#endif /* ver >= 3.5 */
Py_END_ALLOW_THREADS
return KObject_FromK(Py_TYPE(self), x);
}
return PyErr_Format(PyExc_TypeError, "expected a K object, not %.200s",
Py_TYPE(arg)->tp_name);
}
static PyObject *
K_ja(KObject * self, PyObject *arg)
{
switch (self->x->t) {
case 0:{
if (K_Check(arg))
jk(&self->x, r1(((KObject *) arg)->x));
else
R PyErr_Format(PyExc_TypeError,
"K._ja: expected K object, not %s",
Py_TYPE(arg)->tp_name);
break;
}
case KB:{
if (PyBool_Check(arg)) {
G a = (arg == Py_True);
ja(&self->x, &a);
}
else
R PyErr_Format(PyExc_TypeError, "K._ja: expected bool, not %s",
Py_TYPE(arg)->tp_name);
break;
}
case KG:{
G g;
long a = PyInt_AsLong(arg);
if (a == -1 && PyErr_Occurred())
R NULL;
if (a < 0 || a > 257) {
PyErr_Format(PyExc_OverflowError, "Expected an integer between 0 and 257, not %ld", a);
R NULL;
}
g = (G)a;
ja(&self->x, &g);
break;
}
case KH:{
H h;
long a = PyInt_AsLong(arg);
if (a == -1 && PyErr_Occurred()) {
if (arg == Py_None) {
PyErr_Clear();
h = nh;
}
else
R NULL;
}
else {
h = (H)(a < -wh ? -wh : (a > wh ? wh : a));
}
ja(&self->x, &h);
break;
}
case KI:{
I i;
long long a = PyLong_AsLongLong(arg);
if (a == -1 && PyErr_Occurred()) {
if (arg == Py_None) {
PyErr_Clear();
i = ni;
}
else
R NULL;
}
else {
i = (I)(a < -wi ? -wi : (a > wi ? wi : a));
}
ja(&self->x, &i);
break;
}
case KJ:{
int overflow;
J j = PyLong_AsLongLongAndOverflow(arg, &overflow);
if (j == -1 && PyErr_Occurred()) {
if (arg == Py_None) {
PyErr_Clear();
j = nj;
}
else
R NULL;
}
if (overflow) {
j = overflow * wj;
}
ja(&self->x, &j);
break;
}
case KE:{
E e;
double a = PyFloat_AsDouble(arg);
if (a == -1 && PyErr_Occurred()) {
if (arg == Py_None) {
PyErr_Clear();
e = (E)nf;
} else {
R NULL;
}
}
else {
e = (E)(a <= -FLT_MAX ? -wf : a >= FLT_MAX ? wf : a);
}
ja(&self->x, &e);
break;
}
case KF:{
F a = PyFloat_AsDouble(arg);
if (a == -1 && PyErr_Occurred()) {
if (arg == Py_None) {
PyErr_Clear();
a = nf;
} else {
R NULL;
}
}
ja(&self->x, &a);
break;
}
case KC:{
char *a;
Py_ssize_t n;
if (-1 == PyString_AsStringAndSize(arg, &a, &n))
R NULL;
if (n != 1)
R PyErr_Format(PyExc_TypeError,
"K.ja: a one-, not %zd-character string", n);
ja(&self->x, a);
break;
}
case KS:{
char *a;
Py_ssize_t n;
if (-1 == PyString_AsStringAndSize(arg, &a, &n))
R NULL;
/* TODO: check for overflow in the cast to (I). */
js(&self->x, sn(a, (I)n));
break;
}
case KM:{
}
case KD:{
}
case KZ:{
}
case KU:{
}
case KV:{
}
case KT:{
}
default:
R PyErr_Format(PyExc_NotImplementedError, "appending to type %d",
(int)self->x->t);
}
Py_RETURN_NONE;
}
static PyObject *
K_jv(KObject * self, KObject * arg)
{
if (!K_Check(arg))
R PyErr_Format(PyExc_TypeError, "K._jv: expected K object, not %s",
Py_TYPE(arg)->tp_name);
jv(&self->x, arg->x);
Py_RETURN_NONE;
}
static K k_repr;
static PyObject *
K_str(KObject * self)
{
PyObject *res;
K x = self->x;
switch (xt) {
case KC:
return PY_STR_FromStringAndSize((S) xC, (Py_ssize_t)xn);
case -KS:
return PY_STR_InternFromString(xs);
case -KC:
return PY_STR_FromStringAndSize((S) & xg, 1);
case 101:
if(xj == 0)
return PY_STR_FromString("::");
}
if (-xt >= 20 && -xt < ENUMS_END) {
x = k(0, "value", r1(x), (K)0);
if (xt == -128)
return PyErr_SetString(ErrorObject, xs ? xs : (S) "not set"), r0(x),
NULL;
if (xt == -11) {
res = PY_STR_InternFromString(xs);
r0(x);
return res;
}
/* Pass through - deleted enum vector */
}
x = k(0, "@", r1(k_repr), r1(x), (K) 0);
if (xt == -128)
return PyErr_SetString(ErrorObject, xs ? xs : (S) "not set"), r0(x),
NULL;
res = PY_STR_FromStringAndSize((S) xC, (Py_ssize_t)xn);
r0(x);
return res;
}
static PyObject *
K_repr(KObject * self)
{
K x = self->x;
PyObject *f, *s, *r;
/* special-case :: (issue #663) */
if (xt == 101 && xj == 0)
R PY_STR_FromString("k('::')");
x = k(0, "@", r1(k_repr), r1(x), (K) 0);
if (xt == -128) {
r = PY_STR_FromFormat("<k object at %p of type %hd, '%s>",
self->x, (H) self->xt, xs);
R r0(x), r;
}
f = PY_STR_FromString("k(%r)");
if (f == NULL)
R r0(x), NULL;
s = PY_STR_FromStringAndSize((S) xC, (Py_ssize_t)xn);
if (s == NULL) {
Py_DECREF(f);
R r0(x), NULL;
}
r = PY_STR_Format(f, s);
Py_DECREF(f);
Py_DECREF(s);
R r0(x), r;
}
/** Array interface **/
/* Array Interface flags */
#define CONTIGUOUS 0x001
#define FORTRAN 0x002
#define ALIGNED 0x100
#define NOTSWAPPED 0x200
#define WRITEABLE 0x400
#define ARR_HAS_DESCR 0x800
typedef struct {
int version;
int nd;
char typekind;
int itemsize;
int flags;
Py_intptr_t *shape;
Py_intptr_t *strides;
void *data;
PyObject *descr;
} PyArrayInterface;
/* 01234567890123456789 */
static char typechars[20] = "ObXXuiiiffSOiiifiiii";
static int
k_typekind(K x)
{
unsigned int t = abs(xt);
if (t < sizeof(typechars))
return typechars[t];
if (t < ENUMS_END)
return 'i';
return 'X';
}
static int
k_itemsize(K x)
{
static int itemsizes[] = {
sizeof(void *),
1, /* bool */
#if KXVER >= 3
16,
#else
0,
#endif
0, 1, /* byte */
2, /* short */
4, /* int */
8, /* long */
4, /* float */
8, /* real */
1, /* char */
sizeof(void *), /* symbol */
8, /* timestamp */
4, /* month */
4, /* date */
8, /* datetime */
8, /* timespan */
4, /* minute */
4, /* second */
4, /* time */
};
unsigned int t = abs(xt);
if (t < sizeof(itemsizes) / sizeof(*itemsizes))
return itemsizes[t];
if (t < ENUMS_END)
return 4;
return 0;
}
static void
#if PY_MAJOR_VERSION >= 3
k_array_struct_free(PyObject *cap)
{
void *ptr = PyCapsule_GetPointer(cap, NULL);
void *arr = PyCapsule_GetContext(cap);
#else
k_array_struct_free(void *ptr, void *arr)
{
#endif
PyArrayInterface *inter = (PyArrayInterface *) ptr;
if (inter->shape != NULL)
free(inter->shape);
free(inter);
Py_DECREF((PyObject *)arr);
}
static PyObject *
K_array_struct_get(KObject * self)
{
K x = self->x;
PyArrayInterface *inter;
int typekind, nd;
typekind = k_typekind(x);
if (abs(xt) >= KS || xt == 0) {
PyErr_Format(PyExc_AttributeError, "k object of type %dh", xt);
return NULL;
}
if (!(inter = (PyArrayInterface *) malloc(sizeof(PyArrayInterface))))
goto fail_inter;
nd = (xt >= 0); /* scalars have t < 0 in k4 */
inter->version = 2;
inter->nd = nd;
inter->typekind = typekind;
inter->itemsize = k_itemsize(x);
inter->flags = ALIGNED | NOTSWAPPED | WRITEABLE | CONTIGUOUS;
if (nd) {
if (!(inter->shape = (Py_intptr_t *) malloc(sizeof(Py_intptr_t) * 2)))
goto fail_shape;
inter->shape[0] = (Py_ssize_t)xn;
inter->strides = inter->shape + 1;
inter->strides[0] = inter->itemsize;
inter->data = kG(x);
}
else {
inter->shape = inter->strides = NULL;
inter->data = &xg;
}
Py_INCREF(self);
#if PY_MAJOR_VERSION >= 3
PyObject *cap =
PyCapsule_New(inter, NULL, &k_array_struct_free);
if (PyCapsule_SetContext(cap, self)) {
Py_DECREF(cap);
return NULL;
}
return cap;
#else
return PyCObject_FromVoidPtrAndDesc(inter, self, &k_array_struct_free);
#endif
fail_shape:
free(inter);
fail_inter:
return PyErr_NoMemory();
}
static PyObject *
K_array_typestr_get(KObject * self)
{
K x = self->x;
static int const one = 1;
char const endian = "><"[(int)*(char const *)&one];
char const typekind = k_typekind(x);
return PY_STR_FromFormat("%c%c%d", typekind == 'O' ? '|' : endian,
typekind, k_itemsize(x));
}
static PyObject *K_K(PyTypeObject * type, PyObject *arg);
static int
arg_names(K f, S* names, Py_ssize_t *pn)
{
K x;
switch (f->t) {
case 100:
x = kK(f)[1];
*pn = (Py_ssize_t)xn;
DO(xn, names[i] = xS[i]);
break;
case 101:
*pn = 1;
names[0] = "x";
break;
case 102:
*pn = 2;
names[0] = "x";
names[1] = "y";
break;
case 103:
case 104:
case 106:
x = k(0, ".p.an", r1(f), (K) 0);
*pn = (Py_ssize_t)xn;
DO(xn,names[i] = xS[i]);
r0(x);
break;
default:
PyErr_Format(PyExc_TypeError, "K object of type %d cannot be called with kwds", (int)f->t);
return -1;
}
return 0;
}
static PyObject *
call_args(K f, PyObject *args, PyObject *kwds)
/* Return a tuple of arguments extracted from args and kwds.
Compare to inspect.getcallargs.
*/
{
PyObject *ret, *a;
Py_ssize_t i, n, m;
S names[8];
if (arg_names(f, names, &m) == -1)
return NULL;
n = PyTuple_GET_SIZE(args);
if (n > m) {
PyErr_Format(PyExc_TypeError, "too many positional arguments");
return NULL;
}
if (kwds == NULL) {
Py_INCREF(args);
return args;
}
ret = PyTuple_New(m);
if (ret == NULL)
return NULL;
for (i = 0; i < n; ++i) {
a = PyTuple_GET_ITEM(args, i);
Py_INCREF(a);
PyTuple_SET_ITEM(ret, i, a);
/* check that positional arguments are not duplicated in kwds */
a = PyDict_GetItemString(kwds, names[i]);
if (a != NULL) {
Py_DECREF(ret);
ret = PyErr_Format(PyExc_TypeError,
"duplicate value for argument %s", names[i]);
goto done;
}
}
n = PyDict_Size(kwds);
for (; i < m; ++i) {
a = PyDict_GetItemString(kwds, names[i]);
if (a == NULL)
a = KObject_FromK(&K_Type, r1(k_nil));
else
n--; /* track the count of remaining kwds */
Py_INCREF(a); /* NB: PyDict_GetItemString returns a borrowed reference */
PyTuple_SET_ITEM(ret, i, a);
}
if (n > 0) {
PyErr_Format(PyExc_TypeError, "unexpected keyword argument");
Py_DECREF(ret);
ret = NULL;
}
done:
return ret;
}
static PyObject *
K_callargs(KObject *self, PyObject *args, PyObject *kwds)
{
return call_args(self->x, args, kwds);
}
static PyObject *
K_call(KObject * self, PyObject *args, PyObject *kwds)
{
PyTypeObject *type = Py_TYPE(self);
PyObject *ret = NULL, *kargs;
Py_ssize_t i, n;
if (self->xt >= 100 && kwds != NULL && PyDict_Size(kwds) > 0) {
args = call_args(self->x, args, kwds);
if (args == NULL)
return NULL;
}
else
Py_INCREF(args);
switch ((n = PyTuple_GET_SIZE(args))) {
case 0:
Py_DECREF(args);
R K_a0(self);
case 1: {
PyObject *a = PyTuple_GET_ITEM(args, 0);
if (!K_Check(a)) {
a = PyObject_CallFunctionObjArgs((PyObject *)type, a, NULL);
if (a == NULL) {
Py_DECREF(args);
return NULL;
}
}
else
Py_INCREF(a);
ret = K_a1(self, (KObject *)a);
Py_DECREF(a);
Py_DECREF(args);
return ret;
}
}
/* TODO: Unpack arguments directly to K list */
for (i = 0; i < n; ++i) {
PyObject *old_arg, *new_arg;
old_arg = PyTuple_GET_ITEM(args, i);
if (!K_Check(old_arg)) {
new_arg = PyObject_CallFunctionObjArgs((PyObject *)type, old_arg, NULL);
if (new_arg == NULL) {
Py_DECREF(args);
return NULL;
}
PyTuple_SET_ITEM(args, i, new_arg);
Py_DECREF(old_arg);
}
}
kargs = K_K(type, args);
if (kargs == NULL) {
Py_DECREF(args);
R NULL;
}
ret = K_dot(self, (KObject *)kargs);
Py_DECREF(kargs);
Py_DECREF(args);
R ret;
}
static PyObject*
array_descr(PyObject *obj)
{
PyObject *tmp;
tmp = PyObject_GetAttrString(obj, "dtype");
if (tmp == NULL)
return NULL;
obj = tmp;
tmp = PyObject_GetAttrString(obj, "str");
Py_DECREF(obj);