_zope_interface_coptimizations.c 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724
  1. /*###########################################################################
  2. #
  3. # Copyright (c) 2003 Zope Foundation and Contributors.
  4. # All Rights Reserved.
  5. #
  6. # This software is subject to the provisions of the Zope Public License,
  7. # Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
  8. # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
  9. # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  10. # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
  11. # FOR A PARTICULAR PURPOSE.
  12. #
  13. ############################################################################*/
  14. #include "Python.h"
  15. #include "structmember.h"
  16. #define TYPE(O) ((PyTypeObject*)(O))
  17. #define OBJECT(O) ((PyObject*)(O))
  18. #define CLASSIC(O) ((PyClassObject*)(O))
  19. #ifndef PyVarObject_HEAD_INIT
  20. #define PyVarObject_HEAD_INIT(a, b) PyObject_HEAD_INIT(a) b,
  21. #endif
  22. #ifndef Py_TYPE
  23. #define Py_TYPE(o) ((o)->ob_type)
  24. #endif
  25. #if PY_MAJOR_VERSION >= 3
  26. #define PY3K
  27. #endif
  28. static PyObject *str__dict__, *str__implemented__, *strextends;
  29. static PyObject *BuiltinImplementationSpecifications, *str__provides__;
  30. static PyObject *str__class__, *str__providedBy__;
  31. static PyObject *empty, *fallback, *str_implied, *str_cls, *str_implements;
  32. static PyObject *str__conform__, *str_call_conform, *adapter_hooks;
  33. static PyObject *str_uncached_lookup, *str_uncached_lookupAll;
  34. static PyObject *str_uncached_subscriptions;
  35. static PyObject *str_registry, *strro, *str_generation, *strchanged;
  36. static PyTypeObject *Implements;
  37. static int imported_declarations = 0;
  38. static int
  39. import_declarations(void)
  40. {
  41. PyObject *declarations, *i;
  42. declarations = PyImport_ImportModule("zope.interface.declarations");
  43. if (declarations == NULL)
  44. return -1;
  45. BuiltinImplementationSpecifications = PyObject_GetAttrString(
  46. declarations, "BuiltinImplementationSpecifications");
  47. if (BuiltinImplementationSpecifications == NULL)
  48. return -1;
  49. empty = PyObject_GetAttrString(declarations, "_empty");
  50. if (empty == NULL)
  51. return -1;
  52. fallback = PyObject_GetAttrString(declarations, "implementedByFallback");
  53. if (fallback == NULL)
  54. return -1;
  55. i = PyObject_GetAttrString(declarations, "Implements");
  56. if (i == NULL)
  57. return -1;
  58. if (! PyType_Check(i))
  59. {
  60. PyErr_SetString(PyExc_TypeError,
  61. "zope.interface.declarations.Implements is not a type");
  62. return -1;
  63. }
  64. Implements = (PyTypeObject *)i;
  65. Py_DECREF(declarations);
  66. imported_declarations = 1;
  67. return 0;
  68. }
  69. static PyTypeObject SpecType; /* Forward */
  70. static PyObject *
  71. implementedByFallback(PyObject *cls)
  72. {
  73. if (imported_declarations == 0 && import_declarations() < 0)
  74. return NULL;
  75. return PyObject_CallFunctionObjArgs(fallback, cls, NULL);
  76. }
  77. static PyObject *
  78. implementedBy(PyObject *ignored, PyObject *cls)
  79. {
  80. /* Fast retrieval of implements spec, if possible, to optimize
  81. common case. Use fallback code if we get stuck.
  82. */
  83. PyObject *dict = NULL, *spec;
  84. if (PyType_Check(cls))
  85. {
  86. dict = TYPE(cls)->tp_dict;
  87. Py_XINCREF(dict);
  88. }
  89. if (dict == NULL)
  90. dict = PyObject_GetAttr(cls, str__dict__);
  91. if (dict == NULL)
  92. {
  93. /* Probably a security proxied class, use more expensive fallback code */
  94. PyErr_Clear();
  95. return implementedByFallback(cls);
  96. }
  97. spec = PyObject_GetItem(dict, str__implemented__);
  98. Py_DECREF(dict);
  99. if (spec)
  100. {
  101. if (imported_declarations == 0 && import_declarations() < 0)
  102. return NULL;
  103. if (PyObject_TypeCheck(spec, Implements))
  104. return spec;
  105. /* Old-style declaration, use more expensive fallback code */
  106. Py_DECREF(spec);
  107. return implementedByFallback(cls);
  108. }
  109. PyErr_Clear();
  110. /* Maybe we have a builtin */
  111. if (imported_declarations == 0 && import_declarations() < 0)
  112. return NULL;
  113. spec = PyDict_GetItem(BuiltinImplementationSpecifications, cls);
  114. if (spec != NULL)
  115. {
  116. Py_INCREF(spec);
  117. return spec;
  118. }
  119. /* We're stuck, use fallback */
  120. return implementedByFallback(cls);
  121. }
  122. static PyObject *
  123. getObjectSpecification(PyObject *ignored, PyObject *ob)
  124. {
  125. PyObject *cls, *result;
  126. result = PyObject_GetAttr(ob, str__provides__);
  127. if (result != NULL && PyObject_TypeCheck(result, &SpecType))
  128. return result;
  129. PyErr_Clear();
  130. /* We do a getattr here so as not to be defeated by proxies */
  131. cls = PyObject_GetAttr(ob, str__class__);
  132. if (cls == NULL)
  133. {
  134. PyErr_Clear();
  135. if (imported_declarations == 0 && import_declarations() < 0)
  136. return NULL;
  137. Py_INCREF(empty);
  138. return empty;
  139. }
  140. result = implementedBy(NULL, cls);
  141. Py_DECREF(cls);
  142. return result;
  143. }
  144. static PyObject *
  145. providedBy(PyObject *ignored, PyObject *ob)
  146. {
  147. PyObject *result, *cls, *cp;
  148. result = PyObject_GetAttr(ob, str__providedBy__);
  149. if (result == NULL)
  150. {
  151. PyErr_Clear();
  152. return getObjectSpecification(NULL, ob);
  153. }
  154. /* We want to make sure we have a spec. We can't do a type check
  155. because we may have a proxy, so we'll just try to get the
  156. only attribute.
  157. */
  158. if (PyObject_TypeCheck(result, &SpecType)
  159. ||
  160. PyObject_HasAttr(result, strextends)
  161. )
  162. return result;
  163. /*
  164. The object's class doesn't understand descriptors.
  165. Sigh. We need to get an object descriptor, but we have to be
  166. careful. We want to use the instance's __provides__,l if
  167. there is one, but only if it didn't come from the class.
  168. */
  169. Py_DECREF(result);
  170. cls = PyObject_GetAttr(ob, str__class__);
  171. if (cls == NULL)
  172. return NULL;
  173. result = PyObject_GetAttr(ob, str__provides__);
  174. if (result == NULL)
  175. {
  176. /* No __provides__, so just fall back to implementedBy */
  177. PyErr_Clear();
  178. result = implementedBy(NULL, cls);
  179. Py_DECREF(cls);
  180. return result;
  181. }
  182. cp = PyObject_GetAttr(cls, str__provides__);
  183. if (cp == NULL)
  184. {
  185. /* The the class has no provides, assume we're done: */
  186. PyErr_Clear();
  187. Py_DECREF(cls);
  188. return result;
  189. }
  190. if (cp == result)
  191. {
  192. /*
  193. Oops, we got the provides from the class. This means
  194. the object doesn't have it's own. We should use implementedBy
  195. */
  196. Py_DECREF(result);
  197. result = implementedBy(NULL, cls);
  198. }
  199. Py_DECREF(cls);
  200. Py_DECREF(cp);
  201. return result;
  202. }
  203. /*
  204. Get an attribute from an inst dict. Return a borrowed reference.
  205. This has a number of advantages:
  206. - It avoids layers of Python api
  207. - It doesn't waste time looking for descriptors
  208. - It fails wo raising an exception, although that shouldn't really
  209. matter.
  210. */
  211. static PyObject *
  212. inst_attr(PyObject *self, PyObject *name)
  213. {
  214. PyObject **dictp, *v;
  215. dictp = _PyObject_GetDictPtr(self);
  216. if (dictp && *dictp && (v = PyDict_GetItem(*dictp, name)))
  217. return v;
  218. PyErr_SetObject(PyExc_AttributeError, name);
  219. return NULL;
  220. }
  221. static PyObject *
  222. Spec_extends(PyObject *self, PyObject *other)
  223. {
  224. PyObject *implied;
  225. implied = inst_attr(self, str_implied);
  226. if (implied == NULL)
  227. return NULL;
  228. #ifdef Py_True
  229. if (PyDict_GetItem(implied, other) != NULL)
  230. {
  231. Py_INCREF(Py_True);
  232. return Py_True;
  233. }
  234. Py_INCREF(Py_False);
  235. return Py_False;
  236. #else
  237. return PyInt_FromLong(PyDict_GetItem(implied, other) != NULL);
  238. #endif
  239. }
  240. static char Spec_extends__doc__[] =
  241. "Test whether a specification is or extends another"
  242. ;
  243. static char Spec_providedBy__doc__[] =
  244. "Test whether an interface is implemented by the specification"
  245. ;
  246. static PyObject *
  247. Spec_call(PyObject *self, PyObject *args, PyObject *kw)
  248. {
  249. PyObject *spec;
  250. if (! PyArg_ParseTuple(args, "O", &spec))
  251. return NULL;
  252. return Spec_extends(self, spec);
  253. }
  254. static PyObject *
  255. Spec_providedBy(PyObject *self, PyObject *ob)
  256. {
  257. PyObject *decl, *item;
  258. decl = providedBy(NULL, ob);
  259. if (decl == NULL)
  260. return NULL;
  261. if (PyObject_TypeCheck(decl, &SpecType))
  262. item = Spec_extends(decl, self);
  263. else
  264. /* decl is probably a security proxy. We have to go the long way
  265. around.
  266. */
  267. item = PyObject_CallFunctionObjArgs(decl, self, NULL);
  268. Py_DECREF(decl);
  269. return item;
  270. }
  271. static char Spec_implementedBy__doc__[] =
  272. "Test whether the specification is implemented by a class or factory.\n"
  273. "Raise TypeError if argument is neither a class nor a callable."
  274. ;
  275. static PyObject *
  276. Spec_implementedBy(PyObject *self, PyObject *cls)
  277. {
  278. PyObject *decl, *item;
  279. decl = implementedBy(NULL, cls);
  280. if (decl == NULL)
  281. return NULL;
  282. if (PyObject_TypeCheck(decl, &SpecType))
  283. item = Spec_extends(decl, self);
  284. else
  285. item = PyObject_CallFunctionObjArgs(decl, self, NULL);
  286. Py_DECREF(decl);
  287. return item;
  288. }
  289. static struct PyMethodDef Spec_methods[] = {
  290. {"providedBy",
  291. (PyCFunction)Spec_providedBy, METH_O,
  292. Spec_providedBy__doc__},
  293. {"implementedBy",
  294. (PyCFunction)Spec_implementedBy, METH_O,
  295. Spec_implementedBy__doc__},
  296. {"isOrExtends", (PyCFunction)Spec_extends, METH_O,
  297. Spec_extends__doc__},
  298. {NULL, NULL} /* sentinel */
  299. };
  300. static PyTypeObject SpecType = {
  301. PyVarObject_HEAD_INIT(NULL, 0)
  302. /* tp_name */ "_interface_coptimizations."
  303. "SpecificationBase",
  304. /* tp_basicsize */ 0,
  305. /* tp_itemsize */ 0,
  306. /* tp_dealloc */ (destructor)0,
  307. /* tp_print */ (printfunc)0,
  308. /* tp_getattr */ (getattrfunc)0,
  309. /* tp_setattr */ (setattrfunc)0,
  310. /* tp_compare */ 0,
  311. /* tp_repr */ (reprfunc)0,
  312. /* tp_as_number */ 0,
  313. /* tp_as_sequence */ 0,
  314. /* tp_as_mapping */ 0,
  315. /* tp_hash */ (hashfunc)0,
  316. /* tp_call */ (ternaryfunc)Spec_call,
  317. /* tp_str */ (reprfunc)0,
  318. /* tp_getattro */ (getattrofunc)0,
  319. /* tp_setattro */ (setattrofunc)0,
  320. /* tp_as_buffer */ 0,
  321. /* tp_flags */ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
  322. "Base type for Specification objects",
  323. /* tp_traverse */ (traverseproc)0,
  324. /* tp_clear */ (inquiry)0,
  325. /* tp_richcompare */ (richcmpfunc)0,
  326. /* tp_weaklistoffset */ (long)0,
  327. /* tp_iter */ (getiterfunc)0,
  328. /* tp_iternext */ (iternextfunc)0,
  329. /* tp_methods */ Spec_methods,
  330. };
  331. static PyObject *
  332. OSD_descr_get(PyObject *self, PyObject *inst, PyObject *cls)
  333. {
  334. PyObject *provides;
  335. if (inst == NULL)
  336. return getObjectSpecification(NULL, cls);
  337. provides = PyObject_GetAttr(inst, str__provides__);
  338. if (provides != NULL)
  339. return provides;
  340. PyErr_Clear();
  341. return implementedBy(NULL, cls);
  342. }
  343. static PyTypeObject OSDType = {
  344. PyVarObject_HEAD_INIT(NULL, 0)
  345. /* tp_name */ "_interface_coptimizations."
  346. "ObjectSpecificationDescriptor",
  347. /* tp_basicsize */ 0,
  348. /* tp_itemsize */ 0,
  349. /* tp_dealloc */ (destructor)0,
  350. /* tp_print */ (printfunc)0,
  351. /* tp_getattr */ (getattrfunc)0,
  352. /* tp_setattr */ (setattrfunc)0,
  353. /* tp_compare */ 0,
  354. /* tp_repr */ (reprfunc)0,
  355. /* tp_as_number */ 0,
  356. /* tp_as_sequence */ 0,
  357. /* tp_as_mapping */ 0,
  358. /* tp_hash */ (hashfunc)0,
  359. /* tp_call */ (ternaryfunc)0,
  360. /* tp_str */ (reprfunc)0,
  361. /* tp_getattro */ (getattrofunc)0,
  362. /* tp_setattro */ (setattrofunc)0,
  363. /* tp_as_buffer */ 0,
  364. /* tp_flags */ Py_TPFLAGS_DEFAULT
  365. | Py_TPFLAGS_BASETYPE ,
  366. "Object Specification Descriptor",
  367. /* tp_traverse */ (traverseproc)0,
  368. /* tp_clear */ (inquiry)0,
  369. /* tp_richcompare */ (richcmpfunc)0,
  370. /* tp_weaklistoffset */ (long)0,
  371. /* tp_iter */ (getiterfunc)0,
  372. /* tp_iternext */ (iternextfunc)0,
  373. /* tp_methods */ 0,
  374. /* tp_members */ 0,
  375. /* tp_getset */ 0,
  376. /* tp_base */ 0,
  377. /* tp_dict */ 0, /* internal use */
  378. /* tp_descr_get */ (descrgetfunc)OSD_descr_get,
  379. };
  380. static PyObject *
  381. CPB_descr_get(PyObject *self, PyObject *inst, PyObject *cls)
  382. {
  383. PyObject *mycls, *implements;
  384. mycls = inst_attr(self, str_cls);
  385. if (mycls == NULL)
  386. return NULL;
  387. if (cls == mycls)
  388. {
  389. if (inst == NULL)
  390. {
  391. Py_INCREF(self);
  392. return OBJECT(self);
  393. }
  394. implements = inst_attr(self, str_implements);
  395. Py_XINCREF(implements);
  396. return implements;
  397. }
  398. PyErr_SetObject(PyExc_AttributeError, str__provides__);
  399. return NULL;
  400. }
  401. static PyTypeObject CPBType = {
  402. PyVarObject_HEAD_INIT(NULL, 0)
  403. /* tp_name */ "_interface_coptimizations."
  404. "ClassProvidesBase",
  405. /* tp_basicsize */ 0,
  406. /* tp_itemsize */ 0,
  407. /* tp_dealloc */ (destructor)0,
  408. /* tp_print */ (printfunc)0,
  409. /* tp_getattr */ (getattrfunc)0,
  410. /* tp_setattr */ (setattrfunc)0,
  411. /* tp_compare */ 0,
  412. /* tp_repr */ (reprfunc)0,
  413. /* tp_as_number */ 0,
  414. /* tp_as_sequence */ 0,
  415. /* tp_as_mapping */ 0,
  416. /* tp_hash */ (hashfunc)0,
  417. /* tp_call */ (ternaryfunc)0,
  418. /* tp_str */ (reprfunc)0,
  419. /* tp_getattro */ (getattrofunc)0,
  420. /* tp_setattro */ (setattrofunc)0,
  421. /* tp_as_buffer */ 0,
  422. /* tp_flags */ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
  423. "C Base class for ClassProvides",
  424. /* tp_traverse */ (traverseproc)0,
  425. /* tp_clear */ (inquiry)0,
  426. /* tp_richcompare */ (richcmpfunc)0,
  427. /* tp_weaklistoffset */ (long)0,
  428. /* tp_iter */ (getiterfunc)0,
  429. /* tp_iternext */ (iternextfunc)0,
  430. /* tp_methods */ 0,
  431. /* tp_members */ 0,
  432. /* tp_getset */ 0,
  433. /* tp_base */ &SpecType,
  434. /* tp_dict */ 0, /* internal use */
  435. /* tp_descr_get */ (descrgetfunc)CPB_descr_get,
  436. };
  437. /* ==================================================================== */
  438. /* ========== Begin: __call__ and __adapt__ =========================== */
  439. /*
  440. def __adapt__(self, obj):
  441. """Adapt an object to the reciever
  442. """
  443. if self.providedBy(obj):
  444. return obj
  445. for hook in adapter_hooks:
  446. adapter = hook(self, obj)
  447. if adapter is not None:
  448. return adapter
  449. */
  450. static PyObject *
  451. __adapt__(PyObject *self, PyObject *obj)
  452. {
  453. PyObject *decl, *args, *adapter;
  454. int implements, i, l;
  455. decl = providedBy(NULL, obj);
  456. if (decl == NULL)
  457. return NULL;
  458. if (PyObject_TypeCheck(decl, &SpecType))
  459. {
  460. PyObject *implied;
  461. implied = inst_attr(decl, str_implied);
  462. if (implied == NULL)
  463. {
  464. Py_DECREF(decl);
  465. return NULL;
  466. }
  467. implements = PyDict_GetItem(implied, self) != NULL;
  468. Py_DECREF(decl);
  469. }
  470. else
  471. {
  472. /* decl is probably a security proxy. We have to go the long way
  473. around.
  474. */
  475. PyObject *r;
  476. r = PyObject_CallFunctionObjArgs(decl, self, NULL);
  477. Py_DECREF(decl);
  478. if (r == NULL)
  479. return NULL;
  480. implements = PyObject_IsTrue(r);
  481. Py_DECREF(r);
  482. }
  483. if (implements)
  484. {
  485. Py_INCREF(obj);
  486. return obj;
  487. }
  488. l = PyList_GET_SIZE(adapter_hooks);
  489. args = PyTuple_New(2);
  490. if (args == NULL)
  491. return NULL;
  492. Py_INCREF(self);
  493. PyTuple_SET_ITEM(args, 0, self);
  494. Py_INCREF(obj);
  495. PyTuple_SET_ITEM(args, 1, obj);
  496. for (i = 0; i < l; i++)
  497. {
  498. adapter = PyObject_CallObject(PyList_GET_ITEM(adapter_hooks, i), args);
  499. if (adapter == NULL || adapter != Py_None)
  500. {
  501. Py_DECREF(args);
  502. return adapter;
  503. }
  504. Py_DECREF(adapter);
  505. }
  506. Py_DECREF(args);
  507. Py_INCREF(Py_None);
  508. return Py_None;
  509. }
  510. static struct PyMethodDef ib_methods[] = {
  511. {"__adapt__", (PyCFunction)__adapt__, METH_O,
  512. "Adapt an object to the reciever"},
  513. {NULL, NULL} /* sentinel */
  514. };
  515. /*
  516. def __call__(self, obj, alternate=_marker):
  517. conform = getattr(obj, '__conform__', None)
  518. if conform is not None:
  519. adapter = self._call_conform(conform)
  520. if adapter is not None:
  521. return adapter
  522. adapter = self.__adapt__(obj)
  523. if adapter is not None:
  524. return adapter
  525. elif alternate is not _marker:
  526. return alternate
  527. else:
  528. raise TypeError("Could not adapt", obj, self)
  529. */
  530. static PyObject *
  531. ib_call(PyObject *self, PyObject *args, PyObject *kwargs)
  532. {
  533. PyObject *conform, *obj, *alternate=NULL, *adapter;
  534. static char *kwlist[] = {"obj", "alternate", NULL};
  535. if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|O", kwlist,
  536. &obj, &alternate))
  537. return NULL;
  538. conform = PyObject_GetAttr(obj, str__conform__);
  539. if (conform != NULL)
  540. {
  541. adapter = PyObject_CallMethodObjArgs(self, str_call_conform,
  542. conform, NULL);
  543. Py_DECREF(conform);
  544. if (adapter == NULL || adapter != Py_None)
  545. return adapter;
  546. Py_DECREF(adapter);
  547. }
  548. else
  549. PyErr_Clear();
  550. adapter = __adapt__(self, obj);
  551. if (adapter == NULL || adapter != Py_None)
  552. return adapter;
  553. Py_DECREF(adapter);
  554. if (alternate != NULL)
  555. {
  556. Py_INCREF(alternate);
  557. return alternate;
  558. }
  559. adapter = Py_BuildValue("sOO", "Could not adapt", obj, self);
  560. if (adapter != NULL)
  561. {
  562. PyErr_SetObject(PyExc_TypeError, adapter);
  563. Py_DECREF(adapter);
  564. }
  565. return NULL;
  566. }
  567. static PyTypeObject InterfaceBase = {
  568. PyVarObject_HEAD_INIT(NULL, 0)
  569. /* tp_name */ "_zope_interface_coptimizations."
  570. "InterfaceBase",
  571. /* tp_basicsize */ 0,
  572. /* tp_itemsize */ 0,
  573. /* tp_dealloc */ (destructor)0,
  574. /* tp_print */ (printfunc)0,
  575. /* tp_getattr */ (getattrfunc)0,
  576. /* tp_setattr */ (setattrfunc)0,
  577. /* tp_compare */ 0,
  578. /* tp_repr */ (reprfunc)0,
  579. /* tp_as_number */ 0,
  580. /* tp_as_sequence */ 0,
  581. /* tp_as_mapping */ 0,
  582. /* tp_hash */ (hashfunc)0,
  583. /* tp_call */ (ternaryfunc)ib_call,
  584. /* tp_str */ (reprfunc)0,
  585. /* tp_getattro */ (getattrofunc)0,
  586. /* tp_setattro */ (setattrofunc)0,
  587. /* tp_as_buffer */ 0,
  588. /* tp_flags */ Py_TPFLAGS_DEFAULT
  589. | Py_TPFLAGS_BASETYPE ,
  590. /* tp_doc */ "Interface base type providing __call__ and __adapt__",
  591. /* tp_traverse */ (traverseproc)0,
  592. /* tp_clear */ (inquiry)0,
  593. /* tp_richcompare */ (richcmpfunc)0,
  594. /* tp_weaklistoffset */ (long)0,
  595. /* tp_iter */ (getiterfunc)0,
  596. /* tp_iternext */ (iternextfunc)0,
  597. /* tp_methods */ ib_methods,
  598. };
  599. /* =================== End: __call__ and __adapt__ ==================== */
  600. /* ==================================================================== */
  601. /* ==================================================================== */
  602. /* ========================== Begin: Lookup Bases ===================== */
  603. typedef struct {
  604. PyObject_HEAD
  605. PyObject *_cache;
  606. PyObject *_mcache;
  607. PyObject *_scache;
  608. } lookup;
  609. typedef struct {
  610. PyObject_HEAD
  611. PyObject *_cache;
  612. PyObject *_mcache;
  613. PyObject *_scache;
  614. PyObject *_verify_ro;
  615. PyObject *_verify_generations;
  616. } verify;
  617. static int
  618. lookup_traverse(lookup *self, visitproc visit, void *arg)
  619. {
  620. int vret;
  621. if (self->_cache) {
  622. vret = visit(self->_cache, arg);
  623. if (vret != 0)
  624. return vret;
  625. }
  626. if (self->_mcache) {
  627. vret = visit(self->_mcache, arg);
  628. if (vret != 0)
  629. return vret;
  630. }
  631. if (self->_scache) {
  632. vret = visit(self->_scache, arg);
  633. if (vret != 0)
  634. return vret;
  635. }
  636. return 0;
  637. }
  638. static int
  639. lookup_clear(lookup *self)
  640. {
  641. Py_CLEAR(self->_cache);
  642. Py_CLEAR(self->_mcache);
  643. Py_CLEAR(self->_scache);
  644. return 0;
  645. }
  646. static void
  647. lookup_dealloc(lookup *self)
  648. {
  649. lookup_clear(self);
  650. Py_TYPE(self)->tp_free((PyObject*)self);
  651. }
  652. /*
  653. def changed(self, ignored=None):
  654. self._cache.clear()
  655. self._mcache.clear()
  656. self._scache.clear()
  657. */
  658. static PyObject *
  659. lookup_changed(lookup *self, PyObject *ignored)
  660. {
  661. lookup_clear(self);
  662. Py_INCREF(Py_None);
  663. return Py_None;
  664. }
  665. #define ASSURE_DICT(N) if (N == NULL) { N = PyDict_New(); \
  666. if (N == NULL) return NULL; \
  667. }
  668. /*
  669. def _getcache(self, provided, name):
  670. cache = self._cache.get(provided)
  671. if cache is None:
  672. cache = {}
  673. self._cache[provided] = cache
  674. if name:
  675. c = cache.get(name)
  676. if c is None:
  677. c = {}
  678. cache[name] = c
  679. cache = c
  680. return cache
  681. */
  682. static PyObject *
  683. _subcache(PyObject *cache, PyObject *key)
  684. {
  685. PyObject *subcache;
  686. subcache = PyDict_GetItem(cache, key);
  687. if (subcache == NULL)
  688. {
  689. int status;
  690. subcache = PyDict_New();
  691. if (subcache == NULL)
  692. return NULL;
  693. status = PyDict_SetItem(cache, key, subcache);
  694. Py_DECREF(subcache);
  695. if (status < 0)
  696. return NULL;
  697. }
  698. return subcache;
  699. }
  700. static PyObject *
  701. _getcache(lookup *self, PyObject *provided, PyObject *name)
  702. {
  703. PyObject *cache;
  704. ASSURE_DICT(self->_cache);
  705. cache = _subcache(self->_cache, provided);
  706. if (cache == NULL)
  707. return NULL;
  708. if (name != NULL && PyObject_IsTrue(name))
  709. cache = _subcache(cache, name);
  710. return cache;
  711. }
  712. /*
  713. def lookup(self, required, provided, name=u'', default=None):
  714. cache = self._getcache(provided, name)
  715. if len(required) == 1:
  716. result = cache.get(required[0], _not_in_mapping)
  717. else:
  718. result = cache.get(tuple(required), _not_in_mapping)
  719. if result is _not_in_mapping:
  720. result = self._uncached_lookup(required, provided, name)
  721. if len(required) == 1:
  722. cache[required[0]] = result
  723. else:
  724. cache[tuple(required)] = result
  725. if result is None:
  726. return default
  727. return result
  728. */
  729. static PyObject *
  730. tuplefy(PyObject *v)
  731. {
  732. if (! PyTuple_Check(v))
  733. {
  734. v = PyObject_CallFunctionObjArgs(OBJECT(&PyTuple_Type), v, NULL);
  735. if (v == NULL)
  736. return NULL;
  737. }
  738. else
  739. Py_INCREF(v);
  740. return v;
  741. }
  742. static PyObject *
  743. _lookup(lookup *self,
  744. PyObject *required, PyObject *provided, PyObject *name,
  745. PyObject *default_)
  746. {
  747. PyObject *result, *key, *cache;
  748. #ifdef PY3K
  749. if ( name && !PyUnicode_Check(name) )
  750. #else
  751. if ( name && !PyString_Check(name) && !PyUnicode_Check(name) )
  752. #endif
  753. {
  754. PyErr_SetString(PyExc_ValueError,
  755. "name is not a string or unicode");
  756. return NULL;
  757. }
  758. cache = _getcache(self, provided, name);
  759. if (cache == NULL)
  760. return NULL;
  761. required = tuplefy(required);
  762. if (required == NULL)
  763. return NULL;
  764. if (PyTuple_GET_SIZE(required) == 1)
  765. key = PyTuple_GET_ITEM(required, 0);
  766. else
  767. key = required;
  768. result = PyDict_GetItem(cache, key);
  769. if (result == NULL)
  770. {
  771. int status;
  772. result = PyObject_CallMethodObjArgs(OBJECT(self), str_uncached_lookup,
  773. required, provided, name, NULL);
  774. if (result == NULL)
  775. {
  776. Py_DECREF(required);
  777. return NULL;
  778. }
  779. status = PyDict_SetItem(cache, key, result);
  780. Py_DECREF(required);
  781. if (status < 0)
  782. {
  783. Py_DECREF(result);
  784. return NULL;
  785. }
  786. }
  787. else
  788. {
  789. Py_INCREF(result);
  790. Py_DECREF(required);
  791. }
  792. if (result == Py_None && default_ != NULL)
  793. {
  794. Py_DECREF(Py_None);
  795. Py_INCREF(default_);
  796. return default_;
  797. }
  798. return result;
  799. }
  800. static PyObject *
  801. lookup_lookup(lookup *self, PyObject *args, PyObject *kwds)
  802. {
  803. static char *kwlist[] = {"required", "provided", "name", "default", NULL};
  804. PyObject *required, *provided, *name=NULL, *default_=NULL;
  805. if (! PyArg_ParseTupleAndKeywords(args, kwds, "OO|OO", kwlist,
  806. &required, &provided, &name, &default_))
  807. return NULL;
  808. return _lookup(self, required, provided, name, default_);
  809. }
  810. /*
  811. def lookup1(self, required, provided, name=u'', default=None):
  812. cache = self._getcache(provided, name)
  813. result = cache.get(required, _not_in_mapping)
  814. if result is _not_in_mapping:
  815. return self.lookup((required, ), provided, name, default)
  816. if result is None:
  817. return default
  818. return result
  819. */
  820. static PyObject *
  821. _lookup1(lookup *self,
  822. PyObject *required, PyObject *provided, PyObject *name,
  823. PyObject *default_)
  824. {
  825. PyObject *result, *cache;
  826. #ifdef PY3K
  827. if ( name && !PyUnicode_Check(name) )
  828. #else
  829. if ( name && !PyString_Check(name) && !PyUnicode_Check(name) )
  830. #endif
  831. {
  832. PyErr_SetString(PyExc_ValueError,
  833. "name is not a string or unicode");
  834. return NULL;
  835. }
  836. cache = _getcache(self, provided, name);
  837. if (cache == NULL)
  838. return NULL;
  839. result = PyDict_GetItem(cache, required);
  840. if (result == NULL)
  841. {
  842. PyObject *tup;
  843. tup = PyTuple_New(1);
  844. if (tup == NULL)
  845. return NULL;
  846. Py_INCREF(required);
  847. PyTuple_SET_ITEM(tup, 0, required);
  848. result = _lookup(self, tup, provided, name, default_);
  849. Py_DECREF(tup);
  850. }
  851. else
  852. {
  853. if (result == Py_None && default_ != NULL)
  854. {
  855. result = default_;
  856. }
  857. Py_INCREF(result);
  858. }
  859. return result;
  860. }
  861. static PyObject *
  862. lookup_lookup1(lookup *self, PyObject *args, PyObject *kwds)
  863. {
  864. static char *kwlist[] = {"required", "provided", "name", "default", NULL};
  865. PyObject *required, *provided, *name=NULL, *default_=NULL;
  866. if (! PyArg_ParseTupleAndKeywords(args, kwds, "OO|OO", kwlist,
  867. &required, &provided, &name, &default_))
  868. return NULL;
  869. return _lookup1(self, required, provided, name, default_);
  870. }
  871. /*
  872. def adapter_hook(self, provided, object, name=u'', default=None):
  873. required = providedBy(object)
  874. cache = self._getcache(provided, name)
  875. factory = cache.get(required, _not_in_mapping)
  876. if factory is _not_in_mapping:
  877. factory = self.lookup((required, ), provided, name)
  878. if factory is not None:
  879. result = factory(object)
  880. if result is not None:
  881. return result
  882. return default
  883. */
  884. static PyObject *
  885. _adapter_hook(lookup *self,
  886. PyObject *provided, PyObject *object, PyObject *name,
  887. PyObject *default_)
  888. {
  889. PyObject *required, *factory, *result;
  890. #ifdef PY3K
  891. if ( name && !PyUnicode_Check(name) )
  892. #else
  893. if ( name && !PyString_Check(name) && !PyUnicode_Check(name) )
  894. #endif
  895. {
  896. PyErr_SetString(PyExc_ValueError,
  897. "name is not a string or unicode");
  898. return NULL;
  899. }
  900. required = providedBy(NULL, object);
  901. if (required == NULL)
  902. return NULL;
  903. factory = _lookup1(self, required, provided, name, Py_None);
  904. Py_DECREF(required);
  905. if (factory == NULL)
  906. return NULL;
  907. if (factory != Py_None)
  908. {
  909. result = PyObject_CallFunctionObjArgs(factory, object, NULL);
  910. Py_DECREF(factory);
  911. if (result == NULL || result != Py_None)
  912. return result;
  913. }
  914. else
  915. result = factory; /* None */
  916. if (default_ == NULL || default_ == result) /* No default specified, */
  917. return result; /* Return None. result is owned None */
  918. Py_DECREF(result);
  919. Py_INCREF(default_);
  920. return default_;
  921. }
  922. static PyObject *
  923. lookup_adapter_hook(lookup *self, PyObject *args, PyObject *kwds)
  924. {
  925. static char *kwlist[] = {"provided", "object", "name", "default", NULL};
  926. PyObject *object, *provided, *name=NULL, *default_=NULL;
  927. if (! PyArg_ParseTupleAndKeywords(args, kwds, "OO|OO", kwlist,
  928. &provided, &object, &name, &default_))
  929. return NULL;
  930. return _adapter_hook(self, provided, object, name, default_);
  931. }
  932. static PyObject *
  933. lookup_queryAdapter(lookup *self, PyObject *args, PyObject *kwds)
  934. {
  935. static char *kwlist[] = {"object", "provided", "name", "default", NULL};
  936. PyObject *object, *provided, *name=NULL, *default_=NULL;
  937. if (! PyArg_ParseTupleAndKeywords(args, kwds, "OO|OO", kwlist,
  938. &object, &provided, &name, &default_))
  939. return NULL;
  940. return _adapter_hook(self, provided, object, name, default_);
  941. }
  942. /*
  943. def lookupAll(self, required, provided):
  944. cache = self._mcache.get(provided)
  945. if cache is None:
  946. cache = {}
  947. self._mcache[provided] = cache
  948. required = tuple(required)
  949. result = cache.get(required, _not_in_mapping)
  950. if result is _not_in_mapping:
  951. result = self._uncached_lookupAll(required, provided)
  952. cache[required] = result
  953. return result
  954. */
  955. static PyObject *
  956. _lookupAll(lookup *self, PyObject *required, PyObject *provided)
  957. {
  958. PyObject *cache, *result;
  959. ASSURE_DICT(self->_mcache);
  960. cache = _subcache(self->_mcache, provided);
  961. if (cache == NULL)
  962. return NULL;
  963. required = tuplefy(required);
  964. if (required == NULL)
  965. return NULL;
  966. result = PyDict_GetItem(cache, required);
  967. if (result == NULL)
  968. {
  969. int status;
  970. result = PyObject_CallMethodObjArgs(OBJECT(self), str_uncached_lookupAll,
  971. required, provided, NULL);
  972. if (result == NULL)
  973. {
  974. Py_DECREF(required);
  975. return NULL;
  976. }
  977. status = PyDict_SetItem(cache, required, result);
  978. Py_DECREF(required);
  979. if (status < 0)
  980. {
  981. Py_DECREF(result);
  982. return NULL;
  983. }
  984. }
  985. else
  986. {
  987. Py_INCREF(result);
  988. Py_DECREF(required);
  989. }
  990. return result;
  991. }
  992. static PyObject *
  993. lookup_lookupAll(lookup *self, PyObject *args, PyObject *kwds)
  994. {
  995. static char *kwlist[] = {"required", "provided", NULL};
  996. PyObject *required, *provided;
  997. if (! PyArg_ParseTupleAndKeywords(args, kwds, "OO", kwlist,
  998. &required, &provided))
  999. return NULL;
  1000. return _lookupAll(self, required, provided);
  1001. }
  1002. /*
  1003. def subscriptions(self, required, provided):
  1004. cache = self._scache.get(provided)
  1005. if cache is None:
  1006. cache = {}
  1007. self._scache[provided] = cache
  1008. required = tuple(required)
  1009. result = cache.get(required, _not_in_mapping)
  1010. if result is _not_in_mapping:
  1011. result = self._uncached_subscriptions(required, provided)
  1012. cache[required] = result
  1013. return result
  1014. */
  1015. static PyObject *
  1016. _subscriptions(lookup *self, PyObject *required, PyObject *provided)
  1017. {
  1018. PyObject *cache, *result;
  1019. ASSURE_DICT(self->_scache);
  1020. cache = _subcache(self->_scache, provided);
  1021. if (cache == NULL)
  1022. return NULL;
  1023. required = tuplefy(required);
  1024. if (required == NULL)
  1025. return NULL;
  1026. result = PyDict_GetItem(cache, required);
  1027. if (result == NULL)
  1028. {
  1029. int status;
  1030. result = PyObject_CallMethodObjArgs(
  1031. OBJECT(self), str_uncached_subscriptions,
  1032. required, provided, NULL);
  1033. if (result == NULL)
  1034. {
  1035. Py_DECREF(required);
  1036. return NULL;
  1037. }
  1038. status = PyDict_SetItem(cache, required, result);
  1039. Py_DECREF(required);
  1040. if (status < 0)
  1041. {
  1042. Py_DECREF(result);
  1043. return NULL;
  1044. }
  1045. }
  1046. else
  1047. {
  1048. Py_INCREF(result);
  1049. Py_DECREF(required);
  1050. }
  1051. return result;
  1052. }
  1053. static PyObject *
  1054. lookup_subscriptions(lookup *self, PyObject *args, PyObject *kwds)
  1055. {
  1056. static char *kwlist[] = {"required", "provided", NULL};
  1057. PyObject *required, *provided;
  1058. if (! PyArg_ParseTupleAndKeywords(args, kwds, "OO", kwlist,
  1059. &required, &provided))
  1060. return NULL;
  1061. return _subscriptions(self, required, provided);
  1062. }
  1063. static struct PyMethodDef lookup_methods[] = {
  1064. {"changed", (PyCFunction)lookup_changed, METH_O, ""},
  1065. {"lookup", (PyCFunction)lookup_lookup, METH_KEYWORDS | METH_VARARGS, ""},
  1066. {"lookup1", (PyCFunction)lookup_lookup1, METH_KEYWORDS | METH_VARARGS, ""},
  1067. {"queryAdapter", (PyCFunction)lookup_queryAdapter, METH_KEYWORDS | METH_VARARGS, ""},
  1068. {"adapter_hook", (PyCFunction)lookup_adapter_hook, METH_KEYWORDS | METH_VARARGS, ""},
  1069. {"lookupAll", (PyCFunction)lookup_lookupAll, METH_KEYWORDS | METH_VARARGS, ""},
  1070. {"subscriptions", (PyCFunction)lookup_subscriptions, METH_KEYWORDS | METH_VARARGS, ""},
  1071. {NULL, NULL} /* sentinel */
  1072. };
  1073. static PyTypeObject LookupBase = {
  1074. PyVarObject_HEAD_INIT(NULL, 0)
  1075. /* tp_name */ "_zope_interface_coptimizations."
  1076. "LookupBase",
  1077. /* tp_basicsize */ sizeof(lookup),
  1078. /* tp_itemsize */ 0,
  1079. /* tp_dealloc */ (destructor)&lookup_dealloc,
  1080. /* tp_print */ (printfunc)0,
  1081. /* tp_getattr */ (getattrfunc)0,
  1082. /* tp_setattr */ (setattrfunc)0,
  1083. /* tp_compare */ 0,
  1084. /* tp_repr */ (reprfunc)0,
  1085. /* tp_as_number */ 0,
  1086. /* tp_as_sequence */ 0,
  1087. /* tp_as_mapping */ 0,
  1088. /* tp_hash */ (hashfunc)0,
  1089. /* tp_call */ (ternaryfunc)0,
  1090. /* tp_str */ (reprfunc)0,
  1091. /* tp_getattro */ (getattrofunc)0,
  1092. /* tp_setattro */ (setattrofunc)0,
  1093. /* tp_as_buffer */ 0,
  1094. /* tp_flags */ Py_TPFLAGS_DEFAULT
  1095. | Py_TPFLAGS_BASETYPE
  1096. | Py_TPFLAGS_HAVE_GC,
  1097. /* tp_doc */ "",
  1098. /* tp_traverse */ (traverseproc)lookup_traverse,
  1099. /* tp_clear */ (inquiry)lookup_clear,
  1100. /* tp_richcompare */ (richcmpfunc)0,
  1101. /* tp_weaklistoffset */ (long)0,
  1102. /* tp_iter */ (getiterfunc)0,
  1103. /* tp_iternext */ (iternextfunc)0,
  1104. /* tp_methods */ lookup_methods,
  1105. };
  1106. static int
  1107. verifying_traverse(verify *self, visitproc visit, void *arg)
  1108. {
  1109. int vret;
  1110. vret = lookup_traverse((lookup *)self, visit, arg);
  1111. if (vret != 0)
  1112. return vret;
  1113. if (self->_verify_ro) {
  1114. vret = visit(self->_verify_ro, arg);
  1115. if (vret != 0)
  1116. return vret;
  1117. }
  1118. if (self->_verify_generations) {
  1119. vret = visit(self->_verify_generations, arg);
  1120. if (vret != 0)
  1121. return vret;
  1122. }
  1123. return 0;
  1124. }
  1125. static int
  1126. verifying_clear(verify *self)
  1127. {
  1128. lookup_clear((lookup *)self);
  1129. Py_CLEAR(self->_verify_generations);
  1130. Py_CLEAR(self->_verify_ro);
  1131. return 0;
  1132. }
  1133. static void
  1134. verifying_dealloc(verify *self)
  1135. {
  1136. verifying_clear(self);
  1137. Py_TYPE(self)->tp_free((PyObject*)self);
  1138. }
  1139. /*
  1140. def changed(self, originally_changed):
  1141. super(VerifyingBasePy, self).changed(originally_changed)
  1142. self._verify_ro = self._registry.ro[1:]
  1143. self._verify_generations = [r._generation for r in self._verify_ro]
  1144. */
  1145. static PyObject *
  1146. _generations_tuple(PyObject *ro)
  1147. {
  1148. int i, l;
  1149. PyObject *generations;
  1150. l = PyTuple_GET_SIZE(ro);
  1151. generations = PyTuple_New(l);
  1152. for (i=0; i < l; i++)
  1153. {
  1154. PyObject *generation;
  1155. generation = PyObject_GetAttr(PyTuple_GET_ITEM(ro, i), str_generation);
  1156. if (generation == NULL)
  1157. {
  1158. Py_DECREF(generations);
  1159. return NULL;
  1160. }
  1161. PyTuple_SET_ITEM(generations, i, generation);
  1162. }
  1163. return generations;
  1164. }
  1165. static PyObject *
  1166. verifying_changed(verify *self, PyObject *ignored)
  1167. {
  1168. PyObject *t, *ro;
  1169. verifying_clear(self);
  1170. t = PyObject_GetAttr(OBJECT(self), str_registry);
  1171. if (t == NULL)
  1172. return NULL;
  1173. ro = PyObject_GetAttr(t, strro);
  1174. Py_DECREF(t);
  1175. if (ro == NULL)
  1176. return NULL;
  1177. t = PyObject_CallFunctionObjArgs(OBJECT(&PyTuple_Type), ro, NULL);
  1178. Py_DECREF(ro);
  1179. if (t == NULL)
  1180. return NULL;
  1181. ro = PyTuple_GetSlice(t, 1, PyTuple_GET_SIZE(t));
  1182. Py_DECREF(t);
  1183. if (ro == NULL)
  1184. return NULL;
  1185. self->_verify_generations = _generations_tuple(ro);
  1186. if (self->_verify_generations == NULL)
  1187. {
  1188. Py_DECREF(ro);
  1189. return NULL;
  1190. }
  1191. self->_verify_ro = ro;
  1192. Py_INCREF(Py_None);
  1193. return Py_None;
  1194. }
  1195. /*
  1196. def _verify(self):
  1197. if ([r._generation for r in self._verify_ro]
  1198. != self._verify_generations):
  1199. self.changed(None)
  1200. */
  1201. static int
  1202. _verify(verify *self)
  1203. {
  1204. PyObject *changed_result;
  1205. if (self->_verify_ro != NULL && self->_verify_generations != NULL)
  1206. {
  1207. PyObject *generations;
  1208. int changed;
  1209. generations = _generations_tuple(self->_verify_ro);
  1210. if (generations == NULL)
  1211. return -1;
  1212. changed = PyObject_RichCompareBool(self->_verify_generations,
  1213. generations, Py_NE);
  1214. Py_DECREF(generations);
  1215. if (changed == -1)
  1216. return -1;
  1217. if (changed == 0)
  1218. return 0;
  1219. }
  1220. changed_result = PyObject_CallMethodObjArgs(OBJECT(self), strchanged,
  1221. Py_None, NULL);
  1222. if (changed_result == NULL)
  1223. return -1;
  1224. Py_DECREF(changed_result);
  1225. return 0;
  1226. }
  1227. static PyObject *
  1228. verifying_lookup(verify *self, PyObject *args, PyObject *kwds)
  1229. {
  1230. static char *kwlist[] = {"required", "provided", "name", "default", NULL};
  1231. PyObject *required, *provided, *name=NULL, *default_=NULL;
  1232. if (! PyArg_ParseTupleAndKeywords(args, kwds, "OO|OO", kwlist,
  1233. &required, &provided, &name, &default_))
  1234. return NULL;
  1235. if (_verify(self) < 0)
  1236. return NULL;
  1237. return _lookup((lookup *)self, required, provided, name, default_);
  1238. }
  1239. static PyObject *
  1240. verifying_lookup1(verify *self, PyObject *args, PyObject *kwds)
  1241. {
  1242. static char *kwlist[] = {"required", "provided", "name", "default", NULL};
  1243. PyObject *required, *provided, *name=NULL, *default_=NULL;
  1244. if (! PyArg_ParseTupleAndKeywords(args, kwds, "OO|OO", kwlist,
  1245. &required, &provided, &name, &default_))
  1246. return NULL;
  1247. if (_verify(self) < 0)
  1248. return NULL;
  1249. return _lookup1((lookup *)self, required, provided, name, default_);
  1250. }
  1251. static PyObject *
  1252. verifying_adapter_hook(verify *self, PyObject *args, PyObject *kwds)
  1253. {
  1254. static char *kwlist[] = {"provided", "object", "name", "default", NULL};
  1255. PyObject *object, *provided, *name=NULL, *default_=NULL;
  1256. if (! PyArg_ParseTupleAndKeywords(args, kwds, "OO|OO", kwlist,
  1257. &provided, &object, &name, &default_))
  1258. return NULL;
  1259. if (_verify(self) < 0)
  1260. return NULL;
  1261. return _adapter_hook((lookup *)self, provided, object, name, default_);
  1262. }
  1263. static PyObject *
  1264. verifying_queryAdapter(verify *self, PyObject *args, PyObject *kwds)
  1265. {
  1266. static char *kwlist[] = {"object", "provided", "name", "default", NULL};
  1267. PyObject *object, *provided, *name=NULL, *default_=NULL;
  1268. if (! PyArg_ParseTupleAndKeywords(args, kwds, "OO|OO", kwlist,
  1269. &object, &provided, &name, &default_))
  1270. return NULL;
  1271. if (_verify(self) < 0)
  1272. return NULL;
  1273. return _adapter_hook((lookup *)self, provided, object, name, default_);
  1274. }
  1275. static PyObject *
  1276. verifying_lookupAll(verify *self, PyObject *args, PyObject *kwds)
  1277. {
  1278. static char *kwlist[] = {"required", "provided", NULL};
  1279. PyObject *required, *provided;
  1280. if (! PyArg_ParseTupleAndKeywords(args, kwds, "OO", kwlist,
  1281. &required, &provided))
  1282. return NULL;
  1283. if (_verify(self) < 0)
  1284. return NULL;
  1285. return _lookupAll((lookup *)self, required, provided);
  1286. }
  1287. static PyObject *
  1288. verifying_subscriptions(verify *self, PyObject *args, PyObject *kwds)
  1289. {
  1290. static char *kwlist[] = {"required", "provided", NULL};
  1291. PyObject *required, *provided;
  1292. if (! PyArg_ParseTupleAndKeywords(args, kwds, "OO", kwlist,
  1293. &required, &provided))
  1294. return NULL;
  1295. if (_verify(self) < 0)
  1296. return NULL;
  1297. return _subscriptions((lookup *)self, required, provided);
  1298. }
  1299. static struct PyMethodDef verifying_methods[] = {
  1300. {"changed", (PyCFunction)verifying_changed, METH_O, ""},
  1301. {"lookup", (PyCFunction)verifying_lookup, METH_KEYWORDS | METH_VARARGS, ""},
  1302. {"lookup1", (PyCFunction)verifying_lookup1, METH_KEYWORDS | METH_VARARGS, ""},
  1303. {"queryAdapter", (PyCFunction)verifying_queryAdapter, METH_KEYWORDS | METH_VARARGS, ""},
  1304. {"adapter_hook", (PyCFunction)verifying_adapter_hook, METH_KEYWORDS | METH_VARARGS, ""},
  1305. {"lookupAll", (PyCFunction)verifying_lookupAll, METH_KEYWORDS | METH_VARARGS, ""},
  1306. {"subscriptions", (PyCFunction)verifying_subscriptions, METH_KEYWORDS | METH_VARARGS, ""},
  1307. {NULL, NULL} /* sentinel */
  1308. };
  1309. static PyTypeObject VerifyingBase = {
  1310. PyVarObject_HEAD_INIT(NULL, 0)
  1311. /* tp_name */ "_zope_interface_coptimizations."
  1312. "VerifyingBase",
  1313. /* tp_basicsize */ sizeof(verify),
  1314. /* tp_itemsize */ 0,
  1315. /* tp_dealloc */ (destructor)&verifying_dealloc,
  1316. /* tp_print */ (printfunc)0,
  1317. /* tp_getattr */ (getattrfunc)0,
  1318. /* tp_setattr */ (setattrfunc)0,
  1319. /* tp_compare */ 0,
  1320. /* tp_repr */ (reprfunc)0,
  1321. /* tp_as_number */ 0,
  1322. /* tp_as_sequence */ 0,
  1323. /* tp_as_mapping */ 0,
  1324. /* tp_hash */ (hashfunc)0,
  1325. /* tp_call */ (ternaryfunc)0,
  1326. /* tp_str */ (reprfunc)0,
  1327. /* tp_getattro */ (getattrofunc)0,
  1328. /* tp_setattro */ (setattrofunc)0,
  1329. /* tp_as_buffer */ 0,
  1330. /* tp_flags */ Py_TPFLAGS_DEFAULT
  1331. | Py_TPFLAGS_BASETYPE
  1332. | Py_TPFLAGS_HAVE_GC,
  1333. /* tp_doc */ "",
  1334. /* tp_traverse */ (traverseproc)verifying_traverse,
  1335. /* tp_clear */ (inquiry)verifying_clear,
  1336. /* tp_richcompare */ (richcmpfunc)0,
  1337. /* tp_weaklistoffset */ (long)0,
  1338. /* tp_iter */ (getiterfunc)0,
  1339. /* tp_iternext */ (iternextfunc)0,
  1340. /* tp_methods */ verifying_methods,
  1341. /* tp_members */ 0,
  1342. /* tp_getset */ 0,
  1343. /* tp_base */ &LookupBase,
  1344. };
  1345. /* ========================== End: Lookup Bases ======================= */
  1346. /* ==================================================================== */
  1347. static struct PyMethodDef m_methods[] = {
  1348. {"implementedBy", (PyCFunction)implementedBy, METH_O,
  1349. "Interfaces implemented by a class or factory.\n"
  1350. "Raises TypeError if argument is neither a class nor a callable."},
  1351. {"getObjectSpecification", (PyCFunction)getObjectSpecification, METH_O,
  1352. "Get an object's interfaces (internal api)"},
  1353. {"providedBy", (PyCFunction)providedBy, METH_O,
  1354. "Get an object's interfaces"},
  1355. {NULL, (PyCFunction)NULL, 0, NULL} /* sentinel */
  1356. };
  1357. #if PY_MAJOR_VERSION >= 3
  1358. static char module_doc[] = "C optimizations for zope.interface\n\n";
  1359. static struct PyModuleDef _zic_module = {
  1360. PyModuleDef_HEAD_INIT,
  1361. "_zope_interface_coptimizations",
  1362. module_doc,
  1363. -1,
  1364. m_methods,
  1365. NULL,
  1366. NULL,
  1367. NULL,
  1368. NULL
  1369. };
  1370. #endif
  1371. static PyObject *
  1372. init(void)
  1373. {
  1374. PyObject *m;
  1375. #if PY_MAJOR_VERSION < 3
  1376. #define DEFINE_STRING(S) \
  1377. if(! (str ## S = PyString_FromString(# S))) return NULL
  1378. #else
  1379. #define DEFINE_STRING(S) \
  1380. if(! (str ## S = PyUnicode_FromString(# S))) return NULL
  1381. #endif
  1382. DEFINE_STRING(__dict__);
  1383. DEFINE_STRING(__implemented__);
  1384. DEFINE_STRING(__provides__);
  1385. DEFINE_STRING(__class__);
  1386. DEFINE_STRING(__providedBy__);
  1387. DEFINE_STRING(extends);
  1388. DEFINE_STRING(_implied);
  1389. DEFINE_STRING(_implements);
  1390. DEFINE_STRING(_cls);
  1391. DEFINE_STRING(__conform__);
  1392. DEFINE_STRING(_call_conform);
  1393. DEFINE_STRING(_uncached_lookup);
  1394. DEFINE_STRING(_uncached_lookupAll);
  1395. DEFINE_STRING(_uncached_subscriptions);
  1396. DEFINE_STRING(_registry);
  1397. DEFINE_STRING(_generation);
  1398. DEFINE_STRING(ro);
  1399. DEFINE_STRING(changed);
  1400. #undef DEFINE_STRING
  1401. adapter_hooks = PyList_New(0);
  1402. if (adapter_hooks == NULL)
  1403. return NULL;
  1404. /* Initialize types: */
  1405. SpecType.tp_new = PyBaseObject_Type.tp_new;
  1406. if (PyType_Ready(&SpecType) < 0)
  1407. return NULL;
  1408. OSDType.tp_new = PyBaseObject_Type.tp_new;
  1409. if (PyType_Ready(&OSDType) < 0)
  1410. return NULL;
  1411. CPBType.tp_new = PyBaseObject_Type.tp_new;
  1412. if (PyType_Ready(&CPBType) < 0)
  1413. return NULL;
  1414. InterfaceBase.tp_new = PyBaseObject_Type.tp_new;
  1415. if (PyType_Ready(&InterfaceBase) < 0)
  1416. return NULL;
  1417. LookupBase.tp_new = PyBaseObject_Type.tp_new;
  1418. if (PyType_Ready(&LookupBase) < 0)
  1419. return NULL;
  1420. VerifyingBase.tp_new = PyBaseObject_Type.tp_new;
  1421. if (PyType_Ready(&VerifyingBase) < 0)
  1422. return NULL;
  1423. #if PY_MAJOR_VERSION < 3
  1424. /* Create the module and add the functions */
  1425. m = Py_InitModule3("_zope_interface_coptimizations", m_methods,
  1426. "C optimizations for zope.interface\n\n");
  1427. #else
  1428. m = PyModule_Create(&_zic_module);
  1429. #endif
  1430. if (m == NULL)
  1431. return NULL;
  1432. /* Add types: */
  1433. if (PyModule_AddObject(m, "SpecificationBase", OBJECT(&SpecType)) < 0)
  1434. return NULL;
  1435. if (PyModule_AddObject(m, "ObjectSpecificationDescriptor",
  1436. (PyObject *)&OSDType) < 0)
  1437. return NULL;
  1438. if (PyModule_AddObject(m, "ClassProvidesBase", OBJECT(&CPBType)) < 0)
  1439. return NULL;
  1440. if (PyModule_AddObject(m, "InterfaceBase", OBJECT(&InterfaceBase)) < 0)
  1441. return NULL;
  1442. if (PyModule_AddObject(m, "LookupBase", OBJECT(&LookupBase)) < 0)
  1443. return NULL;
  1444. if (PyModule_AddObject(m, "VerifyingBase", OBJECT(&VerifyingBase)) < 0)
  1445. return NULL;
  1446. if (PyModule_AddObject(m, "adapter_hooks", adapter_hooks) < 0)
  1447. return NULL;
  1448. return m;
  1449. }
  1450. PyMODINIT_FUNC
  1451. #if PY_MAJOR_VERSION < 3
  1452. init_zope_interface_coptimizations(void)
  1453. {
  1454. init();
  1455. }
  1456. #else
  1457. PyInit__zope_interface_coptimizations(void)
  1458. {
  1459. return init();
  1460. }
  1461. #endif