-
Notifications
You must be signed in to change notification settings - Fork 15
/
_Acquisition.c
2150 lines (1811 loc) · 57.8 KB
/
_Acquisition.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 (c) 1996-2003 Zope Foundation and Contributors.
All Rights Reserved.
This software is subject to the provisions of the Zope Public License,
Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
FOR A PARTICULAR PURPOSE
****************************************************************************/
#include "ExtensionClass/ExtensionClass.h"
#include "ExtensionClass/_compat.h"
#define _IN_ACQUISITION_C
#include "Acquisition/Acquisition.h"
static ACQUISITIONCAPI AcquisitionCAPI;
static void
PyVar_Assign(PyObject **v, PyObject *e)
{
Py_XDECREF(*v);
*v=e;
}
#define ASSIGN(V,E) PyVar_Assign(&(V),(E))
#define UNLESS(E) if (!(E))
#define UNLESS_ASSIGN(V,E) ASSIGN(V,E); UNLESS(V)
#define OBJECT(O) ((PyObject*)(O))
/* sizeof("x") == 2 because of the '\0' byte. */
#define STR_STARTSWITH(ob, pattern) ((strncmp(ob, pattern, sizeof(pattern) - 1) == 0))
#define STR_EQ(ob, pattern) ((strcmp(ob, pattern) == 0))
#if PY_VERSION_HEX < 0x02050000 && !defined(PY_SSIZE_T_MIN)
typedef int Py_ssize_t;
typedef Py_ssize_t (*lenfunc)(PyObject *);
typedef PyObject *(*ssizeargfunc)(PyObject *, Py_ssize_t);
typedef PyObject *(*ssizessizeargfunc)(PyObject *, Py_ssize_t, Py_ssize_t);
typedef int(*ssizeobjargproc)(PyObject *, Py_ssize_t, PyObject *);
typedef int(*ssizessizeobjargproc)(PyObject *, Py_ssize_t, Py_ssize_t, PyObject *);
#define PY_SSIZE_T_MAX INT_MAX
#define PY_SSIZE_T_MIN INT_MIN
#endif
static PyObject *py__add__, *py__sub__, *py__mul__, *py__div__,
*py__mod__, *py__pow__, *py__divmod__, *py__lshift__, *py__rshift__,
*py__and__, *py__or__, *py__xor__, *py__coerce__, *py__neg__,
*py__pos__, *py__abs__, *py__nonzero__, *py__invert__, *py__int__,
*py__long__, *py__float__, *py__oct__, *py__hex__,
*py__getitem__, *py__setitem__, *py__delitem__,
*py__getslice__, *py__setslice__, *py__delslice__, *py__contains__,
*py__len__, *py__of__, *py__call__, *py__repr__, *py__str__, *py__unicode__,
*py__cmp__, *py__parent__, *py__iter__, *py__bool__, *py__index__, *py__iadd__,
*py__isub__, *py__imul__, *py__imod__, *py__ipow__, *py__ilshift__, *py__irshift__,
*py__iand__, *py__ixor__, *py__ior__, *py__floordiv__, *py__truediv__,
*py__ifloordiv__, *py__itruediv__, *py__matmul__, *py__imatmul__, *py__idiv__;
static PyObject *Acquired=0;
static void
init_py_names(void)
{
#define INIT_PY_NAME(N) py ## N = NATIVE_FROM_STRING(#N)
INIT_PY_NAME(__add__);
INIT_PY_NAME(__sub__);
INIT_PY_NAME(__mul__);
INIT_PY_NAME(__div__);
INIT_PY_NAME(__mod__);
INIT_PY_NAME(__pow__);
INIT_PY_NAME(__divmod__);
INIT_PY_NAME(__lshift__);
INIT_PY_NAME(__rshift__);
INIT_PY_NAME(__and__);
INIT_PY_NAME(__or__);
INIT_PY_NAME(__xor__);
INIT_PY_NAME(__coerce__);
INIT_PY_NAME(__neg__);
INIT_PY_NAME(__pos__);
INIT_PY_NAME(__abs__);
INIT_PY_NAME(__nonzero__);
INIT_PY_NAME(__bool__);
INIT_PY_NAME(__invert__);
INIT_PY_NAME(__int__);
INIT_PY_NAME(__long__);
INIT_PY_NAME(__float__);
INIT_PY_NAME(__oct__);
INIT_PY_NAME(__hex__);
INIT_PY_NAME(__getitem__);
INIT_PY_NAME(__setitem__);
INIT_PY_NAME(__delitem__);
INIT_PY_NAME(__getslice__);
INIT_PY_NAME(__setslice__);
INIT_PY_NAME(__delslice__);
INIT_PY_NAME(__contains__);
INIT_PY_NAME(__len__);
INIT_PY_NAME(__of__);
INIT_PY_NAME(__call__);
INIT_PY_NAME(__repr__);
INIT_PY_NAME(__str__);
INIT_PY_NAME(__unicode__);
INIT_PY_NAME(__cmp__);
INIT_PY_NAME(__parent__);
INIT_PY_NAME(__iter__);
INIT_PY_NAME(__index__);
INIT_PY_NAME(__iadd__);
INIT_PY_NAME(__isub__);
INIT_PY_NAME(__imul__);
INIT_PY_NAME(__imod__);
INIT_PY_NAME(__ipow__);
INIT_PY_NAME(__ilshift__);
INIT_PY_NAME(__irshift__);
INIT_PY_NAME(__iand__);
INIT_PY_NAME(__ixor__);
INIT_PY_NAME(__ior__);
INIT_PY_NAME(__floordiv__);
INIT_PY_NAME(__truediv__);
INIT_PY_NAME(__ifloordiv__);
INIT_PY_NAME(__itruediv__);
INIT_PY_NAME(__matmul__);
INIT_PY_NAME(__imatmul__);
INIT_PY_NAME(__idiv__);
#undef INIT_PY_NAME
}
static PyObject *
CallMethod(PyObject *self, PyObject *name, PyObject *args, PyObject *kwargs)
{
PyObject *callable, *result;
if ((callable = PyObject_GetAttr(self, name)) == NULL) {
return NULL;
}
result = PyEval_CallObjectWithKeywords(callable, args, kwargs);
Py_DECREF(callable);
return result;
}
static PyObject *
CallMethodArgs(PyObject *self, PyObject *name, char *format, ...)
{
va_list args;
PyObject *py_args, *result;
va_start(args, format);
py_args = Py_VaBuildValue(format, args);
va_end(args);
if (py_args == NULL) {
return NULL;
}
result = CallMethod(self, name, py_args, NULL);
Py_DECREF(py_args);
return result;
}
/* For obscure reasons, we need to use tp_richcompare instead of tp_compare.
* The comparisons here all most naturally compute a cmp()-like result.
* This little helper turns that into a bool result for rich comparisons.
*/
static PyObject *
diff_to_bool(int diff, int op)
{
PyObject *result;
int istrue;
switch (op) {
case Py_EQ: istrue = diff == 0; break;
case Py_NE: istrue = diff != 0; break;
case Py_LE: istrue = diff <= 0; break;
case Py_GE: istrue = diff >= 0; break;
case Py_LT: istrue = diff < 0; break;
case Py_GT: istrue = diff > 0; break;
default:
assert(! "op unknown");
istrue = 0; /* To shut up compiler */
}
result = istrue ? Py_True : Py_False;
Py_INCREF(result);
return result;
}
static PyObject*
convert_name(PyObject *name)
{
#ifdef Py_USING_UNICODE
if (PyUnicode_Check(name)) {
name = PyUnicode_AsEncodedString(name, NULL, NULL);
}
else
#endif
if (!PyBytes_Check(name)) {
PyErr_SetString(PyExc_TypeError, "attribute name must be a string");
return NULL;
}
else {
Py_INCREF(name);
}
return name;
}
/* Returns 1 if the current exception set is AttributeError otherwise 0.
* On 1 the AttributeError is removed from the global error indicator.
* On 0 the global error indactor is still set.
*/
static int
swallow_attribute_error(void)
{
PyObject* error;
if ((error = PyErr_Occurred()) == NULL) {
return 0;
}
if (PyErr_GivenExceptionMatches(error, PyExc_AttributeError)) {
PyErr_Clear();
return 1;
}
return 0;
}
/* Declarations for objects of type Wrapper */
typedef struct {
PyObject_HEAD
PyObject *obj;
PyObject *container;
} Wrapper;
static PyExtensionClass Wrappertype, XaqWrappertype;
#define isImplicitWrapper(o) (Py_TYPE(o) == (PyTypeObject*)&Wrappertype)
#define isExplicitWrapper(o) (Py_TYPE(o) == (PyTypeObject*)&XaqWrappertype)
#define isWrapper(o) (isImplicitWrapper(o) || isExplicitWrapper(o))
/* Same as isWrapper but does a check for NULL pointer. */
#define XisWrapper(o) ((o) ? isWrapper(o) : 0)
#define WRAPPER(O) ((Wrapper*)(O))
#define newWrapper(obj, container, Wrappertype) \
PyObject_CallFunctionObjArgs(OBJECT(Wrappertype), obj, container, NULL)
static char *init_kwlist[] = {"obj", "container", NULL};
static int
Wrapper_init(Wrapper *self, PyObject *args, PyObject *kwargs)
{
int rc;
PyObject *obj, *container;
rc = PyArg_ParseTupleAndKeywords(
args, kwargs, "OO:__init__", init_kwlist, &obj, &container);
if (!rc) {
return -1;
}
if (self == WRAPPER(obj)) {
PyErr_SetString(PyExc_ValueError,
"Cannot wrap acquisition wrapper "
"in itself (Wrapper__init__)");
return -1;
}
/* Avoid memory leak if __init__ is called multiple times. */
Py_CLEAR(self->obj);
Py_CLEAR(self->container);
Py_INCREF(obj);
self->obj = obj;
if (container != Py_None) {
Py_INCREF(container);
self->container = container;
}
return 0;
}
static PyObject *
Wrapper__new__(PyTypeObject *type, PyObject *args, PyObject *kwargs)
{
Wrapper *self = WRAPPER(type->tp_alloc(type, 0));
if (Wrapper_init(self, args, kwargs) == -1) {
Py_DECREF(self);
return NULL;
}
return OBJECT(self);
}
static int
Wrapper__init__(Wrapper *self, PyObject *args, PyObject *kwargs)
{
return Wrapper_init(self, args, kwargs);
}
/* ---------------------------------------------------------------- */
/* Creates a new Wrapper object with the values from the old one.
* Steals a reference from 'ob' (also in the error case).
* Returns a new reference.
* Returns NULL on error.
*/
static PyObject *
clone_wrapper(Wrapper *ob)
{
PyObject *tmp;
/* Only clone if its shared with others. */
if (Py_REFCNT(ob) == 1) {
return (PyObject*) ob;
}
tmp = newWrapper(ob->obj, ob->container, Py_TYPE(ob));
Py_DECREF(ob);
return tmp;
}
static PyObject *
__of__(PyObject *inst, PyObject *parent)
{
PyObject *result;
result = PyObject_CallMethodObjArgs(inst, py__of__, parent, NULL);
if (XisWrapper(result) && XisWrapper(WRAPPER(result)->container)) {
while (XisWrapper(WRAPPER(result)->obj) &&
(WRAPPER(WRAPPER(result)->obj)->container ==
WRAPPER(WRAPPER(result)->container)->obj)) {
/* Copy it, because the result could be shared with others. */
if ((result = clone_wrapper(WRAPPER(result))) == NULL) {
return NULL;
}
/* Simplify wrapper */
Py_XINCREF(WRAPPER(WRAPPER(result)->obj)->obj);
ASSIGN(WRAPPER(result)->obj, WRAPPER(WRAPPER(result)->obj)->obj);
}
}
return result;
}
static PyObject *
apply__of__(PyObject *self, PyObject *inst)
{
PyObject *r;
if (!self) {
r = self;
} else if (has__of__(self)) {
r = __of__(self, inst);
Py_DECREF(self);
} else {
r = self;
}
return r;
}
static PyObject *
get_base(PyObject *ob)
{
while (isWrapper(ob)) {
ob = WRAPPER(ob)->obj;
}
return ob;
}
static PyObject *
Wrapper_descrget(Wrapper *self, PyObject *inst, PyObject *cls)
{
if (inst == NULL) {
Py_INCREF(self);
return OBJECT(self);
}
return __of__(OBJECT(self), inst);
}
static int
Wrapper_traverse(Wrapper *self, visitproc visit, void *arg)
{
Py_VISIT(self->obj);
Py_VISIT(self->container);
return 0;
}
static int
Wrapper_clear(Wrapper *self)
{
Py_CLEAR(self->obj);
Py_CLEAR(self->container);
return 0;
}
static void
Wrapper_dealloc(Wrapper *self)
{
Wrapper_clear(self);
Py_TYPE(self)->tp_free(OBJECT(self));
}
static PyObject *
Wrapper_special(Wrapper *self, char *name, PyObject *oname)
{
PyObject *r = NULL;
switch(*name) {
case 'b':
if (strcmp(name, "base") == 0) {
for (r = self->obj; isWrapper(r); r = WRAPPER(r)->obj) {}
Py_INCREF(r);
return r;
}
break;
case 'p':
if (strcmp(name, "parent") == 0) {
r = self->container ? self->container : Py_None;
Py_INCREF(r);
return r;
}
break;
case 's':
if (strcmp(name, "self") == 0) {
Py_INCREF(self->obj);
return self->obj;
}
break;
case 'e':
if (strcmp(name, "explicit") == 0) {
if (isExplicitWrapper(self)) {
Py_INCREF(self);
return OBJECT(self);
}
return newWrapper(self->obj, self->container, &XaqWrappertype);
}
break;
case 'a':
if (strcmp(name, "acquire") == 0) {
return Py_FindAttr(OBJECT(self), oname);
}
break;
case 'c':
if (strcmp(name, "chain") == 0) {
if ((r = PyList_New(0)) == NULL) {
return NULL;
}
while (PyList_Append(r, OBJECT(self)) == 0) {
if (isWrapper(self) && self->container) {
self = WRAPPER(self->container);
} else {
return r;
}
}
Py_DECREF(r);
return NULL;
}
break;
case 'i':
if (strcmp(name, "inContextOf") == 0) {
return Py_FindAttr(OBJECT(self), oname);
} else if (strcmp(name, "inner") == 0) {
for (r = self->obj; isWrapper(r); r = WRAPPER(r)->obj) {
self = WRAPPER(r);
}
Py_INCREF(self);
return OBJECT(self);
}
break;
case 'u':
if (strcmp(name, "uncle") == 0) {
return NATIVE_FROM_STRING("Bob");
}
break;
}
return NULL;
}
static int
apply_filter(PyObject *filter, PyObject *inst, PyObject *oname, PyObject *r,
PyObject *extra, PyObject *orig)
{
/* Calls the filter, passing arguments.
Returns 1 if the filter accepts the value, 0 if not, -1 if an
exception occurred.
Note the special reference counting rule: This function decrements
the refcount of 'r' when it returns 0 or -1. When it returns 1, it
leaves the refcount unchanged.
*/
PyObject *py_res;
int res;
py_res = PyObject_CallFunctionObjArgs(filter, orig, inst, oname, r, extra, NULL);
if (py_res == NULL) {
Py_DECREF(r);
return -1;
}
res = PyObject_IsTrue(py_res);
Py_DECREF(py_res);
if (res == 0 || res == -1) {
Py_DECREF(r);
return res;
}
return 1;
}
static PyObject *
Wrapper_acquire(Wrapper *self, PyObject *oname,
PyObject *filter, PyObject *extra, PyObject *orig,
int explicit, int containment);
static PyObject *
Wrapper_findattr_name(Wrapper *self, char* name, PyObject *oname,
PyObject *filter, PyObject *extra, PyObject *orig,
int sob, int sco, int explicit, int containment);
static PyObject *
Wrapper_findattr(Wrapper *self, PyObject *oname,
PyObject *filter, PyObject *extra, PyObject *orig,
int sob, int sco, int explicit, int containment)
/*
Parameters:
sob
Search self->obj for the 'oname' attribute
sco
Search self->container for the 'oname' attribute
explicit
Explicitly acquire 'oname' attribute from container (assumed with
implicit acquisition wrapper)
containment
Use the innermost wrapper ("aq_inner") for looking up the 'oname'
attribute.
*/
{
PyObject *tmp, *result;
if ((tmp = convert_name(oname)) == NULL) {
return NULL;
}
result = Wrapper_findattr_name(self, PyBytes_AS_STRING(tmp), oname, filter,
extra, orig, sob, sco, explicit, containment);
Py_XDECREF(tmp);
return result;
}
static PyObject *
Wrapper_findattr_name(Wrapper *self, char* name, PyObject *oname,
PyObject *filter, PyObject *extra, PyObject *orig,
int sob, int sco, int explicit, int containment)
/*
Exactly the same as Wrapper_findattr, except that the incoming
Python name string/unicode object has already been decoded
into a C string. This helper function lets us more easily manage
the lifetime of any temporary allocations.
This function uses Wrapper_acquire, which only takes the original
oname value, not the decoded value. That function can call back into
this one (via Wrapper_findattr). Although that may lead to a few
temporary allocations as we walk through the containment hierarchy,
it is correct: This function may modify its internal view of the
`name` value, and if that were propagated up the hierarchy
the incorrect name may be looked up.
*/
{
PyObject *r;
if (STR_STARTSWITH(name, "aq_") || STR_EQ(name, "__parent__")) {
/* __parent__ is an alias to aq_parent */
name = STR_EQ(name, "__parent__") ? "parent" : name + 3;
if ((r = Wrapper_special(self, name, oname))) {
if (filter) {
switch(apply_filter(filter, OBJECT(self), oname, r, extra, orig)) {
case -1: return NULL;
case 1: return r;
}
} else {
return r;
}
} else {
PyErr_Clear();
}
} else if (STR_STARTSWITH(name, "__") &&
(STR_EQ(name, "__reduce__") ||
STR_EQ(name, "__reduce_ex__") ||
STR_EQ(name, "__getstate__"))) {
return PyObject_GenericGetAttr(OBJECT(self), oname);
}
/* If we are doing a containment search, then replace self with aq_inner */
if (containment) {
while (isWrapper(self->obj)) {
self = WRAPPER(self->obj);
}
}
if (sob) {
if (isWrapper(self->obj)) {
if (self == WRAPPER(self->obj)) {
PyErr_SetString(PyExc_RuntimeError,
"Recursion detected in acquisition wrapper");
return NULL;
}
r = Wrapper_findattr(
WRAPPER(self->obj),
oname,
filter,
extra,
orig,
1,
/* Search object container if explicit,
or object is implicit acquirer */
explicit || isImplicitWrapper(self->obj),
explicit,
containment);
if (r) {
if (PyECMethod_Check(r) && PyECMethod_Self(r) == self->obj) {
ASSIGN(r, PyECMethod_New(r, OBJECT(self)));
}
else if (has__of__(r)) {
ASSIGN(r, __of__(r, OBJECT(self)));
}
return r;
} else if (!swallow_attribute_error()) {
return NULL;
}
}
/* Deal with mixed __parent__ / aq_parent circles */
else if (self->container &&
isWrapper(self->container) &&
WRAPPER(self->container)->container &&
self == WRAPPER(WRAPPER(self->container)->container))
{
PyErr_SetString(PyExc_RuntimeError,
"Recursion detected in acquisition wrapper");
return NULL;
}
/* normal attribute lookup */
else if ((r = PyObject_GetAttr(self->obj, oname))) {
if (r == Acquired) {
Py_DECREF(r);
return Wrapper_acquire(
self, oname, filter, extra, orig, 1, containment);
}
if (PyECMethod_Check(r) && PyECMethod_Self(r) == self->obj) {
ASSIGN(r, PyECMethod_New(r, OBJECT(self)));
} else if (has__of__(r)) {
ASSIGN(r, __of__(r, OBJECT(self)));
}
if (r && filter) {
switch(apply_filter(filter, OBJECT(self), oname, r, extra, orig)) {
case -1: return NULL;
case 1: return r;
}
} else {
return r;
}
} else if (!swallow_attribute_error()) {
return NULL;
}
PyErr_Clear();
}
/* Lookup has failed, acquire it from parent. */
if (sco && (*name != '_' || explicit)) {
return Wrapper_acquire(
self, oname, filter, extra, orig, explicit, containment);
}
PyErr_SetObject(PyExc_AttributeError, oname);
return NULL;
}
static PyObject *
Wrapper_acquire(
Wrapper *self,
PyObject *oname,
PyObject *filter,
PyObject *extra,
PyObject *orig,
int explicit,
int containment)
{
PyObject *r;
int sob = 1;
int sco = 1;
if (!self->container) {
PyErr_SetObject(PyExc_AttributeError, oname);
return NULL;
}
/* If the container has an acquisition wrapper itself,
* we'll use Wrapper_findattr to progress further.
*/
if (isWrapper(self->container)) {
if (isWrapper(self->obj)) {
/* Try to optimize search by recognizing repeated
* objects in path.
*/
if (WRAPPER(self->obj)->container == WRAPPER(self->container)->container) {
sco = 0;
} else if (WRAPPER(self->obj)->container == WRAPPER(self->container)->obj) {
sob = 0;
}
}
/* Don't search the container when the container of the
* container is the same object as 'self'.
*/
if (WRAPPER(self->container)->container == WRAPPER(self)->obj) {
sco = 0;
containment = 1;
}
r = Wrapper_findattr(WRAPPER(self->container), oname, filter, extra,
orig, sob, sco, explicit, containment);
return apply__of__(r, OBJECT(self));
}
/* If the container has a __parent__ pointer, we create an
* acquisition wrapper for it accordingly. Then we can proceed
* with Wrapper_findattr, just as if the container had an
* acquisition wrapper in the first place (see above).
*/
else if ((r = PyObject_GetAttr(self->container, py__parent__))) {
/* Don't search the container when the parent of the parent
* is the same object as 'self'
*/
if (r == WRAPPER(self)->obj) {
sco = 0;
}
else if (WRAPPER(r)->obj == WRAPPER(self)->obj) {
sco = 0;
}
ASSIGN(self->container, newWrapper(self->container, r, &Wrappertype));
/* don't need __parent__ anymore */
Py_DECREF(r);
r = Wrapper_findattr(WRAPPER(self->container), oname, filter, extra,
orig, sob, sco, explicit, containment);
/* There's no need to DECREF the wrapper here because it's
* not stored in self->container, thus 'self' owns its
* reference now
*/
return r;
}
/* The container is the end of the acquisition chain; if we
* can't look up the attribute here, we can't look it up at all.
*/
else {
/* We need to clean up the AttributeError from the previous
* getattr (because it has clearly failed).
*/
if(!swallow_attribute_error()) {
return NULL;
}
if ((r = PyObject_GetAttr(self->container, oname)) == NULL) {
/* May be AttributeError or some other kind of error */
return NULL;
}
if (r == Acquired) {
Py_DECREF(r);
} else if (filter) {
switch(apply_filter(filter, self->container, oname, r, extra, orig)) {
case -1: return NULL;
case 1: return apply__of__(r, OBJECT(self));
}
} else {
return apply__of__(r, OBJECT(self));
}
}
PyErr_SetObject(PyExc_AttributeError, oname);
return NULL;
}
static PyObject *
Wrapper_getattro(Wrapper *self, PyObject *oname)
{
return Wrapper_findattr(self, oname, NULL, NULL, NULL, 1, 1, 0, 0);
}
static PyObject *
Xaq_getattro(Wrapper *self, PyObject *oname)
{
PyObject *tmp, *result;
if ((tmp = convert_name(oname)) == NULL) {
return NULL;
}
/* Special case backward-compatible acquire method. */
if (STR_EQ(PyBytes_AS_STRING(tmp), "acquire")) {
result = Py_FindAttr(OBJECT(self), oname);
} else {
result = Wrapper_findattr(self, oname, NULL, NULL, NULL, 1, 0, 0, 0);
}
Py_DECREF(tmp);
return result;
}
static int
Wrapper_setattro(Wrapper *self, PyObject *oname, PyObject *v)
{
PyObject *tmp = NULL;
char *name = "";
int result;
if ((tmp = convert_name(oname)) == NULL) {
return -1;
}
name = PyBytes_AS_STRING(tmp);
if (STR_EQ(name, "aq_parent") || STR_EQ(name, "__parent__")) {
Py_XINCREF(v);
ASSIGN(self->container, v);
result = 0;
} else {
/* Unwrap passed in wrappers! */
while (v && isWrapper(v)) {
v = WRAPPER(v)->obj;
}
if (v) {
result = PyObject_SetAttr(self->obj, oname, v);
}
else {
result = PyObject_DelAttr(self->obj, oname);
}
}
Py_DECREF(tmp);
return result;
}
static int
Wrapper_compare(Wrapper *self, PyObject *w)
{
PyObject *obj, *wobj;
PyObject *m;
int r;
if (OBJECT(self) == w) {
return 0;
}
if ((m = PyObject_GetAttr(OBJECT(self), py__cmp__)) == NULL) {
PyErr_Clear();
/* Unwrap self completely -> obj. */
obj = get_base(OBJECT(self));
/* Unwrap w completely -> wobj. */
wobj = get_base(w);
if (obj == wobj) {
return 0;
} else if (obj < w) {
return -1;
} else {
return 1;
}
}
ASSIGN(m, PyObject_CallFunction(m, "O", w));
if (m == NULL) {
return -1;
}
r = PyLong_AsLong(m);
Py_DECREF(m);
return r;
}
static PyObject *
Wrapper_richcompare(Wrapper *self, PyObject *w, int op)
{
return diff_to_bool(Wrapper_compare(self, w), op);
}
static PyObject *
Wrapper_repr(Wrapper *self)
{
PyObject *r;
if ((r = PyObject_GetAttr(OBJECT(self), py__repr__))) {
ASSIGN(r, PyObject_CallFunction(r, NULL, NULL));
return r;
} else {
PyErr_Clear();
return PyObject_Repr(self->obj);
}
}
static PyObject *
Wrapper_str(Wrapper *self)
{
PyObject *r;
if ((r = PyObject_GetAttr(OBJECT(self), py__str__))) {
ASSIGN(r, PyObject_CallFunction(r,NULL,NULL));
return r;
} else {
PyErr_Clear();
return PyObject_Str(self->obj);
}
}
static PyObject *
Wrapper_unicode(Wrapper *self)
{
PyObject *r;
if ((r = PyObject_GetAttr(OBJECT(self), py__unicode__))) {
ASSIGN(r, PyObject_CallFunction(r, NULL, NULL));
return r;
} else {
PyErr_Clear();
return Wrapper_str(self);
}
}
static long
Wrapper_hash(Wrapper *self)
{
return PyObject_Hash(self->obj);
}
static PyObject *
Wrapper_call(PyObject *self, PyObject *args, PyObject *kw)
{
return CallMethod(self, py__call__, args, kw);
}
/* Code to handle accessing Wrapper objects as sequence objects */
static Py_ssize_t
Wrapper_length(PyObject* self)
{
PyObject *result;
PyObject *callable;
PyObject *tres;
Py_ssize_t res;