Development of an internal social media platform with personalised dashboards for students
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

_Acquisition.c 56KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072
  1. /*****************************************************************************
  2. Copyright (c) 1996-2003 Zope Foundation and Contributors.
  3. All Rights Reserved.
  4. This software is subject to the provisions of the Zope Public License,
  5. Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
  6. THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
  7. WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  8. WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
  9. FOR A PARTICULAR PURPOSE
  10. ****************************************************************************/
  11. #include "ExtensionClass/ExtensionClass.h"
  12. #include "ExtensionClass/_compat.h"
  13. #define _IN_ACQUISITION_C
  14. #include "Acquisition/Acquisition.h"
  15. static ACQUISITIONCAPI AcquisitionCAPI;
  16. // Py_XSETREF is undefined in Python 3.4 only, it's present in 2.7 and 3.5
  17. #ifndef Py_XSETREF
  18. #define Py_XSETREF(op, op2) \
  19. do { \
  20. PyObject *_py_tmp = (PyObject *)(op); \
  21. (op) = (op2); \
  22. Py_XDECREF(_py_tmp); \
  23. } while (0)
  24. #endif
  25. #define ASSIGN(dst, src) Py_XSETREF(dst, src)
  26. #define OBJECT(O) ((PyObject*)(O))
  27. /* sizeof("x") == 2 because of the '\0' byte. */
  28. #define STR_STARTSWITH(ob, pattern) ((strncmp(ob, pattern, sizeof(pattern) - 1) == 0))
  29. #define STR_EQ(ob, pattern) ((strcmp(ob, pattern) == 0))
  30. static PyObject *py__add__, *py__sub__, *py__mul__, *py__div__,
  31. *py__mod__, *py__pow__, *py__divmod__, *py__lshift__, *py__rshift__,
  32. *py__and__, *py__or__, *py__xor__, *py__coerce__, *py__neg__,
  33. *py__pos__, *py__abs__, *py__nonzero__, *py__invert__, *py__int__,
  34. *py__long__, *py__float__, *py__oct__, *py__hex__,
  35. *py__getitem__, *py__setitem__, *py__delitem__,
  36. *py__getslice__, *py__setslice__, *py__delslice__, *py__contains__,
  37. *py__len__, *py__of__, *py__call__, *py__repr__, *py__str__, *py__unicode__,
  38. *py__cmp__, *py__parent__, *py__iter__, *py__bool__, *py__index__, *py__iadd__,
  39. *py__isub__, *py__imul__, *py__imod__, *py__ipow__, *py__ilshift__, *py__irshift__,
  40. *py__iand__, *py__ixor__, *py__ior__, *py__floordiv__, *py__truediv__,
  41. *py__ifloordiv__, *py__itruediv__, *py__matmul__, *py__imatmul__, *py__idiv__;
  42. static PyObject *Acquired = NULL;
  43. static void
  44. init_py_names(void)
  45. {
  46. #define INIT_PY_NAME(N) py ## N = NATIVE_FROM_STRING(#N)
  47. INIT_PY_NAME(__add__);
  48. INIT_PY_NAME(__sub__);
  49. INIT_PY_NAME(__mul__);
  50. INIT_PY_NAME(__div__);
  51. INIT_PY_NAME(__mod__);
  52. INIT_PY_NAME(__pow__);
  53. INIT_PY_NAME(__divmod__);
  54. INIT_PY_NAME(__lshift__);
  55. INIT_PY_NAME(__rshift__);
  56. INIT_PY_NAME(__and__);
  57. INIT_PY_NAME(__or__);
  58. INIT_PY_NAME(__xor__);
  59. INIT_PY_NAME(__coerce__);
  60. INIT_PY_NAME(__neg__);
  61. INIT_PY_NAME(__pos__);
  62. INIT_PY_NAME(__abs__);
  63. INIT_PY_NAME(__nonzero__);
  64. INIT_PY_NAME(__bool__);
  65. INIT_PY_NAME(__invert__);
  66. INIT_PY_NAME(__int__);
  67. INIT_PY_NAME(__long__);
  68. INIT_PY_NAME(__float__);
  69. INIT_PY_NAME(__oct__);
  70. INIT_PY_NAME(__hex__);
  71. INIT_PY_NAME(__getitem__);
  72. INIT_PY_NAME(__setitem__);
  73. INIT_PY_NAME(__delitem__);
  74. INIT_PY_NAME(__getslice__);
  75. INIT_PY_NAME(__setslice__);
  76. INIT_PY_NAME(__delslice__);
  77. INIT_PY_NAME(__contains__);
  78. INIT_PY_NAME(__len__);
  79. INIT_PY_NAME(__of__);
  80. INIT_PY_NAME(__call__);
  81. INIT_PY_NAME(__repr__);
  82. INIT_PY_NAME(__str__);
  83. INIT_PY_NAME(__unicode__);
  84. INIT_PY_NAME(__cmp__);
  85. INIT_PY_NAME(__parent__);
  86. INIT_PY_NAME(__iter__);
  87. INIT_PY_NAME(__index__);
  88. INIT_PY_NAME(__iadd__);
  89. INIT_PY_NAME(__isub__);
  90. INIT_PY_NAME(__imul__);
  91. INIT_PY_NAME(__imod__);
  92. INIT_PY_NAME(__ipow__);
  93. INIT_PY_NAME(__ilshift__);
  94. INIT_PY_NAME(__irshift__);
  95. INIT_PY_NAME(__iand__);
  96. INIT_PY_NAME(__ixor__);
  97. INIT_PY_NAME(__ior__);
  98. INIT_PY_NAME(__floordiv__);
  99. INIT_PY_NAME(__truediv__);
  100. INIT_PY_NAME(__ifloordiv__);
  101. INIT_PY_NAME(__itruediv__);
  102. INIT_PY_NAME(__matmul__);
  103. INIT_PY_NAME(__imatmul__);
  104. INIT_PY_NAME(__idiv__);
  105. #undef INIT_PY_NAME
  106. }
  107. static PyObject *
  108. CallMethod(PyObject *self, PyObject *name, PyObject *args, PyObject *kwargs)
  109. {
  110. PyObject *callable, *result;
  111. if ((callable = PyObject_GetAttr(self, name)) == NULL) {
  112. return NULL;
  113. }
  114. result = PyEval_CallObjectWithKeywords(callable, args, kwargs);
  115. Py_DECREF(callable);
  116. return result;
  117. }
  118. static PyObject *
  119. CallMethodArgs(PyObject *self, PyObject *name, char *format, ...)
  120. {
  121. va_list args;
  122. PyObject *py_args, *result;
  123. va_start(args, format);
  124. py_args = Py_VaBuildValue(format, args);
  125. va_end(args);
  126. if (py_args == NULL) {
  127. return NULL;
  128. }
  129. result = CallMethod(self, name, py_args, NULL);
  130. Py_DECREF(py_args);
  131. return result;
  132. }
  133. /* For obscure reasons, we need to use tp_richcompare instead of tp_compare.
  134. * The comparisons here all most naturally compute a cmp()-like result.
  135. * This little helper turns that into a bool result for rich comparisons.
  136. */
  137. static PyObject *
  138. diff_to_bool(int diff, int op)
  139. {
  140. PyObject *result;
  141. int istrue;
  142. switch (op) {
  143. case Py_EQ: istrue = diff == 0; break;
  144. case Py_NE: istrue = diff != 0; break;
  145. case Py_LE: istrue = diff <= 0; break;
  146. case Py_GE: istrue = diff >= 0; break;
  147. case Py_LT: istrue = diff < 0; break;
  148. case Py_GT: istrue = diff > 0; break;
  149. default:
  150. assert(! "op unknown");
  151. istrue = 0; /* To shut up compiler */
  152. }
  153. result = istrue ? Py_True : Py_False;
  154. Py_INCREF(result);
  155. return result;
  156. }
  157. static PyObject*
  158. convert_name(PyObject *name)
  159. {
  160. #ifdef Py_USING_UNICODE
  161. if (PyUnicode_Check(name)) {
  162. name = PyUnicode_AsEncodedString(name, NULL, NULL);
  163. }
  164. else
  165. #endif
  166. if (!PyBytes_Check(name)) {
  167. PyErr_SetString(PyExc_TypeError, "attribute name must be a string");
  168. return NULL;
  169. }
  170. else {
  171. Py_INCREF(name);
  172. }
  173. return name;
  174. }
  175. /* Returns 1 if the current exception set is AttributeError otherwise 0.
  176. * On 1 the AttributeError is removed from the global error indicator.
  177. * On 0 the global error indactor is still set.
  178. */
  179. static int
  180. swallow_attribute_error(void)
  181. {
  182. PyObject* error;
  183. if ((error = PyErr_Occurred()) == NULL) {
  184. return 0;
  185. }
  186. if (PyErr_GivenExceptionMatches(error, PyExc_AttributeError)) {
  187. PyErr_Clear();
  188. return 1;
  189. }
  190. return 0;
  191. }
  192. /* Declarations for objects of type Wrapper */
  193. typedef struct {
  194. PyObject_HEAD
  195. PyObject *obj;
  196. PyObject *container;
  197. } Wrapper;
  198. static PyExtensionClass Wrappertype, XaqWrappertype;
  199. #define isImplicitWrapper(o) (Py_TYPE(o) == (PyTypeObject*)&Wrappertype)
  200. #define isExplicitWrapper(o) (Py_TYPE(o) == (PyTypeObject*)&XaqWrappertype)
  201. #define isWrapper(o) (isImplicitWrapper(o) || isExplicitWrapper(o))
  202. /* Same as isWrapper but does a check for NULL pointer. */
  203. #define XisWrapper(o) ((o) ? isWrapper(o) : 0)
  204. #define WRAPPER(O) ((Wrapper*)(O))
  205. #define newWrapper(obj, container, Wrappertype) \
  206. PyObject_CallFunctionObjArgs(OBJECT(Wrappertype), obj, container, NULL)
  207. static char *init_kwlist[] = {"obj", "container", NULL};
  208. static int
  209. Wrapper_init(Wrapper *self, PyObject *args, PyObject *kwargs)
  210. {
  211. int rc;
  212. PyObject *obj, *container;
  213. rc = PyArg_ParseTupleAndKeywords(
  214. args, kwargs, "OO:__init__", init_kwlist, &obj, &container);
  215. if (!rc) {
  216. return -1;
  217. }
  218. if (self == WRAPPER(obj)) {
  219. PyErr_SetString(PyExc_ValueError,
  220. "Cannot wrap acquisition wrapper "
  221. "in itself (Wrapper__init__)");
  222. return -1;
  223. }
  224. /* Avoid memory leak if __init__ is called multiple times. */
  225. Py_CLEAR(self->obj);
  226. Py_CLEAR(self->container);
  227. Py_INCREF(obj);
  228. self->obj = obj;
  229. if (container != Py_None) {
  230. Py_INCREF(container);
  231. self->container = container;
  232. }
  233. return 0;
  234. }
  235. static PyObject *
  236. Wrapper__new__(PyTypeObject *type, PyObject *args, PyObject *kwargs)
  237. {
  238. Wrapper *self = WRAPPER(type->tp_alloc(type, 0));
  239. if (Wrapper_init(self, args, kwargs) == -1) {
  240. Py_DECREF(self);
  241. return NULL;
  242. }
  243. return OBJECT(self);
  244. }
  245. static int
  246. Wrapper__init__(Wrapper *self, PyObject *args, PyObject *kwargs)
  247. {
  248. return Wrapper_init(self, args, kwargs);
  249. }
  250. /* ---------------------------------------------------------------- */
  251. /* Creates a new Wrapper object with the values from the old one.
  252. * Steals a reference from 'ob' (also in the error case).
  253. * Returns a new reference.
  254. * Returns NULL on error.
  255. */
  256. static PyObject *
  257. clone_wrapper(Wrapper *ob)
  258. {
  259. PyObject *tmp;
  260. /* Only clone if its shared with others. */
  261. if (Py_REFCNT(ob) == 1) {
  262. return (PyObject*) ob;
  263. }
  264. tmp = newWrapper(ob->obj, ob->container, Py_TYPE(ob));
  265. Py_DECREF(ob);
  266. return tmp;
  267. }
  268. static PyObject *
  269. __of__(PyObject *inst, PyObject *parent)
  270. {
  271. PyObject *result;
  272. result = PyObject_CallMethodObjArgs(inst, py__of__, parent, NULL);
  273. if (XisWrapper(result) && XisWrapper(WRAPPER(result)->container)) {
  274. while (XisWrapper(WRAPPER(result)->obj) &&
  275. (WRAPPER(WRAPPER(result)->obj)->container ==
  276. WRAPPER(WRAPPER(result)->container)->obj)) {
  277. /* Copy it, because the result could be shared with others. */
  278. if ((result = clone_wrapper(WRAPPER(result))) == NULL) {
  279. return NULL;
  280. }
  281. /* Simplify wrapper */
  282. Py_XINCREF(WRAPPER(WRAPPER(result)->obj)->obj);
  283. ASSIGN(WRAPPER(result)->obj, WRAPPER(WRAPPER(result)->obj)->obj);
  284. }
  285. }
  286. return result;
  287. }
  288. static PyObject *
  289. apply__of__(PyObject *self, PyObject *inst)
  290. {
  291. PyObject *r;
  292. if (!self) {
  293. r = self;
  294. } else if (has__of__(self)) {
  295. r = __of__(self, inst);
  296. Py_DECREF(self);
  297. } else {
  298. r = self;
  299. }
  300. return r;
  301. }
  302. static PyObject *
  303. get_inner(PyObject *ob)
  304. {
  305. if (isWrapper(ob)) {
  306. while (isWrapper(WRAPPER(ob)->obj)) {
  307. ob = WRAPPER(ob)->obj;
  308. }
  309. }
  310. return ob;
  311. }
  312. static PyObject *
  313. get_base(PyObject *ob)
  314. {
  315. while (isWrapper(ob)) {
  316. ob = WRAPPER(ob)->obj;
  317. }
  318. return ob;
  319. }
  320. static PyObject *
  321. Wrapper_descrget(Wrapper *self, PyObject *inst, PyObject *cls)
  322. {
  323. if (inst == NULL) {
  324. Py_INCREF(self);
  325. return OBJECT(self);
  326. }
  327. return __of__(OBJECT(self), inst);
  328. }
  329. static int
  330. Wrapper_traverse(Wrapper *self, visitproc visit, void *arg)
  331. {
  332. Py_VISIT(self->obj);
  333. Py_VISIT(self->container);
  334. return 0;
  335. }
  336. static int
  337. Wrapper_clear(Wrapper *self)
  338. {
  339. Py_CLEAR(self->obj);
  340. Py_CLEAR(self->container);
  341. return 0;
  342. }
  343. static void
  344. Wrapper_dealloc(Wrapper *self)
  345. {
  346. PyObject_GC_UnTrack(OBJECT(self));
  347. Wrapper_clear(self);
  348. Py_TYPE(self)->tp_free(OBJECT(self));
  349. }
  350. static PyObject *
  351. Wrapper_special(Wrapper *self, char *name, PyObject *oname)
  352. {
  353. PyObject *r = NULL;
  354. switch(*name) {
  355. case 'b':
  356. if (STR_EQ(name, "base")) {
  357. r = get_base(OBJECT(self));
  358. Py_INCREF(r);
  359. return r;
  360. }
  361. break;
  362. case 'p':
  363. if (STR_EQ(name, "parent")) {
  364. r = self->container ? self->container : Py_None;
  365. Py_INCREF(r);
  366. return r;
  367. }
  368. break;
  369. case 's':
  370. if (STR_EQ(name, "self")) {
  371. Py_INCREF(self->obj);
  372. return self->obj;
  373. }
  374. break;
  375. case 'e':
  376. if (STR_EQ(name, "explicit")) {
  377. if (isExplicitWrapper(self)) {
  378. Py_INCREF(self);
  379. return OBJECT(self);
  380. }
  381. return newWrapper(self->obj, self->container, &XaqWrappertype);
  382. }
  383. break;
  384. case 'a':
  385. if (STR_EQ(name, "acquire")) {
  386. return Py_FindAttr(OBJECT(self), oname);
  387. }
  388. break;
  389. case 'c':
  390. if (STR_EQ(name, "chain")) {
  391. if ((r = PyList_New(0)) == NULL) {
  392. return NULL;
  393. }
  394. while (PyList_Append(r, OBJECT(self)) == 0) {
  395. if (isWrapper(self) && self->container) {
  396. self = WRAPPER(self->container);
  397. } else {
  398. return r;
  399. }
  400. }
  401. Py_DECREF(r);
  402. return NULL;
  403. }
  404. break;
  405. case 'i':
  406. if (STR_EQ(name, "inContextOf")) {
  407. return Py_FindAttr(OBJECT(self), oname);
  408. } else if (STR_EQ(name, "inner")) {
  409. r = get_inner(OBJECT(self));
  410. Py_INCREF(r);
  411. return r;
  412. }
  413. break;
  414. case 'u':
  415. if (STR_EQ(name, "uncle")) {
  416. return NATIVE_FROM_STRING("Bob");
  417. }
  418. break;
  419. }
  420. return NULL;
  421. }
  422. static int
  423. apply_filter(PyObject *filter, PyObject *inst, PyObject *oname, PyObject *r,
  424. PyObject *extra, PyObject *orig)
  425. {
  426. /* Calls the filter, passing arguments.
  427. Returns 1 if the filter accepts the value, 0 if not, -1 if an
  428. exception occurred.
  429. Note the special reference counting rule: This function decrements
  430. the refcount of 'r' when it returns 0 or -1. When it returns 1, it
  431. leaves the refcount unchanged.
  432. */
  433. PyObject *py_res;
  434. int res;
  435. py_res = PyObject_CallFunctionObjArgs(filter, orig, inst, oname, r, extra, NULL);
  436. if (py_res == NULL) {
  437. Py_DECREF(r);
  438. return -1;
  439. }
  440. res = PyObject_IsTrue(py_res);
  441. Py_DECREF(py_res);
  442. if (res == 0 || res == -1) {
  443. Py_DECREF(r);
  444. return res;
  445. }
  446. return 1;
  447. }
  448. static PyObject *
  449. Wrapper_acquire(Wrapper *self, PyObject *oname,
  450. PyObject *filter, PyObject *extra, PyObject *orig,
  451. int explicit, int containment);
  452. static PyObject *
  453. Wrapper_findattr_name(Wrapper *self, char* name, PyObject *oname,
  454. PyObject *filter, PyObject *extra, PyObject *orig,
  455. int sob, int sco, int explicit, int containment);
  456. static PyObject *
  457. Wrapper_findattr(Wrapper *self, PyObject *oname,
  458. PyObject *filter, PyObject *extra, PyObject *orig,
  459. int sob, int sco, int explicit, int containment)
  460. /*
  461. Parameters:
  462. sob
  463. Search self->obj for the 'oname' attribute
  464. sco
  465. Search self->container for the 'oname' attribute
  466. explicit
  467. Explicitly acquire 'oname' attribute from container (assumed with
  468. implicit acquisition wrapper)
  469. containment
  470. Use the innermost wrapper ("aq_inner") for looking up the 'oname'
  471. attribute.
  472. */
  473. {
  474. PyObject *tmp, *result;
  475. if ((tmp = convert_name(oname)) == NULL) {
  476. return NULL;
  477. }
  478. result = Wrapper_findattr_name(self, PyBytes_AS_STRING(tmp), oname, filter,
  479. extra, orig, sob, sco, explicit, containment);
  480. Py_XDECREF(tmp);
  481. return result;
  482. }
  483. static PyObject *
  484. Wrapper_findattr_name(Wrapper *self, char* name, PyObject *oname,
  485. PyObject *filter, PyObject *extra, PyObject *orig,
  486. int sob, int sco, int explicit, int containment)
  487. /*
  488. Exactly the same as Wrapper_findattr, except that the incoming
  489. Python name string/unicode object has already been decoded
  490. into a C string. This helper function lets us more easily manage
  491. the lifetime of any temporary allocations.
  492. This function uses Wrapper_acquire, which only takes the original
  493. oname value, not the decoded value. That function can call back into
  494. this one (via Wrapper_findattr). Although that may lead to a few
  495. temporary allocations as we walk through the containment hierarchy,
  496. it is correct: This function may modify its internal view of the
  497. `name` value, and if that were propagated up the hierarchy
  498. the incorrect name may be looked up.
  499. */
  500. {
  501. PyObject *r;
  502. if (STR_STARTSWITH(name, "aq_") || STR_EQ(name, "__parent__")) {
  503. /* __parent__ is an alias to aq_parent */
  504. name = STR_EQ(name, "__parent__") ? "parent" : name + 3;
  505. if ((r = Wrapper_special(self, name, oname))) {
  506. if (filter) {
  507. switch(apply_filter(filter, OBJECT(self), oname, r, extra, orig)) {
  508. case -1: return NULL;
  509. case 1: return r;
  510. }
  511. } else {
  512. return r;
  513. }
  514. } else {
  515. PyErr_Clear();
  516. }
  517. } else if (STR_STARTSWITH(name, "__") &&
  518. (STR_EQ(name, "__reduce__") ||
  519. STR_EQ(name, "__reduce_ex__") ||
  520. STR_EQ(name, "__getstate__"))) {
  521. return PyObject_GenericGetAttr(OBJECT(self), oname);
  522. }
  523. /* If we are doing a containment search, then replace self with aq_inner */
  524. self = containment ? WRAPPER(get_inner(OBJECT(self))) : self;
  525. if (sob) {
  526. if (isWrapper(self->obj)) {
  527. if (self == WRAPPER(self->obj)) {
  528. PyErr_SetString(PyExc_RuntimeError,
  529. "Recursion detected in acquisition wrapper");
  530. return NULL;
  531. }
  532. r = Wrapper_findattr(
  533. WRAPPER(self->obj),
  534. oname,
  535. filter,
  536. extra,
  537. orig,
  538. 1,
  539. /* Search object container if explicit,
  540. or object is implicit acquirer */
  541. explicit || isImplicitWrapper(self->obj),
  542. explicit,
  543. containment);
  544. if (r) {
  545. if (PyECMethod_Check(r) && PyECMethod_Self(r) == self->obj) {
  546. ASSIGN(r, PyECMethod_New(r, OBJECT(self)));
  547. }
  548. return apply__of__(r, OBJECT(self));
  549. } else if (!swallow_attribute_error()) {
  550. return NULL;
  551. }
  552. }
  553. /* Deal with mixed __parent__ / aq_parent circles */
  554. else if (self->container &&
  555. isWrapper(self->container) &&
  556. WRAPPER(self->container)->container &&
  557. self == WRAPPER(WRAPPER(self->container)->container))
  558. {
  559. PyErr_SetString(PyExc_RuntimeError,
  560. "Recursion detected in acquisition wrapper");
  561. return NULL;
  562. }
  563. /* normal attribute lookup */
  564. else if ((r = PyObject_GetAttr(self->obj, oname))) {
  565. if (r == Acquired) {
  566. Py_DECREF(r);
  567. return Wrapper_acquire(
  568. self, oname, filter, extra, orig, 1, containment);
  569. }
  570. if (PyECMethod_Check(r) && PyECMethod_Self(r) == self->obj) {
  571. ASSIGN(r, PyECMethod_New(r, OBJECT(self)));
  572. }
  573. r = apply__of__(r, OBJECT(self));
  574. if (r && filter) {
  575. switch(apply_filter(filter, OBJECT(self), oname, r, extra, orig)) {
  576. case -1: return NULL;
  577. case 1: return r;
  578. }
  579. } else {
  580. return r;
  581. }
  582. } else if (!swallow_attribute_error()) {
  583. return NULL;
  584. }
  585. PyErr_Clear();
  586. }
  587. /* Lookup has failed, acquire it from parent. */
  588. if (sco && (*name != '_' || explicit)) {
  589. return Wrapper_acquire(
  590. self, oname, filter, extra, orig, explicit, containment);
  591. }
  592. PyErr_SetObject(PyExc_AttributeError, oname);
  593. return NULL;
  594. }
  595. static PyObject *
  596. Wrapper_acquire(
  597. Wrapper *self,
  598. PyObject *oname,
  599. PyObject *filter,
  600. PyObject *extra,
  601. PyObject *orig,
  602. int explicit,
  603. int containment)
  604. {
  605. PyObject *r;
  606. int sob = 1;
  607. int sco = 1;
  608. if (!self->container) {
  609. PyErr_SetObject(PyExc_AttributeError, oname);
  610. return NULL;
  611. }
  612. /* If the container has an acquisition wrapper itself,
  613. * we'll use Wrapper_findattr to progress further.
  614. */
  615. if (isWrapper(self->container)) {
  616. if (isWrapper(self->obj)) {
  617. /* Try to optimize search by recognizing repeated
  618. * objects in path.
  619. */
  620. if (WRAPPER(self->obj)->container == WRAPPER(self->container)->container) {
  621. sco = 0;
  622. } else if (WRAPPER(self->obj)->container == WRAPPER(self->container)->obj) {
  623. sob = 0;
  624. }
  625. }
  626. /* Don't search the container when the container of the
  627. * container is the same object as 'self'.
  628. */
  629. if (WRAPPER(self->container)->container == WRAPPER(self)->obj) {
  630. sco = 0;
  631. containment = 1;
  632. }
  633. r = Wrapper_findattr(WRAPPER(self->container), oname, filter, extra,
  634. orig, sob, sco, explicit, containment);
  635. return apply__of__(r, OBJECT(self));
  636. }
  637. /* If the container has a __parent__ pointer, we create an
  638. * acquisition wrapper for it accordingly. Then we can proceed
  639. * with Wrapper_findattr, just as if the container had an
  640. * acquisition wrapper in the first place (see above).
  641. */
  642. else if ((r = PyObject_GetAttr(self->container, py__parent__))) {
  643. /* Don't search the container when the parent of the parent
  644. * is the same object as 'self'
  645. */
  646. if (r == WRAPPER(self)->obj) {
  647. sco = 0;
  648. }
  649. else if (WRAPPER(r)->obj == WRAPPER(self)->obj) {
  650. sco = 0;
  651. }
  652. ASSIGN(self->container, newWrapper(self->container, r, &Wrappertype));
  653. /* don't need __parent__ anymore */
  654. Py_DECREF(r);
  655. r = Wrapper_findattr(WRAPPER(self->container), oname, filter, extra,
  656. orig, sob, sco, explicit, containment);
  657. /* There's no need to DECREF the wrapper here because it's
  658. * not stored in self->container, thus 'self' owns its
  659. * reference now
  660. */
  661. return r;
  662. }
  663. /* The container is the end of the acquisition chain; if we
  664. * can't look up the attribute here, we can't look it up at all.
  665. */
  666. else {
  667. /* We need to clean up the AttributeError from the previous
  668. * getattr (because it has clearly failed).
  669. */
  670. if(!swallow_attribute_error()) {
  671. return NULL;
  672. }
  673. if ((r = PyObject_GetAttr(self->container, oname)) == NULL) {
  674. /* May be AttributeError or some other kind of error */
  675. return NULL;
  676. }
  677. if (r == Acquired) {
  678. Py_DECREF(r);
  679. } else if (filter) {
  680. switch(apply_filter(filter, self->container, oname, r, extra, orig)) {
  681. case -1: return NULL;
  682. case 1: return apply__of__(r, OBJECT(self));
  683. }
  684. } else {
  685. return apply__of__(r, OBJECT(self));
  686. }
  687. }
  688. PyErr_SetObject(PyExc_AttributeError, oname);
  689. return NULL;
  690. }
  691. static PyObject *
  692. Wrapper_getattro(Wrapper *self, PyObject *oname)
  693. {
  694. return Wrapper_findattr(self, oname, NULL, NULL, NULL, 1, 1, 0, 0);
  695. }
  696. static PyObject *
  697. Xaq_getattro(Wrapper *self, PyObject *oname)
  698. {
  699. PyObject *tmp, *result;
  700. if ((tmp = convert_name(oname)) == NULL) {
  701. return NULL;
  702. }
  703. /* Special case backward-compatible acquire method. */
  704. if (STR_EQ(PyBytes_AS_STRING(tmp), "acquire")) {
  705. result = Py_FindAttr(OBJECT(self), oname);
  706. } else {
  707. result = Wrapper_findattr(self, oname, NULL, NULL, NULL, 1, 0, 0, 0);
  708. }
  709. Py_DECREF(tmp);
  710. return result;
  711. }
  712. static int
  713. Wrapper_setattro(Wrapper *self, PyObject *oname, PyObject *v)
  714. {
  715. PyObject *tmp = NULL;
  716. char *name = "";
  717. int result;
  718. if ((tmp = convert_name(oname)) == NULL) {
  719. return -1;
  720. }
  721. name = PyBytes_AS_STRING(tmp);
  722. if (STR_EQ(name, "aq_parent") || STR_EQ(name, "__parent__")) {
  723. Py_XINCREF(v);
  724. ASSIGN(self->container, v);
  725. result = 0;
  726. } else {
  727. if (v) {
  728. result = PyObject_SetAttr(self->obj, oname, get_base(v));
  729. }
  730. else {
  731. result = PyObject_DelAttr(self->obj, oname);
  732. }
  733. }
  734. Py_DECREF(tmp);
  735. return result;
  736. }
  737. static int
  738. Wrapper_compare(Wrapper *self, PyObject *w)
  739. {
  740. PyObject *obj, *wobj;
  741. PyObject *m;
  742. int r;
  743. if (OBJECT(self) == w) {
  744. return 0;
  745. }
  746. if ((m = PyObject_GetAttr(OBJECT(self), py__cmp__)) == NULL) {
  747. PyErr_Clear();
  748. /* Unwrap self completely -> obj. */
  749. obj = get_base(OBJECT(self));
  750. /* Unwrap w completely -> wobj. */
  751. wobj = get_base(w);
  752. if (obj == wobj) {
  753. return 0;
  754. } else if (obj < w) {
  755. return -1;
  756. } else {
  757. return 1;
  758. }
  759. }
  760. ASSIGN(m, PyObject_CallFunction(m, "O", w));
  761. if (m == NULL) {
  762. return -1;
  763. }
  764. r = PyLong_AsLong(m);
  765. Py_DECREF(m);
  766. return r;
  767. }
  768. static PyObject *
  769. Wrapper_richcompare(Wrapper *self, PyObject *w, int op)
  770. {
  771. return diff_to_bool(Wrapper_compare(self, w), op);
  772. }
  773. static PyObject *
  774. Wrapper_repr(Wrapper *self)
  775. {
  776. PyObject *r;
  777. if ((r = PyObject_GetAttr(OBJECT(self), py__repr__))) {
  778. ASSIGN(r, PyObject_CallFunction(r, NULL, NULL));
  779. return r;
  780. } else {
  781. PyErr_Clear();
  782. return PyObject_Repr(self->obj);
  783. }
  784. }
  785. static PyObject *
  786. Wrapper_str(Wrapper *self)
  787. {
  788. PyObject *r;
  789. if ((r = PyObject_GetAttr(OBJECT(self), py__str__))) {
  790. ASSIGN(r, PyObject_CallFunction(r,NULL,NULL));
  791. return r;
  792. } else {
  793. PyErr_Clear();
  794. return PyObject_Str(self->obj);
  795. }
  796. }
  797. static PyObject *
  798. Wrapper_unicode(Wrapper *self)
  799. {
  800. PyObject *r;
  801. if ((r = PyObject_GetAttr(OBJECT(self), py__unicode__))) {
  802. ASSIGN(r, PyObject_CallFunction(r, NULL, NULL));
  803. return r;
  804. } else {
  805. PyErr_Clear();
  806. return Wrapper_str(self);
  807. }
  808. }
  809. static long
  810. Wrapper_hash(Wrapper *self)
  811. {
  812. return PyObject_Hash(self->obj);
  813. }
  814. static PyObject *
  815. Wrapper_call(PyObject *self, PyObject *args, PyObject *kw)
  816. {
  817. return CallMethod(self, py__call__, args, kw);
  818. }
  819. /* Code to handle accessing Wrapper objects as sequence objects */
  820. static Py_ssize_t
  821. Wrapper_length(PyObject* self)
  822. {
  823. PyObject *result;
  824. PyObject *callable;
  825. PyObject *tres;
  826. Py_ssize_t res;
  827. callable = PyObject_GetAttr(self, py__len__);
  828. if (callable == NULL) {
  829. if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
  830. /* PyObject_LengthHint in Python3 catches only TypeError.
  831. * Python2 catches both (type and attribute error)
  832. */
  833. PyErr_SetString(PyExc_TypeError, "object has no len()");
  834. }
  835. return -1;
  836. }
  837. result = PyObject_CallObject(callable, NULL);
  838. Py_DECREF(callable);
  839. if (result == NULL) {
  840. return -1;
  841. }
  842. /* PyLong_AsSsize_t can only be called on long objects. */
  843. tres = PyNumber_Long(result);
  844. Py_DECREF(result);
  845. if (tres == NULL) {
  846. return -1;
  847. }
  848. res = PyLong_AsSsize_t(tres);
  849. Py_DECREF(tres);
  850. if (res == -1 && PyErr_Occurred()) {
  851. return -1;
  852. }
  853. return res;
  854. }
  855. static PyObject *
  856. Wrapper_add(PyObject *self, PyObject *bb)
  857. {
  858. return CallMethodArgs(self, py__add__, "(O)", bb);
  859. }
  860. static PyObject *
  861. Wrapper_repeat(PyObject *self, Py_ssize_t n)
  862. {
  863. return CallMethodArgs(self, py__mul__, "(n)", n);
  864. }
  865. static PyObject *
  866. Wrapper_item(PyObject *self, Py_ssize_t i)
  867. {
  868. return CallMethodArgs(self, py__getitem__, "(n)", i);
  869. }
  870. static PyObject *
  871. Wrapper_slice(PyObject *self, Py_ssize_t ilow, Py_ssize_t ihigh)
  872. {
  873. return CallMethodArgs(self, py__getslice__, "(nn)", ilow, ihigh);
  874. }
  875. static int
  876. Wrapper_ass_item(PyObject *self, Py_ssize_t i, PyObject *v)
  877. {
  878. if (v) {
  879. v = CallMethodArgs(self, py__setitem__, "(nO)", i, v);
  880. } else {
  881. v = CallMethodArgs(self, py__delitem__, "(n)", i);
  882. }
  883. if (v == NULL) {
  884. return -1;
  885. }
  886. Py_DECREF(v);
  887. return 0;
  888. }
  889. static int
  890. Wrapper_ass_slice(PyObject *self, Py_ssize_t ilow, Py_ssize_t ihigh, PyObject *v)
  891. {
  892. if (v) {
  893. v = CallMethodArgs(self, py__setslice__, "(nnO)", ilow, ihigh, v);
  894. } else {
  895. v = CallMethodArgs(self, py__delslice__, "(nn)", ilow, ihigh);
  896. }
  897. if (v == NULL) {
  898. return -1;
  899. }
  900. Py_DECREF(v);
  901. return 0;
  902. }
  903. static int
  904. Wrapper_contains(PyObject *self, PyObject *v)
  905. {
  906. long result;
  907. if ((v = CallMethodArgs(self, py__contains__, "(O)", v)) == NULL) {
  908. return -1;
  909. }
  910. result = PyLong_AsLong(v);
  911. Py_DECREF(v);
  912. return result;
  913. }
  914. /* Support for iteration cannot rely on the internal implementation of
  915. `PyObject_GetIter`, since the `self` passed into `__iter__` and
  916. `__getitem__` should be acquisition-wrapped (also see LP 360761): The
  917. wrapper obviously supports the iterator protocol so simply calling
  918. `PyObject_GetIter(OBJECT(self))` results in an infinite recursion.
  919. Instead the base object needs to be checked and the wrapper must only
  920. be used when actually calling `__getitem__` or setting up a sequence
  921. iterator. */
  922. static PyObject *
  923. Wrapper_iter(Wrapper *self)
  924. {
  925. PyObject *obj = self->obj;
  926. PyObject *res;
  927. if ((res=PyObject_GetAttr(OBJECT(self),py__iter__))) {
  928. ASSIGN(res,PyObject_CallFunction(res,NULL,NULL));
  929. if (res != NULL && !PyIter_Check(res)) {
  930. PyErr_Format(PyExc_TypeError,
  931. "iter() returned non-iterator "
  932. "of type '%.100s'",
  933. Py_TYPE(res)->tp_name);
  934. Py_DECREF(res);
  935. res = NULL;
  936. }
  937. } else if (PySequence_Check(obj)) {
  938. PyErr_Clear();
  939. ASSIGN(res,PySeqIter_New(OBJECT(self)));
  940. } else {
  941. res = PyErr_Format(PyExc_TypeError, "iteration over non-sequence");
  942. }
  943. return res;
  944. }
  945. static PySequenceMethods Wrapper_as_sequence = {
  946. (lenfunc)Wrapper_length, /*sq_length*/
  947. Wrapper_add, /*sq_concat*/
  948. (ssizeargfunc)Wrapper_repeat, /*sq_repeat*/
  949. (ssizeargfunc)Wrapper_item, /*sq_item*/
  950. (ssizessizeargfunc)Wrapper_slice, /*sq_slice*/
  951. (ssizeobjargproc)Wrapper_ass_item, /*sq_ass_item*/
  952. (ssizessizeobjargproc)Wrapper_ass_slice, /*sq_ass_slice*/
  953. (objobjproc)Wrapper_contains, /*sq_contains*/
  954. };
  955. /* -------------------------------------------------------------- */
  956. /* Code to access Wrapper objects as mappings */
  957. static PyObject *
  958. Wrapper_subscript(PyObject *self, PyObject *key)
  959. {
  960. return CallMethodArgs(self, py__getitem__, "(O)", key);
  961. }
  962. static int
  963. Wrapper_ass_sub(PyObject *self, PyObject *key, PyObject *v)
  964. {
  965. if (v) {
  966. v = CallMethodArgs(self, py__setitem__, "(OO)", key, v);
  967. } else {
  968. v = CallMethodArgs(self, py__delitem__, "(O)", key);
  969. }
  970. if (v == NULL) {
  971. return -1;
  972. }
  973. Py_DECREF(v);
  974. return 0;
  975. }
  976. static PyMappingMethods Wrapper_as_mapping = {
  977. (lenfunc)Wrapper_length, /*mp_length*/
  978. (binaryfunc)Wrapper_subscript, /*mp_subscript*/
  979. (objobjargproc)Wrapper_ass_sub, /*mp_ass_subscript*/
  980. };
  981. /* -------------------------------------------------------------- */
  982. /* Code to access Wrapper objects as numbers */
  983. #define WRAP_UNARYOP(OPNAME) \
  984. static PyObject* Wrapper_##OPNAME(PyObject* self) { \
  985. return PyObject_CallMethodObjArgs(self, py__##OPNAME##__, NULL); \
  986. }
  987. #define WRAP_BINOP(OPNAME) \
  988. static PyObject* Wrapper_##OPNAME(PyObject* self, PyObject* o1) { \
  989. return CallMethodArgs(self, py__##OPNAME##__, "(O)", o1); \
  990. }
  991. #define WRAP_TERNARYOP(OPNAME) \
  992. static PyObject* Wrapper_##OPNAME(PyObject* self, PyObject* o1, PyObject* o2) { \
  993. return CallMethodArgs(self, py__##OPNAME##__, "(OO)", o1, o2); \
  994. }
  995. WRAP_BINOP(sub);
  996. WRAP_BINOP(mul);
  997. #ifndef PY3K
  998. WRAP_BINOP(div);
  999. #endif
  1000. WRAP_BINOP(mod);
  1001. WRAP_BINOP(divmod);
  1002. WRAP_TERNARYOP(pow);
  1003. WRAP_UNARYOP(neg);
  1004. WRAP_UNARYOP(pos);
  1005. WRAP_UNARYOP(abs);
  1006. WRAP_UNARYOP(invert);
  1007. WRAP_BINOP(lshift);
  1008. WRAP_BINOP(rshift);
  1009. WRAP_BINOP(and);
  1010. WRAP_BINOP(xor);
  1011. WRAP_BINOP(or);
  1012. WRAP_UNARYOP(int);
  1013. #ifndef PY3K
  1014. WRAP_UNARYOP(long);
  1015. #endif
  1016. WRAP_UNARYOP(float);
  1017. #ifndef PY3K
  1018. WRAP_UNARYOP(oct);
  1019. WRAP_UNARYOP(hex);
  1020. #endif
  1021. WRAP_BINOP(iadd);
  1022. WRAP_BINOP(isub);
  1023. WRAP_BINOP(imul);
  1024. #ifndef PY3K
  1025. WRAP_BINOP(idiv);
  1026. #endif
  1027. WRAP_BINOP(imod);
  1028. WRAP_TERNARYOP(ipow);
  1029. WRAP_BINOP(ilshift);
  1030. WRAP_BINOP(irshift);
  1031. WRAP_BINOP(iand);
  1032. WRAP_BINOP(ixor);
  1033. WRAP_BINOP(ior);
  1034. WRAP_BINOP(floordiv);
  1035. WRAP_BINOP(truediv);
  1036. WRAP_BINOP(ifloordiv);
  1037. WRAP_BINOP(itruediv);
  1038. WRAP_UNARYOP(index);
  1039. #if ((PY_MAJOR_VERSION == 3) && (PY_MINOR_VERSION > 4))
  1040. WRAP_BINOP(matmul);
  1041. WRAP_BINOP(imatmul);
  1042. #endif
  1043. static int
  1044. Wrapper_nonzero(PyObject *self)
  1045. {
  1046. int res;
  1047. PyObject* result = NULL;
  1048. PyObject* callable = NULL;
  1049. #ifdef PY3K
  1050. callable = PyObject_GetAttr(self, py__bool__);
  1051. #else
  1052. callable = PyObject_GetAttr(self, py__nonzero__);
  1053. #endif
  1054. if (callable == NULL) {
  1055. PyErr_Clear();
  1056. callable = PyObject_GetAttr(self, py__len__);
  1057. if (callable == NULL) {
  1058. PyErr_Clear();
  1059. return 1;
  1060. }
  1061. }
  1062. result = PyObject_CallObject(callable, NULL);
  1063. Py_DECREF(callable);
  1064. if (result == NULL) {
  1065. return -1;
  1066. }
  1067. res = PyObject_IsTrue(result);
  1068. Py_DECREF(result);
  1069. return res;
  1070. }
  1071. #ifndef PY3K
  1072. static int
  1073. Wrapper_coerce(PyObject **self, PyObject **o)
  1074. {
  1075. PyObject *m;
  1076. if ((m=PyObject_GetAttr(*self, py__coerce__)) == NULL) {
  1077. PyErr_Clear();
  1078. Py_INCREF(*self);
  1079. Py_INCREF(*o);
  1080. return 0;
  1081. }
  1082. ASSIGN(m, PyObject_CallFunction(m, "O", *o));
  1083. if (m == NULL) {
  1084. return -1;
  1085. }
  1086. if (!PyArg_ParseTuple(m, "OO", self, o)) {
  1087. Py_DECREF(m);
  1088. return -1;
  1089. }
  1090. Py_INCREF(*self);
  1091. Py_INCREF(*o);
  1092. Py_DECREF(m);
  1093. return 0;
  1094. }
  1095. #endif
  1096. static PyNumberMethods Wrapper_as_number = {
  1097. Wrapper_add, /* nb_add */
  1098. Wrapper_sub, /* nb_subtract */
  1099. Wrapper_mul, /* nb_multiply */
  1100. #ifndef PY3K
  1101. Wrapper_div, /* nb_divide */
  1102. #endif
  1103. Wrapper_mod, /* nb_remainder */
  1104. Wrapper_divmod, /* nb_divmod */
  1105. Wrapper_pow, /* nb_power */
  1106. Wrapper_neg, /* nb_negative */
  1107. Wrapper_pos, /* nb_positive */
  1108. Wrapper_abs, /* nb_absolute */
  1109. Wrapper_nonzero, /* nb_nonzero */
  1110. Wrapper_invert, /* nb_invert */
  1111. Wrapper_lshift, /* nb_lshift */
  1112. Wrapper_rshift, /* nb_rshift */
  1113. Wrapper_and, /* nb_and */
  1114. Wrapper_xor, /* nb_xor */
  1115. Wrapper_or, /* nb_or */
  1116. #ifndef PY3K
  1117. Wrapper_coerce, /* nb_coerce */
  1118. #endif
  1119. Wrapper_int, /* nb_int */
  1120. #ifdef PY3K
  1121. NULL,
  1122. #else
  1123. Wrapper_long, /* nb_long */
  1124. #endif
  1125. Wrapper_float, /* nb_float */
  1126. #ifndef PY3K
  1127. Wrapper_oct, /* nb_oct*/
  1128. Wrapper_hex, /* nb_hex*/
  1129. #endif
  1130. Wrapper_iadd, /* nb_inplace_add */
  1131. Wrapper_isub, /* nb_inplace_subtract */
  1132. Wrapper_imul, /* nb_inplace_multiply */
  1133. #ifndef PY3K
  1134. Wrapper_idiv, /* nb_inplace_divide */
  1135. #endif
  1136. Wrapper_imod, /* nb_inplace_remainder */
  1137. Wrapper_ipow, /* nb_inplace_power */
  1138. Wrapper_ilshift, /* nb_inplace_lshift */
  1139. Wrapper_irshift, /* nb_inplace_rshift */
  1140. Wrapper_iand, /* nb_inplace_and */
  1141. Wrapper_ixor, /* nb_inplace_xor */
  1142. Wrapper_ior, /* nb_inplace_or */
  1143. Wrapper_floordiv, /* nb_floor_divide */
  1144. Wrapper_truediv, /* nb_true_divide */
  1145. Wrapper_ifloordiv, /* nb_inplace_floor_divide */
  1146. Wrapper_itruediv, /* nb_inplace_true_divide */
  1147. Wrapper_index, /* nb_index */
  1148. #if ((PY_MAJOR_VERSION == 3) && (PY_MINOR_VERSION > 4))
  1149. Wrapper_matmul, /* nb_matrix_multiply */
  1150. Wrapper_imatmul, /* nb_inplace_matrix_multiply */
  1151. #endif
  1152. };
  1153. /* -------------------------------------------------------- */
  1154. static char *acquire_args[] = {"object", "name", "filter", "extra", "explicit",
  1155. "default", "containment", NULL};
  1156. static PyObject *
  1157. Wrapper_acquire_method(Wrapper *self, PyObject *args, PyObject *kw)
  1158. {
  1159. PyObject *name, *filter = NULL, *extra = Py_None;
  1160. PyObject *expl = NULL, *defalt = NULL;
  1161. int explicit = 1;
  1162. int containment = 0;
  1163. PyObject *result;
  1164. if (!PyArg_ParseTupleAndKeywords(args, kw, "O|OOOOi", acquire_args+1,
  1165. &name, &filter, &extra, &expl,
  1166. &defalt, &containment))
  1167. {
  1168. return NULL;
  1169. }
  1170. if (expl) {
  1171. explicit = PyObject_IsTrue(expl);
  1172. }
  1173. if (filter == Py_None) {
  1174. filter = NULL;
  1175. }
  1176. result = Wrapper_findattr(self, name, filter, extra, OBJECT(self), 1,
  1177. explicit || isImplicitWrapper(self),
  1178. explicit, containment);
  1179. if (result == NULL && defalt != NULL) {
  1180. /* as "Python/bltinmodule.c:builtin_getattr" turn
  1181. * only 'AttributeError' into a default value, such
  1182. * that e.g. "ConflictError" and errors raised by the filter
  1183. * are not mapped to the default value.
  1184. */
  1185. if (swallow_attribute_error()) {
  1186. Py_INCREF(defalt);
  1187. result = defalt;
  1188. }
  1189. }
  1190. return result;
  1191. }
  1192. /* forward declaration so that we can use it in Wrapper_inContextOf */
  1193. static PyObject * capi_aq_inContextOf(PyObject *self, PyObject *o, int inner);
  1194. static PyObject *
  1195. Wrapper_inContextOf(Wrapper *self, PyObject *args)
  1196. {
  1197. PyObject *o;
  1198. int inner = 1;
  1199. if (!PyArg_ParseTuple(args, "O|i", &o, &inner)) {
  1200. return NULL;
  1201. }
  1202. return capi_aq_inContextOf(OBJECT(self), o, inner);
  1203. }
  1204. PyObject *
  1205. Wrappers_are_not_picklable(PyObject *wrapper, PyObject *args)
  1206. {
  1207. PyErr_SetString(PyExc_TypeError,
  1208. "Can't pickle objects in acquisition wrappers.");
  1209. return NULL;
  1210. }
  1211. static PyObject *
  1212. Wrapper___getnewargs__(PyObject *self)
  1213. {
  1214. return PyTuple_New(0);
  1215. }
  1216. static struct PyMethodDef Wrapper_methods[] = {
  1217. {"acquire", (PyCFunction)Wrapper_acquire_method,
  1218. METH_VARARGS|METH_KEYWORDS,
  1219. "Get an attribute, acquiring it if necessary"},
  1220. {"aq_acquire", (PyCFunction)Wrapper_acquire_method,
  1221. METH_VARARGS|METH_KEYWORDS,
  1222. "Get an attribute, acquiring it if necessary"},
  1223. {"aq_inContextOf", (PyCFunction)Wrapper_inContextOf, METH_VARARGS,
  1224. "Test whether the object is currently in the context of the argument"},
  1225. {"__getnewargs__", (PyCFunction)Wrapper___getnewargs__, METH_NOARGS,
  1226. "Get arguments to be passed to __new__"},
  1227. {"__getstate__", (PyCFunction)Wrappers_are_not_picklable, METH_VARARGS,
  1228. "Wrappers are not picklable"},
  1229. {"__reduce__", (PyCFunction)Wrappers_are_not_picklable, METH_VARARGS,
  1230. "Wrappers are not picklable"},
  1231. {"__reduce_ex__", (PyCFunction)Wrappers_are_not_picklable, METH_VARARGS,
  1232. "Wrappers are not picklable"},
  1233. {"__unicode__", (PyCFunction)Wrapper_unicode, METH_NOARGS,
  1234. "Unicode"},
  1235. {NULL, NULL}
  1236. };
  1237. static PyExtensionClass Wrappertype = {
  1238. PyVarObject_HEAD_INIT(NULL, 0)
  1239. "Acquisition.ImplicitAcquisitionWrapper", /* tp_name */
  1240. sizeof(Wrapper), /* tp_basicsize */
  1241. 0, /* tp_itemsize */
  1242. (destructor)Wrapper_dealloc, /* tp_dealloc */
  1243. (printfunc)0, /* tp_print */
  1244. (getattrfunc)0, /* tp_getattr */
  1245. (setattrfunc)0, /* tp_setattr */
  1246. 0, /* tp_compare */
  1247. (reprfunc)Wrapper_repr, /* tp_repr */
  1248. &Wrapper_as_number, /* tp_as_number */
  1249. &Wrapper_as_sequence, /* tp_as_sequence */
  1250. &Wrapper_as_mapping, /* tp_as_mapping */
  1251. (hashfunc)Wrapper_hash, /* tp_hash */
  1252. (ternaryfunc)Wrapper_call, /* tp_call */
  1253. (reprfunc)Wrapper_str, /* tp_str */
  1254. (getattrofunc)Wrapper_getattro, /* tp_getattro */
  1255. (setattrofunc)Wrapper_setattro, /* tp_setattro */
  1256. 0, /* tp_as_buffer */
  1257. Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE |
  1258. Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_HAVE_VERSION_TAG, /* tp_flags */
  1259. "Wrapper object for implicit acquisition", /* tp_doc */
  1260. (traverseproc)Wrapper_traverse, /* tp_traverse */
  1261. (inquiry)Wrapper_clear, /* tp_clear */
  1262. (richcmpfunc)Wrapper_richcompare, /* tp_richcompare */
  1263. 0, /* tp_weaklistoffset */
  1264. (getiterfunc)Wrapper_iter, /* tp_iter */
  1265. 0, /* tp_iternext */
  1266. Wrapper_methods, /* tp_methods */
  1267. 0, /* tp_members */
  1268. 0, /* tp_getset */
  1269. 0, /* tp_base */
  1270. 0, /* tp_dict */
  1271. (descrgetfunc)Wrapper_descrget, /* tp_descr_get */
  1272. 0, /* tp_descr_set */
  1273. 0, /* tp_dictoffset */
  1274. (initproc)Wrapper__init__, /* tp_init */
  1275. 0, /* tp_alloc */
  1276. Wrapper__new__ /* tp_new */
  1277. };
  1278. static PyExtensionClass XaqWrappertype = {
  1279. PyVarObject_HEAD_INIT(NULL, 0)
  1280. "Acquisition.ExplicitAcquisitionWrapper", /*tp_name*/
  1281. sizeof(Wrapper), /* tp_basicsize */
  1282. 0, /* tp_itemsize */
  1283. (destructor)Wrapper_dealloc, /* tp_dealloc */
  1284. (printfunc)0, /* tp_print */
  1285. (getattrfunc)0, /* tp_getattr */
  1286. (setattrfunc)0, /* tp_setattr */
  1287. 0, /* tp_compare */
  1288. (reprfunc)Wrapper_repr, /* tp_repr */
  1289. &Wrapper_as_number, /* tp_as_number */
  1290. &Wrapper_as_sequence, /* tp_as_sequence */
  1291. &Wrapper_as_mapping, /* tp_as_mapping */
  1292. (hashfunc)Wrapper_hash, /* tp_hash */
  1293. (ternaryfunc)Wrapper_call, /* tp_call */
  1294. (reprfunc)Wrapper_str, /* tp_str */
  1295. (getattrofunc)Xaq_getattro, /* tp_getattro */
  1296. (setattrofunc)Wrapper_setattro, /* tp_setattro */
  1297. 0, /* tp_as_buffer */
  1298. Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE |
  1299. Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_HAVE_VERSION_TAG, /* tp_flags */
  1300. "Wrapper object for explicit acquisition", /* tp_doc */
  1301. (traverseproc)Wrapper_traverse, /* tp_traverse */
  1302. (inquiry)Wrapper_clear, /* tp_clear */
  1303. (richcmpfunc)Wrapper_richcompare, /* tp_richcompare */
  1304. 0, /* tp_weaklistoffset */
  1305. (getiterfunc)Wrapper_iter, /* tp_iter */
  1306. 0, /* tp_iternext */
  1307. Wrapper_methods, /* tp_methods */
  1308. 0, /* tp_members */
  1309. 0, /* tp_getset */
  1310. 0, /* tp_base */
  1311. 0, /* tp_dict */
  1312. (descrgetfunc)Wrapper_descrget, /* tp_descr_get */
  1313. 0, /* tp_descr_set */
  1314. 0, /* tp_dictoffset */
  1315. (initproc)Wrapper__init__, /* tp_init */
  1316. 0, /* tp_alloc */
  1317. Wrapper__new__ /* tp_new */
  1318. };
  1319. static PyObject *
  1320. acquire_of(PyObject *self, PyObject *inst, PyExtensionClass *target)
  1321. {
  1322. if (!PyExtensionInstance_Check(inst)) {
  1323. PyErr_SetString(PyExc_TypeError,
  1324. "attempt to wrap extension method using an object that"
  1325. " is not an extension class instance.");
  1326. return NULL;
  1327. }
  1328. return newWrapper(self, inst, target);
  1329. }
  1330. static PyObject *
  1331. aq__of__(PyObject *self, PyObject *inst)
  1332. {
  1333. return acquire_of(self, inst, &Wrappertype);
  1334. }
  1335. static PyObject *
  1336. xaq__of__(PyObject *self, PyObject *inst)
  1337. {
  1338. return acquire_of(self, inst, &XaqWrappertype);
  1339. }
  1340. static struct PyMethodDef Acquirer_methods[] = {
  1341. {"__of__",(PyCFunction)aq__of__, METH_O,
  1342. "__of__(context) -- return the object in a context"},
  1343. {NULL, NULL}
  1344. };
  1345. static struct PyMethodDef ExplicitAcquirer_methods[] = {
  1346. {"__of__",(PyCFunction)xaq__of__, METH_O,
  1347. "__of__(context) -- return the object in a context"},
  1348. {NULL, NULL}
  1349. };
  1350. static PyObject *
  1351. capi_aq_acquire(
  1352. PyObject *self,
  1353. PyObject *name,
  1354. PyObject *filter,
  1355. PyObject *extra,
  1356. int explicit,
  1357. PyObject *defalt,
  1358. int containment)
  1359. {
  1360. PyObject *result;
  1361. if (filter == Py_None) {
  1362. filter = NULL;
  1363. }
  1364. /* We got a wrapped object, so business as usual */
  1365. if (isWrapper(self)) {
  1366. result = Wrapper_findattr(WRAPPER(self), name, filter, extra,
  1367. OBJECT(self), 1,
  1368. explicit || isImplicitWrapper(self),
  1369. explicit, containment);
  1370. }
  1371. /* Not wrapped; check if we have a __parent__ pointer. If that's
  1372. * the case, create a wrapper and pretend it's business as usual.
  1373. */
  1374. else if ((result = PyObject_GetAttr(self, py__parent__))) {
  1375. self = newWrapper(self, result, &Wrappertype);
  1376. /* don't need __parent__ anymore */
  1377. Py_DECREF(result);
  1378. result = Wrapper_findattr(WRAPPER(self), name, filter, extra,
  1379. OBJECT(self), 1, 1, explicit, containment);
  1380. /* Get rid of temporary wrapper */
  1381. Py_DECREF(self);
  1382. }
  1383. /* No wrapper and no __parent__, so just getattr. */
  1384. else {
  1385. /* Clean up the AttributeError from the previous getattr
  1386. * (because it has clearly failed).
  1387. */
  1388. if (!swallow_attribute_error()) {
  1389. return NULL;
  1390. }
  1391. if (!filter) {
  1392. result = PyObject_GetAttr(self, name);
  1393. } else {
  1394. /* Construct a wrapper so we can use Wrapper_findattr */
  1395. if ((self = newWrapper(self, Py_None, &Wrappertype)) == NULL) {
  1396. return NULL;
  1397. }
  1398. result = Wrapper_findattr(WRAPPER(self), name, filter, extra,
  1399. OBJECT(self), 1, 1, explicit, containment);
  1400. /* Get rid of temporary wrapper */
  1401. Py_DECREF(self);
  1402. }
  1403. }
  1404. if (result == NULL && defalt != NULL) {
  1405. /* Python/bltinmodule.c:builtin_getattr turns only 'AttributeError'
  1406. * into a default value.
  1407. */
  1408. if (swallow_attribute_error()) {
  1409. Py_INCREF(defalt);
  1410. result = defalt;
  1411. }
  1412. }
  1413. return result;
  1414. }
  1415. static PyObject *
  1416. module_aq_acquire(PyObject *ignored, PyObject *args, PyObject *kw)
  1417. {
  1418. PyObject *self;
  1419. PyObject *name, *filter = NULL, *extra = Py_None;
  1420. PyObject *expl = NULL, *defalt = NULL;
  1421. int explicit = 1, containment = 0;
  1422. if (!PyArg_ParseTupleAndKeywords(args, kw, "OO|OOOOi", acquire_args,
  1423. &self, &name, &filter, &extra, &expl,
  1424. &defalt, &containment))
  1425. {
  1426. return NULL;
  1427. }
  1428. if (expl) {
  1429. explicit = PyObject_IsTrue(expl);
  1430. }
  1431. return capi_aq_acquire(self, name, filter, extra,
  1432. explicit, defalt, containment);
  1433. }
  1434. static PyObject *
  1435. capi_aq_get(PyObject *self, PyObject *name, PyObject *defalt, int containment)
  1436. {
  1437. PyObject *result;
  1438. result = capi_aq_acquire(self, name, NULL, NULL, 1, defalt, containment);
  1439. if (result == NULL && defalt) {
  1440. PyErr_Clear();
  1441. Py_INCREF(defalt);
  1442. return defalt;
  1443. } else {
  1444. return result;
  1445. }
  1446. }
  1447. static PyObject *
  1448. module_aq_get(PyObject *r, PyObject *args)
  1449. {
  1450. PyObject *self, *name, *defalt = NULL;
  1451. int containment = 0;
  1452. if (!PyArg_ParseTuple(args, "OO|Oi", &self, &name, &defalt, &containment)) {
  1453. return NULL;
  1454. }
  1455. return capi_aq_get(self, name, defalt, containment);
  1456. }
  1457. static int
  1458. capi_aq_iswrapper(PyObject *self) {
  1459. return isWrapper(self);
  1460. }
  1461. static PyObject *
  1462. capi_aq_base(PyObject *self)
  1463. {
  1464. PyObject *result = get_base(self);
  1465. Py_INCREF(result);
  1466. return result;
  1467. }
  1468. static PyObject *
  1469. module_aq_base(PyObject *ignored, PyObject *self)
  1470. {
  1471. return capi_aq_base(self);
  1472. }
  1473. static PyObject *
  1474. capi_aq_parent(PyObject *self)
  1475. {
  1476. PyObject *result;
  1477. if (isWrapper(self) && WRAPPER(self)->container) {
  1478. Py_INCREF(WRAPPER(self)->container);
  1479. return WRAPPER(self)->container;
  1480. }
  1481. else if ((result = PyObject_GetAttr(self, py__parent__))) {
  1482. /* We already own the reference to result (PyObject_GetAttr gives
  1483. * it to us), no need to INCREF here.
  1484. */
  1485. return result;
  1486. } else {
  1487. /* We need to clean up the AttributeError from the previous
  1488. * getattr (because it has clearly failed).
  1489. */
  1490. if (!swallow_attribute_error()) {
  1491. return NULL;
  1492. }
  1493. Py_RETURN_NONE;
  1494. }
  1495. }
  1496. static PyObject *
  1497. module_aq_parent(PyObject *ignored, PyObject *self)
  1498. {
  1499. return capi_aq_parent(self);
  1500. }
  1501. static PyObject *
  1502. capi_aq_self(PyObject *self)
  1503. {
  1504. PyObject *result;
  1505. if (!isWrapper(self)) {
  1506. result = self;
  1507. } else {
  1508. result = WRAPPER(self)->obj;
  1509. }
  1510. Py_INCREF(result);
  1511. return result;
  1512. }
  1513. static PyObject *
  1514. module_aq_self(PyObject *ignored, PyObject *self)
  1515. {
  1516. return capi_aq_self(self);
  1517. }
  1518. static PyObject *
  1519. capi_aq_inner(PyObject *self)
  1520. {
  1521. self = get_inner(self);
  1522. Py_INCREF(self);
  1523. return self;
  1524. }
  1525. static PyObject *
  1526. module_aq_inner(PyObject *ignored, PyObject *self)
  1527. {
  1528. return capi_aq_inner(self);
  1529. }
  1530. static PyObject *
  1531. capi_aq_chain(PyObject *self, int containment)
  1532. {
  1533. PyObject *result;
  1534. /* This allows Py_XDECREF at the end.
  1535. * Needed, because the result of PyObject_GetAttr(self, py__parent__) must
  1536. * be kept alive until not needed anymore. It could be that the refcount of
  1537. * its return value is 1 => calling Py_DECREF too early leads to segfault.
  1538. */
  1539. Py_INCREF(self);
  1540. if ((result = PyList_New(0)) == NULL) {
  1541. return NULL;
  1542. }
  1543. while (1) {
  1544. if (isWrapper(self)) {
  1545. if (containment) {
  1546. ASSIGN(self, get_inner(self));
  1547. Py_INCREF(self);
  1548. }
  1549. if (PyList_Append(result, OBJECT(self)) < 0) {
  1550. goto err;
  1551. }
  1552. if (WRAPPER(self)->container) {
  1553. ASSIGN(self, WRAPPER(self)->container);
  1554. Py_INCREF(self);
  1555. continue;
  1556. }
  1557. } else {
  1558. if (PyList_Append(result, self) < 0) {
  1559. goto err;
  1560. }
  1561. ASSIGN(self, PyObject_GetAttr(self, py__parent__));
  1562. if (self) {
  1563. if (self != Py_None) {
  1564. continue;
  1565. }
  1566. } else if (!swallow_attribute_error()) {
  1567. goto err;
  1568. }
  1569. }
  1570. break;
  1571. }
  1572. Py_XDECREF(self);
  1573. return result;
  1574. err:
  1575. Py_XDECREF(self);
  1576. Py_DECREF(result);
  1577. return NULL;
  1578. }
  1579. static PyObject *
  1580. module_aq_chain(PyObject *ignored, PyObject *args)
  1581. {
  1582. PyObject *self;
  1583. int containment = 0;
  1584. if (!PyArg_ParseTuple(args, "O|i", &self, &containment)) {
  1585. return NULL;
  1586. }
  1587. return capi_aq_chain(self, containment);
  1588. }
  1589. static PyObject *
  1590. capi_aq_inContextOf(PyObject *self, PyObject *o, int inner)
  1591. {
  1592. PyObject *result = Py_False;
  1593. o = get_base(o);
  1594. /* This allows Py_DECREF at the end, if the while loop did nothing. */
  1595. Py_INCREF(self);
  1596. while (1) {
  1597. /* if aq_base(self) is o: return 1 */
  1598. if (get_base(self) == o) {
  1599. result = Py_True;
  1600. break;
  1601. }
  1602. if (inner) {
  1603. ASSIGN(self, capi_aq_inner(self));
  1604. if (self == NULL) {
  1605. return NULL;
  1606. } else if (self == Py_None) {
  1607. result = Py_False;
  1608. break;
  1609. }
  1610. }
  1611. ASSIGN(self, capi_aq_parent(self));
  1612. if (self == NULL) {
  1613. return NULL;
  1614. } else if (self == Py_None) {
  1615. result = Py_False;
  1616. break;
  1617. }
  1618. }
  1619. Py_DECREF(self);
  1620. Py_INCREF(result);
  1621. return result;
  1622. }
  1623. static PyObject *
  1624. module_aq_inContextOf(PyObject *ignored, PyObject *args)
  1625. {
  1626. PyObject *self, *o;
  1627. int inner = 1;
  1628. if (!PyArg_ParseTuple(args, "OO|i", &self, &o, &inner)) {
  1629. return NULL;
  1630. }
  1631. return capi_aq_inContextOf(self, o, inner);
  1632. }
  1633. static struct PyMethodDef methods[] = {
  1634. {"aq_acquire", (PyCFunction)module_aq_acquire, METH_VARARGS|METH_KEYWORDS,
  1635. "aq_acquire(ob, name [, filter, extra, explicit]) -- "
  1636. "Get an attribute, acquiring it if necessary"
  1637. },
  1638. {"aq_get", (PyCFunction)module_aq_get, METH_VARARGS,
  1639. "aq_get(ob, name [, default]) -- "
  1640. "Get an attribute, acquiring it if necessary."
  1641. },
  1642. {"aq_base", (PyCFunction)module_aq_base, METH_O,
  1643. "aq_base(ob) -- Get the object unwrapped"},
  1644. {"aq_parent", (PyCFunction)module_aq_parent, METH_O,
  1645. "aq_parent(ob) -- Get the parent of an object"},
  1646. {"aq_self", (PyCFunction)module_aq_self, METH_O,
  1647. "aq_self(ob) -- Get the object with the outermost wrapper removed"},
  1648. {"aq_inner", (PyCFunction)module_aq_inner, METH_O,
  1649. "aq_inner(ob) -- "
  1650. "Get the object with all but the innermost wrapper removed"},
  1651. {"aq_chain", (PyCFunction)module_aq_chain, METH_VARARGS,
  1652. "aq_chain(ob [, containment]) -- "
  1653. "Get a list of objects in the acquisition environment"},
  1654. {"aq_inContextOf", (PyCFunction)module_aq_inContextOf, METH_VARARGS,
  1655. "aq_inContextOf(base, ob [, inner]) -- "
  1656. "Determine whether the object is in the acquisition context of base."},
  1657. {NULL, NULL}
  1658. };
  1659. #ifdef PY3K
  1660. static struct PyModuleDef moduledef =
  1661. {
  1662. PyModuleDef_HEAD_INIT,
  1663. "_Acquisition", /* m_name */
  1664. "Provide base classes for acquiring objects", /* m_doc */
  1665. -1, /* m_size */
  1666. methods, /* m_methods */
  1667. NULL, /* m_reload */
  1668. NULL, /* m_traverse */
  1669. NULL, /* m_clear */
  1670. NULL, /* m_free */
  1671. };
  1672. #endif
  1673. static PyObject*
  1674. module_init(void)
  1675. {
  1676. PyObject *m, *d;
  1677. PyObject *api;
  1678. PURE_MIXIN_CLASS(Acquirer,
  1679. "Base class for objects that implicitly"
  1680. " acquire attributes from containers\n",
  1681. Acquirer_methods);
  1682. PURE_MIXIN_CLASS(ExplicitAcquirer,
  1683. "Base class for objects that explicitly"
  1684. " acquire attributes from containers\n",
  1685. ExplicitAcquirer_methods);
  1686. if (!ExtensionClassImported) {
  1687. return NULL;
  1688. }
  1689. Acquired = NATIVE_FROM_STRING("<Special Object Used to Force Acquisition>");
  1690. if (Acquired == NULL) {
  1691. return NULL;
  1692. }
  1693. #ifdef PY3K
  1694. m = PyModule_Create(&moduledef);
  1695. #else
  1696. m = Py_InitModule3("_Acquisition",
  1697. methods,
  1698. "Provide base classes for acquiring objects\n\n");
  1699. #endif
  1700. d = PyModule_GetDict(m);
  1701. init_py_names();
  1702. PyExtensionClass_Export(d,"Acquirer", AcquirerType);
  1703. PyExtensionClass_Export(d,"ImplicitAcquisitionWrapper", Wrappertype);
  1704. PyExtensionClass_Export(d,"ExplicitAcquirer", ExplicitAcquirerType);
  1705. PyExtensionClass_Export(d,"ExplicitAcquisitionWrapper", XaqWrappertype);
  1706. /* Create aliases */
  1707. PyDict_SetItemString(d,"Implicit", OBJECT(&AcquirerType));
  1708. PyDict_SetItemString(d,"Explicit", OBJECT(&ExplicitAcquirerType));
  1709. PyDict_SetItemString(d,"Acquired", Acquired);
  1710. AcquisitionCAPI.AQ_Acquire = capi_aq_acquire;
  1711. AcquisitionCAPI.AQ_Get = capi_aq_get;
  1712. AcquisitionCAPI.AQ_IsWrapper = capi_aq_iswrapper;
  1713. AcquisitionCAPI.AQ_Base = capi_aq_base;
  1714. AcquisitionCAPI.AQ_Parent = capi_aq_parent;
  1715. AcquisitionCAPI.AQ_Self = capi_aq_self;
  1716. AcquisitionCAPI.AQ_Inner = capi_aq_inner;
  1717. AcquisitionCAPI.AQ_Chain = capi_aq_chain;
  1718. api = PyCapsule_New(&AcquisitionCAPI, "Acquisition.AcquisitionCAPI", NULL);
  1719. PyDict_SetItemString(d, "AcquisitionCAPI", api);
  1720. Py_DECREF(api);
  1721. return m;
  1722. }
  1723. #ifdef PY3K
  1724. PyMODINIT_FUNC PyInit__Acquisition(void)
  1725. {
  1726. return module_init();
  1727. }
  1728. #else
  1729. PyMODINIT_FUNC init_Acquisition(void)
  1730. {
  1731. module_init();
  1732. }
  1733. #endif