Coverage Report

Created: 2026-10-31 06:21

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/cpython/Objects/clinic/weakrefobject.c.h
Line
Count
Source
1
/*[clinic input]
2
preserve
3
[clinic start generated code]*/
4
5
#include "pycore_critical_section.h"// Py_BEGIN_CRITICAL_SECTION()
6
#include "pycore_modsupport.h"    // _PyArg_CheckPositional()
7
8
static Py_hash_t
9
weakref_hash_impl(PyWeakReference *self);
10
11
static Py_hash_t
12
weakref_hash(PyObject *self)
13
423k
{
14
423k
    Py_hash_t return_value = -1;
15
16
423k
    Py_BEGIN_CRITICAL_SECTION(self);
17
423k
    return_value = weakref_hash_impl((PyWeakReference *)self);
18
423k
    Py_END_CRITICAL_SECTION();
19
20
423k
    return return_value;
21
423k
}
22
23
PyDoc_STRVAR(weakref_ref_new__doc__,
24
"ReferenceType(object, callback=None, /)\n"
25
"--\n"
26
"\n"
27
"Create a weak reference to object.\n"
28
"\n"
29
"Calling the reference returns object, or None once it has been garbage\n"
30
"collected.  If callback is given, it is called with the reference as\n"
31
"its only argument when object is about to be finalized.");
32
33
static PyObject *
34
weakref_ref_new_impl(PyTypeObject *type, PyObject *object,
35
                     PyObject *callback);
36
37
static PyObject *
38
weakref_ref_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
39
13.9k
{
40
13.9k
    PyObject *return_value = NULL;
41
13.9k
    PyTypeObject *base_tp = &_PyWeakref_RefType;
42
13.9k
    PyObject *object;
43
13.9k
    PyObject *callback = NULL;
44
45
13.9k
    if ((type == base_tp || type->tp_init == base_tp->tp_init) &&
46
0
        !_PyArg_NoKeywords("ReferenceType", kwargs)) {
47
0
        goto exit;
48
0
    }
49
13.9k
    if (!_PyArg_CheckPositional("ReferenceType", PyTuple_GET_SIZE(args), 1, 2)) {
50
0
        goto exit;
51
0
    }
52
13.9k
    object = PyTuple_GET_ITEM(args, 0);
53
13.9k
    if (PyTuple_GET_SIZE(args) < 2) {
54
0
        goto skip_optional;
55
0
    }
56
13.9k
    callback = PyTuple_GET_ITEM(args, 1);
57
13.9k
skip_optional:
58
13.9k
    return_value = weakref_ref_new_impl(type, object, callback);
59
60
13.9k
exit:
61
13.9k
    return return_value;
62
13.9k
}
63
64
static PyObject *
65
weakref_ref_vectorcall(PyObject *type, PyObject *const *args,
66
    size_t nargsf, PyObject *kwnames)
67
211k
{
68
211k
    PyObject *return_value = NULL;
69
211k
    Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
70
211k
    PyObject *object;
71
211k
    PyObject *callback = NULL;
72
73
211k
    assert(_PyType_CAST(type)->tp_new == weakref_ref_new);
74
    /* Make sure the type object is immutable: the generated
75
     * vectorcall doesn't deal e.g. with users reassigning __init__. */
76
211k
    assert(PyType_HasFeature(_PyType_CAST(type), Py_TPFLAGS_IMMUTABLETYPE));
77
211k
    if (!_PyArg_NoKwnames("ReferenceType", kwnames)) {
78
0
        goto exit;
79
0
    }
80
211k
    if (!_PyArg_CheckPositional("ReferenceType", nargs, 1, 2)) {
81
0
        goto exit;
82
0
    }
83
211k
    object = args[0];
84
211k
    if (nargs < 2) {
85
97.6k
        goto skip_optional;
86
97.6k
    }
87
113k
    callback = args[1];
88
211k
skip_optional:
89
211k
    return_value = weakref_ref_new_impl(_PyType_CAST(type), object, callback);
90
91
211k
exit:
92
211k
    return return_value;
93
211k
}
94
95
static int
96
weakref_ref_init_impl(PyWeakReference *self, PyObject *object,
97
                      PyObject *callback);
98
99
static int
100
weakref_ref_init(PyObject *self, PyObject *args, PyObject *kwargs)
101
13.9k
{
102
13.9k
    int return_value = -1;
103
13.9k
    PyTypeObject *base_tp = &_PyWeakref_RefType;
104
13.9k
    PyObject *object;
105
13.9k
    PyObject *callback = Py_None;
106
107
13.9k
    if ((Py_IS_TYPE(self, base_tp) ||
108
13.9k
         Py_TYPE(self)->tp_new == base_tp->tp_new) &&
109
0
        !_PyArg_NoKeywords("ReferenceType", kwargs)) {
110
0
        goto exit;
111
0
    }
112
13.9k
    if (!_PyArg_CheckPositional("ReferenceType", PyTuple_GET_SIZE(args), 1, 2)) {
113
0
        goto exit;
114
0
    }
115
13.9k
    object = PyTuple_GET_ITEM(args, 0);
116
13.9k
    if (PyTuple_GET_SIZE(args) < 2) {
117
0
        goto skip_optional;
118
0
    }
119
13.9k
    callback = PyTuple_GET_ITEM(args, 1);
120
13.9k
skip_optional:
121
13.9k
    return_value = weakref_ref_init_impl((PyWeakReference *)self, object, callback);
122
123
13.9k
exit:
124
13.9k
    return return_value;
125
13.9k
}
126
/*[clinic end generated code: output=774282aa1017bab5 input=a9049054013a1b77]*/