Coverage Report

Created: 2026-10-31 06:21

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/cpython/Objects/descrobject.c
Line
Count
Source
1
/* Descriptors -- a new, flexible way to describe attributes */
2
3
#include "Python.h"
4
#include "pycore_abstract.h"      // _PyObject_RealIsSubclass()
5
#include "pycore_call.h"          // _PyStack_AsDict()
6
#include "pycore_ceval.h"         // _Py_EnterRecursiveCallTstate()
7
#include "pycore_wasm_trampoline.h" // descr_set_trampoline_call(), descr_get_trampoline_call()
8
#include "pycore_descrobject.h"   // _PyMethodWrapper_Type
9
#include "pycore_modsupport.h"    // _PyArg_UnpackStack()
10
#include "pycore_object.h"        // _PyObject_GC_UNTRACK()
11
#include "pycore_object_deferred.h" // _PyObject_SetDeferredRefcount()
12
#include "pycore_pystate.h"       // _PyThreadState_GET()
13
#include "pycore_tuple.h"         // _PyTuple_ITEMS()
14
15
16
/*[clinic input]
17
class mappingproxy "mappingproxyobject *" "&PyDictProxy_Type"
18
class property "propertyobject *" "&PyProperty_Type"
19
[clinic start generated code]*/
20
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=556352653fd4c02e]*/
21
22
#include "clinic/descrobject.c.h"
23
24
static void
25
descr_dealloc(PyObject *self)
26
1.66k
{
27
1.66k
    PyDescrObject *descr = (PyDescrObject *)self;
28
1.66k
    _PyObject_GC_UNTRACK(descr);
29
1.66k
    Py_XDECREF(descr->d_type);
30
1.66k
    Py_XDECREF(descr->d_name);
31
1.66k
    Py_XDECREF(descr->d_qualname);
32
1.66k
    PyObject_GC_Del(descr);
33
1.66k
}
34
35
static PyObject *
36
descr_name(PyDescrObject *descr)
37
0
{
38
0
    if (descr->d_name != NULL && PyUnicode_Check(descr->d_name))
39
0
        return descr->d_name;
40
0
    return NULL;
41
0
}
42
43
static PyObject *
44
descr_repr(PyDescrObject *descr, const char *kind)
45
0
{
46
0
    PyObject *name = NULL;
47
0
    if (descr->d_name != NULL && PyUnicode_Check(descr->d_name))
48
0
        name = descr->d_name;
49
50
0
    if (descr->d_type == &PyBaseObject_Type) {
51
0
        return PyUnicode_FromFormat("<%s '%V'>", kind, name, "?");
52
0
    }
53
0
    return PyUnicode_FromFormat("<%s '%V' of '%s' objects>",
54
0
                                kind, name, "?", descr->d_type->tp_name);
55
0
}
56
57
static PyObject *
58
method_repr(PyObject *descr)
59
0
{
60
0
    return descr_repr((PyDescrObject *)descr, "method");
61
0
}
62
63
static PyObject *
64
member_repr(PyObject *descr)
65
0
{
66
0
    return descr_repr((PyDescrObject *)descr, "member");
67
0
}
68
69
static PyObject *
70
getset_repr(PyObject *descr)
71
0
{
72
0
    return descr_repr((PyDescrObject *)descr, "attribute");
73
0
}
74
75
static PyObject *
76
wrapperdescr_repr(PyObject *descr)
77
0
{
78
0
    return descr_repr((PyDescrObject *)descr, "slot wrapper");
79
0
}
80
81
static int
82
descr_check(PyDescrObject *descr, PyObject *obj)
83
318M
{
84
318M
    if (!PyObject_TypeCheck(obj, descr->d_type)) {
85
0
        PyErr_Format(PyExc_TypeError,
86
0
                     "descriptor '%V' for '%.100s' objects "
87
0
                     "doesn't apply to a '%.100s' object",
88
0
                     descr_name((PyDescrObject *)descr), "?",
89
0
                     descr->d_type->tp_name,
90
0
                     Py_TYPE(obj)->tp_name);
91
0
        return -1;
92
0
    }
93
318M
    return 0;
94
318M
}
95
96
static PyObject *
97
classmethod_get(PyObject *self, PyObject *obj, PyObject *type)
98
16.2M
{
99
16.2M
    PyMethodDescrObject *descr = (PyMethodDescrObject *)self;
100
    /* Ensure a valid type.  Class methods ignore obj. */
101
16.2M
    if (type == NULL) {
102
0
        if (obj != NULL)
103
0
            type = (PyObject *)Py_TYPE(obj);
104
0
        else {
105
            /* Wot - no type?! */
106
0
            PyErr_Format(PyExc_TypeError,
107
0
                         "descriptor '%V' for type '%.100s' "
108
0
                         "needs either an object or a type",
109
0
                         descr_name((PyDescrObject *)descr), "?",
110
0
                         PyDescr_TYPE(descr)->tp_name);
111
0
            return NULL;
112
0
        }
113
0
    }
114
16.2M
    if (!PyType_Check(type)) {
115
0
        PyErr_Format(PyExc_TypeError,
116
0
                     "descriptor '%V' for type '%.100s' "
117
0
                     "needs a type, not a '%.100s' as arg 2",
118
0
                     descr_name((PyDescrObject *)descr), "?",
119
0
                     PyDescr_TYPE(descr)->tp_name,
120
0
                     Py_TYPE(type)->tp_name);
121
0
        return NULL;
122
0
    }
123
16.2M
    if (!PyType_IsSubtype((PyTypeObject *)type, PyDescr_TYPE(descr))) {
124
0
        PyErr_Format(PyExc_TypeError,
125
0
                     "descriptor '%V' requires a subtype of '%.100s' "
126
0
                     "but received '%.100s'",
127
0
                     descr_name((PyDescrObject *)descr), "?",
128
0
                     PyDescr_TYPE(descr)->tp_name,
129
0
                     ((PyTypeObject *)type)->tp_name);
130
0
        return NULL;
131
0
    }
132
16.2M
    PyTypeObject *cls = NULL;
133
16.2M
    if (descr->d_method->ml_flags & METH_METHOD) {
134
4.47k
        cls = descr->d_common.d_type;
135
4.47k
    }
136
16.2M
    return PyCMethod_New(descr->d_method, type, NULL, cls);
137
16.2M
}
138
139
static PyObject *
140
method_get(PyObject *self, PyObject *obj, PyObject *type)
141
155M
{
142
155M
    PyMethodDescrObject *descr = (PyMethodDescrObject *)self;
143
155M
    if (obj == NULL) {
144
29.5k
        return Py_NewRef(descr);
145
29.5k
    }
146
155M
    if (descr_check((PyDescrObject *)descr, obj) < 0) {
147
0
        return NULL;
148
0
    }
149
155M
    if (descr->d_method->ml_flags & METH_METHOD) {
150
11.0M
        if (type == NULL || PyType_Check(type)) {
151
11.0M
            return PyCMethod_New(descr->d_method, obj, NULL, descr->d_common.d_type);
152
11.0M
        } else {
153
0
            PyErr_Format(PyExc_TypeError,
154
0
                        "descriptor '%V' needs a type, not '%s', as arg 2",
155
0
                        descr_name((PyDescrObject *)descr), "?",
156
0
                        Py_TYPE(type)->tp_name);
157
0
            return NULL;
158
0
        }
159
144M
    } else {
160
144M
        return PyCFunction_NewEx(descr->d_method, obj, NULL);
161
144M
    }
162
155M
}
163
164
static PyObject *
165
member_get(PyObject *self, PyObject *obj, PyObject *type)
166
877k
{
167
877k
    PyMemberDescrObject *descr = (PyMemberDescrObject *)self;
168
877k
    if (obj == NULL) {
169
16
        return Py_NewRef(descr);
170
16
    }
171
877k
    if (descr_check((PyDescrObject *)descr, obj) < 0) {
172
0
        return NULL;
173
0
    }
174
175
877k
    if (descr->d_member->flags & Py_AUDIT_READ) {
176
0
        if (PySys_Audit("object.__getattr__", "Os",
177
0
            obj ? obj : Py_None, descr->d_member->name) < 0) {
178
0
            return NULL;
179
0
        }
180
0
    }
181
182
877k
    return PyMember_GetOne((char *)obj, descr->d_member);
183
877k
}
184
185
static PyObject *
186
getset_get(PyObject *self, PyObject *obj, PyObject *type)
187
74.3M
{
188
74.3M
    PyGetSetDescrObject *descr = (PyGetSetDescrObject *)self;
189
74.3M
    if (obj == NULL) {
190
2
        return Py_NewRef(descr);
191
2
    }
192
74.3M
    if (descr_check((PyDescrObject *)descr, obj) < 0) {
193
0
        return NULL;
194
0
    }
195
74.3M
    if (descr->d_getset->get != NULL)
196
74.3M
        return descr_get_trampoline_call(
197
74.3M
            descr->d_getset->get, obj, descr->d_getset->closure);
198
0
    PyErr_Format(PyExc_AttributeError,
199
0
                 "attribute '%V' of '%.100s' objects is not readable",
200
0
                 descr_name((PyDescrObject *)descr), "?",
201
0
                 PyDescr_TYPE(descr)->tp_name);
202
0
    return NULL;
203
74.3M
}
204
205
static PyObject *
206
wrapperdescr_get(PyObject *self, PyObject *obj, PyObject *type)
207
12.3M
{
208
12.3M
    PyWrapperDescrObject *descr = (PyWrapperDescrObject *)self;
209
12.3M
    if (obj == NULL) {
210
2.77k
        return Py_NewRef(descr);
211
2.77k
    }
212
12.3M
    if (descr_check((PyDescrObject *)descr, obj) < 0) {
213
0
        return NULL;
214
0
    }
215
12.3M
    return PyWrapper_New((PyObject *)descr, obj);
216
12.3M
}
217
218
static int
219
descr_setcheck(PyDescrObject *descr, PyObject *obj, PyObject *value)
220
14.0M
{
221
14.0M
    assert(obj != NULL);
222
14.0M
    if (!PyObject_TypeCheck(obj, descr->d_type)) {
223
0
        PyErr_Format(PyExc_TypeError,
224
0
                     "descriptor '%V' for '%.100s' objects "
225
0
                     "doesn't apply to a '%.100s' object",
226
0
                     descr_name(descr), "?",
227
0
                     descr->d_type->tp_name,
228
0
                     Py_TYPE(obj)->tp_name);
229
0
        return -1;
230
0
    }
231
14.0M
    return 0;
232
14.0M
}
233
234
static int
235
member_set(PyObject *self, PyObject *obj, PyObject *value)
236
3.55M
{
237
3.55M
    PyMemberDescrObject *descr = (PyMemberDescrObject *)self;
238
3.55M
    if (descr_setcheck((PyDescrObject *)descr, obj, value) < 0) {
239
0
        return -1;
240
0
    }
241
3.55M
    return PyMember_SetOne((char *)obj, descr->d_member, value);
242
3.55M
}
243
244
static int
245
getset_set(PyObject *self, PyObject *obj, PyObject *value)
246
10.5M
{
247
10.5M
    PyGetSetDescrObject *descr = (PyGetSetDescrObject *)self;
248
10.5M
    if (descr_setcheck((PyDescrObject *)descr, obj, value) < 0) {
249
0
        return -1;
250
0
    }
251
10.5M
    if (descr->d_getset->set != NULL) {
252
10.5M
        return descr_set_trampoline_call(
253
10.5M
            descr->d_getset->set, obj, value,
254
10.5M
            descr->d_getset->closure);
255
10.5M
    }
256
0
    PyErr_Format(PyExc_AttributeError,
257
0
                 "attribute '%V' of '%.100s' objects is not writable",
258
0
                 descr_name((PyDescrObject *)descr), "?",
259
0
                 PyDescr_TYPE(descr)->tp_name);
260
0
    return -1;
261
10.5M
}
262
263
264
/* Vectorcall functions for each of the PyMethodDescr calling conventions.
265
 *
266
 * First, common helpers
267
 */
268
static inline int
269
method_check_args(PyObject *func, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
270
75.2M
{
271
75.2M
    assert(!PyErr_Occurred());
272
75.2M
    if (nargs < 1) {
273
0
        PyObject *funcstr = _PyObject_FunctionStr(func);
274
0
        if (funcstr != NULL) {
275
0
            PyErr_Format(PyExc_TypeError,
276
0
                         "unbound method %U needs an argument", funcstr);
277
0
            Py_DECREF(funcstr);
278
0
        }
279
0
        return -1;
280
0
    }
281
75.2M
    PyObject *self = args[0];
282
75.2M
    if (descr_check((PyDescrObject *)func, self) < 0) {
283
0
        return -1;
284
0
    }
285
75.2M
    if (kwnames && PyTuple_GET_SIZE(kwnames)) {
286
0
        PyObject *funcstr = _PyObject_FunctionStr(func);
287
0
        if (funcstr != NULL) {
288
0
            PyErr_Format(PyExc_TypeError,
289
0
                         "%U takes no keyword arguments", funcstr);
290
0
            Py_DECREF(funcstr);
291
0
        }
292
0
        return -1;
293
0
    }
294
75.2M
    return 0;
295
75.2M
}
296
297
typedef void (*funcptr)(void);
298
299
static inline funcptr
300
method_enter_call(PyThreadState *tstate, PyObject *func)
301
75.2M
{
302
75.2M
    if (_Py_EnterRecursiveCallTstate(tstate, " while calling a Python object")) {
303
0
        return NULL;
304
0
    }
305
75.2M
    return (funcptr)((PyMethodDescrObject *)func)->d_method->ml_meth;
306
75.2M
}
307
308
/* Now the actual vectorcall functions */
309
static PyObject *
310
method_vectorcall_VARARGS(
311
    PyObject *func, PyObject *const *args, size_t nargsf, PyObject *kwnames)
312
0
{
313
0
    PyThreadState *tstate = _PyThreadState_GET();
314
0
    Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
315
0
    if (method_check_args(func, args, nargs, kwnames)) {
316
0
        return NULL;
317
0
    }
318
0
    PyObject *argstuple = PyTuple_FromArray(args+1, nargs-1);
319
0
    if (argstuple == NULL) {
320
0
        return NULL;
321
0
    }
322
0
    PyCFunction meth = (PyCFunction)method_enter_call(tstate, func);
323
0
    if (meth == NULL) {
324
0
        Py_DECREF(argstuple);
325
0
        return NULL;
326
0
    }
327
0
    PyObject *result = _PyCFunction_TrampolineCall(
328
0
        meth, args[0], argstuple);
329
0
    Py_DECREF(argstuple);
330
0
    _Py_LeaveRecursiveCallTstate(tstate);
331
0
    return result;
332
0
}
333
334
static PyObject *
335
method_vectorcall_VARARGS_KEYWORDS(
336
    PyObject *func, PyObject *const *args, size_t nargsf, PyObject *kwnames)
337
9.63M
{
338
9.63M
    PyThreadState *tstate = _PyThreadState_GET();
339
9.63M
    Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
340
9.63M
    if (method_check_args(func, args, nargs, NULL)) {
341
0
        return NULL;
342
0
    }
343
9.63M
    PyObject *argstuple = PyTuple_FromArray(args+1, nargs-1);
344
9.63M
    if (argstuple == NULL) {
345
0
        return NULL;
346
0
    }
347
9.63M
    PyObject *result = NULL;
348
    /* Create a temporary dict for keyword arguments */
349
9.63M
    PyObject *kwdict = NULL;
350
9.63M
    if (kwnames != NULL && PyTuple_GET_SIZE(kwnames) > 0) {
351
342
        kwdict = _PyStack_AsDict(args + nargs, kwnames);
352
342
        if (kwdict == NULL) {
353
0
            goto exit;
354
0
        }
355
342
    }
356
9.63M
    PyCFunctionWithKeywords meth = (PyCFunctionWithKeywords)
357
9.63M
                                   method_enter_call(tstate, func);
358
9.63M
    if (meth == NULL) {
359
0
        goto exit;
360
0
    }
361
9.63M
    result = _PyCFunctionWithKeywords_TrampolineCall(
362
9.63M
        meth, args[0], argstuple, kwdict);
363
9.63M
    _Py_LeaveRecursiveCallTstate(tstate);
364
9.63M
exit:
365
9.63M
    Py_DECREF(argstuple);
366
9.63M
    Py_XDECREF(kwdict);
367
9.63M
    return result;
368
9.63M
}
369
370
static PyObject *
371
method_vectorcall_FASTCALL_KEYWORDS_METHOD(
372
    PyObject *func, PyObject *const *args, size_t nargsf, PyObject *kwnames)
373
35.3M
{
374
35.3M
    PyThreadState *tstate = _PyThreadState_GET();
375
35.3M
    Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
376
35.3M
    if (method_check_args(func, args, nargs, NULL)) {
377
0
        return NULL;
378
0
    }
379
35.3M
    PyCMethod meth = (PyCMethod) method_enter_call(tstate, func);
380
35.3M
    if (meth == NULL) {
381
0
        return NULL;
382
0
    }
383
35.3M
    PyObject *result = meth(args[0],
384
35.3M
                            ((PyMethodDescrObject *)func)->d_common.d_type,
385
35.3M
                            args+1, nargs-1, kwnames);
386
35.3M
    _Py_LeaveRecursiveCall();
387
35.3M
    return result;
388
35.3M
}
389
390
static PyObject *
391
method_vectorcall_FASTCALL(
392
    PyObject *func, PyObject *const *args, size_t nargsf, PyObject *kwnames)
393
114k
{
394
114k
    PyThreadState *tstate = _PyThreadState_GET();
395
114k
    Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
396
114k
    if (method_check_args(func, args, nargs, kwnames)) {
397
0
        return NULL;
398
0
    }
399
114k
    PyCFunctionFast meth = (PyCFunctionFast)
400
114k
                            method_enter_call(tstate, func);
401
114k
    if (meth == NULL) {
402
0
        return NULL;
403
0
    }
404
114k
    PyObject *result = meth(args[0], args+1, nargs-1);
405
114k
    _Py_LeaveRecursiveCallTstate(tstate);
406
114k
    return result;
407
114k
}
408
409
static PyObject *
410
method_vectorcall_FASTCALL_KEYWORDS(
411
    PyObject *func, PyObject *const *args, size_t nargsf, PyObject *kwnames)
412
4.48M
{
413
4.48M
    PyThreadState *tstate = _PyThreadState_GET();
414
4.48M
    Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
415
4.48M
    if (method_check_args(func, args, nargs, NULL)) {
416
0
        return NULL;
417
0
    }
418
4.48M
    PyCFunctionFastWithKeywords meth = (PyCFunctionFastWithKeywords)
419
4.48M
                                        method_enter_call(tstate, func);
420
4.48M
    if (meth == NULL) {
421
0
        return NULL;
422
0
    }
423
4.48M
    PyObject *result = meth(args[0], args+1, nargs-1, kwnames);
424
4.48M
    _Py_LeaveRecursiveCallTstate(tstate);
425
4.48M
    return result;
426
4.48M
}
427
428
static PyObject *
429
method_vectorcall_NOARGS(
430
    PyObject *func, PyObject *const *args, size_t nargsf, PyObject *kwnames)
431
3.50M
{
432
3.50M
    PyThreadState *tstate = _PyThreadState_GET();
433
3.50M
    Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
434
3.50M
    if (method_check_args(func, args, nargs, kwnames)) {
435
0
        return NULL;
436
0
    }
437
3.50M
    if (nargs != 1) {
438
0
        PyObject *funcstr = _PyObject_FunctionStr(func);
439
0
        if (funcstr != NULL) {
440
0
            PyErr_Format(PyExc_TypeError,
441
0
                "%U takes no arguments (%zd given)", funcstr, nargs-1);
442
0
            Py_DECREF(funcstr);
443
0
        }
444
0
        return NULL;
445
0
    }
446
3.50M
    PyCFunction meth = (PyCFunction)method_enter_call(tstate, func);
447
3.50M
    if (meth == NULL) {
448
0
        return NULL;
449
0
    }
450
3.50M
    PyObject *result = _PyCFunction_TrampolineCall(meth, args[0], NULL);
451
3.50M
    _Py_LeaveRecursiveCallTstate(tstate);
452
3.50M
    return result;
453
3.50M
}
454
455
static PyObject *
456
method_vectorcall_O(
457
    PyObject *func, PyObject *const *args, size_t nargsf, PyObject *kwnames)
458
22.1M
{
459
22.1M
    PyThreadState *tstate = _PyThreadState_GET();
460
22.1M
    Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
461
22.1M
    if (method_check_args(func, args, nargs, kwnames)) {
462
0
        return NULL;
463
0
    }
464
22.1M
    if (nargs != 2) {
465
0
        PyObject *funcstr = _PyObject_FunctionStr(func);
466
0
        if (funcstr != NULL) {
467
0
            PyErr_Format(PyExc_TypeError,
468
0
                "%U takes exactly one argument (%zd given)",
469
0
                funcstr, nargs-1);
470
0
            Py_DECREF(funcstr);
471
0
        }
472
0
        return NULL;
473
0
    }
474
22.1M
    PyCFunction meth = (PyCFunction)method_enter_call(tstate, func);
475
22.1M
    if (meth == NULL) {
476
0
        return NULL;
477
0
    }
478
22.1M
    PyObject *result = _PyCFunction_TrampolineCall(meth, args[0], args[1]);
479
22.1M
    _Py_LeaveRecursiveCallTstate(tstate);
480
22.1M
    return result;
481
22.1M
}
482
483
484
/* Instances of classmethod_descriptor are unlikely to be called directly.
485
   For one, the analogous class "classmethod" (for Python classes) is not
486
   callable. Second, users are not likely to access a classmethod_descriptor
487
   directly, since it means pulling it from the class __dict__.
488
489
   This is just an excuse to say that this doesn't need to be optimized:
490
   we implement this simply by calling __get__ and then calling the result.
491
*/
492
static PyObject *
493
classmethoddescr_call(PyObject *_descr, PyObject *args,
494
                      PyObject *kwds)
495
0
{
496
0
    PyMethodDescrObject *descr = (PyMethodDescrObject *)_descr;
497
0
    Py_ssize_t argc = PyTuple_GET_SIZE(args);
498
0
    if (argc < 1) {
499
0
        PyErr_Format(PyExc_TypeError,
500
0
                     "descriptor '%V' of '%.100s' "
501
0
                     "object needs an argument",
502
0
                     descr_name((PyDescrObject *)descr), "?",
503
0
                     PyDescr_TYPE(descr)->tp_name);
504
0
        return NULL;
505
0
    }
506
0
    PyObject *self = PyTuple_GET_ITEM(args, 0);
507
0
    PyObject *bound = classmethod_get((PyObject *)descr, NULL, self);
508
0
    if (bound == NULL) {
509
0
        return NULL;
510
0
    }
511
0
    PyObject *res = PyObject_VectorcallDict(bound, _PyTuple_ITEMS(args)+1,
512
0
                                           argc-1, kwds);
513
0
    Py_DECREF(bound);
514
0
    return res;
515
0
}
516
517
Py_LOCAL_INLINE(PyObject *)
518
wrapperdescr_raw_call(PyWrapperDescrObject *descr, PyObject *self,
519
                      PyObject *args, PyObject *kwds)
520
30.7M
{
521
30.7M
    wrapperfunc wrapper = descr->d_base->wrapper;
522
523
30.7M
    if (descr->d_base->flags & PyWrapperFlag_KEYWORDS) {
524
22.5M
        wrapperfunc_kwds wk = _Py_FUNC_CAST(wrapperfunc_kwds, wrapper);
525
22.5M
        return (*wk)(self, args, descr->d_wrapped, kwds);
526
22.5M
    }
527
528
8.27M
    if (kwds != NULL && (!PyDict_Check(kwds) || PyDict_GET_SIZE(kwds) != 0)) {
529
0
        PyErr_Format(PyExc_TypeError,
530
0
                     "wrapper %s() takes no keyword arguments",
531
0
                     descr->d_base->name);
532
0
        return NULL;
533
0
    }
534
8.27M
    return (*wrapper)(self, args, descr->d_wrapped);
535
8.27M
}
536
537
static PyObject *
538
wrapperdescr_call(PyObject *_descr, PyObject *args, PyObject *kwds)
539
18.3M
{
540
18.3M
    PyWrapperDescrObject *descr = (PyWrapperDescrObject *)_descr;
541
18.3M
    Py_ssize_t argc;
542
18.3M
    PyObject *self, *result;
543
544
    /* Make sure that the first argument is acceptable as 'self' */
545
18.3M
    assert(PyTuple_Check(args));
546
18.3M
    argc = PyTuple_GET_SIZE(args);
547
18.3M
    if (argc < 1) {
548
0
        PyErr_Format(PyExc_TypeError,
549
0
                     "descriptor '%V' of '%.100s' "
550
0
                     "object needs an argument",
551
0
                     descr_name((PyDescrObject *)descr), "?",
552
0
                     PyDescr_TYPE(descr)->tp_name);
553
0
        return NULL;
554
0
    }
555
18.3M
    self = PyTuple_GET_ITEM(args, 0);
556
18.3M
    if (!_PyObject_RealIsSubclass((PyObject *)Py_TYPE(self),
557
18.3M
                                  (PyObject *)PyDescr_TYPE(descr))) {
558
0
        PyErr_Format(PyExc_TypeError,
559
0
                     "descriptor '%V' "
560
0
                     "requires a '%.100s' object "
561
0
                     "but received a '%.100s'",
562
0
                     descr_name((PyDescrObject *)descr), "?",
563
0
                     PyDescr_TYPE(descr)->tp_name,
564
0
                     Py_TYPE(self)->tp_name);
565
0
        return NULL;
566
0
    }
567
568
18.3M
    args = PyTuple_GetSlice(args, 1, argc);
569
18.3M
    if (args == NULL) {
570
0
        return NULL;
571
0
    }
572
18.3M
    result = wrapperdescr_raw_call(descr, self, args, kwds);
573
18.3M
    Py_DECREF(args);
574
18.3M
    return result;
575
18.3M
}
576
577
578
static PyObject *
579
method_get_doc(PyObject *_descr, void *closure)
580
0
{
581
0
    PyMethodDescrObject *descr = (PyMethodDescrObject *)_descr;
582
0
    return _PyType_GetDocFromInternalDoc(descr->d_method->ml_name, descr->d_method->ml_doc);
583
0
}
584
585
static PyObject *
586
method_get_text_signature(PyObject *_descr, void *closure)
587
0
{
588
0
    PyMethodDescrObject *descr = (PyMethodDescrObject *)_descr;
589
0
    return _PyType_GetTextSignatureFromInternalDoc(descr->d_method->ml_name,
590
0
                                                   descr->d_method->ml_doc,
591
0
                                                   descr->d_method->ml_flags);
592
0
}
593
594
static PyObject *
595
calculate_qualname(PyDescrObject *descr)
596
0
{
597
0
    PyObject *type_qualname, *res;
598
599
0
    if (descr->d_name == NULL || !PyUnicode_Check(descr->d_name)) {
600
0
        PyErr_SetString(PyExc_TypeError,
601
0
                        "<descriptor>.__name__ is not a unicode object");
602
0
        return NULL;
603
0
    }
604
605
0
    type_qualname = PyObject_GetAttr(
606
0
            (PyObject *)descr->d_type, &_Py_ID(__qualname__));
607
0
    if (type_qualname == NULL)
608
0
        return NULL;
609
610
0
    if (!PyUnicode_Check(type_qualname)) {
611
0
        PyErr_SetString(PyExc_TypeError, "<descriptor>.__objclass__."
612
0
                        "__qualname__ is not a unicode object");
613
0
        Py_XDECREF(type_qualname);
614
0
        return NULL;
615
0
    }
616
617
0
    res = PyUnicode_FromFormat("%S.%S", type_qualname, descr->d_name);
618
0
    Py_DECREF(type_qualname);
619
0
    return res;
620
0
}
621
622
static PyObject *
623
descr_get_qualname(PyObject *self, void *Py_UNUSED(ignored))
624
0
{
625
0
    PyDescrObject *descr = (PyDescrObject *)self;
626
0
    PyObject *qualname;
627
0
    Py_BEGIN_CRITICAL_SECTION(self);
628
0
    if (descr->d_qualname == NULL) {
629
0
        PyObject *new_qualname = calculate_qualname(descr);
630
0
        if (new_qualname != NULL) {
631
0
            Py_XSETREF(descr->d_qualname, new_qualname);
632
0
        }
633
0
    }
634
0
    qualname = Py_XNewRef(descr->d_qualname);
635
0
    Py_END_CRITICAL_SECTION();
636
0
    return qualname;
637
0
}
638
639
static PyObject *
640
descr_reduce(PyObject *self, PyObject *Py_UNUSED(ignored))
641
0
{
642
0
    PyDescrObject *descr = (PyDescrObject *)self;
643
0
    return Py_BuildValue("N(OO)", _PyEval_GetBuiltin(&_Py_ID(getattr)),
644
0
                         PyDescr_TYPE(descr), PyDescr_NAME(descr));
645
0
}
646
647
static PyMethodDef descr_methods[] = {
648
    {"__reduce__", descr_reduce, METH_NOARGS, NULL},
649
    {NULL, NULL}
650
};
651
652
static PyMemberDef descr_members[] = {
653
    {"__objclass__", _Py_T_OBJECT, offsetof(PyDescrObject, d_type), Py_READONLY},
654
    {"__name__", _Py_T_OBJECT, offsetof(PyDescrObject, d_name), Py_READONLY},
655
    {0}
656
};
657
658
static PyGetSetDef method_getset[] = {
659
    {"__doc__", method_get_doc},
660
    {"__qualname__", descr_get_qualname},
661
    {"__text_signature__", method_get_text_signature},
662
    {0}
663
};
664
665
static PyObject *
666
member_get_doc(PyObject *_descr, void *closure)
667
0
{
668
0
    PyMemberDescrObject *descr = (PyMemberDescrObject *)_descr;
669
0
    if (descr->d_member->doc == NULL) {
670
0
        Py_RETURN_NONE;
671
0
    }
672
0
    return PyUnicode_FromString(descr->d_member->doc);
673
0
}
674
675
static PyGetSetDef member_getset[] = {
676
    {"__doc__", member_get_doc},
677
    {"__qualname__", descr_get_qualname},
678
    {0}
679
};
680
681
static PyObject *
682
getset_get_doc(PyObject *self, void *closure)
683
0
{
684
0
    PyGetSetDescrObject *descr = (PyGetSetDescrObject *)self;
685
0
    if (descr->d_getset->doc == NULL) {
686
0
        Py_RETURN_NONE;
687
0
    }
688
0
    return PyUnicode_FromString(descr->d_getset->doc);
689
0
}
690
691
static PyGetSetDef getset_getset[] = {
692
    {"__doc__", getset_get_doc},
693
    {"__qualname__", descr_get_qualname},
694
    {0}
695
};
696
697
static PyObject *
698
wrapperdescr_get_doc(PyObject *self, void *closure)
699
0
{
700
0
    PyWrapperDescrObject *descr = (PyWrapperDescrObject *)self;
701
0
    return _PyType_GetDocFromInternalDoc(descr->d_base->name, descr->d_base->doc);
702
0
}
703
704
static PyObject *
705
wrapperdescr_get_text_signature(PyObject *self, void *closure)
706
0
{
707
0
    PyWrapperDescrObject *descr = (PyWrapperDescrObject *)self;
708
0
    return _PyType_GetTextSignatureFromInternalDoc(descr->d_base->name,
709
0
                                                   descr->d_base->doc, 0);
710
0
}
711
712
static PyGetSetDef wrapperdescr_getset[] = {
713
    {"__doc__", wrapperdescr_get_doc},
714
    {"__qualname__", descr_get_qualname},
715
    {"__text_signature__", wrapperdescr_get_text_signature},
716
    {0}
717
};
718
719
static int
720
descr_traverse(PyObject *self, visitproc visit, void *arg)
721
4.41M
{
722
4.41M
    PyDescrObject *descr = (PyDescrObject *)self;
723
4.41M
    Py_VISIT(descr->d_type);
724
4.41M
    return 0;
725
4.41M
}
726
727
PyTypeObject PyMethodDescr_Type = {
728
    PyVarObject_HEAD_INIT(&PyType_Type, 0)
729
    "method_descriptor",
730
    sizeof(PyMethodDescrObject),
731
    0,
732
    descr_dealloc,                              /* tp_dealloc */
733
    offsetof(PyMethodDescrObject, vectorcall),  /* tp_vectorcall_offset */
734
    0,                                          /* tp_getattr */
735
    0,                                          /* tp_setattr */
736
    0,                                          /* tp_as_async */
737
    method_repr,                                /* tp_repr */
738
    0,                                          /* tp_as_number */
739
    0,                                          /* tp_as_sequence */
740
    0,                                          /* tp_as_mapping */
741
    0,                                          /* tp_hash */
742
    PyVectorcall_Call,                          /* tp_call */
743
    0,                                          /* tp_str */
744
    PyObject_GenericGetAttr,                    /* tp_getattro */
745
    0,                                          /* tp_setattro */
746
    0,                                          /* tp_as_buffer */
747
    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
748
    Py_TPFLAGS_HAVE_VECTORCALL |
749
    Py_TPFLAGS_METHOD_DESCRIPTOR,               /* tp_flags */
750
    0,                                          /* tp_doc */
751
    descr_traverse,                             /* tp_traverse */
752
    0,                                          /* tp_clear */
753
    0,                                          /* tp_richcompare */
754
    0,                                          /* tp_weaklistoffset */
755
    0,                                          /* tp_iter */
756
    0,                                          /* tp_iternext */
757
    descr_methods,                              /* tp_methods */
758
    descr_members,                              /* tp_members */
759
    method_getset,                              /* tp_getset */
760
    0,                                          /* tp_base */
761
    0,                                          /* tp_dict */
762
    method_get,                                 /* tp_descr_get */
763
    0,                                          /* tp_descr_set */
764
};
765
766
/* This is for METH_CLASS in C, not for "f = classmethod(f)" in Python! */
767
PyTypeObject PyClassMethodDescr_Type = {
768
    PyVarObject_HEAD_INIT(&PyType_Type, 0)
769
    "classmethod_descriptor",
770
    sizeof(PyMethodDescrObject),
771
    0,
772
    descr_dealloc,                              /* tp_dealloc */
773
    0,                                          /* tp_vectorcall_offset */
774
    0,                                          /* tp_getattr */
775
    0,                                          /* tp_setattr */
776
    0,                                          /* tp_as_async */
777
    method_repr,                                /* tp_repr */
778
    0,                                          /* tp_as_number */
779
    0,                                          /* tp_as_sequence */
780
    0,                                          /* tp_as_mapping */
781
    0,                                          /* tp_hash */
782
    classmethoddescr_call,                      /* tp_call */
783
    0,                                          /* tp_str */
784
    PyObject_GenericGetAttr,                    /* tp_getattro */
785
    0,                                          /* tp_setattro */
786
    0,                                          /* tp_as_buffer */
787
    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
788
    0,                                          /* tp_doc */
789
    descr_traverse,                             /* tp_traverse */
790
    0,                                          /* tp_clear */
791
    0,                                          /* tp_richcompare */
792
    0,                                          /* tp_weaklistoffset */
793
    0,                                          /* tp_iter */
794
    0,                                          /* tp_iternext */
795
    0,                                          /* tp_methods */
796
    descr_members,                              /* tp_members */
797
    method_getset,                              /* tp_getset */
798
    0,                                          /* tp_base */
799
    0,                                          /* tp_dict */
800
    classmethod_get,                            /* tp_descr_get */
801
    0,                                          /* tp_descr_set */
802
};
803
804
PyTypeObject PyMemberDescr_Type = {
805
    PyVarObject_HEAD_INIT(&PyType_Type, 0)
806
    "member_descriptor",
807
    sizeof(PyMemberDescrObject),
808
    0,
809
    descr_dealloc,                              /* tp_dealloc */
810
    0,                                          /* tp_vectorcall_offset */
811
    0,                                          /* tp_getattr */
812
    0,                                          /* tp_setattr */
813
    0,                                          /* tp_as_async */
814
    member_repr,                                /* tp_repr */
815
    0,                                          /* tp_as_number */
816
    0,                                          /* tp_as_sequence */
817
    0,                                          /* tp_as_mapping */
818
    0,                                          /* tp_hash */
819
    0,                                          /* tp_call */
820
    0,                                          /* tp_str */
821
    PyObject_GenericGetAttr,                    /* tp_getattro */
822
    0,                                          /* tp_setattro */
823
    0,                                          /* tp_as_buffer */
824
    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
825
    0,                                          /* tp_doc */
826
    descr_traverse,                             /* tp_traverse */
827
    0,                                          /* tp_clear */
828
    0,                                          /* tp_richcompare */
829
    0,                                          /* tp_weaklistoffset */
830
    0,                                          /* tp_iter */
831
    0,                                          /* tp_iternext */
832
    descr_methods,                              /* tp_methods */
833
    descr_members,                              /* tp_members */
834
    member_getset,                              /* tp_getset */
835
    0,                                          /* tp_base */
836
    0,                                          /* tp_dict */
837
    member_get,                                 /* tp_descr_get */
838
    member_set,                                 /* tp_descr_set */
839
};
840
841
PyTypeObject PyGetSetDescr_Type = {
842
    PyVarObject_HEAD_INIT(&PyType_Type, 0)
843
    "getset_descriptor",
844
    sizeof(PyGetSetDescrObject),
845
    0,
846
    descr_dealloc,                              /* tp_dealloc */
847
    0,                                          /* tp_vectorcall_offset */
848
    0,                                          /* tp_getattr */
849
    0,                                          /* tp_setattr */
850
    0,                                          /* tp_as_async */
851
    getset_repr,                                /* tp_repr */
852
    0,                                          /* tp_as_number */
853
    0,                                          /* tp_as_sequence */
854
    0,                                          /* tp_as_mapping */
855
    0,                                          /* tp_hash */
856
    0,                                          /* tp_call */
857
    0,                                          /* tp_str */
858
    PyObject_GenericGetAttr,                    /* tp_getattro */
859
    0,                                          /* tp_setattro */
860
    0,                                          /* tp_as_buffer */
861
    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
862
    0,                                          /* tp_doc */
863
    descr_traverse,                             /* tp_traverse */
864
    0,                                          /* tp_clear */
865
    0,                                          /* tp_richcompare */
866
    0,                                          /* tp_weaklistoffset */
867
    0,                                          /* tp_iter */
868
    0,                                          /* tp_iternext */
869
    0,                                          /* tp_methods */
870
    descr_members,                              /* tp_members */
871
    getset_getset,                              /* tp_getset */
872
    0,                                          /* tp_base */
873
    0,                                          /* tp_dict */
874
    getset_get,                                 /* tp_descr_get */
875
    getset_set,                                 /* tp_descr_set */
876
};
877
878
PyTypeObject PyWrapperDescr_Type = {
879
    PyVarObject_HEAD_INIT(&PyType_Type, 0)
880
    "wrapper_descriptor",
881
    sizeof(PyWrapperDescrObject),
882
    0,
883
    descr_dealloc,                              /* tp_dealloc */
884
    0,                                          /* tp_vectorcall_offset */
885
    0,                                          /* tp_getattr */
886
    0,                                          /* tp_setattr */
887
    0,                                          /* tp_as_async */
888
    wrapperdescr_repr,                          /* tp_repr */
889
    0,                                          /* tp_as_number */
890
    0,                                          /* tp_as_sequence */
891
    0,                                          /* tp_as_mapping */
892
    0,                                          /* tp_hash */
893
    wrapperdescr_call,                          /* tp_call */
894
    0,                                          /* tp_str */
895
    PyObject_GenericGetAttr,                    /* tp_getattro */
896
    0,                                          /* tp_setattro */
897
    0,                                          /* tp_as_buffer */
898
    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
899
    Py_TPFLAGS_METHOD_DESCRIPTOR,               /* tp_flags */
900
    0,                                          /* tp_doc */
901
    descr_traverse,                             /* tp_traverse */
902
    0,                                          /* tp_clear */
903
    0,                                          /* tp_richcompare */
904
    0,                                          /* tp_weaklistoffset */
905
    0,                                          /* tp_iter */
906
    0,                                          /* tp_iternext */
907
    descr_methods,                              /* tp_methods */
908
    descr_members,                              /* tp_members */
909
    wrapperdescr_getset,                        /* tp_getset */
910
    0,                                          /* tp_base */
911
    0,                                          /* tp_dict */
912
    wrapperdescr_get,                           /* tp_descr_get */
913
    0,                                          /* tp_descr_set */
914
};
915
916
static PyDescrObject *
917
descr_new(PyTypeObject *descrtype, PyTypeObject *type, const char *name)
918
100k
{
919
100k
    PyDescrObject *descr;
920
921
100k
    descr = (PyDescrObject *)PyType_GenericAlloc(descrtype, 0);
922
100k
    if (descr != NULL) {
923
100k
        _PyObject_SetDeferredRefcount((PyObject *)descr);
924
100k
        descr->d_type = (PyTypeObject*)Py_XNewRef(type);
925
100k
        descr->d_name = PyUnicode_InternFromString(name);
926
100k
        if (descr->d_name == NULL) {
927
0
            Py_SETREF(descr, NULL);
928
0
        }
929
100k
        else {
930
100k
            descr->d_qualname = NULL;
931
100k
        }
932
100k
    }
933
100k
    return descr;
934
100k
}
935
936
PyObject *
937
PyDescr_NewMethod(PyTypeObject *type, PyMethodDef *method)
938
31.8k
{
939
    /* Figure out correct vectorcall function to use */
940
31.8k
    vectorcallfunc vectorcall;
941
31.8k
    switch (method->ml_flags & _Py_METH_CALL_FLAGS)
942
31.8k
    {
943
41
        case METH_VARARGS:
944
41
            vectorcall = method_vectorcall_VARARGS;
945
41
            break;
946
885
        case METH_VARARGS | METH_KEYWORDS:
947
885
            vectorcall = method_vectorcall_VARARGS_KEYWORDS;
948
885
            break;
949
4.79k
        case METH_FASTCALL:
950
4.79k
            vectorcall = method_vectorcall_FASTCALL;
951
4.79k
            break;
952
2.25k
        case METH_FASTCALL | METH_KEYWORDS:
953
2.25k
            vectorcall = method_vectorcall_FASTCALL_KEYWORDS;
954
2.25k
            break;
955
16.3k
        case METH_NOARGS:
956
16.3k
            vectorcall = method_vectorcall_NOARGS;
957
16.3k
            break;
958
6.11k
        case METH_O:
959
6.11k
            vectorcall = method_vectorcall_O;
960
6.11k
            break;
961
1.45k
        case METH_METHOD | METH_FASTCALL | METH_KEYWORDS:
962
1.45k
            vectorcall = method_vectorcall_FASTCALL_KEYWORDS_METHOD;
963
1.45k
            break;
964
0
        default:
965
0
            PyErr_Format(PyExc_SystemError,
966
0
                         "%s() method: bad call flags", method->ml_name);
967
0
            return NULL;
968
31.8k
    }
969
970
31.8k
    PyMethodDescrObject *descr;
971
972
31.8k
    descr = (PyMethodDescrObject *)descr_new(&PyMethodDescr_Type,
973
31.8k
                                             type, method->ml_name);
974
31.8k
    if (descr != NULL) {
975
31.8k
        descr->d_method = method;
976
31.8k
        descr->vectorcall = vectorcall;
977
31.8k
    }
978
31.8k
    return (PyObject *)descr;
979
31.8k
}
980
981
PyObject *
982
PyDescr_NewClassMethod(PyTypeObject *type, PyMethodDef *method)
983
2.25k
{
984
2.25k
    PyMethodDescrObject *descr;
985
986
2.25k
    descr = (PyMethodDescrObject *)descr_new(&PyClassMethodDescr_Type,
987
2.25k
                                             type, method->ml_name);
988
2.25k
    if (descr != NULL)
989
2.25k
        descr->d_method = method;
990
2.25k
    return (PyObject *)descr;
991
2.25k
}
992
993
PyObject *
994
PyDescr_NewMember(PyTypeObject *type, PyMemberDef *member)
995
15.3k
{
996
15.3k
    PyMemberDescrObject *descr;
997
998
15.3k
    if (member->flags & Py_RELATIVE_OFFSET) {
999
0
        PyErr_SetString(
1000
0
            PyExc_SystemError,
1001
0
            "PyDescr_NewMember used with Py_RELATIVE_OFFSET");
1002
0
        return NULL;
1003
0
    }
1004
15.3k
    descr = (PyMemberDescrObject *)descr_new(&PyMemberDescr_Type,
1005
15.3k
                                             type, member->name);
1006
15.3k
    if (descr != NULL)
1007
15.3k
        descr->d_member = member;
1008
15.3k
    return (PyObject *)descr;
1009
15.3k
}
1010
1011
PyObject *
1012
PyDescr_NewGetSet(PyTypeObject *type, PyGetSetDef *getset)
1013
9.03k
{
1014
9.03k
    PyGetSetDescrObject *descr;
1015
1016
9.03k
    descr = (PyGetSetDescrObject *)descr_new(&PyGetSetDescr_Type,
1017
9.03k
                                             type, getset->name);
1018
9.03k
    if (descr != NULL)
1019
9.03k
        descr->d_getset = getset;
1020
9.03k
    return (PyObject *)descr;
1021
9.03k
}
1022
1023
PyObject *
1024
PyDescr_NewWrapper(PyTypeObject *type, struct wrapperbase *base, void *wrapped)
1025
42.3k
{
1026
42.3k
    PyWrapperDescrObject *descr;
1027
1028
42.3k
    descr = (PyWrapperDescrObject *)descr_new(&PyWrapperDescr_Type,
1029
42.3k
                                             type, base->name);
1030
42.3k
    if (descr != NULL) {
1031
42.3k
        descr->d_base = base;
1032
42.3k
        descr->d_wrapped = wrapped;
1033
42.3k
    }
1034
42.3k
    return (PyObject *)descr;
1035
42.3k
}
1036
1037
int
1038
PyDescr_IsData(PyObject *ob)
1039
212M
{
1040
212M
    return Py_TYPE(ob)->tp_descr_set != NULL;
1041
212M
}
1042
1043
/* --- mappingproxy: read-only proxy for mappings --- */
1044
1045
/* This has no reason to be in this file except that adding new files is a
1046
   bit of a pain */
1047
1048
typedef struct {
1049
    PyObject_HEAD
1050
    PyObject *mapping;
1051
} mappingproxyobject;
1052
1053
static Py_ssize_t
1054
mappingproxy_len(PyObject *self)
1055
112
{
1056
112
    mappingproxyobject *pp = (mappingproxyobject *)self;
1057
112
    return PyObject_Size(pp->mapping);
1058
112
}
1059
1060
static PyObject *
1061
mappingproxy_getitem(PyObject *self, PyObject *key)
1062
1.24M
{
1063
1.24M
    mappingproxyobject *pp = (mappingproxyobject *)self;
1064
1.24M
    return PyObject_GetItem(pp->mapping, key);
1065
1.24M
}
1066
1067
static PyMappingMethods mappingproxy_as_mapping = {
1068
    mappingproxy_len,                           /* mp_length */
1069
    mappingproxy_getitem,                       /* mp_subscript */
1070
    0,                                          /* mp_ass_subscript */
1071
};
1072
1073
static PyObject *
1074
mappingproxy_or(PyObject *left, PyObject *right)
1075
0
{
1076
0
    if (PyObject_TypeCheck(left, &PyDictProxy_Type)) {
1077
0
        left = ((mappingproxyobject*)left)->mapping;
1078
0
    }
1079
0
    if (PyObject_TypeCheck(right, &PyDictProxy_Type)) {
1080
0
        right = ((mappingproxyobject*)right)->mapping;
1081
0
    }
1082
0
    return PyNumber_Or(left, right);
1083
0
}
1084
1085
static PyObject *
1086
mappingproxy_ior(PyObject *self, PyObject *Py_UNUSED(other))
1087
0
{
1088
0
    return PyErr_Format(PyExc_TypeError,
1089
0
        "'|=' is not supported by %s; use '|' instead", Py_TYPE(self)->tp_name);
1090
0
}
1091
1092
static PyNumberMethods mappingproxy_as_number = {
1093
    .nb_or = mappingproxy_or,
1094
    .nb_inplace_or = mappingproxy_ior,
1095
};
1096
1097
static int
1098
mappingproxy_contains(PyObject *self, PyObject *key)
1099
3.49k
{
1100
3.49k
    mappingproxyobject *pp = (mappingproxyobject *)self;
1101
3.49k
    if (PyDict_CheckExact(pp->mapping))
1102
3.28k
        return PyDict_Contains(pp->mapping, key);
1103
206
    else
1104
206
        return PySequence_Contains(pp->mapping, key);
1105
3.49k
}
1106
1107
static PySequenceMethods mappingproxy_as_sequence = {
1108
    0,                                          /* sq_length */
1109
    0,                                          /* sq_concat */
1110
    0,                                          /* sq_repeat */
1111
    0,                                          /* sq_item */
1112
    0,                                          /* sq_slice */
1113
    0,                                          /* sq_ass_item */
1114
    0,                                          /* sq_ass_slice */
1115
    mappingproxy_contains,                      /* sq_contains */
1116
    0,                                          /* sq_inplace_concat */
1117
    0,                                          /* sq_inplace_repeat */
1118
};
1119
1120
static PyObject *
1121
mappingproxy_get(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
1122
6.99k
{
1123
6.99k
    mappingproxyobject *pp = (mappingproxyobject *)self;
1124
    /* newargs: mapping, key, default=None */
1125
6.99k
    PyObject *newargs[3];
1126
6.99k
    newargs[0] = pp->mapping;
1127
6.99k
    newargs[2] = Py_None;
1128
1129
6.99k
    if (!_PyArg_UnpackStack(args, nargs, "get", 1, 2,
1130
6.99k
                            &newargs[1], &newargs[2]))
1131
0
    {
1132
0
        return NULL;
1133
0
    }
1134
6.99k
    return PyObject_VectorcallMethod(&_Py_ID(get), newargs,
1135
6.99k
                                     3 | PY_VECTORCALL_ARGUMENTS_OFFSET,
1136
6.99k
                                     NULL);
1137
6.99k
}
1138
1139
static PyObject *
1140
mappingproxy_keys(PyObject *self, PyObject *Py_UNUSED(ignored))
1141
64.4k
{
1142
64.4k
    mappingproxyobject *pp = (mappingproxyobject *)self;
1143
64.4k
    return PyObject_CallMethodNoArgs(pp->mapping, &_Py_ID(keys));
1144
64.4k
}
1145
1146
static PyObject *
1147
mappingproxy_values(PyObject *self, PyObject *Py_UNUSED(ignored))
1148
2.78k
{
1149
2.78k
    mappingproxyobject *pp = (mappingproxyobject *)self;
1150
2.78k
    return PyObject_CallMethodNoArgs(pp->mapping, &_Py_ID(values));
1151
2.78k
}
1152
1153
static PyObject *
1154
mappingproxy_items(PyObject *self, PyObject *Py_UNUSED(ignored))
1155
2.24k
{
1156
2.24k
    mappingproxyobject *pp = (mappingproxyobject *)self;
1157
2.24k
    return PyObject_CallMethodNoArgs(pp->mapping, &_Py_ID(items));
1158
2.24k
}
1159
1160
static PyObject *
1161
mappingproxy_copy(PyObject *self, PyObject *Py_UNUSED(ignored))
1162
0
{
1163
0
    mappingproxyobject *pp = (mappingproxyobject *)self;
1164
0
    return PyObject_CallMethodNoArgs(pp->mapping, &_Py_ID(copy));
1165
0
}
1166
1167
static PyObject *
1168
mappingproxy_reversed(PyObject *self, PyObject *Py_UNUSED(ignored))
1169
0
{
1170
0
    mappingproxyobject *pp = (mappingproxyobject *)self;
1171
0
    return PyObject_CallMethodNoArgs(pp->mapping, &_Py_ID(__reversed__));
1172
0
}
1173
1174
/* WARNING: mappingproxy methods must not give access
1175
            to the underlying mapping */
1176
1177
static PyMethodDef mappingproxy_methods[] = {
1178
    {"get",       _PyCFunction_CAST(mappingproxy_get), METH_FASTCALL,
1179
     PyDoc_STR("get($self, key, default=None, /)\n--\n\n"
1180
        "Return the value for key if key is in the mapping, else default.")},
1181
    {"keys",      mappingproxy_keys,       METH_NOARGS,
1182
     PyDoc_STR("D.keys() -> a set-like object providing a view on D's keys")},
1183
    {"values",    mappingproxy_values,     METH_NOARGS,
1184
     PyDoc_STR("D.values() -> an object providing a view on D's values")},
1185
    {"items",     mappingproxy_items,      METH_NOARGS,
1186
     PyDoc_STR("D.items() -> a set-like object providing a view on D's items")},
1187
    {"copy",      mappingproxy_copy,       METH_NOARGS,
1188
     PyDoc_STR("D.copy() -> a shallow copy of D")},
1189
    {"__class_getitem__", Py_GenericAlias, METH_O|METH_CLASS,
1190
     PyDoc_STR("mappingproxy objects are generic over two types, signifying (respectively) the types of their keys and values")},
1191
    {"__reversed__", mappingproxy_reversed, METH_NOARGS,
1192
     PyDoc_STR("D.__reversed__() -> reverse iterator")},
1193
    {0}
1194
};
1195
1196
static void
1197
mappingproxy_dealloc(PyObject *self)
1198
79.6k
{
1199
79.6k
    mappingproxyobject *pp = (mappingproxyobject *)self;
1200
79.6k
    _PyObject_GC_UNTRACK(pp);
1201
79.6k
    Py_DECREF(pp->mapping);
1202
79.6k
    PyObject_GC_Del(pp);
1203
79.6k
}
1204
1205
static PyObject *
1206
mappingproxy_getiter(PyObject *self)
1207
620
{
1208
620
    mappingproxyobject *pp = (mappingproxyobject *)self;
1209
620
    return PyObject_GetIter(pp->mapping);
1210
620
}
1211
1212
static Py_hash_t
1213
mappingproxy_hash(PyObject *self)
1214
0
{
1215
0
    mappingproxyobject *pp = (mappingproxyobject *)self;
1216
0
    return PyObject_Hash(pp->mapping);
1217
0
}
1218
1219
static PyObject *
1220
mappingproxy_str(PyObject *self)
1221
0
{
1222
0
    mappingproxyobject *pp = (mappingproxyobject *)self;
1223
0
    return PyObject_Str(pp->mapping);
1224
0
}
1225
1226
static PyObject *
1227
mappingproxy_repr(PyObject *self)
1228
0
{
1229
0
    mappingproxyobject *pp = (mappingproxyobject *)self;
1230
0
    return PyUnicode_FromFormat("mappingproxy(%R)", pp->mapping);
1231
0
}
1232
1233
static int
1234
mappingproxy_traverse(PyObject *self, visitproc visit, void *arg)
1235
398
{
1236
398
    mappingproxyobject *pp = (mappingproxyobject *)self;
1237
398
    Py_VISIT(pp->mapping);
1238
398
    return 0;
1239
398
}
1240
1241
static PyObject *
1242
mappingproxy_richcompare(PyObject *self, PyObject *w, int op)
1243
0
{
1244
0
    if (op == Py_EQ || op == Py_NE) {
1245
0
        mappingproxyobject *v = (mappingproxyobject *)self;
1246
        // We have to guard the mutable `dict` instances, because it can
1247
        // otherwise mutate the type's `__dict__` entries and cause crashes.
1248
        // But, do not create copies on known types like `OrderedDict`
1249
        // or immutable types like `frozendict`
1250
        // for memory optimization. See gh-152405 for the details.
1251
0
        if (
1252
0
            PyDict_CheckExact(v->mapping) &&
1253
0
            !(PyAnyDict_CheckExact(w) ||
1254
0
                Py_TYPE(w) == &PyDictProxy_Type ||
1255
0
                PyODict_CheckExact(w))
1256
0
        ) {
1257
            // So, instead we send a copy:
1258
0
            PyObject *copy = PyDict_Copy(v->mapping);
1259
0
            if (copy == NULL) {
1260
0
                return NULL;
1261
0
            }
1262
0
            PyObject *res = PyObject_RichCompare(copy, w, op);
1263
0
            Py_DECREF(copy);
1264
0
            return res;
1265
0
        }
1266
        // Otherwise we are free to share the mapping directly:
1267
0
        return PyObject_RichCompare(v->mapping, w, op);
1268
0
    }
1269
0
    Py_RETURN_NOTIMPLEMENTED;
1270
0
}
1271
1272
static int
1273
mappingproxy_check_mapping(PyObject *mapping)
1274
79.7k
{
1275
79.7k
    if (!PyMapping_Check(mapping)
1276
79.7k
        || PyList_Check(mapping)
1277
79.7k
        || PyTuple_Check(mapping)) {
1278
0
        PyErr_Format(PyExc_TypeError,
1279
0
                    "mappingproxy() argument must be a mapping, not %s",
1280
0
                    Py_TYPE(mapping)->tp_name);
1281
0
        return -1;
1282
0
    }
1283
79.7k
    return 0;
1284
79.7k
}
1285
1286
PyObject *
1287
PyDictProxy_New(PyObject *mapping)
1288
79.7k
{
1289
79.7k
    mappingproxyobject *pp;
1290
1291
79.7k
    if (mappingproxy_check_mapping(mapping) == -1)
1292
0
        return NULL;
1293
1294
79.7k
    pp = PyObject_GC_New(mappingproxyobject, &PyDictProxy_Type);
1295
79.7k
    if (pp != NULL) {
1296
79.7k
        pp->mapping = Py_NewRef(mapping);
1297
79.7k
        _PyObject_GC_TRACK(pp);
1298
79.7k
    }
1299
79.7k
    return (PyObject *)pp;
1300
79.7k
}
1301
1302
/*[clinic input]
1303
@classmethod
1304
mappingproxy.__new__ as mappingproxy_new
1305
1306
    mapping: object
1307
1308
Read-only proxy of a mapping.
1309
[clinic start generated code]*/
1310
1311
static PyObject *
1312
mappingproxy_new_impl(PyTypeObject *type, PyObject *mapping)
1313
/*[clinic end generated code: output=65f27f02d5b68fa7 input=c156df096ef7590c]*/
1314
983
{
1315
983
    return PyDictProxy_New(mapping);
1316
983
}
1317
1318
1319
/* --- Wrapper object for "slot" methods --- */
1320
1321
/* This has no reason to be in this file except that adding new files is a
1322
   bit of a pain */
1323
1324
typedef struct {
1325
    PyObject_HEAD
1326
    PyWrapperDescrObject *descr;
1327
    PyObject *self;
1328
} wrapperobject;
1329
1330
0
#define Wrapper_Check(v) Py_IS_TYPE(v, &_PyMethodWrapper_Type)
1331
1332
static void
1333
wrapper_dealloc(PyObject *self)
1334
12.3M
{
1335
12.3M
    wrapperobject *wp = (wrapperobject *)self;
1336
12.3M
    PyObject_GC_UnTrack(wp);
1337
12.3M
    Py_XDECREF(wp->descr);
1338
12.3M
    Py_XDECREF(wp->self);
1339
12.3M
    PyObject_GC_Del(wp);
1340
12.3M
}
1341
1342
static PyObject *
1343
wrapper_richcompare(PyObject *a, PyObject *b, int op)
1344
0
{
1345
0
    wrapperobject *wa, *wb;
1346
0
    int eq;
1347
1348
0
    assert(a != NULL && b != NULL);
1349
1350
    /* both arguments should be wrapperobjects */
1351
0
    if ((op != Py_EQ && op != Py_NE)
1352
0
        || !Wrapper_Check(a) || !Wrapper_Check(b))
1353
0
    {
1354
0
        Py_RETURN_NOTIMPLEMENTED;
1355
0
    }
1356
1357
0
    wa = (wrapperobject *)a;
1358
0
    wb = (wrapperobject *)b;
1359
0
    eq = (wa->descr == wb->descr && wa->self == wb->self);
1360
0
    if (eq == (op == Py_EQ)) {
1361
0
        Py_RETURN_TRUE;
1362
0
    }
1363
0
    else {
1364
0
        Py_RETURN_FALSE;
1365
0
    }
1366
0
}
1367
1368
static Py_hash_t
1369
wrapper_hash(PyObject *self)
1370
0
{
1371
0
    wrapperobject *wp = (wrapperobject *)self;
1372
0
    Py_hash_t x, y;
1373
0
    x = PyObject_GenericHash(wp->self);
1374
0
    y = Py_HashPointer(wp->descr);
1375
0
    x = x ^ y;
1376
0
    if (x == -1)
1377
0
        x = -2;
1378
0
    return x;
1379
0
}
1380
1381
static PyObject *
1382
wrapper_repr(PyObject *self)
1383
0
{
1384
0
    wrapperobject *wp = (wrapperobject *)self;
1385
0
    return PyUnicode_FromFormat("<method-wrapper '%s' of %s object at %p>",
1386
0
                               wp->descr->d_base->name,
1387
0
                               Py_TYPE(wp->self)->tp_name,
1388
0
                               wp->self);
1389
0
}
1390
1391
static PyObject *
1392
wrapper_reduce(PyObject *self, PyObject *Py_UNUSED(ignored))
1393
0
{
1394
0
    wrapperobject *wp = (wrapperobject *)self;
1395
0
    return Py_BuildValue("N(OO)", _PyEval_GetBuiltin(&_Py_ID(getattr)),
1396
0
                         wp->self, PyDescr_NAME(wp->descr));
1397
0
}
1398
1399
static PyMethodDef wrapper_methods[] = {
1400
    {"__reduce__", wrapper_reduce, METH_NOARGS, NULL},
1401
    {NULL, NULL}
1402
};
1403
1404
static PyMemberDef wrapper_members[] = {
1405
    {"__self__", _Py_T_OBJECT, offsetof(wrapperobject, self), Py_READONLY},
1406
    {0}
1407
};
1408
1409
static PyObject *
1410
wrapper_objclass(PyObject *wp, void *Py_UNUSED(ignored))
1411
0
{
1412
0
    PyObject *c = (PyObject *)PyDescr_TYPE(((wrapperobject *)wp)->descr);
1413
1414
0
    return Py_NewRef(c);
1415
0
}
1416
1417
static PyObject *
1418
wrapper_name(PyObject *wp, void *Py_UNUSED(ignored))
1419
0
{
1420
0
    const char *s = ((wrapperobject *)wp)->descr->d_base->name;
1421
1422
0
    return PyUnicode_FromString(s);
1423
0
}
1424
1425
static PyObject *
1426
wrapper_doc(PyObject *self, void *Py_UNUSED(ignored))
1427
0
{
1428
0
    wrapperobject *wp = (wrapperobject *)self;
1429
0
    return _PyType_GetDocFromInternalDoc(wp->descr->d_base->name, wp->descr->d_base->doc);
1430
0
}
1431
1432
static PyObject *
1433
wrapper_text_signature(PyObject *self, void *Py_UNUSED(ignored))
1434
0
{
1435
0
    wrapperobject *wp = (wrapperobject *)self;
1436
0
    return _PyType_GetTextSignatureFromInternalDoc(wp->descr->d_base->name,
1437
0
                                                   wp->descr->d_base->doc, 0);
1438
0
}
1439
1440
static PyObject *
1441
wrapper_qualname(PyObject *self, void *Py_UNUSED(ignored))
1442
0
{
1443
0
    wrapperobject *wp = (wrapperobject *)self;
1444
0
    return descr_get_qualname((PyObject *)wp->descr, NULL);
1445
0
}
1446
1447
static PyGetSetDef wrapper_getsets[] = {
1448
    {"__objclass__", wrapper_objclass},
1449
    {"__name__", wrapper_name},
1450
    {"__qualname__", wrapper_qualname},
1451
    {"__doc__", wrapper_doc},
1452
    {"__text_signature__", wrapper_text_signature},
1453
    {0}
1454
};
1455
1456
static PyObject *
1457
wrapper_call(PyObject *self, PyObject *args, PyObject *kwds)
1458
12.4M
{
1459
12.4M
    wrapperobject *wp = (wrapperobject *)self;
1460
12.4M
    return wrapperdescr_raw_call(wp->descr, wp->self, args, kwds);
1461
12.4M
}
1462
1463
static int
1464
wrapper_traverse(PyObject *self, visitproc visit, void *arg)
1465
5.80k
{
1466
5.80k
    wrapperobject *wp = (wrapperobject *)self;
1467
5.80k
    Py_VISIT(wp->descr);
1468
5.80k
    Py_VISIT(wp->self);
1469
5.80k
    return 0;
1470
5.80k
}
1471
1472
PyTypeObject _PyMethodWrapper_Type = {
1473
    PyVarObject_HEAD_INIT(&PyType_Type, 0)
1474
    "method-wrapper",                           /* tp_name */
1475
    sizeof(wrapperobject),                      /* tp_basicsize */
1476
    0,                                          /* tp_itemsize */
1477
    /* methods */
1478
    wrapper_dealloc,                            /* tp_dealloc */
1479
    0,                                          /* tp_vectorcall_offset */
1480
    0,                                          /* tp_getattr */
1481
    0,                                          /* tp_setattr */
1482
    0,                                          /* tp_as_async */
1483
    wrapper_repr,                               /* tp_repr */
1484
    0,                                          /* tp_as_number */
1485
    0,                                          /* tp_as_sequence */
1486
    0,                                          /* tp_as_mapping */
1487
    wrapper_hash,                               /* tp_hash */
1488
    wrapper_call,                               /* tp_call */
1489
    0,                                          /* tp_str */
1490
    PyObject_GenericGetAttr,                    /* tp_getattro */
1491
    0,                                          /* tp_setattro */
1492
    0,                                          /* tp_as_buffer */
1493
    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
1494
    0,                                          /* tp_doc */
1495
    wrapper_traverse,                           /* tp_traverse */
1496
    0,                                          /* tp_clear */
1497
    wrapper_richcompare,                        /* tp_richcompare */
1498
    0,                                          /* tp_weaklistoffset */
1499
    0,                                          /* tp_iter */
1500
    0,                                          /* tp_iternext */
1501
    wrapper_methods,                            /* tp_methods */
1502
    wrapper_members,                            /* tp_members */
1503
    wrapper_getsets,                            /* tp_getset */
1504
    0,                                          /* tp_base */
1505
    0,                                          /* tp_dict */
1506
    0,                                          /* tp_descr_get */
1507
    0,                                          /* tp_descr_set */
1508
};
1509
1510
PyObject *
1511
PyWrapper_New(PyObject *d, PyObject *self)
1512
12.3M
{
1513
12.3M
    wrapperobject *wp;
1514
12.3M
    PyWrapperDescrObject *descr;
1515
1516
12.3M
    assert(PyObject_TypeCheck(d, &PyWrapperDescr_Type));
1517
12.3M
    descr = (PyWrapperDescrObject *)d;
1518
12.3M
    assert(_PyObject_RealIsSubclass((PyObject *)Py_TYPE(self),
1519
12.3M
                                    (PyObject *)PyDescr_TYPE(descr)));
1520
1521
12.3M
    wp = PyObject_GC_New(wrapperobject, &_PyMethodWrapper_Type);
1522
12.3M
    if (wp != NULL) {
1523
12.3M
        wp->descr = (PyWrapperDescrObject*)Py_NewRef(descr);
1524
12.3M
        wp->self = Py_NewRef(self);
1525
12.3M
        _PyObject_GC_TRACK(wp);
1526
12.3M
    }
1527
12.3M
    return (PyObject *)wp;
1528
12.3M
}
1529
1530
1531
/* A built-in 'property' type */
1532
1533
287
#define _propertyobject_CAST(op)    ((propertyobject *)(op))
1534
1535
/*
1536
class property(object):
1537
1538
    def __init__(self, fget=None, fset=None, fdel=None, doc=None):
1539
        if doc is None and fget is not None and hasattr(fget, "__doc__"):
1540
            doc = fget.__doc__
1541
        self.__get = fget
1542
        self.__set = fset
1543
        self.__del = fdel
1544
        try:
1545
            self.__doc__ = doc
1546
        except AttributeError:  # read-only or dict-less class
1547
            pass
1548
        self.__name = None
1549
1550
    def __set_name__(self, owner, name):
1551
        self.__name = name
1552
1553
    @property
1554
    def __name__(self):
1555
        return self.__name if self.__name is not None else self.fget.__name__
1556
1557
    @__name__.setter
1558
    def __name__(self, value):
1559
        self.__name = value
1560
1561
    def __get__(self, inst, type=None):
1562
        if inst is None:
1563
            return self
1564
        if self.__get is None:
1565
            raise AttributeError("property has no getter")
1566
        return self.__get(inst)
1567
1568
    def __set__(self, inst, value):
1569
        if self.__set is None:
1570
            raise AttributeError("property has no setter")
1571
        return self.__set(inst, value)
1572
1573
    def __delete__(self, inst):
1574
        if self.__del is None:
1575
            raise AttributeError("property has no deleter")
1576
        return self.__del(inst)
1577
1578
*/
1579
1580
static PyObject * property_copy(PyObject *, PyObject *, PyObject *,
1581
                                  PyObject *);
1582
1583
static PyMemberDef property_members[] = {
1584
    {"fget", _Py_T_OBJECT, offsetof(propertyobject, prop_get), Py_READONLY},
1585
    {"fset", _Py_T_OBJECT, offsetof(propertyobject, prop_set), Py_READONLY},
1586
    {"fdel", _Py_T_OBJECT, offsetof(propertyobject, prop_del), Py_READONLY},
1587
    {"__doc__",  _Py_T_OBJECT, offsetof(propertyobject, prop_doc), 0},
1588
    {0}
1589
};
1590
1591
1592
PyDoc_STRVAR(getter_doc,
1593
             "Descriptor to obtain a copy of the property with a different getter.");
1594
1595
static PyObject *
1596
property_getter(PyObject *self, PyObject *getter)
1597
0
{
1598
0
    return property_copy(self, getter, NULL, NULL);
1599
0
}
1600
1601
1602
PyDoc_STRVAR(setter_doc,
1603
             "Descriptor to obtain a copy of the property with a different setter.");
1604
1605
static PyObject *
1606
property_setter(PyObject *self, PyObject *setter)
1607
172
{
1608
172
    return property_copy(self, NULL, setter, NULL);
1609
172
}
1610
1611
1612
PyDoc_STRVAR(deleter_doc,
1613
             "Descriptor to obtain a copy of the property with a different deleter.");
1614
1615
static PyObject *
1616
property_deleter(PyObject *self, PyObject *deleter)
1617
0
{
1618
0
    return property_copy(self, NULL, NULL, deleter);
1619
0
}
1620
1621
1622
/*[clinic input]
1623
property.__set_name__
1624
1625
    owner: object
1626
    name: object
1627
    /
1628
1629
Method to set name of a property.
1630
[clinic start generated code]*/
1631
1632
static PyObject *
1633
property___set_name___impl(propertyobject *self, PyObject *owner,
1634
                           PyObject *name)
1635
/*[clinic end generated code: output=959e301a91f7fb85 input=0211b5b7ace099c5]*/
1636
2.66k
{
1637
2.66k
    Py_XSETREF(self->prop_name, Py_XNewRef(name));
1638
1639
2.66k
    Py_RETURN_NONE;
1640
2.66k
}
1641
1642
static PyMethodDef property_methods[] = {
1643
    {"getter", property_getter, METH_O, getter_doc},
1644
    {"setter", property_setter, METH_O, setter_doc},
1645
    {"deleter", property_deleter, METH_O, deleter_doc},
1646
    PROPERTY___SET_NAME___METHODDEF
1647
    {0}
1648
};
1649
1650
1651
static void
1652
property_dealloc(PyObject *self)
1653
172
{
1654
172
    propertyobject *gs = (propertyobject *)self;
1655
1656
172
    _PyObject_GC_UNTRACK(self);
1657
172
    Py_XDECREF(gs->prop_get);
1658
172
    Py_XDECREF(gs->prop_set);
1659
172
    Py_XDECREF(gs->prop_del);
1660
172
    Py_XDECREF(gs->prop_doc);
1661
172
    Py_XDECREF(gs->prop_name);
1662
172
    Py_TYPE(self)->tp_free(self);
1663
172
}
1664
1665
static int
1666
property_name(propertyobject *prop, PyObject **name)
1667
0
{
1668
0
    if (prop->prop_name != NULL) {
1669
0
        *name = Py_NewRef(prop->prop_name);
1670
0
        return 1;
1671
0
    }
1672
0
    if (prop->prop_get == NULL) {
1673
0
        *name = NULL;
1674
0
        return 0;
1675
0
    }
1676
0
    return PyObject_GetOptionalAttr(prop->prop_get, &_Py_ID(__name__), name);
1677
0
}
1678
1679
static PyObject *
1680
property_descr_get(PyObject *self, PyObject *obj, PyObject *type)
1681
12.0M
{
1682
12.0M
    if (obj == NULL || obj == Py_None) {
1683
38
        return Py_NewRef(self);
1684
38
    }
1685
1686
12.0M
    propertyobject *gs = (propertyobject *)self;
1687
12.0M
    if (gs->prop_get == NULL) {
1688
0
        PyObject *propname;
1689
0
        if (property_name(gs, &propname) < 0) {
1690
0
            return NULL;
1691
0
        }
1692
0
        PyObject *qualname = PyType_GetQualName(Py_TYPE(obj));
1693
0
        if (propname != NULL && qualname != NULL) {
1694
0
            PyErr_Format(PyExc_AttributeError,
1695
0
                         "property %R of %R object has no getter",
1696
0
                         propname,
1697
0
                         qualname);
1698
0
        }
1699
0
        else if (qualname != NULL) {
1700
0
            PyErr_Format(PyExc_AttributeError,
1701
0
                         "property of %R object has no getter",
1702
0
                         qualname);
1703
0
        } else {
1704
0
            PyErr_SetString(PyExc_AttributeError,
1705
0
                            "property has no getter");
1706
0
        }
1707
0
        Py_XDECREF(propname);
1708
0
        Py_XDECREF(qualname);
1709
0
        return NULL;
1710
0
    }
1711
1712
12.0M
    return PyObject_CallOneArg(gs->prop_get, obj);
1713
12.0M
}
1714
1715
static int
1716
property_descr_set(PyObject *self, PyObject *obj, PyObject *value)
1717
8.50k
{
1718
8.50k
    propertyobject *gs = (propertyobject *)self;
1719
8.50k
    PyObject *func, *res;
1720
1721
8.50k
    if (value == NULL) {
1722
0
        func = gs->prop_del;
1723
0
    }
1724
8.50k
    else {
1725
8.50k
        func = gs->prop_set;
1726
8.50k
    }
1727
1728
8.50k
    if (func == NULL) {
1729
0
        PyObject *propname;
1730
0
        if (property_name(gs, &propname) < 0) {
1731
0
            return -1;
1732
0
        }
1733
0
        PyObject *qualname = NULL;
1734
0
        if (obj != NULL) {
1735
0
            qualname = PyType_GetQualName(Py_TYPE(obj));
1736
0
        }
1737
0
        if (propname != NULL && qualname != NULL) {
1738
0
            PyErr_Format(PyExc_AttributeError,
1739
0
                        value == NULL ?
1740
0
                        "property %R of %R object has no deleter" :
1741
0
                        "property %R of %R object has no setter",
1742
0
                        propname,
1743
0
                        qualname);
1744
0
        }
1745
0
        else if (qualname != NULL) {
1746
0
            PyErr_Format(PyExc_AttributeError,
1747
0
                            value == NULL ?
1748
0
                            "property of %R object has no deleter" :
1749
0
                            "property of %R object has no setter",
1750
0
                            qualname);
1751
0
        }
1752
0
        else {
1753
0
            PyErr_SetString(PyExc_AttributeError,
1754
0
                         value == NULL ?
1755
0
                         "property has no deleter" :
1756
0
                         "property has no setter");
1757
0
        }
1758
0
        Py_XDECREF(propname);
1759
0
        Py_XDECREF(qualname);
1760
0
        return -1;
1761
0
    }
1762
1763
8.50k
    if (value == NULL) {
1764
0
        res = PyObject_CallOneArg(func, obj);
1765
0
    }
1766
8.50k
    else {
1767
8.50k
        EVAL_CALL_STAT_INC_IF_FUNCTION(EVAL_CALL_API, func);
1768
8.50k
        PyObject *args[] = { obj, value };
1769
8.50k
        res = PyObject_Vectorcall(func, args, 2, NULL);
1770
8.50k
    }
1771
1772
8.50k
    if (res == NULL) {
1773
0
        return -1;
1774
0
    }
1775
1776
8.50k
    Py_DECREF(res);
1777
8.50k
    return 0;
1778
8.50k
}
1779
1780
static PyObject *
1781
property_copy(PyObject *old, PyObject *get, PyObject *set, PyObject *del)
1782
172
{
1783
172
    propertyobject *pold = (propertyobject *)old;
1784
172
    PyObject *new, *type, *doc;
1785
1786
172
    type = PyObject_Type(old);
1787
172
    if (type == NULL)
1788
0
        return NULL;
1789
1790
172
    if (get == NULL || get == Py_None) {
1791
172
        get = pold->prop_get ? pold->prop_get : Py_None;
1792
172
    }
1793
172
    if (set == NULL || set == Py_None) {
1794
0
        set = pold->prop_set ? pold->prop_set : Py_None;
1795
0
    }
1796
172
    if (del == NULL || del == Py_None) {
1797
172
        del = pold->prop_del ? pold->prop_del : Py_None;
1798
172
    }
1799
172
    if (pold->getter_doc && get != Py_None) {
1800
        /* make _init use __doc__ from getter */
1801
58
        doc = Py_None;
1802
58
    }
1803
114
    else {
1804
114
        doc = pold->prop_doc ? pold->prop_doc : Py_None;
1805
114
    }
1806
1807
172
    new =  PyObject_CallFunctionObjArgs(type, get, set, del, doc, NULL);
1808
172
    Py_DECREF(type);
1809
172
    if (new == NULL)
1810
0
        return NULL;
1811
1812
172
    if (PyObject_TypeCheck((new), &PyProperty_Type)) {
1813
172
        Py_XSETREF(((propertyobject *) new)->prop_name, Py_XNewRef(pold->prop_name));
1814
172
    }
1815
172
    return new;
1816
172
}
1817
1818
/*[clinic input]
1819
property.__init__ as property_init
1820
1821
    fget: object(c_default="NULL") = None
1822
        function to be used for getting an attribute value
1823
    fset: object(c_default="NULL") = None
1824
        function to be used for setting an attribute value
1825
    fdel: object(c_default="NULL") = None
1826
        function to be used for del'ing an attribute
1827
    doc: object(c_default="NULL") = None
1828
        docstring
1829
1830
Property attribute.
1831
1832
Typical use is to define a managed attribute x:
1833
1834
class C(object):
1835
    def getx(self): return self._x
1836
    def setx(self, value): self._x = value
1837
    def delx(self): del self._x
1838
    x = property(getx, setx, delx, "I'm the 'x' property.")
1839
1840
Decorators make defining new properties or modifying existing ones easy:
1841
1842
class C(object):
1843
    @property
1844
    def x(self):
1845
        "I am the 'x' property."
1846
        return self._x
1847
    @x.setter
1848
    def x(self, value):
1849
        self._x = value
1850
    @x.deleter
1851
    def x(self):
1852
        del self._x
1853
[clinic start generated code]*/
1854
1855
static int
1856
property_init_impl(propertyobject *self, PyObject *fget, PyObject *fset,
1857
                   PyObject *fdel, PyObject *doc)
1858
/*[clinic end generated code: output=01a960742b692b57 input=dfb5dbbffc6932d5]*/
1859
2.63k
{
1860
2.63k
    if (fget == Py_None)
1861
0
        fget = NULL;
1862
2.63k
    if (fset == Py_None)
1863
0
        fset = NULL;
1864
2.63k
    if (fdel == Py_None)
1865
172
        fdel = NULL;
1866
1867
2.63k
    Py_XSETREF(self->prop_get, Py_XNewRef(fget));
1868
2.63k
    Py_XSETREF(self->prop_set, Py_XNewRef(fset));
1869
2.63k
    Py_XSETREF(self->prop_del, Py_XNewRef(fdel));
1870
2.63k
    Py_XSETREF(self->prop_doc, NULL);
1871
2.63k
    Py_XSETREF(self->prop_name, NULL);
1872
1873
2.63k
    self->getter_doc = 0;
1874
2.63k
    PyObject *prop_doc = NULL;
1875
1876
2.63k
    if (doc != NULL && doc != Py_None) {
1877
0
        prop_doc = Py_XNewRef(doc);
1878
0
    }
1879
    /* if no docstring given and the getter has one, use that one */
1880
2.63k
    else if (fget != NULL) {
1881
2.63k
        int rc = PyObject_GetOptionalAttr(fget, &_Py_ID(__doc__), &prop_doc);
1882
2.63k
        if (rc < 0) {
1883
0
            return rc;
1884
0
        }
1885
2.63k
        if (prop_doc == Py_None) {
1886
1.72k
            prop_doc = NULL;
1887
1.72k
            Py_DECREF(Py_None);
1888
1.72k
        }
1889
2.63k
        if (prop_doc != NULL){
1890
918
            self->getter_doc = 1;
1891
918
        }
1892
2.63k
    }
1893
1894
    /* At this point `prop_doc` is either NULL or
1895
       a non-None object with incremented ref counter */
1896
1897
2.63k
    if (Py_IS_TYPE(self, &PyProperty_Type)) {
1898
2.63k
        Py_XSETREF(self->prop_doc, prop_doc);
1899
2.63k
    } else {
1900
        /* If this is a property subclass, put __doc__ in the dict
1901
           or designated slot of the subclass instance instead, otherwise
1902
           it gets shadowed by __doc__ in the class's dict. */
1903
1904
0
        if (prop_doc == NULL) {
1905
0
            prop_doc = Py_NewRef(Py_None);
1906
0
        }
1907
0
        int err = PyObject_SetAttr(
1908
0
                    (PyObject *)self, &_Py_ID(__doc__), prop_doc);
1909
0
        Py_DECREF(prop_doc);
1910
0
        if (err < 0) {
1911
0
            assert(PyErr_Occurred());
1912
0
            if (!self->getter_doc &&
1913
0
                PyErr_ExceptionMatches(PyExc_AttributeError))
1914
0
            {
1915
0
                PyErr_Clear();
1916
                // https://github.com/python/cpython/issues/98963#issuecomment-1574413319
1917
                // Python silently dropped this doc assignment through 3.11.
1918
                // We preserve that behavior for backwards compatibility.
1919
                //
1920
                // If we ever want to deprecate this behavior, only raise a
1921
                // warning or error when proc_doc is not None so that
1922
                // property without a specific doc= still works.
1923
0
                return 0;
1924
0
            } else {
1925
0
                return -1;
1926
0
            }
1927
0
        }
1928
0
    }
1929
1930
2.63k
    return 0;
1931
2.63k
}
1932
1933
static PyObject *
1934
property_get__name__(PyObject *op, void *Py_UNUSED(ignored))
1935
0
{
1936
0
    propertyobject *prop = _propertyobject_CAST(op);
1937
0
    PyObject *name;
1938
0
    if (property_name(prop, &name) < 0) {
1939
0
        return NULL;
1940
0
    }
1941
0
    if (name == NULL) {
1942
0
        PyErr_SetString(PyExc_AttributeError,
1943
0
                        "'property' object has no attribute '__name__'");
1944
0
    }
1945
0
    return name;
1946
0
}
1947
1948
static int
1949
property_set__name__(PyObject *op, PyObject *value, void *Py_UNUSED(ignored))
1950
0
{
1951
0
    propertyobject *prop = _propertyobject_CAST(op);
1952
0
    Py_XSETREF(prop->prop_name, Py_XNewRef(value));
1953
0
    return 0;
1954
0
}
1955
1956
static PyObject *
1957
property_get___isabstractmethod__(PyObject *op, void *closure)
1958
287
{
1959
287
    propertyobject *prop = _propertyobject_CAST(op);
1960
287
    int res = _PyObject_IsAbstract(prop->prop_get);
1961
287
    if (res == -1) {
1962
0
        return NULL;
1963
0
    }
1964
287
    else if (res) {
1965
20
        Py_RETURN_TRUE;
1966
20
    }
1967
1968
267
    res = _PyObject_IsAbstract(prop->prop_set);
1969
267
    if (res == -1) {
1970
0
        return NULL;
1971
0
    }
1972
267
    else if (res) {
1973
0
        Py_RETURN_TRUE;
1974
0
    }
1975
1976
267
    res = _PyObject_IsAbstract(prop->prop_del);
1977
267
    if (res == -1) {
1978
0
        return NULL;
1979
0
    }
1980
267
    else if (res) {
1981
0
        Py_RETURN_TRUE;
1982
0
    }
1983
267
    Py_RETURN_FALSE;
1984
267
}
1985
1986
static PyGetSetDef property_getsetlist[] = {
1987
    {"__name__", property_get__name__, property_set__name__, NULL, NULL},
1988
    {"__isabstractmethod__", property_get___isabstractmethod__, NULL,
1989
     NULL,
1990
     NULL},
1991
    {NULL} /* Sentinel */
1992
};
1993
1994
static int
1995
property_traverse(PyObject *self, visitproc visit, void *arg)
1996
99.2k
{
1997
99.2k
    propertyobject *pp = (propertyobject *)self;
1998
99.2k
    Py_VISIT(pp->prop_get);
1999
99.2k
    Py_VISIT(pp->prop_set);
2000
99.2k
    Py_VISIT(pp->prop_del);
2001
99.2k
    Py_VISIT(pp->prop_doc);
2002
99.2k
    Py_VISIT(pp->prop_name);
2003
99.2k
    return 0;
2004
99.2k
}
2005
2006
static int
2007
property_clear(PyObject *self)
2008
0
{
2009
0
    propertyobject *pp = (propertyobject *)self;
2010
0
    Py_CLEAR(pp->prop_get);
2011
0
    Py_CLEAR(pp->prop_set);
2012
0
    Py_CLEAR(pp->prop_del);
2013
0
    Py_CLEAR(pp->prop_doc);
2014
    Py_CLEAR(pp->prop_name);
2015
0
    return 0;
2016
0
}
2017
2018
2019
PyTypeObject PyDictProxy_Type = {
2020
    PyVarObject_HEAD_INIT(&PyType_Type, 0)
2021
    "mappingproxy",                             /* tp_name */
2022
    sizeof(mappingproxyobject),                 /* tp_basicsize */
2023
    0,                                          /* tp_itemsize */
2024
    /* methods */
2025
    mappingproxy_dealloc,                       /* tp_dealloc */
2026
    0,                                          /* tp_vectorcall_offset */
2027
    0,                                          /* tp_getattr */
2028
    0,                                          /* tp_setattr */
2029
    0,                                          /* tp_as_async */
2030
    mappingproxy_repr,                          /* tp_repr */
2031
    &mappingproxy_as_number,                    /* tp_as_number */
2032
    &mappingproxy_as_sequence,                  /* tp_as_sequence */
2033
    &mappingproxy_as_mapping,                   /* tp_as_mapping */
2034
    mappingproxy_hash,                          /* tp_hash */
2035
    0,                                          /* tp_call */
2036
    mappingproxy_str,                           /* tp_str */
2037
    PyObject_GenericGetAttr,                    /* tp_getattro */
2038
    0,                                          /* tp_setattro */
2039
    0,                                          /* tp_as_buffer */
2040
    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
2041
        Py_TPFLAGS_MAPPING,                     /* tp_flags */
2042
    mappingproxy_new__doc__,                    /* tp_doc */
2043
    mappingproxy_traverse,                      /* tp_traverse */
2044
    0,                                          /* tp_clear */
2045
    mappingproxy_richcompare,                   /* tp_richcompare */
2046
    0,                                          /* tp_weaklistoffset */
2047
    mappingproxy_getiter,                       /* tp_iter */
2048
    0,                                          /* tp_iternext */
2049
    mappingproxy_methods,                       /* tp_methods */
2050
    0,                                          /* tp_members */
2051
    0,                                          /* tp_getset */
2052
    0,                                          /* tp_base */
2053
    0,                                          /* tp_dict */
2054
    0,                                          /* tp_descr_get */
2055
    0,                                          /* tp_descr_set */
2056
    0,                                          /* tp_dictoffset */
2057
    0,                                          /* tp_init */
2058
    0,                                          /* tp_alloc */
2059
    mappingproxy_new,                           /* tp_new */
2060
};
2061
2062
PyTypeObject PyProperty_Type = {
2063
    PyVarObject_HEAD_INIT(&PyType_Type, 0)
2064
    "property",                                 /* tp_name */
2065
    sizeof(propertyobject),                     /* tp_basicsize */
2066
    0,                                          /* tp_itemsize */
2067
    /* methods */
2068
    property_dealloc,                           /* tp_dealloc */
2069
    0,                                          /* tp_vectorcall_offset */
2070
    0,                                          /* tp_getattr */
2071
    0,                                          /* tp_setattr */
2072
    0,                                          /* tp_as_async */
2073
    0,                                          /* tp_repr */
2074
    0,                                          /* tp_as_number */
2075
    0,                                          /* tp_as_sequence */
2076
    0,                                          /* tp_as_mapping */
2077
    0,                                          /* tp_hash */
2078
    0,                                          /* tp_call */
2079
    0,                                          /* tp_str */
2080
    PyObject_GenericGetAttr,                    /* tp_getattro */
2081
    0,                                          /* tp_setattro */
2082
    0,                                          /* tp_as_buffer */
2083
    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
2084
        Py_TPFLAGS_BASETYPE,                    /* tp_flags */
2085
    property_init__doc__,                       /* tp_doc */
2086
    property_traverse,                          /* tp_traverse */
2087
    property_clear,                             /* tp_clear */
2088
    0,                                          /* tp_richcompare */
2089
    0,                                          /* tp_weaklistoffset */
2090
    0,                                          /* tp_iter */
2091
    0,                                          /* tp_iternext */
2092
    property_methods,                           /* tp_methods */
2093
    property_members,                           /* tp_members */
2094
    property_getsetlist,                        /* tp_getset */
2095
    0,                                          /* tp_base */
2096
    0,                                          /* tp_dict */
2097
    property_descr_get,                         /* tp_descr_get */
2098
    property_descr_set,                         /* tp_descr_set */
2099
    0,                                          /* tp_dictoffset */
2100
    property_init,                              /* tp_init */
2101
    PyType_GenericAlloc,                        /* tp_alloc */
2102
    PyType_GenericNew,                          /* tp_new */
2103
    PyObject_GC_Del,                            /* tp_free */
2104
};