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.

_zope_interface_coptimizations.c 45KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726
  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. PyObject_GC_UnTrack((PyObject *)self);
  650. lookup_clear(self);
  651. Py_TYPE(self)->tp_free((PyObject*)self);
  652. }
  653. /*
  654. def changed(self, ignored=None):
  655. self._cache.clear()
  656. self._mcache.clear()
  657. self._scache.clear()
  658. */
  659. static PyObject *
  660. lookup_changed(lookup *self, PyObject *ignored)
  661. {
  662. lookup_clear(self);
  663. Py_INCREF(Py_None);
  664. return Py_None;
  665. }
  666. #define ASSURE_DICT(N) if (N == NULL) { N = PyDict_New(); \
  667. if (N == NULL) return NULL; \
  668. }
  669. /*
  670. def _getcache(self, provided, name):
  671. cache = self._cache.get(provided)
  672. if cache is None:
  673. cache = {}
  674. self._cache[provided] = cache
  675. if name:
  676. c = cache.get(name)
  677. if c is None:
  678. c = {}
  679. cache[name] = c
  680. cache = c
  681. return cache
  682. */
  683. static PyObject *
  684. _subcache(PyObject *cache, PyObject *key)
  685. {
  686. PyObject *subcache;
  687. subcache = PyDict_GetItem(cache, key);
  688. if (subcache == NULL)
  689. {
  690. int status;
  691. subcache = PyDict_New();
  692. if (subcache == NULL)
  693. return NULL;
  694. status = PyDict_SetItem(cache, key, subcache);
  695. Py_DECREF(subcache);
  696. if (status < 0)
  697. return NULL;
  698. }
  699. return subcache;
  700. }
  701. static PyObject *
  702. _getcache(lookup *self, PyObject *provided, PyObject *name)
  703. {
  704. PyObject *cache;
  705. ASSURE_DICT(self->_cache);
  706. cache = _subcache(self->_cache, provided);
  707. if (cache == NULL)
  708. return NULL;
  709. if (name != NULL && PyObject_IsTrue(name))
  710. cache = _subcache(cache, name);
  711. return cache;
  712. }
  713. /*
  714. def lookup(self, required, provided, name=u'', default=None):
  715. cache = self._getcache(provided, name)
  716. if len(required) == 1:
  717. result = cache.get(required[0], _not_in_mapping)
  718. else:
  719. result = cache.get(tuple(required), _not_in_mapping)
  720. if result is _not_in_mapping:
  721. result = self._uncached_lookup(required, provided, name)
  722. if len(required) == 1:
  723. cache[required[0]] = result
  724. else:
  725. cache[tuple(required)] = result
  726. if result is None:
  727. return default
  728. return result
  729. */
  730. static PyObject *
  731. tuplefy(PyObject *v)
  732. {
  733. if (! PyTuple_Check(v))
  734. {
  735. v = PyObject_CallFunctionObjArgs(OBJECT(&PyTuple_Type), v, NULL);
  736. if (v == NULL)
  737. return NULL;
  738. }
  739. else
  740. Py_INCREF(v);
  741. return v;
  742. }
  743. static PyObject *
  744. _lookup(lookup *self,
  745. PyObject *required, PyObject *provided, PyObject *name,
  746. PyObject *default_)
  747. {
  748. PyObject *result, *key, *cache;
  749. #ifdef PY3K
  750. if ( name && !PyUnicode_Check(name) )
  751. #else
  752. if ( name && !PyString_Check(name) && !PyUnicode_Check(name) )
  753. #endif
  754. {
  755. PyErr_SetString(PyExc_ValueError,
  756. "name is not a string or unicode");
  757. return NULL;
  758. }
  759. cache = _getcache(self, provided, name);
  760. if (cache == NULL)
  761. return NULL;
  762. required = tuplefy(required);
  763. if (required == NULL)
  764. return NULL;
  765. if (PyTuple_GET_SIZE(required) == 1)
  766. key = PyTuple_GET_ITEM(required, 0);
  767. else
  768. key = required;
  769. result = PyDict_GetItem(cache, key);
  770. if (result == NULL)
  771. {
  772. int status;
  773. result = PyObject_CallMethodObjArgs(OBJECT(self), str_uncached_lookup,
  774. required, provided, name, NULL);
  775. if (result == NULL)
  776. {
  777. Py_DECREF(required);
  778. return NULL;
  779. }
  780. status = PyDict_SetItem(cache, key, result);
  781. Py_DECREF(required);
  782. if (status < 0)
  783. {
  784. Py_DECREF(result);
  785. return NULL;
  786. }
  787. }
  788. else
  789. {
  790. Py_INCREF(result);
  791. Py_DECREF(required);
  792. }
  793. if (result == Py_None && default_ != NULL)
  794. {
  795. Py_DECREF(Py_None);
  796. Py_INCREF(default_);
  797. return default_;
  798. }
  799. return result;
  800. }
  801. static PyObject *
  802. lookup_lookup(lookup *self, PyObject *args, PyObject *kwds)
  803. {
  804. static char *kwlist[] = {"required", "provided", "name", "default", NULL};
  805. PyObject *required, *provided, *name=NULL, *default_=NULL;
  806. if (! PyArg_ParseTupleAndKeywords(args, kwds, "OO|OO", kwlist,
  807. &required, &provided, &name, &default_))
  808. return NULL;
  809. return _lookup(self, required, provided, name, default_);
  810. }
  811. /*
  812. def lookup1(self, required, provided, name=u'', default=None):
  813. cache = self._getcache(provided, name)
  814. result = cache.get(required, _not_in_mapping)
  815. if result is _not_in_mapping:
  816. return self.lookup((required, ), provided, name, default)
  817. if result is None:
  818. return default
  819. return result
  820. */
  821. static PyObject *
  822. _lookup1(lookup *self,
  823. PyObject *required, PyObject *provided, PyObject *name,
  824. PyObject *default_)
  825. {
  826. PyObject *result, *cache;
  827. #ifdef PY3K
  828. if ( name && !PyUnicode_Check(name) )
  829. #else
  830. if ( name && !PyString_Check(name) && !PyUnicode_Check(name) )
  831. #endif
  832. {
  833. PyErr_SetString(PyExc_ValueError,
  834. "name is not a string or unicode");
  835. return NULL;
  836. }
  837. cache = _getcache(self, provided, name);
  838. if (cache == NULL)
  839. return NULL;
  840. result = PyDict_GetItem(cache, required);
  841. if (result == NULL)
  842. {
  843. PyObject *tup;
  844. tup = PyTuple_New(1);
  845. if (tup == NULL)
  846. return NULL;
  847. Py_INCREF(required);
  848. PyTuple_SET_ITEM(tup, 0, required);
  849. result = _lookup(self, tup, provided, name, default_);
  850. Py_DECREF(tup);
  851. }
  852. else
  853. {
  854. if (result == Py_None && default_ != NULL)
  855. {
  856. result = default_;
  857. }
  858. Py_INCREF(result);
  859. }
  860. return result;
  861. }
  862. static PyObject *
  863. lookup_lookup1(lookup *self, PyObject *args, PyObject *kwds)
  864. {
  865. static char *kwlist[] = {"required", "provided", "name", "default", NULL};
  866. PyObject *required, *provided, *name=NULL, *default_=NULL;
  867. if (! PyArg_ParseTupleAndKeywords(args, kwds, "OO|OO", kwlist,
  868. &required, &provided, &name, &default_))
  869. return NULL;
  870. return _lookup1(self, required, provided, name, default_);
  871. }
  872. /*
  873. def adapter_hook(self, provided, object, name=u'', default=None):
  874. required = providedBy(object)
  875. cache = self._getcache(provided, name)
  876. factory = cache.get(required, _not_in_mapping)
  877. if factory is _not_in_mapping:
  878. factory = self.lookup((required, ), provided, name)
  879. if factory is not None:
  880. result = factory(object)
  881. if result is not None:
  882. return result
  883. return default
  884. */
  885. static PyObject *
  886. _adapter_hook(lookup *self,
  887. PyObject *provided, PyObject *object, PyObject *name,
  888. PyObject *default_)
  889. {
  890. PyObject *required, *factory, *result;
  891. #ifdef PY3K
  892. if ( name && !PyUnicode_Check(name) )
  893. #else
  894. if ( name && !PyString_Check(name) && !PyUnicode_Check(name) )
  895. #endif
  896. {
  897. PyErr_SetString(PyExc_ValueError,
  898. "name is not a string or unicode");
  899. return NULL;
  900. }
  901. required = providedBy(NULL, object);
  902. if (required == NULL)
  903. return NULL;
  904. factory = _lookup1(self, required, provided, name, Py_None);
  905. Py_DECREF(required);
  906. if (factory == NULL)
  907. return NULL;
  908. if (factory != Py_None)
  909. {
  910. result = PyObject_CallFunctionObjArgs(factory, object, NULL);
  911. Py_DECREF(factory);
  912. if (result == NULL || result != Py_None)
  913. return result;
  914. }
  915. else
  916. result = factory; /* None */
  917. if (default_ == NULL || default_ == result) /* No default specified, */
  918. return result; /* Return None. result is owned None */
  919. Py_DECREF(result);
  920. Py_INCREF(default_);
  921. return default_;
  922. }
  923. static PyObject *
  924. lookup_adapter_hook(lookup *self, PyObject *args, PyObject *kwds)
  925. {
  926. static char *kwlist[] = {"provided", "object", "name", "default", NULL};
  927. PyObject *object, *provided, *name=NULL, *default_=NULL;
  928. if (! PyArg_ParseTupleAndKeywords(args, kwds, "OO|OO", kwlist,
  929. &provided, &object, &name, &default_))
  930. return NULL;
  931. return _adapter_hook(self, provided, object, name, default_);
  932. }
  933. static PyObject *
  934. lookup_queryAdapter(lookup *self, PyObject *args, PyObject *kwds)
  935. {
  936. static char *kwlist[] = {"object", "provided", "name", "default", NULL};
  937. PyObject *object, *provided, *name=NULL, *default_=NULL;
  938. if (! PyArg_ParseTupleAndKeywords(args, kwds, "OO|OO", kwlist,
  939. &object, &provided, &name, &default_))
  940. return NULL;
  941. return _adapter_hook(self, provided, object, name, default_);
  942. }
  943. /*
  944. def lookupAll(self, required, provided):
  945. cache = self._mcache.get(provided)
  946. if cache is None:
  947. cache = {}
  948. self._mcache[provided] = cache
  949. required = tuple(required)
  950. result = cache.get(required, _not_in_mapping)
  951. if result is _not_in_mapping:
  952. result = self._uncached_lookupAll(required, provided)
  953. cache[required] = result
  954. return result
  955. */
  956. static PyObject *
  957. _lookupAll(lookup *self, PyObject *required, PyObject *provided)
  958. {
  959. PyObject *cache, *result;
  960. ASSURE_DICT(self->_mcache);
  961. cache = _subcache(self->_mcache, provided);
  962. if (cache == NULL)
  963. return NULL;
  964. required = tuplefy(required);
  965. if (required == NULL)
  966. return NULL;
  967. result = PyDict_GetItem(cache, required);
  968. if (result == NULL)
  969. {
  970. int status;
  971. result = PyObject_CallMethodObjArgs(OBJECT(self), str_uncached_lookupAll,
  972. required, provided, NULL);
  973. if (result == NULL)
  974. {
  975. Py_DECREF(required);
  976. return NULL;
  977. }
  978. status = PyDict_SetItem(cache, required, result);
  979. Py_DECREF(required);
  980. if (status < 0)
  981. {
  982. Py_DECREF(result);
  983. return NULL;
  984. }
  985. }
  986. else
  987. {
  988. Py_INCREF(result);
  989. Py_DECREF(required);
  990. }
  991. return result;
  992. }
  993. static PyObject *
  994. lookup_lookupAll(lookup *self, PyObject *args, PyObject *kwds)
  995. {
  996. static char *kwlist[] = {"required", "provided", NULL};
  997. PyObject *required, *provided;
  998. if (! PyArg_ParseTupleAndKeywords(args, kwds, "OO", kwlist,
  999. &required, &provided))
  1000. return NULL;
  1001. return _lookupAll(self, required, provided);
  1002. }
  1003. /*
  1004. def subscriptions(self, required, provided):
  1005. cache = self._scache.get(provided)
  1006. if cache is None:
  1007. cache = {}
  1008. self._scache[provided] = cache
  1009. required = tuple(required)
  1010. result = cache.get(required, _not_in_mapping)
  1011. if result is _not_in_mapping:
  1012. result = self._uncached_subscriptions(required, provided)
  1013. cache[required] = result
  1014. return result
  1015. */
  1016. static PyObject *
  1017. _subscriptions(lookup *self, PyObject *required, PyObject *provided)
  1018. {
  1019. PyObject *cache, *result;
  1020. ASSURE_DICT(self->_scache);
  1021. cache = _subcache(self->_scache, provided);
  1022. if (cache == NULL)
  1023. return NULL;
  1024. required = tuplefy(required);
  1025. if (required == NULL)
  1026. return NULL;
  1027. result = PyDict_GetItem(cache, required);
  1028. if (result == NULL)
  1029. {
  1030. int status;
  1031. result = PyObject_CallMethodObjArgs(
  1032. OBJECT(self), str_uncached_subscriptions,
  1033. required, provided, NULL);
  1034. if (result == NULL)
  1035. {
  1036. Py_DECREF(required);
  1037. return NULL;
  1038. }
  1039. status = PyDict_SetItem(cache, required, result);
  1040. Py_DECREF(required);
  1041. if (status < 0)
  1042. {
  1043. Py_DECREF(result);
  1044. return NULL;
  1045. }
  1046. }
  1047. else
  1048. {
  1049. Py_INCREF(result);
  1050. Py_DECREF(required);
  1051. }
  1052. return result;
  1053. }
  1054. static PyObject *
  1055. lookup_subscriptions(lookup *self, PyObject *args, PyObject *kwds)
  1056. {
  1057. static char *kwlist[] = {"required", "provided", NULL};
  1058. PyObject *required, *provided;
  1059. if (! PyArg_ParseTupleAndKeywords(args, kwds, "OO", kwlist,
  1060. &required, &provided))
  1061. return NULL;
  1062. return _subscriptions(self, required, provided);
  1063. }
  1064. static struct PyMethodDef lookup_methods[] = {
  1065. {"changed", (PyCFunction)lookup_changed, METH_O, ""},
  1066. {"lookup", (PyCFunction)lookup_lookup, METH_KEYWORDS | METH_VARARGS, ""},
  1067. {"lookup1", (PyCFunction)lookup_lookup1, METH_KEYWORDS | METH_VARARGS, ""},
  1068. {"queryAdapter", (PyCFunction)lookup_queryAdapter, METH_KEYWORDS | METH_VARARGS, ""},
  1069. {"adapter_hook", (PyCFunction)lookup_adapter_hook, METH_KEYWORDS | METH_VARARGS, ""},
  1070. {"lookupAll", (PyCFunction)lookup_lookupAll, METH_KEYWORDS | METH_VARARGS, ""},
  1071. {"subscriptions", (PyCFunction)lookup_subscriptions, METH_KEYWORDS | METH_VARARGS, ""},
  1072. {NULL, NULL} /* sentinel */
  1073. };
  1074. static PyTypeObject LookupBase = {
  1075. PyVarObject_HEAD_INIT(NULL, 0)
  1076. /* tp_name */ "_zope_interface_coptimizations."
  1077. "LookupBase",
  1078. /* tp_basicsize */ sizeof(lookup),
  1079. /* tp_itemsize */ 0,
  1080. /* tp_dealloc */ (destructor)&lookup_dealloc,
  1081. /* tp_print */ (printfunc)0,
  1082. /* tp_getattr */ (getattrfunc)0,
  1083. /* tp_setattr */ (setattrfunc)0,
  1084. /* tp_compare */ 0,
  1085. /* tp_repr */ (reprfunc)0,
  1086. /* tp_as_number */ 0,
  1087. /* tp_as_sequence */ 0,
  1088. /* tp_as_mapping */ 0,
  1089. /* tp_hash */ (hashfunc)0,
  1090. /* tp_call */ (ternaryfunc)0,
  1091. /* tp_str */ (reprfunc)0,
  1092. /* tp_getattro */ (getattrofunc)0,
  1093. /* tp_setattro */ (setattrofunc)0,
  1094. /* tp_as_buffer */ 0,
  1095. /* tp_flags */ Py_TPFLAGS_DEFAULT
  1096. | Py_TPFLAGS_BASETYPE
  1097. | Py_TPFLAGS_HAVE_GC,
  1098. /* tp_doc */ "",
  1099. /* tp_traverse */ (traverseproc)lookup_traverse,
  1100. /* tp_clear */ (inquiry)lookup_clear,
  1101. /* tp_richcompare */ (richcmpfunc)0,
  1102. /* tp_weaklistoffset */ (long)0,
  1103. /* tp_iter */ (getiterfunc)0,
  1104. /* tp_iternext */ (iternextfunc)0,
  1105. /* tp_methods */ lookup_methods,
  1106. };
  1107. static int
  1108. verifying_traverse(verify *self, visitproc visit, void *arg)
  1109. {
  1110. int vret;
  1111. vret = lookup_traverse((lookup *)self, visit, arg);
  1112. if (vret != 0)
  1113. return vret;
  1114. if (self->_verify_ro) {
  1115. vret = visit(self->_verify_ro, arg);
  1116. if (vret != 0)
  1117. return vret;
  1118. }
  1119. if (self->_verify_generations) {
  1120. vret = visit(self->_verify_generations, arg);
  1121. if (vret != 0)
  1122. return vret;
  1123. }
  1124. return 0;
  1125. }
  1126. static int
  1127. verifying_clear(verify *self)
  1128. {
  1129. lookup_clear((lookup *)self);
  1130. Py_CLEAR(self->_verify_generations);
  1131. Py_CLEAR(self->_verify_ro);
  1132. return 0;
  1133. }
  1134. static void
  1135. verifying_dealloc(verify *self)
  1136. {
  1137. PyObject_GC_UnTrack((PyObject *)self);
  1138. verifying_clear(self);
  1139. Py_TYPE(self)->tp_free((PyObject*)self);
  1140. }
  1141. /*
  1142. def changed(self, originally_changed):
  1143. super(VerifyingBasePy, self).changed(originally_changed)
  1144. self._verify_ro = self._registry.ro[1:]
  1145. self._verify_generations = [r._generation for r in self._verify_ro]
  1146. */
  1147. static PyObject *
  1148. _generations_tuple(PyObject *ro)
  1149. {
  1150. int i, l;
  1151. PyObject *generations;
  1152. l = PyTuple_GET_SIZE(ro);
  1153. generations = PyTuple_New(l);
  1154. for (i=0; i < l; i++)
  1155. {
  1156. PyObject *generation;
  1157. generation = PyObject_GetAttr(PyTuple_GET_ITEM(ro, i), str_generation);
  1158. if (generation == NULL)
  1159. {
  1160. Py_DECREF(generations);
  1161. return NULL;
  1162. }
  1163. PyTuple_SET_ITEM(generations, i, generation);
  1164. }
  1165. return generations;
  1166. }
  1167. static PyObject *
  1168. verifying_changed(verify *self, PyObject *ignored)
  1169. {
  1170. PyObject *t, *ro;
  1171. verifying_clear(self);
  1172. t = PyObject_GetAttr(OBJECT(self), str_registry);
  1173. if (t == NULL)
  1174. return NULL;
  1175. ro = PyObject_GetAttr(t, strro);
  1176. Py_DECREF(t);
  1177. if (ro == NULL)
  1178. return NULL;
  1179. t = PyObject_CallFunctionObjArgs(OBJECT(&PyTuple_Type), ro, NULL);
  1180. Py_DECREF(ro);
  1181. if (t == NULL)
  1182. return NULL;
  1183. ro = PyTuple_GetSlice(t, 1, PyTuple_GET_SIZE(t));
  1184. Py_DECREF(t);
  1185. if (ro == NULL)
  1186. return NULL;
  1187. self->_verify_generations = _generations_tuple(ro);
  1188. if (self->_verify_generations == NULL)
  1189. {
  1190. Py_DECREF(ro);
  1191. return NULL;
  1192. }
  1193. self->_verify_ro = ro;
  1194. Py_INCREF(Py_None);
  1195. return Py_None;
  1196. }
  1197. /*
  1198. def _verify(self):
  1199. if ([r._generation for r in self._verify_ro]
  1200. != self._verify_generations):
  1201. self.changed(None)
  1202. */
  1203. static int
  1204. _verify(verify *self)
  1205. {
  1206. PyObject *changed_result;
  1207. if (self->_verify_ro != NULL && self->_verify_generations != NULL)
  1208. {
  1209. PyObject *generations;
  1210. int changed;
  1211. generations = _generations_tuple(self->_verify_ro);
  1212. if (generations == NULL)
  1213. return -1;
  1214. changed = PyObject_RichCompareBool(self->_verify_generations,
  1215. generations, Py_NE);
  1216. Py_DECREF(generations);
  1217. if (changed == -1)
  1218. return -1;
  1219. if (changed == 0)
  1220. return 0;
  1221. }
  1222. changed_result = PyObject_CallMethodObjArgs(OBJECT(self), strchanged,
  1223. Py_None, NULL);
  1224. if (changed_result == NULL)
  1225. return -1;
  1226. Py_DECREF(changed_result);
  1227. return 0;
  1228. }
  1229. static PyObject *
  1230. verifying_lookup(verify *self, PyObject *args, PyObject *kwds)
  1231. {
  1232. static char *kwlist[] = {"required", "provided", "name", "default", NULL};
  1233. PyObject *required, *provided, *name=NULL, *default_=NULL;
  1234. if (! PyArg_ParseTupleAndKeywords(args, kwds, "OO|OO", kwlist,
  1235. &required, &provided, &name, &default_))
  1236. return NULL;
  1237. if (_verify(self) < 0)
  1238. return NULL;
  1239. return _lookup((lookup *)self, required, provided, name, default_);
  1240. }
  1241. static PyObject *
  1242. verifying_lookup1(verify *self, PyObject *args, PyObject *kwds)
  1243. {
  1244. static char *kwlist[] = {"required", "provided", "name", "default", NULL};
  1245. PyObject *required, *provided, *name=NULL, *default_=NULL;
  1246. if (! PyArg_ParseTupleAndKeywords(args, kwds, "OO|OO", kwlist,
  1247. &required, &provided, &name, &default_))
  1248. return NULL;
  1249. if (_verify(self) < 0)
  1250. return NULL;
  1251. return _lookup1((lookup *)self, required, provided, name, default_);
  1252. }
  1253. static PyObject *
  1254. verifying_adapter_hook(verify *self, PyObject *args, PyObject *kwds)
  1255. {
  1256. static char *kwlist[] = {"provided", "object", "name", "default", NULL};
  1257. PyObject *object, *provided, *name=NULL, *default_=NULL;
  1258. if (! PyArg_ParseTupleAndKeywords(args, kwds, "OO|OO", kwlist,
  1259. &provided, &object, &name, &default_))
  1260. return NULL;
  1261. if (_verify(self) < 0)
  1262. return NULL;
  1263. return _adapter_hook((lookup *)self, provided, object, name, default_);
  1264. }
  1265. static PyObject *
  1266. verifying_queryAdapter(verify *self, PyObject *args, PyObject *kwds)
  1267. {
  1268. static char *kwlist[] = {"object", "provided", "name", "default", NULL};
  1269. PyObject *object, *provided, *name=NULL, *default_=NULL;
  1270. if (! PyArg_ParseTupleAndKeywords(args, kwds, "OO|OO", kwlist,
  1271. &object, &provided, &name, &default_))
  1272. return NULL;
  1273. if (_verify(self) < 0)
  1274. return NULL;
  1275. return _adapter_hook((lookup *)self, provided, object, name, default_);
  1276. }
  1277. static PyObject *
  1278. verifying_lookupAll(verify *self, PyObject *args, PyObject *kwds)
  1279. {
  1280. static char *kwlist[] = {"required", "provided", NULL};
  1281. PyObject *required, *provided;
  1282. if (! PyArg_ParseTupleAndKeywords(args, kwds, "OO", kwlist,
  1283. &required, &provided))
  1284. return NULL;
  1285. if (_verify(self) < 0)
  1286. return NULL;
  1287. return _lookupAll((lookup *)self, required, provided);
  1288. }
  1289. static PyObject *
  1290. verifying_subscriptions(verify *self, PyObject *args, PyObject *kwds)
  1291. {
  1292. static char *kwlist[] = {"required", "provided", NULL};
  1293. PyObject *required, *provided;
  1294. if (! PyArg_ParseTupleAndKeywords(args, kwds, "OO", kwlist,
  1295. &required, &provided))
  1296. return NULL;
  1297. if (_verify(self) < 0)
  1298. return NULL;
  1299. return _subscriptions((lookup *)self, required, provided);
  1300. }
  1301. static struct PyMethodDef verifying_methods[] = {
  1302. {"changed", (PyCFunction)verifying_changed, METH_O, ""},
  1303. {"lookup", (PyCFunction)verifying_lookup, METH_KEYWORDS | METH_VARARGS, ""},
  1304. {"lookup1", (PyCFunction)verifying_lookup1, METH_KEYWORDS | METH_VARARGS, ""},
  1305. {"queryAdapter", (PyCFunction)verifying_queryAdapter, METH_KEYWORDS | METH_VARARGS, ""},
  1306. {"adapter_hook", (PyCFunction)verifying_adapter_hook, METH_KEYWORDS | METH_VARARGS, ""},
  1307. {"lookupAll", (PyCFunction)verifying_lookupAll, METH_KEYWORDS | METH_VARARGS, ""},
  1308. {"subscriptions", (PyCFunction)verifying_subscriptions, METH_KEYWORDS | METH_VARARGS, ""},
  1309. {NULL, NULL} /* sentinel */
  1310. };
  1311. static PyTypeObject VerifyingBase = {
  1312. PyVarObject_HEAD_INIT(NULL, 0)
  1313. /* tp_name */ "_zope_interface_coptimizations."
  1314. "VerifyingBase",
  1315. /* tp_basicsize */ sizeof(verify),
  1316. /* tp_itemsize */ 0,
  1317. /* tp_dealloc */ (destructor)&verifying_dealloc,
  1318. /* tp_print */ (printfunc)0,
  1319. /* tp_getattr */ (getattrfunc)0,
  1320. /* tp_setattr */ (setattrfunc)0,
  1321. /* tp_compare */ 0,
  1322. /* tp_repr */ (reprfunc)0,
  1323. /* tp_as_number */ 0,
  1324. /* tp_as_sequence */ 0,
  1325. /* tp_as_mapping */ 0,
  1326. /* tp_hash */ (hashfunc)0,
  1327. /* tp_call */ (ternaryfunc)0,
  1328. /* tp_str */ (reprfunc)0,
  1329. /* tp_getattro */ (getattrofunc)0,
  1330. /* tp_setattro */ (setattrofunc)0,
  1331. /* tp_as_buffer */ 0,
  1332. /* tp_flags */ Py_TPFLAGS_DEFAULT
  1333. | Py_TPFLAGS_BASETYPE
  1334. | Py_TPFLAGS_HAVE_GC,
  1335. /* tp_doc */ "",
  1336. /* tp_traverse */ (traverseproc)verifying_traverse,
  1337. /* tp_clear */ (inquiry)verifying_clear,
  1338. /* tp_richcompare */ (richcmpfunc)0,
  1339. /* tp_weaklistoffset */ (long)0,
  1340. /* tp_iter */ (getiterfunc)0,
  1341. /* tp_iternext */ (iternextfunc)0,
  1342. /* tp_methods */ verifying_methods,
  1343. /* tp_members */ 0,
  1344. /* tp_getset */ 0,
  1345. /* tp_base */ &LookupBase,
  1346. };
  1347. /* ========================== End: Lookup Bases ======================= */
  1348. /* ==================================================================== */
  1349. static struct PyMethodDef m_methods[] = {
  1350. {"implementedBy", (PyCFunction)implementedBy, METH_O,
  1351. "Interfaces implemented by a class or factory.\n"
  1352. "Raises TypeError if argument is neither a class nor a callable."},
  1353. {"getObjectSpecification", (PyCFunction)getObjectSpecification, METH_O,
  1354. "Get an object's interfaces (internal api)"},
  1355. {"providedBy", (PyCFunction)providedBy, METH_O,
  1356. "Get an object's interfaces"},
  1357. {NULL, (PyCFunction)NULL, 0, NULL} /* sentinel */
  1358. };
  1359. #if PY_MAJOR_VERSION >= 3
  1360. static char module_doc[] = "C optimizations for zope.interface\n\n";
  1361. static struct PyModuleDef _zic_module = {
  1362. PyModuleDef_HEAD_INIT,
  1363. "_zope_interface_coptimizations",
  1364. module_doc,
  1365. -1,
  1366. m_methods,
  1367. NULL,
  1368. NULL,
  1369. NULL,
  1370. NULL
  1371. };
  1372. #endif
  1373. static PyObject *
  1374. init(void)
  1375. {
  1376. PyObject *m;
  1377. #if PY_MAJOR_VERSION < 3
  1378. #define DEFINE_STRING(S) \
  1379. if(! (str ## S = PyString_FromString(# S))) return NULL
  1380. #else
  1381. #define DEFINE_STRING(S) \
  1382. if(! (str ## S = PyUnicode_FromString(# S))) return NULL
  1383. #endif
  1384. DEFINE_STRING(__dict__);
  1385. DEFINE_STRING(__implemented__);
  1386. DEFINE_STRING(__provides__);
  1387. DEFINE_STRING(__class__);
  1388. DEFINE_STRING(__providedBy__);
  1389. DEFINE_STRING(extends);
  1390. DEFINE_STRING(_implied);
  1391. DEFINE_STRING(_implements);
  1392. DEFINE_STRING(_cls);
  1393. DEFINE_STRING(__conform__);
  1394. DEFINE_STRING(_call_conform);
  1395. DEFINE_STRING(_uncached_lookup);
  1396. DEFINE_STRING(_uncached_lookupAll);
  1397. DEFINE_STRING(_uncached_subscriptions);
  1398. DEFINE_STRING(_registry);
  1399. DEFINE_STRING(_generation);
  1400. DEFINE_STRING(ro);
  1401. DEFINE_STRING(changed);
  1402. #undef DEFINE_STRING
  1403. adapter_hooks = PyList_New(0);
  1404. if (adapter_hooks == NULL)
  1405. return NULL;
  1406. /* Initialize types: */
  1407. SpecType.tp_new = PyBaseObject_Type.tp_new;
  1408. if (PyType_Ready(&SpecType) < 0)
  1409. return NULL;
  1410. OSDType.tp_new = PyBaseObject_Type.tp_new;
  1411. if (PyType_Ready(&OSDType) < 0)
  1412. return NULL;
  1413. CPBType.tp_new = PyBaseObject_Type.tp_new;
  1414. if (PyType_Ready(&CPBType) < 0)
  1415. return NULL;
  1416. InterfaceBase.tp_new = PyBaseObject_Type.tp_new;
  1417. if (PyType_Ready(&InterfaceBase) < 0)
  1418. return NULL;
  1419. LookupBase.tp_new = PyBaseObject_Type.tp_new;
  1420. if (PyType_Ready(&LookupBase) < 0)
  1421. return NULL;
  1422. VerifyingBase.tp_new = PyBaseObject_Type.tp_new;
  1423. if (PyType_Ready(&VerifyingBase) < 0)
  1424. return NULL;
  1425. #if PY_MAJOR_VERSION < 3
  1426. /* Create the module and add the functions */
  1427. m = Py_InitModule3("_zope_interface_coptimizations", m_methods,
  1428. "C optimizations for zope.interface\n\n");
  1429. #else
  1430. m = PyModule_Create(&_zic_module);
  1431. #endif
  1432. if (m == NULL)
  1433. return NULL;
  1434. /* Add types: */
  1435. if (PyModule_AddObject(m, "SpecificationBase", OBJECT(&SpecType)) < 0)
  1436. return NULL;
  1437. if (PyModule_AddObject(m, "ObjectSpecificationDescriptor",
  1438. (PyObject *)&OSDType) < 0)
  1439. return NULL;
  1440. if (PyModule_AddObject(m, "ClassProvidesBase", OBJECT(&CPBType)) < 0)
  1441. return NULL;
  1442. if (PyModule_AddObject(m, "InterfaceBase", OBJECT(&InterfaceBase)) < 0)
  1443. return NULL;
  1444. if (PyModule_AddObject(m, "LookupBase", OBJECT(&LookupBase)) < 0)
  1445. return NULL;
  1446. if (PyModule_AddObject(m, "VerifyingBase", OBJECT(&VerifyingBase)) < 0)
  1447. return NULL;
  1448. if (PyModule_AddObject(m, "adapter_hooks", adapter_hooks) < 0)
  1449. return NULL;
  1450. return m;
  1451. }
  1452. PyMODINIT_FUNC
  1453. #if PY_MAJOR_VERSION < 3
  1454. init_zope_interface_coptimizations(void)
  1455. {
  1456. init();
  1457. }
  1458. #else
  1459. PyInit__zope_interface_coptimizations(void)
  1460. {
  1461. return init();
  1462. }
  1463. #endif