Coverage Report

Created: 2026-11-30 07:19

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