-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patharray.c
More file actions
1409 lines (1235 loc) · 28.9 KB
/
Copy patharray.c
File metadata and controls
1409 lines (1235 loc) · 28.9 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
/*
* Array base type
*/
#include <setjmp.h>
#define PY_SSIZE_T_CLEAN
#include <Python.h>
#include <structmember.h>
#include "postgres.h"
#include "fmgr.h"
#include "access/heapam.h"
#include "access/htup.h"
#include "access/transam.h"
#include "access/tupdesc.h"
#include "catalog/pg_proc.h"
#include "catalog/pg_type.h"
#include "catalog/pg_namespace.h"
#include "catalog/pg_conversion.h"
#include "catalog/pg_operator.h"
#include "catalog/pg_opclass.h"
#include "catalog/namespace.h"
#include "nodes/params.h"
#include "parser/parse_func.h"
#include "tcop/dest.h"
#include "tcop/tcopprot.h"
#include "utils/array.h"
#include "utils/datum.h"
#include "utils/elog.h"
#include "utils/palloc.h"
#include "utils/builtins.h"
#include "utils/syscache.h"
#include "utils/relcache.h"
#include "utils/typcache.h"
#include "pypg/python.h"
#include "pypg/postgres.h"
#include "pypg/pl.h"
#include "pypg/error.h"
#include "pypg/type/type.h"
#include "pypg/type/object.h"
#include "pypg/type/bitwise.h"
#include "pypg/type/string.h"
#include "pypg/type/array.h"
/*
* py_list_depth - get the ndims of nested PyLists
*
* If the depth exceeds MAXDIM, an error will be thrown.
* This will protect against recursive lists.
*/
static int
py_list_depth(PyObj seq)
{
int d = 0;
Assert(PyList_CheckExact(seq));
while (PyList_CheckExact(seq))
{
++d;
if (d > MAXDIM)
{
ereport(ERROR,
(errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
errmsg("depth of list exceeds the maximum allowed dimensions (%d)",
MAXDIM),
errhint("A recursive list object can also be the cause this error.")));
}
if (PyList_GET_SIZE(seq) > 0)
seq = PyList_GET_ITEM(seq, 0);
else
{
/*
* Not a PyList, then it's not a "dimension".
*/
break;
}
}
return(d);
}
/*
* py_list_dimensions - given a PyList, calculate the dimensions and ndims
*
* dims should be a MAXDIM sized int array. ndims is returned.
*/
static int
py_list_dimensions(PyObj seq, int *dims)
{
int i, ndims;
/*
* Get the number of dimensions, and make an array for holding
* their measurements.
*/
ndims = py_list_depth(seq);
/*
* The depth is restricted to the counted ndims (py_list_depth).
* This guarantees that 'seq' will always be a PyListObject, so
* use the optimized routines.
*/
for (i = 0; i < ndims; ++i)
{
dims[i] = PyList_GET_SIZE(seq);
if (dims[i] > 0)
seq = PyList_GET_ITEM(seq, 0);
}
return(ndims);
}
/*
* fill_element - create all the Datums and NULLs using PyPgType_DatumNew
*
* This is the nasty routine that navigates through the nested lists coercing
* the objects into the array's element type.
*/
static void
fill_elements(
PyObj element_type, PyObj listob, int mod,
unsigned int nelems, int ndims, int *dims,
Datum *datums, bool *nulls)
{
int elements_per = dims[ndims-1];
int position[MAXDIM] = {0,};
PyObj dstack[MAXDIM] = {listob, NULL,};
unsigned int i = 0; /* current, absolute element position */
int j, axis = 0; /* top */
Assert(PyList_GET_SIZE(listob) == dims[0]);
/*
* The filling of the Datum array ends when the total number of elements
* have been processed. datums and nulls *must* be allocated to fit nelems.
*/
while (i < nelems)
{
/*
* push until we are at element depth.
*/
while (axis < ndims - 1)
{
PyObj pushed;
++axis; /* go deeper */
/*
* use the position of the previous axis to identify which list will be used
* for this one.
*/
pushed = dstack[axis] = PyList_GET_ITEM(dstack[axis-1], position[axis-1]);
position[axis] = 0; /* just started */
/* just consumed position[axis-1], so increment */
position[axis-1] = position[axis-1] + 1;
/*
* Check the object.
*/
if (!PyList_CheckExact(pushed))
{
/*
* Do *not* be nice and instantiate the list because we don't
* want to be holding any references.
*/
PyErr_Format(PyExc_ValueError,
"array boundaries must be list objects not '%s'",
Py_TYPE(pushed)->tp_name);
PyErr_RelayException();
}
/*
* Make sure it's consistent with the expectations.
*
* This check never hits the root list object, but that's fine
* because the dims[] is derived from the list lengths.
*/
if (PyList_GET_SIZE(pushed) != dims[axis])
{
PyErr_Format(PyExc_ValueError,
"cannot make array from unbalanced lists", dims[axis]);
PyErr_RelayException();
}
}
/*
* Build the element datums.
*/
for (j = 0; j < elements_per; ++j)
{
PyPgType_DatumNew(element_type, PyList_GET_ITEM(dstack[ndims-1], j),
mod, &(datums[i]), &(nulls[i]));
++i;
}
/*
* pop it like it's hot
*
* No need to DECREF anything as PyList_GET_ITEM borrows.
*
* Also, the root list is never popped, so stop before zero.
*/
while (axis > 0)
{
/*
* Stop pop'ing when we identify a position that has more lists to
* process.
*/
--axis;
if (position[axis] < dims[axis])
break;
}
}
}
/*
* array_from_py_list - given an element type and a list(), build an array
*/
static ArrayType *
array_from_py_list(PyObj element_type, PyObj listob, int elemmod)
{
PyPgTypeInfo typinfo = PyPgTypeInfo(element_type);
Datum * volatile datums = NULL;
bool * volatile nulls = NULL;
unsigned int nelems;
int i, ndims;
int dims[MAXDIM];
int lbs[MAXDIM];
ArrayType *rat = NULL;
ndims = py_list_dimensions(listob, dims);
Assert(ndims <= MAXDIM);
/*
* From the dimensions, calculate the expected number of elements.
* At this point it is not known if the array is balanced
*/
nelems = dims[ndims-1];
for (i = ndims-2; i > -1; --i)
{
unsigned int n = nelems * dims[i];
if (n < nelems)
{
elog(ERROR, "too many elements for array");
}
nelems = n;
}
if (nelems == 0 && ndims > 1)
elog(ERROR, "malformed nesting of list objects");
PG_TRY();
{
/*
* palloc0 as cleanup in the PG_CATCH() will depend on this.
*/
nulls = palloc0(sizeof(bool) * nelems);
datums = palloc0(sizeof(Datum) * nelems);
/*
* elog's on failure, this will validate the balance/sizes of the
* dimensions.
*/
fill_elements(element_type, listob, elemmod, nelems, ndims, dims,
(Datum *) datums, (bool *) nulls);
/*
* Arrays built from lists don't support custom lower bounds,
* so initialize it to the default '1'.
*/
for (i = 0; i < ndims; ++i)
lbs[i] = 1;
/*
* Everything has been allocated, make the array.
*/
rat = construct_md_array(
(Datum *) datums, (bool *) nulls,
ndims, dims, lbs,
typinfo->typoid,
typinfo->typlen,
typinfo->typbyval,
typinfo->typalign);
/*
* Cleanup.
*/
if (!typinfo->typbyval)
{
for (i = 0; i < nelems; ++i)
{
/*
* Array construction completed successfully,
* so go over the entire array of datums.
*/
if (!nulls[i])
pfree(DatumGetPointer(datums[i]));
}
}
pfree((bool *) nulls);
pfree((Datum *) datums);
}
PG_CATCH();
{
/*
* Try and cleanup as much memory as possible.
*
* Currently, this code will run in the procedure context,
* so whatever leaks here will remain allocated for the duration of the
* procedure.
*/
if (rat != NULL)
{
/*
* When rat != NULL, failure occurred after the array
* was built, which means it had trouble freeing the resources.
* Attempt to free rat, but leave it at that.
*/
pfree(rat);
}
else
{
if (datums != NULL && nulls != NULL)
{
if (!typinfo->typbyval)
{
/*
* This is a bit different from the non-error case;
* rather than pfree'ing everything, we watch for
* NULL pointers..
*/
for (i = 0; i < nelems; ++i)
{
char *p = DatumGetPointer(datums[i]);
if (nulls[i])
continue;
if (PointerIsValid(p))
pfree(p);
else
break;
}
}
}
if (datums != NULL)
pfree((Datum *) datums);
if (nulls != NULL)
pfree((bool *) nulls);
}
PG_RE_THROW();
}
PG_END_TRY();
return(rat);
}
/*
* array_from_list - given an element type and a list(), build an array
* using the described structure.
*
* Sets a Python error and returns NULL on failure.
*/
static ArrayType *
array_from_list_and_info(PyObj element_type, PyObj listob, int elemmod,
int ndims, int *dims, int *lbs)
{
PyPgTypeInfo typinfo = PyPgTypeInfo(element_type);
unsigned int nelems;
int i;
Datum * volatile datums = NULL;
bool * volatile nulls = NULL;
ArrayType * volatile rat = NULL;
Assert(PyList_CheckExact(listob));
nelems = PyList_GET_SIZE(listob);
PG_TRY();
{
/*
* palloc0 as cleanup in the PG_CATCH() will depend on this.
*/
nulls = palloc0(sizeof(bool) * nelems);
datums = palloc0(sizeof(Datum) * nelems);
for (i = 0; i < nelems; ++i)
{
PyPgType_DatumNew(element_type, PyList_GET_ITEM(listob, i),
elemmod, (Datum *) &(datums[i]), (bool *) &(nulls[i]));
}
/*
* Everything has been allocated, make the array.
*/
rat = construct_md_array(
(Datum *) datums, (bool *) nulls,
ndims, dims, lbs,
typinfo->typoid,
typinfo->typlen,
typinfo->typbyval,
typinfo->typalign);
/*
* Cleanup.
*/
if (!typinfo->typbyval)
{
for (i = 0; i < nelems; ++i)
{
/*
* Array construction completed successfully,
* so go over the entire array of datums.
*/
if (!nulls[i])
pfree(DatumGetPointer(datums[i]));
}
}
pfree((bool *) nulls);
pfree((Datum *) datums);
}
PG_CATCH();
{
/*
* Try and cleanup as much memory as possible.
*
* Currently, this code will run in the procedure context,
* so whatever leaks here will remain allocated for the duration of the
* procedure. If failure is often the part of a loop, the leaks could
* be problematic.
*/
if (rat != NULL)
{
/*
* When rat != NULL, failure occurred after the array
* was built, which means it had trouble freeing the resources.
* Attempt to free rat, but leave it at that.
*/
pfree((char *) rat);
}
else
{
if (datums != NULL && nulls != NULL)
{
if (!typinfo->typbyval)
{
/*
* This is a bit different from the non-error case;
* rather than pfree'ing everything, we watch for
* NULL pointers..
*/
for (i = 0; i < nelems; ++i)
{
char *p = DatumGetPointer(datums[i]);
if (nulls[i])
continue;
if (PointerIsValid(p))
pfree(p);
else
break;
}
}
}
if (datums != NULL)
pfree((Datum *) datums);
if (nulls != NULL)
pfree((bool *) nulls);
}
PyErr_SetPgError(false);
rat = NULL;
}
PG_END_TRY();
return((ArrayType *) rat);
}
/*
* Concatenate, but subjectively for supporting the distinction drawn by PyList
* objects.
*/
static PyObj
array_add(PyObj self, PyObj with)
{
PyObj wrapper, rob;
if (!PyList_CheckExact(with) && !PyPgObject_Check(with))
{
/*
* It's probably an element object.
*/
wrapper = PyList_New(1);
if (wrapper == NULL)
return(NULL);
PyList_SET_ITEM(wrapper, 0, with);
Py_INCREF(with);
}
else
{
wrapper = with;
Py_INCREF(wrapper);
}
rob = PyPgObject_Operate("||", self, wrapper);
Py_DECREF(wrapper);
return(rob);
}
static PyNumberMethods array_as_number = {
array_add, /* nb_add */
NULL,
};
/*
* len(o) - Python semantics
*/
static Py_ssize_t
py_array_length(PyObj self)
{
ArrayType *at;
at = DatumGetArrayTypeP(PyPgObject_GetDatum(self));
if (ARR_NDIM(at) == 0)
return(0);
else
return(ARR_DIMS(at)[0]);
}
static PyObj
array_item(PyObj self, Py_ssize_t item)
{
volatile PyObj rob = NULL;
PyPgTypeInfo typinfo, atypinfo;
ArrayType *at;
Datum rd;
bool isnull = false;
int index = (int) item;
PyObj elm;
elm = PyPgType_GetElementType(Py_TYPE(self));
typinfo = PyPgTypeInfo(elm);
atypinfo = PyPgTypeInfo(Py_TYPE(self));
at = DatumGetArrayTypeP(PyPgObject_GetDatum(self));
/* convert index */
++index;
if (ARR_NDIM(at) == 0)
{
PyErr_SetString(PyExc_IndexError, "empty array");
return(NULL);
}
/*
* Note that the comparison is '>', not '>='.
*/
if (index > ARR_DIMS(at)[0])
{
PyErr_Format(PyExc_IndexError, "index %d out of range %d",
item, ARR_DIMS(at)[0]);
return(NULL);
}
/*
* Single dimenion array? Get an element.
*/
if (ARR_NDIM(at) == 1)
{
PG_TRY();
{
rd = array_ref(at, 1, &index, atypinfo->typlen,
typinfo->typlen, typinfo->typbyval, typinfo->typalign, &isnull);
if (isnull)
{
rob = Py_None;
Py_INCREF(rob);
}
else
{
/*
* It points into the array structure, so there's no need to free.
*/
rob = PyPgObject_New(elm, rd);
}
}
PG_CATCH();
{
Py_XDECREF(rob);
rob = NULL;
PyErr_SetPgError(false);
return(NULL);
}
PG_END_TRY();
}
else
{
ArrayType *rat;
int lower[MAXDIM] = {index,0,};
int upper[MAXDIM] = {index,0,};
/*
* Multiple dimensions, so get a slice.
*/
PG_TRY();
{
ArrayType *xat;
Datum *elements;
bool *nulls;
int nelems;
int ndims, i;
int lbs[MAXDIM];
int dims[MAXDIM];
xat = array_get_slice(at, 1, upper, lower, atypinfo->typlen,
typinfo->typlen, typinfo->typbyval, typinfo->typalign);
/*
* Eventually, this should probably be changed to change the already
* allocated ArrayType at 'xat', but for now use the available
* interfaces for creating the expected result.
*/
deconstruct_array(xat,
typinfo->typoid, typinfo->typlen, typinfo->typbyval, typinfo->typalign,
&elements, &nulls, &nelems
);
/*
* Alter dims, lbs, and ndims: we are removing the first dimension.
*/
ndims = ARR_NDIM(xat);
for (i = 1; i < ndims; ++i)
lbs[i-1] = ARR_LBOUND(xat)[i];
for (i = 1; i < ndims; ++i)
dims[i-1] = ARR_DIMS(xat)[i];
--ndims;
/*
* Construct the expected result to a Python itemget call.
*/
rat = construct_md_array(elements, nulls, ndims, dims, lbs,
typinfo->typoid, typinfo->typlen, typinfo->typbyval, typinfo->typalign);
pfree(elements);
pfree(nulls);
pfree(xat);
rob = PyPgObject_New(Py_TYPE(self), PointerGetDatum(rat));
pfree(rat);
}
PG_CATCH();
{
PyErr_SetPgError(false);
return(NULL);
}
PG_END_TRY();
}
return(rob);
}
static PyObj
array_slice(PyObj self, Py_ssize_t from, Py_ssize_t to)
{
PyObj elm;
PyPgTypeInfo etc;
ArrayType *at, *rat = NULL;
PyObj rob = NULL;
int idx_lower[MAXDIM] = {(int) from+1, 0,};
int idx_upper[MAXDIM] = {(int) to+1, 0,};
elm = PyPgType_GetElementType(Py_TYPE(self));
Assert(elm != NULL);
etc = PyPgTypeInfo(elm);
Assert(etc != NULL);
at = DatumGetArrayTypeP(PyPgObject_GetDatum(self));
Assert(at != NULL);
PG_TRY();
{
rat = array_get_slice(at, 1, idx_upper, idx_lower,
PyPgTypeInfo(Py_TYPE(self))->typlen,
etc->typlen, etc->typbyval, etc->typalign);
rob = PyPgObject_New(Py_TYPE(self), PointerGetDatum(rat));
if (rob == NULL)
pfree(rat);
}
PG_CATCH();
{
PyErr_SetPgError(false);
return(NULL);
}
PG_END_TRY();
return(rob);
}
static PySequenceMethods array_as_sequence = {
py_array_length, /* sq_length */
NULL, /* sq_concat */
NULL, /* sq_repeat */
array_item, /* sq_item */
array_slice, /* sq_slice */
NULL, /* sq_ass_item */
NULL, /* sq_ass_slice */
NULL, /* sq_contains */
NULL, /* sq_inplace_concat */
NULL, /* sq_inplace_repeat */
};
static PyObj
array_subscript(PyObj self, PyObj arg)
{
Py_ssize_t len = py_array_length(self);
if (PyIndex_Check(arg))
{
Py_ssize_t i = PyNumber_AsSsize_t(arg, PyExc_IndexError);
if (i == -1 && PyErr_Occurred())
return(NULL);
if (i < 0)
i += py_array_length(self);
return(array_item(self, i));
}
else if (PySlice_Check(arg))
{
Py_ssize_t start, stop, step, slicelength;
int r;
r = PySlice_GetIndicesEx((PySliceObject *) arg, len,
&start, &stop, &step, &slicelength);
if (r < 0)
return(NULL);
if (step != 1)
{
/* TODO: implement custom step values for array subscript */
PyErr_Format(PyExc_NotImplementedError,
"unsupported step value in array subscript");
return(NULL);
}
if (slicelength == len && start == 0)
{
Py_INCREF(self);
return(self);
}
return(array_slice(self, start, stop));
}
else
{
PyErr_Format(PyExc_TypeError, "array indexes must be integers, not %.200s",
Py_TYPE(arg)->tp_name);
return(NULL);
}
}
static PyMappingMethods array_as_mapping = {py_array_length, array_subscript,};
static PyObj
array_get_lowerbounds(PyObj self, void *closure)
{
ArrayType *at;
PyObj rob;
int i, ndim, *lbs;
at = DatumGetArrayTypeP(PyPgObject_GetDatum(self));
ndim = ARR_NDIM(at);
lbs = ARR_LBOUND(at);
rob = PyTuple_New(ndim);
for (i = 0; i < ndim; ++i)
{
PyObj ob;
ob = PyLong_FromLong(lbs[i]);
if (ob == NULL)
{
Py_DECREF(rob);
return(NULL);
}
PyTuple_SET_ITEM(rob, i, ob);
}
return(rob);
}
static PyObj
array_get_dimensions(PyObj self, void *closure)
{
ArrayType *at;
PyObj rob;
int i, ndim, *dims;
at = DatumGetArrayTypeP(PyPgObject_GetDatum(self));
ndim = ARR_NDIM(at);
dims = ARR_DIMS(at);
rob = PyTuple_New(ndim);
for (i = 0; i < ndim; ++i)
{
PyObj ob;
ob = PyLong_FromLong(dims[i]);
if (ob == NULL)
{
Py_DECREF(rob);
return(NULL);
}
PyTuple_SET_ITEM(rob, i, ob);
}
return(rob);
}
static PyObj
array_get_ndim(PyObj self, void *closure)
{
PyObj rob;
rob = PyLong_FromLong(ARR_NDIM(DatumGetArrayTypeP(PyPgObject_GetDatum(self))));
return(rob);
}
static PyObj
array_has_null(PyObj self, void *closure)
{
PyObj rob;
if (ARR_HASNULL(DatumGetArrayTypeP(PyPgObject_GetDatum(self))))
rob = Py_True;
else
rob = Py_False;
Py_INCREF(rob);
return(rob);
}
static PyObj
array_get_nelements(PyObj self, void *closure)
{
long nelements;
ArrayType *at;
int ndim, *dims;
at = DatumGetArrayTypeP(PyPgObject_GetDatum(self));
ndim = ARR_NDIM(at);
dims = ARR_DIMS(at);
if (ndim == 0)
nelements = 0;
else
{
int i;
nelements = 1;
for (i = 0; i < ndim; ++i)
{
nelements = (dims[i] * nelements);
}
}
return(PyLong_FromLong(nelements));
}
/*
* Very similar to type_get_Element.
*/
static PyObj
array_get_Element_type(PyObj self, void *closure)
{
PyObj rob;
PyPgTypeInfo typinfo = PyPgTypeInfo(Py_TYPE(self));
rob = typinfo->array.x_yes.typelem_Type;
/*
* The array type shouldn't exist without having an element type.
*/
Assert(rob != NULL);
Py_INCREF(rob);
return(rob);
}
static PyGetSetDef array_getset[] = {
{"Element", array_get_Element_type, NULL,
PyDoc_STR("The array's element type")},
{"dimensions", array_get_dimensions, NULL,
PyDoc_STR("The array's dimensions")},
{"has_null", array_has_null, NULL,
PyDoc_STR("Whether the array has a NULL inside of it")},
{"lowerbounds", array_get_lowerbounds, NULL,
PyDoc_STR("The array's lower bounds")},
{"ndim", array_get_ndim, NULL,
PyDoc_STR("The number of array dimensions")},
{"nelements", array_get_nelements, NULL,
PyDoc_STR("The number of elements in the array")},
{NULL}
};
/*
* Array.get_element(indexes) - Get an element from the array.
*
* This uses Python sequence semantics(zero-based indexes, IndexError's).
*/
static PyObj
array_get_element(PyObj self, PyObj indexes_ob)
{
PyObj tup, element_type, rob = NULL;
PyPgTypeInfo atypinfo, typinfo;
ArrayType *at;
int i, nindexes, indexes[MAXDIM] = {0,};
/*
* Convert the indexes_ob into a tuple and extract the values
* into the indexes[] array. Do any necessary checks along the way.
*/
tup = Py_Call((PyObj) &PyTuple_Type, indexes_ob);
if (tup == NULL)
return(NULL);
nindexes = (int) PyTuple_GET_SIZE(tup);
if (!(nindexes > 0))
{
Py_DECREF(tup);
PyErr_SetString(PyExc_ValueError, "empty index tuple");
return(NULL);
}
at = DatumGetArrayTypeP(PyPgObject_GetDatum(self));
Assert(at != NULL);
if (nindexes != ARR_NDIM(at))
{
Py_DECREF(tup);
if (ARR_NDIM(at) == 0)
PyErr_SetString(PyExc_IndexError, "no elements in array");
else
PyErr_Format(PyExc_ValueError, "element access requires exactly %d indexes, given %d",
ARR_NDIM(at), nindexes);
return(NULL);
}
for (i = 0; i < nindexes; ++i)
{
int index;
index = (int) PyNumber_AsSsize_t(PyTuple_GET_ITEM(tup, i),
NULL);
if (PyErr_Occurred())
{
Py_DECREF(tup);
return(NULL);
}
/*
* Adjust for backwards based access. (feature of get_element)
*/
if (index < 0)
indexes[i] = index + ARR_DIMS(at)[i];
else
indexes[i] = index;
if (indexes[i] >= ARR_DIMS(at)[i] || indexes[i] < 0)
{
PyErr_Format(PyExc_IndexError, "index %d out of range %d for axis %d",
index, ARR_DIMS(at)[0], i);
Py_DECREF(tup);
return(NULL);
}
/*
* Adjust by the lowerbounds..
*/
indexes[i] = indexes[i] + ARR_LBOUND(at)[i];
}
Py_DECREF(tup);
atypinfo = PyPgTypeInfo(Py_TYPE(self));
element_type = PyPgType_GetElementType(Py_TYPE(self));
typinfo = PyPgTypeInfo(element_type);