mirror of
https://github.com/python/cpython.git
synced 2025-10-09 16:34:44 +00:00
gh-112213: Update _weakref module to use new AC feature (gh-112250)
This commit is contained in:
parent
b8c952af72
commit
2bcc0f7d34
2 changed files with 31 additions and 25 deletions
|
@ -1,5 +1,4 @@
|
|||
#include "Python.h"
|
||||
#include "pycore_critical_section.h" // Py_BEGIN_CRITICAL_SECTION()
|
||||
#include "pycore_dict.h" // _PyDict_DelItemIf()
|
||||
#include "pycore_object.h" // _PyObject_GET_WEAKREFS_LISTPTR()
|
||||
#include "pycore_weakref.h" // _PyWeakref_IS_DEAD()
|
||||
|
@ -15,7 +14,7 @@ module _weakref
|
|||
#include "clinic/_weakref.c.h"
|
||||
|
||||
/*[clinic input]
|
||||
|
||||
@critical_section object
|
||||
_weakref.getweakrefcount -> Py_ssize_t
|
||||
|
||||
object: object
|
||||
|
@ -26,17 +25,13 @@ Return the number of weak references to 'object'.
|
|||
|
||||
static Py_ssize_t
|
||||
_weakref_getweakrefcount_impl(PyObject *module, PyObject *object)
|
||||
/*[clinic end generated code: output=301806d59558ff3e input=cedb69711b6a2507]*/
|
||||
/*[clinic end generated code: output=301806d59558ff3e input=6535a580f1d0ebdc]*/
|
||||
{
|
||||
PyWeakReference **list;
|
||||
|
||||
if (!_PyType_SUPPORTS_WEAKREFS(Py_TYPE(object)))
|
||||
if (!_PyType_SUPPORTS_WEAKREFS(Py_TYPE(object))) {
|
||||
return 0;
|
||||
Py_ssize_t count;
|
||||
Py_BEGIN_CRITICAL_SECTION(object);
|
||||
list = GET_WEAKREFS_LISTPTR(object);
|
||||
count = _PyWeakref_GetWeakrefCount(*list);
|
||||
Py_END_CRITICAL_SECTION();
|
||||
}
|
||||
PyWeakReference **list = GET_WEAKREFS_LISTPTR(object);
|
||||
Py_ssize_t count = _PyWeakref_GetWeakrefCount(*list);
|
||||
return count;
|
||||
}
|
||||
|
||||
|
@ -48,11 +43,7 @@ is_dead_weakref(PyObject *value)
|
|||
PyErr_SetString(PyExc_TypeError, "not a weakref");
|
||||
return -1;
|
||||
}
|
||||
int is_dead;
|
||||
Py_BEGIN_CRITICAL_SECTION(value);
|
||||
is_dead = _PyWeakref_IS_DEAD(value);
|
||||
Py_END_CRITICAL_SECTION();
|
||||
return is_dead;
|
||||
return _PyWeakref_IS_DEAD(value);
|
||||
}
|
||||
|
||||
/*[clinic input]
|
||||
|
@ -86,6 +77,7 @@ _weakref__remove_dead_weakref_impl(PyObject *module, PyObject *dct,
|
|||
|
||||
|
||||
/*[clinic input]
|
||||
@critical_section object
|
||||
_weakref.getweakrefs
|
||||
object: object
|
||||
/
|
||||
|
@ -94,21 +86,19 @@ Return a list of all weak reference objects pointing to 'object'.
|
|||
[clinic start generated code]*/
|
||||
|
||||
static PyObject *
|
||||
_weakref_getweakrefs(PyObject *module, PyObject *object)
|
||||
/*[clinic end generated code: output=25c7731d8e011824 input=00c6d0e5d3206693]*/
|
||||
_weakref_getweakrefs_impl(PyObject *module, PyObject *object)
|
||||
/*[clinic end generated code: output=5ec268989fb8f035 input=3dea95b8f5b31bbb]*/
|
||||
{
|
||||
if (!_PyType_SUPPORTS_WEAKREFS(Py_TYPE(object))) {
|
||||
return PyList_New(0);
|
||||
}
|
||||
|
||||
PyObject *result;
|
||||
Py_BEGIN_CRITICAL_SECTION(object);
|
||||
PyWeakReference **list = GET_WEAKREFS_LISTPTR(object);
|
||||
Py_ssize_t count = _PyWeakref_GetWeakrefCount(*list);
|
||||
|
||||
result = PyList_New(count);
|
||||
PyObject *result = PyList_New(count);
|
||||
if (result == NULL) {
|
||||
goto exit;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
PyWeakReference *current = *list;
|
||||
|
@ -116,8 +106,6 @@ _weakref_getweakrefs(PyObject *module, PyObject *object)
|
|||
PyList_SET_ITEM(result, i, Py_NewRef(current));
|
||||
current = current->wr_next;
|
||||
}
|
||||
exit:
|
||||
Py_END_CRITICAL_SECTION();
|
||||
return result;
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue