/
tableobject.c
1226 lines (1011 loc) · 30.8 KB
/
tableobject.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
/*
* Copyright 2004 Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License"); you
* may not use this file except in compliance with the License. You
* may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied. See the License for the specific language governing
* permissions and limitations under the License.
*
* Originally developed by Gregory Trubetskoy.
*
*
* tableobject.c
*
*
*/
#include "mod_python.h"
/** XXX this is a hack. because apr_table_t
is not available in a header file */
#define TABLE_HASH_SIZE 32
struct apr_table_t {
apr_array_header_t a;
#ifdef MAKE_TABLE_PROFILE
void *creator;
#endif
apr_uint32_t index_initialized;
int index_first[TABLE_HASH_SIZE];
int index_last[TABLE_HASH_SIZE];
};
/**
** MpTable_FromTable
**
* This routine creates a Python tableobject given an Apache
* table pointer.
*
*/
PyObject * MpTable_FromTable(apr_table_t *t)
{
tableobject *result;
result = PyObject_New(tableobject, &MpTable_Type);
if (! result)
return PyErr_NoMemory();
result->table = t;
result->pool = NULL;
return (PyObject *)result;
}
/**
** MpTable_New
**
* This returns a new object of built-in type table.
*
* NOTE: The ap_table gets greated in its own pool, which lives
* throught the live of the tableobject. This is because this
* object may persist from hit to hit.
*
* ALSO NOTE: table_new() also creates tables, independent of this
* (it gets called when table is being subclassed)
*
*/
PyObject * MpTable_New()
{
tableobject *t;
apr_pool_t *p;
/* XXX need second arg abort function to report mem error */
apr_pool_create_ex(&p, NULL, NULL, NULL);
/* two is a wild guess */
t = (tableobject *)MpTable_FromTable(apr_table_make(p, 2));
/* remember the pointer to our own pool */
t->pool = p;
return (PyObject *)t;
}
/**
** table_dealloc
**
* Frees table's memory
*/
static void table_dealloc(register tableobject *self)
{
if (MpTable_Check(self)) {
if (self->pool)
apr_pool_destroy(self->pool);
PyObject_Del(self);
}
else
self->ob_type->tp_free((PyObject *)self);
}
/**
** table_print
**
* prints table like a dictionary
*/
static int table_print(register tableobject *self, register FILE *fp, register int flags)
{
const apr_array_header_t *ah = NULL;
apr_table_entry_t *elts;
register int i;
fprintf(fp, "{");
ah = apr_table_elts(self->table);
elts = (apr_table_entry_t *) ah->elts;
i = ah->nelts;
if (i == 0) {
fprintf(fp, "}");
return 0;
}
while (i--)
if (elts[i].key)
{
fprintf(fp, "'%s': '%s'", elts[i].key, elts[i].val);
if (i > 0)
fprintf(fp, ", ");
else
fprintf(fp, "}");
}
return 0;
}
/**
** table_repr
**
* prints table like a dictionary
*/
static PyObject * table_repr(tableobject *self)
{
PyObject *s;
PyObject *t = NULL;
const apr_array_header_t *ah;
apr_table_entry_t *elts;
int i;
s = PyString_FromString("{");
ah = apr_table_elts (self->table);
elts = (apr_table_entry_t *) ah->elts;
i = ah->nelts;
if (i == 0)
PyString_ConcatAndDel(&s, PyString_FromString("}"));
while (i--)
if (elts[i].key)
{
t = PyString_FromString(elts[i].key);
PyString_ConcatAndDel(&s, PyObject_Repr(t));
Py_XDECREF(t);
PyString_ConcatAndDel(&s, PyString_FromString(": "));
if (elts[i].val) {
t = PyString_FromString(elts[i].val);
} else {
t = Py_None;
Py_INCREF(t);
}
PyString_ConcatAndDel(&s, PyObject_Repr(t));
Py_XDECREF(t);
if (i > 0)
PyString_ConcatAndDel(&s, PyString_FromString(", "));
else
PyString_ConcatAndDel(&s, PyString_FromString("}"));
}
return s;
}
/**
** tablelength
**
* Number of elements in a table. Called
* when you do len(table) in Python.
*/
static int tablelength(tableobject *self)
{
return apr_table_elts(self->table)->nelts;
}
/**
** table_subscript
**
* Gets a dictionary item
*/
static PyObject * table_subscript(tableobject *self, register PyObject *key)
{
char *k;
const apr_array_header_t *ah;
apr_table_entry_t *elts;
register int i;
PyObject *list;
if (key && !PyString_CheckExact(key)) {
PyErr_SetString(PyExc_TypeError,
"table keys must be strings");
return NULL;
}
k = PyString_AsString(key);
/* it's possible that we have duplicate keys, so
we can't simply use apr_table_get since that just
returns the first match.
*/
/* PYTHON 2.5: 'PyList_New' uses Py_ssize_t for input parameters */
list = PyList_New(0);
if (!list)
return NULL;
ah = apr_table_elts (self->table);
elts = (apr_table_entry_t *) ah->elts;
i = ah->nelts;
while (i--)
if (elts[i].key) {
if (apr_strnatcasecmp(elts[i].key, k) == 0) {
PyObject *v = NULL;
if (elts[i].val != NULL) {
v = PyString_FromString(elts[i].val);
} else {
v = Py_None;
Py_INCREF(v);
}
/* PYTHON 2.5: 'PyList_Insert' uses Py_ssize_t for input parameters */
PyList_Insert(list, 0, v);
Py_DECREF(v);
}
}
/* if no match */
/* PYTHON 2.5: 'PyList_Size' uses Py_ssize_t for return values (may need overflow check) */
if (PyList_Size(list) == 0) {
Py_DECREF(list);
PyErr_SetObject(PyExc_KeyError, key);
return NULL;
}
/* if we got one match */
/* PYTHON 2.5: 'PyList_Size' uses Py_ssize_t for return values (may need overflow check) */
if (PyList_Size(list) == 1) {
/* PYTHON 2.5: 'PyList_GetItem' uses Py_ssize_t for input parameters */
PyObject *v = PyList_GetItem(list, 0);
Py_INCREF(v);
Py_DECREF(list);
return v;
}
/* else we return a list */
return list;
}
/**
** table_ass_subscript
**
* insert into table dictionary-style
* *** NOTE ***
* Since the underlying ap_table_set makes a *copy* of the string,
* there is no need to increment the reference to the Python
* string passed in.
*/
static int table_ass_subscript(tableobject *self, PyObject *key,
PyObject *val)
{
char *k;
if (key && !PyString_CheckExact(key)) {
PyErr_SetString(PyExc_TypeError,
"table keys must be strings");
return -1;
}
k = PyString_AsString(key);
if (val == NULL) {
apr_table_unset(self->table, k);
}
else {
if (val && !PyString_CheckExact(val)) {
PyErr_SetString(PyExc_TypeError,
"table values must be strings");
return -1;
}
apr_table_set(self->table, k, PyString_AsString(val));
}
return 0;
}
/* table as mapping */
static PyMappingMethods table_as_mapping = {
/* PYTHON 2.5: 'inquiry' should be perhaps replaced with 'lenfunc' */
(inquiry) tablelength, /*mp_length*/
(binaryfunc) table_subscript, /*mp_subscript*/
(objobjargproc) table_ass_subscript, /*mp_ass_subscript*/
};
/**
** table_keys
**
*
* Implements dictionary's keys() method.
*/
static PyObject * table_keys(register tableobject *self)
{
PyObject *v;
const apr_array_header_t *ah;
apr_table_entry_t *elts;
int i, j;
ah = apr_table_elts(self->table);
elts = (apr_table_entry_t *) ah->elts;
/* PYTHON 2.5: 'PyList_New' uses Py_ssize_t for input parameters */
v = PyList_New(ah->nelts);
for (i = 0, j = 0; i < ah->nelts; i++)
{
if (elts[i].key)
{
PyObject *key = PyString_FromString(elts[i].key);
/* PYTHON 2.5: 'PyList_SetItem' uses Py_ssize_t for input parameters */
PyList_SetItem(v, j, key);
j++;
}
}
return v;
}
/**
** table_values
**
*
* Implements dictionary's values() method.
*/
static PyObject * table_values(register tableobject *self)
{
PyObject *v;
const apr_array_header_t *ah;
apr_table_entry_t *elts;
int i, j;
ah = apr_table_elts(self->table);
elts = (apr_table_entry_t *) ah->elts;
/* PYTHON 2.5: 'PyList_New' uses Py_ssize_t for input parameters */
v = PyList_New(ah->nelts);
for (i = 0, j = 0; i < ah->nelts; i++)
{
if (elts[i].key)
{
PyObject *val = NULL;
if (elts[i].val != NULL) {
val = PyString_FromString(elts[i].val);
} else {
val = Py_None;
Py_INCREF(val);
}
/* PYTHON 2.5: 'PyList_SetItem' uses Py_ssize_t for input parameters */
PyList_SetItem(v, j, val);
j++;
}
}
return v;
}
/**
** table_items
**
*
* Implements dictionary's items() method.
*/
static PyObject * table_items(register tableobject *self)
{
PyObject *v;
const apr_array_header_t *ah;
apr_table_entry_t *elts;
int i, j;
ah = apr_table_elts(self->table);
elts = (apr_table_entry_t *) ah->elts;
/* PYTHON 2.5: 'PyList_New' uses Py_ssize_t for input parameters */
v = PyList_New(ah->nelts);
for (i = 0, j = 0; i < ah->nelts; i++)
{
if (elts[i].key)
{
PyObject *keyval = Py_BuildValue("(s,s)", elts[i].key, elts[i].val);
/* PYTHON 2.5: 'PyList_SetItem' uses Py_ssize_t for input parameters */
PyList_SetItem(v, j, keyval);
j++;
}
}
return v;
}
/**
** table_merge
**
* Since tables can only store strings, key/vals from
* mapping object b will be str()ingized.
*/
static int table_merge(tableobject *a, PyObject *b, int override)
{
/* Do it the generic, slower way */
PyObject *keys = PyMapping_Keys(b);
PyObject *iter;
PyObject *key, *value, *skey, *svalue;
int status;
if (keys == NULL)
return -1;
iter = PyObject_GetIter(keys);
Py_DECREF(keys);
if (iter == NULL)
return -1;
for (key = PyIter_Next(iter); key; key = PyIter_Next(iter)) {
skey = PyObject_Str(key);
if (skey == NULL) {
Py_DECREF(iter);
Py_DECREF(key);
return -1;
}
if (!override && apr_table_get(a->table, PyString_AsString(skey)) != NULL) {
Py_DECREF(key);
Py_DECREF(skey);
continue;
}
value = PyObject_GetItem(b, key);
if (value == NULL) {
Py_DECREF(iter);
Py_DECREF(key);
Py_DECREF(skey);
return -1;
}
svalue = PyObject_Str(value);
if (svalue == NULL) {
Py_DECREF(iter);
Py_DECREF(key);
Py_DECREF(skey);
Py_DECREF(value);
return -1;
}
status = table_ass_subscript(a, skey, svalue);
Py_DECREF(key);
Py_DECREF(value);
Py_DECREF(skey);
Py_DECREF(svalue);
if (status < 0) {
Py_DECREF(iter);
return -1;
}
}
Py_DECREF(iter);
if (PyErr_Occurred())
/* Iterator completed, via error */
return -1;
return 0;
}
/**
** table_update
**
*/
static PyObject *table_update(tableobject *self, PyObject *other)
{
if (table_merge(self, other, 1) < 0)
return NULL;
Py_INCREF(Py_None);
return Py_None;
}
/**
** table_mergefromseq2
**
* Similar to PyDict_MergeFromSeq2 (code borrowed from there).
*/
static int table_mergefromseq2(tableobject *self, PyObject *seq2, int override)
{
PyObject *it; /* iter(seq2) */
int i; /* index into seq2 of current element */
PyObject *item; /* seq2[i] */
PyObject *fast; /* item as a 2-tuple or 2-list */
it = PyObject_GetIter(seq2);
if (it == NULL)
return -1;
for (i = 0; ; ++i) {
PyObject *key, *value, *skey, *svalue;
int n;
fast = NULL;
item = PyIter_Next(it);
if (item == NULL) {
if (PyErr_Occurred())
goto Fail;
break;
}
/* Convert item to sequence, and verify length 2. */
fast = PySequence_Fast(item, "");
if (fast == NULL) {
if (PyErr_ExceptionMatches(PyExc_TypeError))
PyErr_Format(PyExc_TypeError,
"cannot convert table update "
"sequence element #%d to a sequence",
i);
goto Fail;
}
n = PySequence_Fast_GET_SIZE(fast);
if (n != 2) {
PyErr_Format(PyExc_ValueError,
"table update sequence element #%d "
"has length %d; 2 is required",
i, n);
goto Fail;
}
/* Update/merge with this (key, value) pair. */
key = PySequence_Fast_GET_ITEM(fast, 0);
value = PySequence_Fast_GET_ITEM(fast, 1);
skey = PyObject_Str(key);
if (skey == NULL)
goto Fail;
svalue = PyObject_Str(value);
if (svalue == NULL) {
Py_DECREF(svalue);
goto Fail;
}
if (override || apr_table_get(self->table,
PyString_AsString(skey)) == NULL) {
int status = table_ass_subscript(self, skey, svalue);
if (status < 0) {
Py_DECREF(skey);
Py_DECREF(svalue);
goto Fail;
}
}
Py_DECREF(skey);
Py_DECREF(svalue);
Py_DECREF(fast);
Py_DECREF(item);
}
i = 0;
goto Return;
Fail:
Py_XDECREF(item);
Py_XDECREF(fast);
i = -1;
Return:
Py_DECREF(it);
return i;
}
/**
** table_copy
**
*/
static PyObject *table_copy(register tableobject *from)
{
tableobject *to = (tableobject *)MpTable_New();
if (to != NULL)
apr_table_overlap(to->table, from->table, 0);
return (PyObject*)to;
}
/**
** table_compare
**
*/
static int table_compare(tableobject *a, tableobject *b)
{
/*
we're so lazy that we just copy tables to dicts
and rely on dict's compare ability. this is not
the best way to do this to say the least
*/
PyObject *ad, *bd;
int result;
ad = PyDict_New();
bd = PyDict_New();
PyDict_Merge(ad, (PyObject*)a, 0);
PyDict_Merge(bd, (PyObject*)b, 0);
result = PyObject_Compare(ad, bd);
Py_DECREF(ad);
Py_DECREF(bd);
return result;
}
/**
** table_richcompare
**
*/
static PyObject *table_richcompare(PyObject *v, PyObject *w, int op)
{
Py_INCREF(Py_NotImplemented); /* XXX */
return Py_NotImplemented;
}
/**
** table_has_key
**
*/
static PyObject * table_has_key(tableobject *self, PyObject *key)
{
const char *val;
if (!PyString_CheckExact(key)) {
PyErr_SetString(PyExc_TypeError,
"table keys must be strings");
return NULL;
}
val = apr_table_get(self->table, PyString_AsString(key));
if (val)
return PyInt_FromLong(1);
else
return PyInt_FromLong(0);
}
/**
** table_get
**
* implements get([failobj]) method
*/
static PyObject *table_get(register tableobject *self, PyObject *args)
{
PyObject *key;
PyObject *failobj = Py_None;
PyObject *val = NULL;
const char *sval;
if (!PyArg_ParseTuple(args, "S|O:get", &key, &failobj))
return NULL;
sval = apr_table_get(self->table, PyString_AsString(key));
if (sval == NULL) {
val = failobj;
Py_INCREF(val);
}
else
val = PyString_FromString(sval);
return val;
}
/**
** table_setdefault
**
* implements setdefault(key, [val]) method
*/
static PyObject *table_setdefault(register tableobject *self, PyObject *args)
{
PyObject *key;
PyObject *failobj = NULL;
PyObject *val = NULL;
char *k = NULL;
const char *v = NULL;
if (!PyArg_ParseTuple(args, "O|O:setdefault", &key, &failobj))
return NULL;
if (!PyString_CheckExact(key)) {
PyErr_SetString(PyExc_TypeError,
"table keys must be strings");
return NULL;
}
if (failobj && !PyString_CheckExact(failobj)) {
PyErr_SetString(PyExc_TypeError,
"table values must be strings");
return NULL;
}
k = PyString_AsString(key);
v = apr_table_get(self->table, k);
if (v == NULL) {
if (failobj == NULL) {
apr_table_set(self->table, k, "");
val = PyString_FromString("");
}
else {
apr_table_set(self->table, k, PyString_AsString(failobj));
val = failobj;
Py_XINCREF(val);
}
}
else
val = PyString_FromString(v);
return val;
}
/**
** table_clear
**
*/
static PyObject *table_clear(register tableobject *self)
{
apr_table_clear(self->table);
Py_INCREF(Py_None);
return Py_None;
}
/**
** table_popitem
**
*/
static PyObject *table_popitem(tableobject *self)
{
apr_array_header_t *ah;
apr_table_entry_t *elts;
PyObject *res;
ah = (apr_array_header_t *) apr_table_elts(self->table);
elts = (apr_table_entry_t *) ah->elts;
if (ah->nelts == 0) {
PyErr_SetString(PyExc_KeyError,
"popitem(): table is empty");
return NULL;
}
res = Py_BuildValue("(s,s)", elts[0].key, elts[0].val);
ah->nelts--;
elts++;
return res;
}
/**
** table_traverse
**
*/
static int table_traverse(tableobject *self, visitproc visit, void *arg)
{
const apr_array_header_t *ah;
apr_table_entry_t *elts;
register int i;
ah = apr_table_elts (self->table);
elts = (apr_table_entry_t *) ah->elts;
i = ah->nelts;
while (i--)
if (elts[i].key) {
int err;
PyObject *v = NULL;
if (elts[i].val != NULL) {
v = PyString_FromString(elts[i].val);
} else {
v = Py_None;
Py_INCREF(v);
}
err = visit(v, arg);
Py_XDECREF(v);
if (err)
return err;
}
return 0;
}
/**
** table_tp_clear
**
*/
static int table_tp_clear(tableobject *self)
{
table_clear(self);
return 0;
}
/**
** mp_table_add
**
* this function is equivalent of ap_table_add -
* it can create duplicate entries.
*/
static PyObject * mp_table_add(tableobject *self, PyObject *args)
{
const char *val, *key;
if (! PyArg_ParseTuple(args, "ss", &key, &val))
return NULL;
apr_table_add(self->table, key, val);
Py_INCREF(Py_None);
return Py_None;
}
typedef PyObject * (*tableselectfunc)(apr_table_entry_t *);
staticforward PyObject *tableiter_new(tableobject *, tableselectfunc);
static PyObject *select_key(apr_table_entry_t *elts)
{
return PyString_FromString(elts->key);
}
static PyObject *select_value(apr_table_entry_t *elts)
{
PyObject *val = NULL;
if (elts->val != NULL) {
val = PyString_FromString(elts->val);
} else {
val = Py_None;
Py_INCREF(val);
}
return val;
}
static PyObject *select_item(apr_table_entry_t *elts)
{
return Py_BuildValue("(s,s)", elts->key, elts->val);
}
static PyObject *table_iterkeys(tableobject *self)
{
return tableiter_new(self, select_key);
}
static PyObject *table_itervalues(tableobject *self)
{
return tableiter_new(self, select_value);
}
static PyObject *table_iteritems(tableobject *self)
{
return tableiter_new(self, select_item);
}
static char has_key__doc__[] =
"T.has_key(k) -> 1 if T has a key k, else 0";
static char get__doc__[] =
"T.get(k[,d]) -> T[k] if T.has_key(k), else d. d defaults to None.";
static char setdefault_doc__[] =
"T.setdefault(k[,d]) -> T.get(k,d), also set T[k]=d if not T.has_key(k)";
static char popitem__doc__[] =
"T.popitem() -> (k, v), remove and return some (key, value) pair as a\n\
2-tuple; but raise KeyError if T is empty";
static char keys__doc__[] =
"T.keys() -> list of T's keys";
static char items__doc__[] =
"T.items() -> list of T's (key, value) pairs, as 2-tuples";
static char values__doc__[] =
"T.values() -> list of T's values";
static char update__doc__[] =
"T.update(E) -> None. Update T from E: for k in E.keys(): T[k] = E[k]";
static char clear__doc__[] =
"T.clear() -> None. Remove all items from T.";
static char copy__doc__[] =
"T.copy() -> a shallow copy of T";
static char iterkeys__doc__[] =
"T.iterkeys() -> an iterator over the keys of T";
static char itervalues__doc__[] =
"T.itervalues() -> an iterator over the values of T";
static char iteritems__doc__[] =
"T.iteritems() -> an iterator over the (key, value) items of T";
static char add__doc__[] =
"T.add(k, v) -> add (as oppsed to replace) a key k and value v";
/* table method definitions */
static PyMethodDef mp_table_methods[] = {
{"has_key", (PyCFunction)table_has_key, METH_O, has_key__doc__},
{"get", (PyCFunction)table_get, METH_VARARGS, get__doc__},
{"setdefault", (PyCFunction)table_setdefault, METH_VARARGS, setdefault_doc__},
{"popitem", (PyCFunction)table_popitem, METH_NOARGS, popitem__doc__},
{"keys", (PyCFunction)table_keys, METH_NOARGS, keys__doc__},
{"items", (PyCFunction)table_items, METH_NOARGS, items__doc__},
{"values", (PyCFunction)table_values, METH_NOARGS, values__doc__},
{"update", (PyCFunction)table_update, METH_O, update__doc__},
{"clear", (PyCFunction)table_clear, METH_NOARGS, clear__doc__},
{"copy", (PyCFunction)table_copy, METH_NOARGS, copy__doc__},
{"iterkeys", (PyCFunction)table_iterkeys, METH_NOARGS, iterkeys__doc__},
{"itervalues", (PyCFunction)table_itervalues, METH_NOARGS, itervalues__doc__},
{"iteritems", (PyCFunction)table_iteritems, METH_NOARGS, iteritems__doc__},
{"add", (PyCFunction)mp_table_add, METH_VARARGS, add__doc__},
{NULL, NULL} /* sentinel */
};
static int table_contains(tableobject *self, PyObject *key)
{
return apr_table_get(self->table, PyString_AsString(key)) != NULL;
}
/* Hack to implement "key in table" */
static PySequenceMethods table_as_sequence = {
0, /* sq_length */
0, /* sq_concat */
0, /* sq_repeat */
0, /* sq_item */
0, /* sq_slice */
0, /* sq_ass_item */
0, /* sq_ass_slice */
(objobjproc)table_contains, /* sq_contains */
0, /* sq_inplace_concat */
0, /* sq_inplace_repeat */
};
/**