_weakref.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. #include "Python.h"
  2. #include "pycore_object.h" // _PyObject_GET_WEAKREFS_LISTPTR
  3. #define GET_WEAKREFS_LISTPTR(o) \
  4. ((PyWeakReference **) _PyObject_GET_WEAKREFS_LISTPTR(o))
  5. /*[clinic input]
  6. module _weakref
  7. [clinic start generated code]*/
  8. /*[clinic end generated code: output=da39a3ee5e6b4b0d input=ffec73b85846596d]*/
  9. #include "clinic/_weakref.c.h"
  10. /*[clinic input]
  11. _weakref.getweakrefcount -> Py_ssize_t
  12. object: object
  13. /
  14. Return the number of weak references to 'object'.
  15. [clinic start generated code]*/
  16. static Py_ssize_t
  17. _weakref_getweakrefcount_impl(PyObject *module, PyObject *object)
  18. /*[clinic end generated code: output=301806d59558ff3e input=cedb69711b6a2507]*/
  19. {
  20. PyWeakReference **list;
  21. if (!_PyType_SUPPORTS_WEAKREFS(Py_TYPE(object)))
  22. return 0;
  23. list = GET_WEAKREFS_LISTPTR(object);
  24. return _PyWeakref_GetWeakrefCount(*list);
  25. }
  26. static int
  27. is_dead_weakref(PyObject *value)
  28. {
  29. if (!PyWeakref_Check(value)) {
  30. PyErr_SetString(PyExc_TypeError, "not a weakref");
  31. return -1;
  32. }
  33. return PyWeakref_GET_OBJECT(value) == Py_None;
  34. }
  35. /*[clinic input]
  36. _weakref._remove_dead_weakref -> object
  37. dct: object(subclass_of='&PyDict_Type')
  38. key: object
  39. /
  40. Atomically remove key from dict if it points to a dead weakref.
  41. [clinic start generated code]*/
  42. static PyObject *
  43. _weakref__remove_dead_weakref_impl(PyObject *module, PyObject *dct,
  44. PyObject *key)
  45. /*[clinic end generated code: output=d9ff53061fcb875c input=19fc91f257f96a1d]*/
  46. {
  47. if (_PyDict_DelItemIf(dct, key, is_dead_weakref) < 0) {
  48. if (PyErr_ExceptionMatches(PyExc_KeyError))
  49. /* This function is meant to allow safe weak-value dicts
  50. with GC in another thread (see issue #28427), so it's
  51. ok if the key doesn't exist anymore.
  52. */
  53. PyErr_Clear();
  54. else
  55. return NULL;
  56. }
  57. Py_RETURN_NONE;
  58. }
  59. /*[clinic input]
  60. _weakref.getweakrefs
  61. object: object
  62. /
  63. Return a list of all weak reference objects pointing to 'object'.
  64. [clinic start generated code]*/
  65. static PyObject *
  66. _weakref_getweakrefs(PyObject *module, PyObject *object)
  67. /*[clinic end generated code: output=25c7731d8e011824 input=00c6d0e5d3206693]*/
  68. {
  69. PyObject *result = NULL;
  70. if (_PyType_SUPPORTS_WEAKREFS(Py_TYPE(object))) {
  71. PyWeakReference **list = GET_WEAKREFS_LISTPTR(object);
  72. Py_ssize_t count = _PyWeakref_GetWeakrefCount(*list);
  73. result = PyList_New(count);
  74. if (result != NULL) {
  75. PyWeakReference *current = *list;
  76. Py_ssize_t i;
  77. for (i = 0; i < count; ++i) {
  78. PyList_SET_ITEM(result, i, (PyObject *) current);
  79. Py_INCREF(current);
  80. current = current->wr_next;
  81. }
  82. }
  83. }
  84. else {
  85. result = PyList_New(0);
  86. }
  87. return result;
  88. }
  89. /*[clinic input]
  90. _weakref.proxy
  91. object: object
  92. callback: object(c_default="NULL") = None
  93. /
  94. Create a proxy object that weakly references 'object'.
  95. 'callback', if given, is called with a reference to the
  96. proxy when 'object' is about to be finalized.
  97. [clinic start generated code]*/
  98. static PyObject *
  99. _weakref_proxy_impl(PyObject *module, PyObject *object, PyObject *callback)
  100. /*[clinic end generated code: output=d68fa4ad9ea40519 input=4808adf22fd137e7]*/
  101. {
  102. return PyWeakref_NewProxy(object, callback);
  103. }
  104. static PyMethodDef
  105. weakref_functions[] = {
  106. _WEAKREF_GETWEAKREFCOUNT_METHODDEF
  107. _WEAKREF__REMOVE_DEAD_WEAKREF_METHODDEF
  108. _WEAKREF_GETWEAKREFS_METHODDEF
  109. _WEAKREF_PROXY_METHODDEF
  110. {NULL, NULL, 0, NULL}
  111. };
  112. static int
  113. weakref_exec(PyObject *module)
  114. {
  115. Py_INCREF(&_PyWeakref_RefType);
  116. if (PyModule_AddObject(module, "ref", (PyObject *) &_PyWeakref_RefType) < 0) {
  117. Py_DECREF(&_PyWeakref_RefType);
  118. return -1;
  119. }
  120. Py_INCREF(&_PyWeakref_RefType);
  121. if (PyModule_AddObject(module, "ReferenceType",
  122. (PyObject *) &_PyWeakref_RefType) < 0) {
  123. Py_DECREF(&_PyWeakref_RefType);
  124. return -1;
  125. }
  126. Py_INCREF(&_PyWeakref_ProxyType);
  127. if (PyModule_AddObject(module, "ProxyType",
  128. (PyObject *) &_PyWeakref_ProxyType) < 0) {
  129. Py_DECREF(&_PyWeakref_ProxyType);
  130. return -1;
  131. }
  132. Py_INCREF(&_PyWeakref_CallableProxyType);
  133. if (PyModule_AddObject(module, "CallableProxyType",
  134. (PyObject *) &_PyWeakref_CallableProxyType) < 0) {
  135. Py_DECREF(&_PyWeakref_CallableProxyType);
  136. return -1;
  137. }
  138. return 0;
  139. }
  140. static struct PyModuleDef_Slot weakref_slots[] = {
  141. {Py_mod_exec, weakref_exec},
  142. {Py_mod_multiple_interpreters, Py_MOD_PER_INTERPRETER_GIL_SUPPORTED},
  143. {0, NULL}
  144. };
  145. static struct PyModuleDef weakrefmodule = {
  146. PyModuleDef_HEAD_INIT,
  147. "_weakref",
  148. "Weak-reference support module.",
  149. 0,
  150. weakref_functions,
  151. weakref_slots,
  152. NULL,
  153. NULL,
  154. NULL
  155. };
  156. PyMODINIT_FUNC
  157. PyInit__weakref(void)
  158. {
  159. return PyModuleDef_Init(&weakrefmodule);
  160. }