Coverage Report

Created: 2026-12-01 06:09

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
151k
{
14
151k
    Py_hash_t return_value = -1;
15
16
151k
    Py_BEGIN_CRITICAL_SECTION(self);
17
151k
    return_value = weakref_hash_impl((PyWeakReference *)self);
18
151k
    Py_END_CRITICAL_SECTION();
19
20
151k
    return return_value;
21
151k
}
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
7.25k
{
40
7.25k
    PyObject *return_value = NULL;
41
7.25k
    PyTypeObject *base_tp = &_PyWeakref_RefType;
42
7.25k
    PyObject *object;
43
7.25k
    PyObject *callback = NULL;
44
45
7.25k
    if ((type == base_tp || type->tp_init == base_tp->tp_init) &&
46
0
        !_PyArg_NoKeywords("ReferenceType", kwargs)) {
47
0
        goto exit;
48
0
    }
49
7.25k
    if (!_PyArg_CheckPositional("ReferenceType", PyTuple_GET_SIZE(args), 1, 2)) {
50
0
        goto exit;
51
0
    }
52
7.25k
    object = PyTuple_GET_ITEM(args, 0);
53
7.25k
    if (PyTuple_GET_SIZE(args) < 2) {
54
0
        goto skip_optional;
55
0
    }
56
7.25k
    callback = PyTuple_GET_ITEM(args, 1);
57
7.25k
skip_optional:
58
7.25k
    return_value = weakref_ref_new_impl(type, object, callback);
59
60
7.25k
exit:
61
7.25k
    return return_value;
62
7.25k
}
63
64
static PyObject *
65
weakref_ref_vectorcall(PyObject *type, PyObject *const *args,
66
    size_t nargsf, PyObject *kwnames)
67
140k
{
68
140k
    PyObject *return_value = NULL;
69
140k
    Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
70
140k
    PyObject *object;
71
140k
    PyObject *callback = NULL;
72
73
140k
    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
140k
    assert(PyType_HasFeature(_PyType_CAST(type), Py_TPFLAGS_IMMUTABLETYPE));
77
140k
    if (!_PyArg_NoKwnames("ReferenceType", kwnames)) {
78
0
        goto exit;
79
0
    }
80
140k
    if (!_PyArg_CheckPositional("ReferenceType", nargs, 1, 2)) {
81
0
        goto exit;
82
0
    }
83
140k
    object = args[0];
84
140k
    if (nargs < 2) {
85
65.8k
        goto skip_optional;
86
65.8k
    }
87
74.7k
    callback = args[1];
88
140k
skip_optional:
89
140k
    return_value = weakref_ref_new_impl(_PyType_CAST(type), object, callback);
90
91
140k
exit:
92
140k
    return return_value;
93
140k
}
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
7.25k
{
102
7.25k
    int return_value = -1;
103
7.25k
    PyTypeObject *base_tp = &_PyWeakref_RefType;
104
7.25k
    PyObject *object;
105
7.25k
    PyObject *callback = Py_None;
106
107
7.25k
    if ((Py_IS_TYPE(self, base_tp) ||
108
7.25k
         Py_TYPE(self)->tp_new == base_tp->tp_new) &&
109
0
        !_PyArg_NoKeywords("ReferenceType", kwargs)) {
110
0
        goto exit;
111
0
    }
112
7.25k
    if (!_PyArg_CheckPositional("ReferenceType", PyTuple_GET_SIZE(args), 1, 2)) {
113
0
        goto exit;
114
0
    }
115
7.25k
    object = PyTuple_GET_ITEM(args, 0);
116
7.25k
    if (PyTuple_GET_SIZE(args) < 2) {
117
0
        goto skip_optional;
118
0
    }
119
7.25k
    callback = PyTuple_GET_ITEM(args, 1);
120
7.25k
skip_optional:
121
7.25k
    return_value = weakref_ref_init_impl((PyWeakReference *)self, object, callback);
122
123
7.25k
exit:
124
7.25k
    return return_value;
125
7.25k
}
126
/*[clinic end generated code: output=774282aa1017bab5 input=a9049054013a1b77]*/