PageRenderTime 57ms CodeModel.GetById 32ms RepoModel.GetById 1ms app.codeStats 0ms

/Modules/_abc.c

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