Coverage Report

Created: 2026-12-01 06:09

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