_abc.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967
  1. /* ABCMeta implementation */
  2. #ifndef Py_BUILD_CORE_BUILTIN
  3. # define Py_BUILD_CORE_MODULE 1
  4. #endif
  5. #include "Python.h"
  6. #include "pycore_moduleobject.h" // _PyModule_GetState()
  7. #include "pycore_object.h" // _PyType_GetSubclasses()
  8. #include "pycore_runtime.h" // _Py_ID()
  9. #include "pycore_typeobject.h" // _PyType_GetMRO()
  10. #include "clinic/_abc.c.h"
  11. /*[clinic input]
  12. module _abc
  13. [clinic start generated code]*/
  14. /*[clinic end generated code: output=da39a3ee5e6b4b0d input=964f5328e1aefcda]*/
  15. PyDoc_STRVAR(_abc__doc__,
  16. "Module contains faster C implementation of abc.ABCMeta");
  17. typedef struct {
  18. PyTypeObject *_abc_data_type;
  19. unsigned long long abc_invalidation_counter;
  20. } _abcmodule_state;
  21. static inline _abcmodule_state*
  22. get_abc_state(PyObject *module)
  23. {
  24. void *state = _PyModule_GetState(module);
  25. assert(state != NULL);
  26. return (_abcmodule_state *)state;
  27. }
  28. /* This object stores internal state for ABCs.
  29. Note that we can use normal sets for caches,
  30. since they are never iterated over. */
  31. typedef struct {
  32. PyObject_HEAD
  33. PyObject *_abc_registry;
  34. PyObject *_abc_cache; /* Normal set of weak references. */
  35. PyObject *_abc_negative_cache; /* Normal set of weak references. */
  36. unsigned long long _abc_negative_cache_version;
  37. } _abc_data;
  38. static int
  39. abc_data_traverse(_abc_data *self, visitproc visit, void *arg)
  40. {
  41. Py_VISIT(Py_TYPE(self));
  42. Py_VISIT(self->_abc_registry);
  43. Py_VISIT(self->_abc_cache);
  44. Py_VISIT(self->_abc_negative_cache);
  45. return 0;
  46. }
  47. static int
  48. abc_data_clear(_abc_data *self)
  49. {
  50. Py_CLEAR(self->_abc_registry);
  51. Py_CLEAR(self->_abc_cache);
  52. Py_CLEAR(self->_abc_negative_cache);
  53. return 0;
  54. }
  55. static void
  56. abc_data_dealloc(_abc_data *self)
  57. {
  58. PyObject_GC_UnTrack(self);
  59. PyTypeObject *tp = Py_TYPE(self);
  60. (void)abc_data_clear(self);
  61. tp->tp_free(self);
  62. Py_DECREF(tp);
  63. }
  64. static PyObject *
  65. abc_data_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
  66. {
  67. _abc_data *self = (_abc_data *) type->tp_alloc(type, 0);
  68. _abcmodule_state *state = NULL;
  69. if (self == NULL) {
  70. return NULL;
  71. }
  72. state = _PyType_GetModuleState(type);
  73. if (state == NULL) {
  74. Py_DECREF(self);
  75. return NULL;
  76. }
  77. self->_abc_registry = NULL;
  78. self->_abc_cache = NULL;
  79. self->_abc_negative_cache = NULL;
  80. self->_abc_negative_cache_version = state->abc_invalidation_counter;
  81. return (PyObject *) self;
  82. }
  83. PyDoc_STRVAR(abc_data_doc,
  84. "Internal state held by ABC machinery.");
  85. static PyType_Slot _abc_data_type_spec_slots[] = {
  86. {Py_tp_doc, (void *)abc_data_doc},
  87. {Py_tp_new, abc_data_new},
  88. {Py_tp_dealloc, abc_data_dealloc},
  89. {Py_tp_traverse, abc_data_traverse},
  90. {Py_tp_clear, abc_data_clear},
  91. {0, 0}
  92. };
  93. static PyType_Spec _abc_data_type_spec = {
  94. .name = "_abc._abc_data",
  95. .basicsize = sizeof(_abc_data),
  96. .flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,
  97. .slots = _abc_data_type_spec_slots,
  98. };
  99. static _abc_data *
  100. _get_impl(PyObject *module, PyObject *self)
  101. {
  102. _abcmodule_state *state = get_abc_state(module);
  103. PyObject *impl = PyObject_GetAttr(self, &_Py_ID(_abc_impl));
  104. if (impl == NULL) {
  105. return NULL;
  106. }
  107. if (!Py_IS_TYPE(impl, state->_abc_data_type)) {
  108. PyErr_SetString(PyExc_TypeError, "_abc_impl is set to a wrong type");
  109. Py_DECREF(impl);
  110. return NULL;
  111. }
  112. return (_abc_data *)impl;
  113. }
  114. static int
  115. _in_weak_set(PyObject *set, PyObject *obj)
  116. {
  117. if (set == NULL || PySet_GET_SIZE(set) == 0) {
  118. return 0;
  119. }
  120. PyObject *ref = PyWeakref_NewRef(obj, NULL);
  121. if (ref == NULL) {
  122. if (PyErr_ExceptionMatches(PyExc_TypeError)) {
  123. PyErr_Clear();
  124. return 0;
  125. }
  126. return -1;
  127. }
  128. int res = PySet_Contains(set, ref);
  129. Py_DECREF(ref);
  130. return res;
  131. }
  132. static PyObject *
  133. _destroy(PyObject *setweakref, PyObject *objweakref)
  134. {
  135. PyObject *set;
  136. set = PyWeakref_GET_OBJECT(setweakref);
  137. if (set == Py_None) {
  138. Py_RETURN_NONE;
  139. }
  140. Py_INCREF(set);
  141. if (PySet_Discard(set, objweakref) < 0) {
  142. Py_DECREF(set);
  143. return NULL;
  144. }
  145. Py_DECREF(set);
  146. Py_RETURN_NONE;
  147. }
  148. static PyMethodDef _destroy_def = {
  149. "_destroy", (PyCFunction) _destroy, METH_O
  150. };
  151. static int
  152. _add_to_weak_set(PyObject **pset, PyObject *obj)
  153. {
  154. if (*pset == NULL) {
  155. *pset = PySet_New(NULL);
  156. if (*pset == NULL) {
  157. return -1;
  158. }
  159. }
  160. PyObject *set = *pset;
  161. PyObject *ref, *wr;
  162. PyObject *destroy_cb;
  163. wr = PyWeakref_NewRef(set, NULL);
  164. if (wr == NULL) {
  165. return -1;
  166. }
  167. destroy_cb = PyCFunction_NewEx(&_destroy_def, wr, NULL);
  168. if (destroy_cb == NULL) {
  169. Py_DECREF(wr);
  170. return -1;
  171. }
  172. ref = PyWeakref_NewRef(obj, destroy_cb);
  173. Py_DECREF(destroy_cb);
  174. if (ref == NULL) {
  175. Py_DECREF(wr);
  176. return -1;
  177. }
  178. int ret = PySet_Add(set, ref);
  179. Py_DECREF(wr);
  180. Py_DECREF(ref);
  181. return ret;
  182. }
  183. /*[clinic input]
  184. _abc._reset_registry
  185. self: object
  186. /
  187. Internal ABC helper to reset registry of a given class.
  188. Should be only used by refleak.py
  189. [clinic start generated code]*/
  190. static PyObject *
  191. _abc__reset_registry(PyObject *module, PyObject *self)
  192. /*[clinic end generated code: output=92d591a43566cc10 input=12a0b7eb339ac35c]*/
  193. {
  194. _abc_data *impl = _get_impl(module, self);
  195. if (impl == NULL) {
  196. return NULL;
  197. }
  198. if (impl->_abc_registry != NULL && PySet_Clear(impl->_abc_registry) < 0) {
  199. Py_DECREF(impl);
  200. return NULL;
  201. }
  202. Py_DECREF(impl);
  203. Py_RETURN_NONE;
  204. }
  205. /*[clinic input]
  206. _abc._reset_caches
  207. self: object
  208. /
  209. Internal ABC helper to reset both caches of a given class.
  210. Should be only used by refleak.py
  211. [clinic start generated code]*/
  212. static PyObject *
  213. _abc__reset_caches(PyObject *module, PyObject *self)
  214. /*[clinic end generated code: output=f296f0d5c513f80c input=c0ac616fd8acfb6f]*/
  215. {
  216. _abc_data *impl = _get_impl(module, self);
  217. if (impl == NULL) {
  218. return NULL;
  219. }
  220. if (impl->_abc_cache != NULL && PySet_Clear(impl->_abc_cache) < 0) {
  221. Py_DECREF(impl);
  222. return NULL;
  223. }
  224. /* also the second cache */
  225. if (impl->_abc_negative_cache != NULL &&
  226. PySet_Clear(impl->_abc_negative_cache) < 0) {
  227. Py_DECREF(impl);
  228. return NULL;
  229. }
  230. Py_DECREF(impl);
  231. Py_RETURN_NONE;
  232. }
  233. /*[clinic input]
  234. _abc._get_dump
  235. self: object
  236. /
  237. Internal ABC helper for cache and registry debugging.
  238. Return shallow copies of registry, of both caches, and
  239. negative cache version. Don't call this function directly,
  240. instead use ABC._dump_registry() for a nice repr.
  241. [clinic start generated code]*/
  242. static PyObject *
  243. _abc__get_dump(PyObject *module, PyObject *self)
  244. /*[clinic end generated code: output=9d9569a8e2c1c443 input=2c5deb1bfe9e3c79]*/
  245. {
  246. _abc_data *impl = _get_impl(module, self);
  247. if (impl == NULL) {
  248. return NULL;
  249. }
  250. PyObject *res = Py_BuildValue("NNNK",
  251. PySet_New(impl->_abc_registry),
  252. PySet_New(impl->_abc_cache),
  253. PySet_New(impl->_abc_negative_cache),
  254. impl->_abc_negative_cache_version);
  255. Py_DECREF(impl);
  256. return res;
  257. }
  258. // Compute set of abstract method names.
  259. static int
  260. compute_abstract_methods(PyObject *self)
  261. {
  262. int ret = -1;
  263. PyObject *abstracts = PyFrozenSet_New(NULL);
  264. if (abstracts == NULL) {
  265. return -1;
  266. }
  267. PyObject *ns = NULL, *items = NULL, *bases = NULL; // Py_XDECREF()ed on error.
  268. /* Stage 1: direct abstract methods. */
  269. ns = PyObject_GetAttr(self, &_Py_ID(__dict__));
  270. if (!ns) {
  271. goto error;
  272. }
  273. // We can't use PyDict_Next(ns) even when ns is dict because
  274. // _PyObject_IsAbstract() can mutate ns.
  275. items = PyMapping_Items(ns);
  276. if (!items) {
  277. goto error;
  278. }
  279. assert(PyList_Check(items));
  280. for (Py_ssize_t pos = 0; pos < PyList_GET_SIZE(items); pos++) {
  281. PyObject *it = PySequence_Fast(
  282. PyList_GET_ITEM(items, pos),
  283. "items() returned non-iterable");
  284. if (!it) {
  285. goto error;
  286. }
  287. if (PySequence_Fast_GET_SIZE(it) != 2) {
  288. PyErr_SetString(PyExc_TypeError,
  289. "items() returned item which size is not 2");
  290. Py_DECREF(it);
  291. goto error;
  292. }
  293. // borrowed
  294. PyObject *key = PySequence_Fast_GET_ITEM(it, 0);
  295. PyObject *value = PySequence_Fast_GET_ITEM(it, 1);
  296. // items or it may be cleared while accessing __abstractmethod__
  297. // So we need to keep strong reference for key
  298. Py_INCREF(key);
  299. int is_abstract = _PyObject_IsAbstract(value);
  300. if (is_abstract < 0 ||
  301. (is_abstract && PySet_Add(abstracts, key) < 0)) {
  302. Py_DECREF(it);
  303. Py_DECREF(key);
  304. goto error;
  305. }
  306. Py_DECREF(key);
  307. Py_DECREF(it);
  308. }
  309. /* Stage 2: inherited abstract methods. */
  310. bases = PyObject_GetAttr(self, &_Py_ID(__bases__));
  311. if (!bases) {
  312. goto error;
  313. }
  314. if (!PyTuple_Check(bases)) {
  315. PyErr_SetString(PyExc_TypeError, "__bases__ is not tuple");
  316. goto error;
  317. }
  318. for (Py_ssize_t pos = 0; pos < PyTuple_GET_SIZE(bases); pos++) {
  319. PyObject *item = PyTuple_GET_ITEM(bases, pos); // borrowed
  320. PyObject *base_abstracts, *iter;
  321. if (_PyObject_LookupAttr(item, &_Py_ID(__abstractmethods__),
  322. &base_abstracts) < 0) {
  323. goto error;
  324. }
  325. if (base_abstracts == NULL) {
  326. continue;
  327. }
  328. if (!(iter = PyObject_GetIter(base_abstracts))) {
  329. Py_DECREF(base_abstracts);
  330. goto error;
  331. }
  332. Py_DECREF(base_abstracts);
  333. PyObject *key, *value;
  334. while ((key = PyIter_Next(iter))) {
  335. if (_PyObject_LookupAttr(self, key, &value) < 0) {
  336. Py_DECREF(key);
  337. Py_DECREF(iter);
  338. goto error;
  339. }
  340. if (value == NULL) {
  341. Py_DECREF(key);
  342. continue;
  343. }
  344. int is_abstract = _PyObject_IsAbstract(value);
  345. Py_DECREF(value);
  346. if (is_abstract < 0 ||
  347. (is_abstract && PySet_Add(abstracts, key) < 0))
  348. {
  349. Py_DECREF(key);
  350. Py_DECREF(iter);
  351. goto error;
  352. }
  353. Py_DECREF(key);
  354. }
  355. Py_DECREF(iter);
  356. if (PyErr_Occurred()) {
  357. goto error;
  358. }
  359. }
  360. if (PyObject_SetAttr(self, &_Py_ID(__abstractmethods__), abstracts) < 0) {
  361. goto error;
  362. }
  363. ret = 0;
  364. error:
  365. Py_DECREF(abstracts);
  366. Py_XDECREF(ns);
  367. Py_XDECREF(items);
  368. Py_XDECREF(bases);
  369. return ret;
  370. }
  371. #define COLLECTION_FLAGS (Py_TPFLAGS_SEQUENCE | Py_TPFLAGS_MAPPING)
  372. /*[clinic input]
  373. _abc._abc_init
  374. self: object
  375. /
  376. Internal ABC helper for class set-up. Should be never used outside abc module.
  377. [clinic start generated code]*/
  378. static PyObject *
  379. _abc__abc_init(PyObject *module, PyObject *self)
  380. /*[clinic end generated code: output=594757375714cda1 input=8d7fe470ff77f029]*/
  381. {
  382. _abcmodule_state *state = get_abc_state(module);
  383. PyObject *data;
  384. if (compute_abstract_methods(self) < 0) {
  385. return NULL;
  386. }
  387. /* Set up inheritance registry. */
  388. data = abc_data_new(state->_abc_data_type, NULL, NULL);
  389. if (data == NULL) {
  390. return NULL;
  391. }
  392. if (PyObject_SetAttr(self, &_Py_ID(_abc_impl), data) < 0) {
  393. Py_DECREF(data);
  394. return NULL;
  395. }
  396. Py_DECREF(data);
  397. /* If __abc_tpflags__ & COLLECTION_FLAGS is set, then set the corresponding bit(s)
  398. * in the new class.
  399. * Used by collections.abc.Sequence and collections.abc.Mapping to indicate
  400. * their special status w.r.t. pattern matching. */
  401. if (PyType_Check(self)) {
  402. PyTypeObject *cls = (PyTypeObject *)self;
  403. PyObject *dict = _PyType_GetDict(cls);
  404. PyObject *flags = PyDict_GetItemWithError(dict,
  405. &_Py_ID(__abc_tpflags__));
  406. if (flags == NULL) {
  407. if (PyErr_Occurred()) {
  408. return NULL;
  409. }
  410. }
  411. else {
  412. if (PyLong_CheckExact(flags)) {
  413. long val = PyLong_AsLong(flags);
  414. if (val == -1 && PyErr_Occurred()) {
  415. return NULL;
  416. }
  417. if ((val & COLLECTION_FLAGS) == COLLECTION_FLAGS) {
  418. PyErr_SetString(PyExc_TypeError, "__abc_tpflags__ cannot be both Py_TPFLAGS_SEQUENCE and Py_TPFLAGS_MAPPING");
  419. return NULL;
  420. }
  421. ((PyTypeObject *)self)->tp_flags |= (val & COLLECTION_FLAGS);
  422. }
  423. if (PyDict_DelItem(dict, &_Py_ID(__abc_tpflags__)) < 0) {
  424. return NULL;
  425. }
  426. }
  427. }
  428. Py_RETURN_NONE;
  429. }
  430. static void
  431. set_collection_flag_recursive(PyTypeObject *child, unsigned long flag)
  432. {
  433. assert(flag == Py_TPFLAGS_MAPPING || flag == Py_TPFLAGS_SEQUENCE);
  434. if (PyType_HasFeature(child, Py_TPFLAGS_IMMUTABLETYPE) ||
  435. (child->tp_flags & COLLECTION_FLAGS) == flag)
  436. {
  437. return;
  438. }
  439. child->tp_flags &= ~COLLECTION_FLAGS;
  440. child->tp_flags |= flag;
  441. PyObject *grandchildren = _PyType_GetSubclasses(child);
  442. if (grandchildren == NULL) {
  443. return;
  444. }
  445. for (Py_ssize_t i = 0; i < PyList_GET_SIZE(grandchildren); i++) {
  446. PyObject *grandchild = PyList_GET_ITEM(grandchildren, i);
  447. set_collection_flag_recursive((PyTypeObject *)grandchild, flag);
  448. }
  449. Py_DECREF(grandchildren);
  450. }
  451. /*[clinic input]
  452. _abc._abc_register
  453. self: object
  454. subclass: object
  455. /
  456. Internal ABC helper for subclasss registration. Should be never used outside abc module.
  457. [clinic start generated code]*/
  458. static PyObject *
  459. _abc__abc_register_impl(PyObject *module, PyObject *self, PyObject *subclass)
  460. /*[clinic end generated code: output=7851e7668c963524 input=ca589f8c3080e67f]*/
  461. {
  462. if (!PyType_Check(subclass)) {
  463. PyErr_SetString(PyExc_TypeError, "Can only register classes");
  464. return NULL;
  465. }
  466. int result = PyObject_IsSubclass(subclass, self);
  467. if (result > 0) {
  468. return Py_NewRef(subclass); /* Already a subclass. */
  469. }
  470. if (result < 0) {
  471. return NULL;
  472. }
  473. /* Subtle: test for cycles *after* testing for "already a subclass";
  474. this means we allow X.register(X) and interpret it as a no-op. */
  475. result = PyObject_IsSubclass(self, subclass);
  476. if (result > 0) {
  477. /* This would create a cycle, which is bad for the algorithm below. */
  478. PyErr_SetString(PyExc_RuntimeError, "Refusing to create an inheritance cycle");
  479. return NULL;
  480. }
  481. if (result < 0) {
  482. return NULL;
  483. }
  484. _abc_data *impl = _get_impl(module, self);
  485. if (impl == NULL) {
  486. return NULL;
  487. }
  488. if (_add_to_weak_set(&impl->_abc_registry, subclass) < 0) {
  489. Py_DECREF(impl);
  490. return NULL;
  491. }
  492. Py_DECREF(impl);
  493. /* Invalidate negative cache */
  494. get_abc_state(module)->abc_invalidation_counter++;
  495. /* Set Py_TPFLAGS_SEQUENCE or Py_TPFLAGS_MAPPING flag */
  496. if (PyType_Check(self)) {
  497. unsigned long collection_flag = ((PyTypeObject *)self)->tp_flags & COLLECTION_FLAGS;
  498. if (collection_flag) {
  499. set_collection_flag_recursive((PyTypeObject *)subclass, collection_flag);
  500. }
  501. }
  502. return Py_NewRef(subclass);
  503. }
  504. /*[clinic input]
  505. _abc._abc_instancecheck
  506. self: object
  507. instance: object
  508. /
  509. Internal ABC helper for instance checks. Should be never used outside abc module.
  510. [clinic start generated code]*/
  511. static PyObject *
  512. _abc__abc_instancecheck_impl(PyObject *module, PyObject *self,
  513. PyObject *instance)
  514. /*[clinic end generated code: output=b8b5148f63b6b56f input=a4f4525679261084]*/
  515. {
  516. PyObject *subtype, *result = NULL, *subclass = NULL;
  517. _abc_data *impl = _get_impl(module, self);
  518. if (impl == NULL) {
  519. return NULL;
  520. }
  521. subclass = PyObject_GetAttr(instance, &_Py_ID(__class__));
  522. if (subclass == NULL) {
  523. Py_DECREF(impl);
  524. return NULL;
  525. }
  526. /* Inline the cache checking. */
  527. int incache = _in_weak_set(impl->_abc_cache, subclass);
  528. if (incache < 0) {
  529. goto end;
  530. }
  531. if (incache > 0) {
  532. result = Py_NewRef(Py_True);
  533. goto end;
  534. }
  535. subtype = (PyObject *)Py_TYPE(instance);
  536. if (subtype == subclass) {
  537. if (impl->_abc_negative_cache_version == get_abc_state(module)->abc_invalidation_counter) {
  538. incache = _in_weak_set(impl->_abc_negative_cache, subclass);
  539. if (incache < 0) {
  540. goto end;
  541. }
  542. if (incache > 0) {
  543. result = Py_NewRef(Py_False);
  544. goto end;
  545. }
  546. }
  547. /* Fall back to the subclass check. */
  548. result = PyObject_CallMethodOneArg(self, &_Py_ID(__subclasscheck__),
  549. subclass);
  550. goto end;
  551. }
  552. result = PyObject_CallMethodOneArg(self, &_Py_ID(__subclasscheck__),
  553. subclass);
  554. if (result == NULL) {
  555. goto end;
  556. }
  557. switch (PyObject_IsTrue(result)) {
  558. case -1:
  559. Py_SETREF(result, NULL);
  560. break;
  561. case 0:
  562. Py_DECREF(result);
  563. result = PyObject_CallMethodOneArg(self, &_Py_ID(__subclasscheck__),
  564. subtype);
  565. break;
  566. case 1: // Nothing to do.
  567. break;
  568. default:
  569. Py_UNREACHABLE();
  570. }
  571. end:
  572. Py_XDECREF(impl);
  573. Py_XDECREF(subclass);
  574. return result;
  575. }
  576. // Return -1 when exception occurred.
  577. // Return 1 when result is set.
  578. // Return 0 otherwise.
  579. static int subclasscheck_check_registry(_abc_data *impl, PyObject *subclass,
  580. PyObject **result);
  581. /*[clinic input]
  582. _abc._abc_subclasscheck
  583. self: object
  584. subclass: object
  585. /
  586. Internal ABC helper for subclasss checks. Should be never used outside abc module.
  587. [clinic start generated code]*/
  588. static PyObject *
  589. _abc__abc_subclasscheck_impl(PyObject *module, PyObject *self,
  590. PyObject *subclass)
  591. /*[clinic end generated code: output=b56c9e4a530e3894 input=1d947243409d10b8]*/
  592. {
  593. if (!PyType_Check(subclass)) {
  594. PyErr_SetString(PyExc_TypeError, "issubclass() arg 1 must be a class");
  595. return NULL;
  596. }
  597. PyObject *ok, *subclasses = NULL, *result = NULL;
  598. _abcmodule_state *state = NULL;
  599. Py_ssize_t pos;
  600. int incache;
  601. _abc_data *impl = _get_impl(module, self);
  602. if (impl == NULL) {
  603. return NULL;
  604. }
  605. /* 1. Check cache. */
  606. incache = _in_weak_set(impl->_abc_cache, subclass);
  607. if (incache < 0) {
  608. goto end;
  609. }
  610. if (incache > 0) {
  611. result = Py_True;
  612. goto end;
  613. }
  614. state = get_abc_state(module);
  615. /* 2. Check negative cache; may have to invalidate. */
  616. if (impl->_abc_negative_cache_version < state->abc_invalidation_counter) {
  617. /* Invalidate the negative cache. */
  618. if (impl->_abc_negative_cache != NULL &&
  619. PySet_Clear(impl->_abc_negative_cache) < 0)
  620. {
  621. goto end;
  622. }
  623. impl->_abc_negative_cache_version = state->abc_invalidation_counter;
  624. }
  625. else {
  626. incache = _in_weak_set(impl->_abc_negative_cache, subclass);
  627. if (incache < 0) {
  628. goto end;
  629. }
  630. if (incache > 0) {
  631. result = Py_False;
  632. goto end;
  633. }
  634. }
  635. /* 3. Check the subclass hook. */
  636. ok = PyObject_CallMethodOneArg(
  637. (PyObject *)self, &_Py_ID(__subclasshook__), subclass);
  638. if (ok == NULL) {
  639. goto end;
  640. }
  641. if (ok == Py_True) {
  642. Py_DECREF(ok);
  643. if (_add_to_weak_set(&impl->_abc_cache, subclass) < 0) {
  644. goto end;
  645. }
  646. result = Py_True;
  647. goto end;
  648. }
  649. if (ok == Py_False) {
  650. Py_DECREF(ok);
  651. if (_add_to_weak_set(&impl->_abc_negative_cache, subclass) < 0) {
  652. goto end;
  653. }
  654. result = Py_False;
  655. goto end;
  656. }
  657. if (ok != Py_NotImplemented) {
  658. Py_DECREF(ok);
  659. PyErr_SetString(PyExc_AssertionError, "__subclasshook__ must return either"
  660. " False, True, or NotImplemented");
  661. goto end;
  662. }
  663. Py_DECREF(ok);
  664. /* 4. Check if it's a direct subclass. */
  665. PyObject *mro = _PyType_GetMRO((PyTypeObject *)subclass);
  666. assert(PyTuple_Check(mro));
  667. for (pos = 0; pos < PyTuple_GET_SIZE(mro); pos++) {
  668. PyObject *mro_item = PyTuple_GET_ITEM(mro, pos);
  669. assert(mro_item != NULL);
  670. if ((PyObject *)self == mro_item) {
  671. if (_add_to_weak_set(&impl->_abc_cache, subclass) < 0) {
  672. goto end;
  673. }
  674. result = Py_True;
  675. goto end;
  676. }
  677. }
  678. /* 5. Check if it's a subclass of a registered class (recursive). */
  679. if (subclasscheck_check_registry(impl, subclass, &result)) {
  680. // Exception occurred or result is set.
  681. goto end;
  682. }
  683. /* 6. Check if it's a subclass of a subclass (recursive). */
  684. subclasses = PyObject_CallMethod(self, "__subclasses__", NULL);
  685. if (subclasses == NULL) {
  686. goto end;
  687. }
  688. if (!PyList_Check(subclasses)) {
  689. PyErr_SetString(PyExc_TypeError, "__subclasses__() must return a list");
  690. goto end;
  691. }
  692. for (pos = 0; pos < PyList_GET_SIZE(subclasses); pos++) {
  693. PyObject *scls = PyList_GET_ITEM(subclasses, pos);
  694. Py_INCREF(scls);
  695. int r = PyObject_IsSubclass(subclass, scls);
  696. Py_DECREF(scls);
  697. if (r > 0) {
  698. if (_add_to_weak_set(&impl->_abc_cache, subclass) < 0) {
  699. goto end;
  700. }
  701. result = Py_True;
  702. goto end;
  703. }
  704. if (r < 0) {
  705. goto end;
  706. }
  707. }
  708. /* No dice; update negative cache. */
  709. if (_add_to_weak_set(&impl->_abc_negative_cache, subclass) < 0) {
  710. goto end;
  711. }
  712. result = Py_False;
  713. end:
  714. Py_DECREF(impl);
  715. Py_XDECREF(subclasses);
  716. return Py_XNewRef(result);
  717. }
  718. static int
  719. subclasscheck_check_registry(_abc_data *impl, PyObject *subclass,
  720. PyObject **result)
  721. {
  722. // Fast path: check subclass is in weakref directly.
  723. int ret = _in_weak_set(impl->_abc_registry, subclass);
  724. if (ret < 0) {
  725. *result = NULL;
  726. return -1;
  727. }
  728. if (ret > 0) {
  729. *result = Py_True;
  730. return 1;
  731. }
  732. if (impl->_abc_registry == NULL) {
  733. return 0;
  734. }
  735. Py_ssize_t registry_size = PySet_Size(impl->_abc_registry);
  736. if (registry_size == 0) {
  737. return 0;
  738. }
  739. // Weakref callback may remove entry from set.
  740. // So we take snapshot of registry first.
  741. PyObject **copy = PyMem_Malloc(sizeof(PyObject*) * registry_size);
  742. if (copy == NULL) {
  743. PyErr_NoMemory();
  744. return -1;
  745. }
  746. PyObject *key;
  747. Py_ssize_t pos = 0;
  748. Py_hash_t hash;
  749. Py_ssize_t i = 0;
  750. while (_PySet_NextEntry(impl->_abc_registry, &pos, &key, &hash)) {
  751. copy[i++] = Py_NewRef(key);
  752. }
  753. assert(i == registry_size);
  754. for (i = 0; i < registry_size; i++) {
  755. PyObject *rkey = PyWeakref_GetObject(copy[i]);
  756. if (rkey == NULL) {
  757. // Someone inject non-weakref type in the registry.
  758. ret = -1;
  759. break;
  760. }
  761. if (rkey == Py_None) {
  762. continue;
  763. }
  764. Py_INCREF(rkey);
  765. int r = PyObject_IsSubclass(subclass, rkey);
  766. Py_DECREF(rkey);
  767. if (r < 0) {
  768. ret = -1;
  769. break;
  770. }
  771. if (r > 0) {
  772. if (_add_to_weak_set(&impl->_abc_cache, subclass) < 0) {
  773. ret = -1;
  774. break;
  775. }
  776. *result = Py_True;
  777. ret = 1;
  778. break;
  779. }
  780. }
  781. for (i = 0; i < registry_size; i++) {
  782. Py_DECREF(copy[i]);
  783. }
  784. PyMem_Free(copy);
  785. return ret;
  786. }
  787. /*[clinic input]
  788. _abc.get_cache_token
  789. Returns the current ABC cache token.
  790. The token is an opaque object (supporting equality testing) identifying the
  791. current version of the ABC cache for virtual subclasses. The token changes
  792. with every call to register() on any ABC.
  793. [clinic start generated code]*/
  794. static PyObject *
  795. _abc_get_cache_token_impl(PyObject *module)
  796. /*[clinic end generated code: output=c7d87841e033dacc input=70413d1c423ad9f9]*/
  797. {
  798. _abcmodule_state *state = get_abc_state(module);
  799. return PyLong_FromUnsignedLongLong(state->abc_invalidation_counter);
  800. }
  801. static struct PyMethodDef _abcmodule_methods[] = {
  802. _ABC_GET_CACHE_TOKEN_METHODDEF
  803. _ABC__ABC_INIT_METHODDEF
  804. _ABC__RESET_REGISTRY_METHODDEF
  805. _ABC__RESET_CACHES_METHODDEF
  806. _ABC__GET_DUMP_METHODDEF
  807. _ABC__ABC_REGISTER_METHODDEF
  808. _ABC__ABC_INSTANCECHECK_METHODDEF
  809. _ABC__ABC_SUBCLASSCHECK_METHODDEF
  810. {NULL, NULL} /* sentinel */
  811. };
  812. static int
  813. _abcmodule_exec(PyObject *module)
  814. {
  815. _abcmodule_state *state = get_abc_state(module);
  816. state->abc_invalidation_counter = 0;
  817. state->_abc_data_type = (PyTypeObject *)PyType_FromModuleAndSpec(module, &_abc_data_type_spec, NULL);
  818. if (state->_abc_data_type == NULL) {
  819. return -1;
  820. }
  821. return 0;
  822. }
  823. static int
  824. _abcmodule_traverse(PyObject *module, visitproc visit, void *arg)
  825. {
  826. _abcmodule_state *state = get_abc_state(module);
  827. Py_VISIT(state->_abc_data_type);
  828. return 0;
  829. }
  830. static int
  831. _abcmodule_clear(PyObject *module)
  832. {
  833. _abcmodule_state *state = get_abc_state(module);
  834. Py_CLEAR(state->_abc_data_type);
  835. return 0;
  836. }
  837. static void
  838. _abcmodule_free(void *module)
  839. {
  840. _abcmodule_clear((PyObject *)module);
  841. }
  842. static PyModuleDef_Slot _abcmodule_slots[] = {
  843. {Py_mod_exec, _abcmodule_exec},
  844. {Py_mod_multiple_interpreters, Py_MOD_PER_INTERPRETER_GIL_SUPPORTED},
  845. {0, NULL}
  846. };
  847. static struct PyModuleDef _abcmodule = {
  848. PyModuleDef_HEAD_INIT,
  849. .m_name = "_abc",
  850. .m_doc = _abc__doc__,
  851. .m_size = sizeof(_abcmodule_state),
  852. .m_methods = _abcmodule_methods,
  853. .m_slots = _abcmodule_slots,
  854. .m_traverse = _abcmodule_traverse,
  855. .m_clear = _abcmodule_clear,
  856. .m_free = _abcmodule_free,
  857. };
  858. PyMODINIT_FUNC
  859. PyInit__abc(void)
  860. {
  861. return PyModuleDef_Init(&_abcmodule);
  862. }