Coverage Report

Created: 2026-10-17 06:15

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.67k
{
27
1.67k
    PyDescrObject *descr = (PyDescrObject *)self;
28
1.67k
    _PyObject_GC_UNTRACK(descr);
29
1.67k
    Py_XDECREF(descr->d_type);
30
1.67k
    Py_XDECREF(descr->d_name);
31
1.67k
    Py_XDECREF(descr->d_qualname);
32
1.67k
    PyObject_GC_Del(descr);
33
1.67k
}
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
310M
{
84
310M
    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
310M
    return 0;
94
310M
}
95
96
static PyObject *
97
classmethod_get(PyObject *self, PyObject *obj, PyObject *type)
98
14.5M
{
99
14.5M
    PyMethodDescrObject *descr = (PyMethodDescrObject *)self;
100
    /* Ensure a valid type.  Class methods ignore obj. */
101
14.5M
    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
14.5M
    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
14.5M
    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
14.5M
    PyTypeObject *cls = NULL;
133
14.5M
    if (descr->d_method->ml_flags & METH_METHOD) {
134
4.52k
        cls = descr->d_common.d_type;
135
4.52k
    }
136
14.5M
    return PyCMethod_New(descr->d_method, type, NULL, cls);
137
14.5M
}
138
139
static PyObject *
140
method_get(PyObject *self, PyObject *obj, PyObject *type)
141
151M
{
142
151M
    PyMethodDescrObject *descr = (PyMethodDescrObject *)self;
143
151M
    if (obj == NULL) {
144
29.8k
        return Py_NewRef(descr);
145
29.8k
    }
146
151M
    if (descr_check((PyDescrObject *)descr, obj) < 0) {
147
0
        return NULL;
148
0
    }
149
151M
    if (descr->d_method->ml_flags & METH_METHOD) {
150
10.4M
        if (type == NULL || PyType_Check(type)) {
151
10.4M
            return PyCMethod_New(descr->d_method, obj, NULL, descr->d_common.d_type);
152
10.4M
        } 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
141M
    } else {
160
141M
        return PyCFunction_NewEx(descr->d_method, obj, NULL);
161
141M
    }
162
151M
}
163
164
static PyObject *
165
member_get(PyObject *self, PyObject *obj, PyObject *type)
166
912k
{
167
912k
    PyMemberDescrObject *descr = (PyMemberDescrObject *)self;
168
912k
    if (obj == NULL) {
169
16
        return Py_NewRef(descr);
170
16
    }
171
912k
    if (descr_check((PyDescrObject *)descr, obj) < 0) {
172
0
        return NULL;
173
0
    }
174
175
912k
    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
912k
    return PyMember_GetOne((char *)obj, descr->d_member);
183
912k
}
184
185
static PyObject *
186
getset_get(PyObject *self, PyObject *obj, PyObject *type)
187
76.5M
{
188
76.5M
    PyGetSetDescrObject *descr = (PyGetSetDescrObject *)self;
189
76.5M
    if (obj == NULL) {
190
6
        return Py_NewRef(descr);
191
6
    }
192
76.5M
    if (descr_check((PyDescrObject *)descr, obj) < 0) {
193
0
        return NULL;
194
0
    }
195
76.5M
    if (descr->d_getset->get != NULL)
196
76.5M
        return descr_get_trampoline_call(
197
76.5M
            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
76.5M
}
204
205
static PyObject *
206
wrapperdescr_get(PyObject *self, PyObject *obj, PyObject *type)
207
10.5M
{
208
10.5M
    PyWrapperDescrObject *descr = (PyWrapperDescrObject *)self;
209
10.5M
    if (obj == NULL) {
210
3.24k
        return Py_NewRef(descr);
211
3.24k
    }
212
10.4M
    if (descr_check((PyDescrObject *)descr, obj) < 0) {
213
0
        return NULL;
214
0
    }
215
10.4M
    return PyWrapper_New((PyObject *)descr, obj);
216
10.4M
}
217
218
static int
219
descr_setcheck(PyDescrObject *descr, PyObject *obj, PyObject *value)
220
14.6M
{
221
14.6M
    assert(obj != NULL);
222
14.6M
    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.6M
    return 0;
232
14.6M
}
233
234
static int
235
member_set(PyObject *self, PyObject *obj, PyObject *value)
236
4.40M
{
237
4.40M
    PyMemberDescrObject *descr = (PyMemberDescrObject *)self;
238
4.40M
    if (descr_setcheck((PyDescrObject *)descr, obj, value) < 0) {
239
0
        return -1;
240
0
    }
241
4.40M
    return PyMember_SetOne((char *)obj, descr->d_member, value);
242
4.40M
}
243
244
static int
245
getset_set(PyObject *self, PyObject *obj, PyObject *value)
246
10.2M
{
247
10.2M
    PyGetSetDescrObject *descr = (PyGetSetDescrObject *)self;
248
10.2M
    if (descr_setcheck((PyDescrObject *)descr, obj, value) < 0) {
249
0
        return -1;
250
0
    }
251
10.2M
    if (descr->d_getset->set != NULL) {
252
10.2M
        return descr_set_trampoline_call(
253
10.2M
            descr->d_getset->set, obj, value,
254
10.2M
            descr->d_getset->closure);
255
10.2M
    }
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.2M
}
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
70.3M
{
271
70.3M
    assert(!PyErr_Occurred());
272
70.3M
    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
70.3M
    PyObject *self = args[0];
282
70.3M
    if (descr_check((PyDescrObject *)func, self) < 0) {
283
0
        return -1;
284
0
    }
285
70.3M
    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
70.3M
    return 0;
295
70.3M
}
296
297
typedef void (*funcptr)(void);
298
299
static inline funcptr
300
method_enter_call(PyThreadState *tstate, PyObject *func)
301
70.3M
{
302
70.3M
    if (_Py_EnterRecursiveCallTstate(tstate, " while calling a Python object")) {
303
0
        return NULL;
304
0
    }
305
70.3M
    return (funcptr)((PyMethodDescrObject *)func)->d_method->ml_meth;
306
70.3M
}
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.12M
{
338
9.12M
    PyThreadState *tstate = _PyThreadState_GET();
339
9.12M
    Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
340
9.12M
    if (method_check_args(func, args, nargs, NULL)) {
341
0
        return NULL;
342
0
    }
343
9.12M
    PyObject *argstuple = PyTuple_FromArray(args+1, nargs-1);
344
9.12M
    if (argstuple == NULL) {
345
0
        return NULL;
346
0
    }
347
9.12M
    PyObject *result = NULL;
348
    /* Create a temporary dict for keyword arguments */
349
9.12M
    PyObject *kwdict = NULL;
350
9.12M
    if (kwnames != NULL && PyTuple_GET_SIZE(kwnames) > 0) {
351
5.52k
        kwdict = _PyStack_AsDict(args + nargs, kwnames);
352
5.52k
        if (kwdict == NULL) {
353
0
            goto exit;
354
0
        }
355
5.52k
    }
356
9.12M
    PyCFunctionWithKeywords meth = (PyCFunctionWithKeywords)
357
9.12M
                                   method_enter_call(tstate, func);
358
9.12M
    if (meth == NULL) {
359
0
        goto exit;
360
0
    }
361
9.12M
    result = _PyCFunctionWithKeywords_TrampolineCall(
362
9.12M
        meth, args[0], argstuple, kwdict);
363
9.12M
    _Py_LeaveRecursiveCallTstate(tstate);
364
9.12M
exit:
365
9.12M
    Py_DECREF(argstuple);
366
9.12M
    Py_XDECREF(kwdict);
367
9.12M
    return result;
368
9.12M
}
369
370
static PyObject *
371
method_vectorcall_FASTCALL_KEYWORDS_METHOD(
372
    PyObject *func, PyObject *const *args, size_t nargsf, PyObject *kwnames)
373
34.2M
{
374
34.2M
    PyThreadState *tstate = _PyThreadState_GET();
375
34.2M
    Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
376
34.2M
    if (method_check_args(func, args, nargs, NULL)) {
377
0
        return NULL;
378
0
    }
379
34.2M
    PyCMethod meth = (PyCMethod) method_enter_call(tstate, func);
380
34.2M
    if (meth == NULL) {
381
0
        return NULL;
382
0
    }
383
34.2M
    PyObject *result = meth(args[0],
384
34.2M
                            ((PyMethodDescrObject *)func)->d_common.d_type,
385
34.2M
                            args+1, nargs-1, kwnames);
386
34.2M
    _Py_LeaveRecursiveCall();
387
34.2M
    return result;
388
34.2M
}
389
390
static PyObject *
391
method_vectorcall_FASTCALL(
392
    PyObject *func, PyObject *const *args, size_t nargsf, PyObject *kwnames)
393
128k
{
394
128k
    PyThreadState *tstate = _PyThreadState_GET();
395
128k
    Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
396
128k
    if (method_check_args(func, args, nargs, kwnames)) {
397
0
        return NULL;
398
0
    }
399
128k
    PyCFunctionFast meth = (PyCFunctionFast)
400
128k
                            method_enter_call(tstate, func);
401
128k
    if (meth == NULL) {
402
0
        return NULL;
403
0
    }
404
128k
    PyObject *result = meth(args[0], args+1, nargs-1);
405
128k
    _Py_LeaveRecursiveCallTstate(tstate);
406
128k
    return result;
407
128k
}
408
409
static PyObject *
410
method_vectorcall_FASTCALL_KEYWORDS(
411
    PyObject *func, PyObject *const *args, size_t nargsf, PyObject *kwnames)
412
4.12M
{
413
4.12M
    PyThreadState *tstate = _PyThreadState_GET();
414
4.12M
    Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
415
4.12M
    if (method_check_args(func, args, nargs, NULL)) {
416
0
        return NULL;
417
0
    }
418
4.12M
    PyCFunctionFastWithKeywords meth = (PyCFunctionFastWithKeywords)
419
4.12M
                                        method_enter_call(tstate, func);
420
4.12M
    if (meth == NULL) {
421
0
        return NULL;
422
0
    }
423
4.12M
    PyObject *result = meth(args[0], args+1, nargs-1, kwnames);
424
4.12M
    _Py_LeaveRecursiveCallTstate(tstate);
425
4.12M
    return result;
426
4.12M
}
427
428
static PyObject *
429
method_vectorcall_NOARGS(
430
    PyObject *func, PyObject *const *args, size_t nargsf, PyObject *kwnames)
431
3.49M
{
432
3.49M
    PyThreadState *tstate = _PyThreadState_GET();
433
3.49M
    Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
434
3.49M
    if (method_check_args(func, args, nargs, kwnames)) {
435
0
        return NULL;
436
0
    }
437
3.49M
    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.49M
    PyCFunction meth = (PyCFunction)method_enter_call(tstate, func);
447
3.49M
    if (meth == NULL) {
448
0
        return NULL;
449
0
    }
450
3.49M
    PyObject *result = _PyCFunction_TrampolineCall(meth, args[0], NULL);
451
3.49M
    _Py_LeaveRecursiveCallTstate(tstate);
452
3.49M
    return result;
453
3.49M
}
454
455
static PyObject *
456
method_vectorcall_O(
457
    PyObject *func, PyObject *const *args, size_t nargsf, PyObject *kwnames)
458
19.2M
{
459
19.2M
    PyThreadState *tstate = _PyThreadState_GET();
460
19.2M
    Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
461
19.2M
    if (method_check_args(func, args, nargs, kwnames)) {
462
0
        return NULL;
463
0
    }
464
19.2M
    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
19.2M
    PyCFunction meth = (PyCFunction)method_enter_call(tstate, func);
475
19.2M
    if (meth == NULL) {
476
0
        return NULL;
477
0
    }
478
19.2M
    PyObject *result = _PyCFunction_TrampolineCall(meth, args[0], args[1]);
479
19.2M
    _Py_LeaveRecursiveCallTstate(tstate);
480
19.2M
    return result;
481
19.2M
}
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
27.1M
{
521
27.1M
    wrapperfunc wrapper = descr->d_base->wrapper;
522
523
27.1M
    if (descr->d_base->flags & PyWrapperFlag_KEYWORDS) {
524
19.1M
        wrapperfunc_kwds wk = _Py_FUNC_CAST(wrapperfunc_kwds, wrapper);
525
19.1M
        return (*wk)(self, args, descr->d_wrapped, kwds);
526
19.1M
    }
527
528
7.94M
    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
7.94M
    return (*wrapper)(self, args, descr->d_wrapped);
535
7.94M
}
536
537
static PyObject *
538
wrapperdescr_call(PyObject *_descr, PyObject *args, PyObject *kwds)
539
16.5M
{
540
16.5M
    PyWrapperDescrObject *descr = (PyWrapperDescrObject *)_descr;
541
16.5M
    Py_ssize_t argc;
542
16.5M
    PyObject *self, *result;
543
544
    /* Make sure that the first argument is acceptable as 'self' */
545
16.5M
    assert(PyTuple_Check(args));
546
16.5M
    argc = PyTuple_GET_SIZE(args);
547
16.5M
    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
16.5M
    self = PyTuple_GET_ITEM(args, 0);
556
16.5M
    if (!_PyObject_RealIsSubclass((PyObject *)Py_TYPE(self),
557
16.5M
                                  (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
16.5M
    args = PyTuple_GetSlice(args, 1, argc);
569
16.5M
    if (args == NULL) {
570
0
        return NULL;
571
0
    }
572
16.5M
    result = wrapperdescr_raw_call(descr, self, args, kwds);
573
16.5M
    Py_DECREF(args);
574
16.5M
    return result;
575
16.5M
}
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
4
{
707
4
    PyWrapperDescrObject *descr = (PyWrapperDescrObject *)self;
708
4
    return _PyType_GetTextSignatureFromInternalDoc(descr->d_base->name,
709
4
                                                   descr->d_base->doc, 0);
710
4
}
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.33M
{
722
4.33M
    PyDescrObject *descr = (PyDescrObject *)self;
723
4.33M
    Py_VISIT(descr->d_type);
724
4.33M
    return 0;
725
4.33M
}
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
101k
{
919
101k
    PyDescrObject *descr;
920
921
101k
    descr = (PyDescrObject *)PyType_GenericAlloc(descrtype, 0);
922
101k
    if (descr != NULL) {
923
101k
        _PyObject_SetDeferredRefcount((PyObject *)descr);
924
101k
        descr->d_type = (PyTypeObject*)Py_XNewRef(type);
925
101k
        descr->d_name = PyUnicode_InternFromString(name);
926
101k
        if (descr->d_name == NULL) {
927
0
            Py_SETREF(descr, NULL);
928
0
        }
929
101k
        else {
930
101k
            descr->d_qualname = NULL;
931
101k
        }
932
101k
    }
933
101k
    return descr;
934
101k
}
935
936
PyObject *
937
PyDescr_NewMethod(PyTypeObject *type, PyMethodDef *method)
938
32.2k
{
939
    /* Figure out correct vectorcall function to use */
940
32.2k
    vectorcallfunc vectorcall;
941
32.2k
    switch (method->ml_flags & _Py_METH_CALL_FLAGS)
942
32.2k
    {
943
86
        case METH_VARARGS:
944
86
            vectorcall = method_vectorcall_VARARGS;
945
86
            break;
946
889
        case METH_VARARGS | METH_KEYWORDS:
947
889
            vectorcall = method_vectorcall_VARARGS_KEYWORDS;
948
889
            break;
949
4.83k
        case METH_FASTCALL:
950
4.83k
            vectorcall = method_vectorcall_FASTCALL;
951
4.83k
            break;
952
2.30k
        case METH_FASTCALL | METH_KEYWORDS:
953
2.30k
            vectorcall = method_vectorcall_FASTCALL_KEYWORDS;
954
2.30k
            break;
955
16.4k
        case METH_NOARGS:
956
16.4k
            vectorcall = method_vectorcall_NOARGS;
957
16.4k
            break;
958
6.21k
        case METH_O:
959
6.21k
            vectorcall = method_vectorcall_O;
960
6.21k
            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
32.2k
    }
969
970
32.2k
    PyMethodDescrObject *descr;
971
972
32.2k
    descr = (PyMethodDescrObject *)descr_new(&PyMethodDescr_Type,
973
32.2k
                                             type, method->ml_name);
974
32.2k
    if (descr != NULL) {
975
32.2k
        descr->d_method = method;
976
32.2k
        descr->vectorcall = vectorcall;
977
32.2k
    }
978
32.2k
    return (PyObject *)descr;
979
32.2k
}
980
981
PyObject *
982
PyDescr_NewClassMethod(PyTypeObject *type, PyMethodDef *method)
983
2.24k
{
984
2.24k
    PyMethodDescrObject *descr;
985
986
2.24k
    descr = (PyMethodDescrObject *)descr_new(&PyClassMethodDescr_Type,
987
2.24k
                                             type, method->ml_name);
988
2.24k
    if (descr != NULL)
989
2.24k
        descr->d_method = method;
990
2.24k
    return (PyObject *)descr;
991
2.24k
}
992
993
PyObject *
994
PyDescr_NewMember(PyTypeObject *type, PyMemberDef *member)
995
15.2k
{
996
15.2k
    PyMemberDescrObject *descr;
997
998
15.2k
    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.2k
    descr = (PyMemberDescrObject *)descr_new(&PyMemberDescr_Type,
1005
15.2k
                                             type, member->name);
1006
15.2k
    if (descr != NULL)
1007
15.2k
        descr->d_member = member;
1008
15.2k
    return (PyObject *)descr;
1009
15.2k
}
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.5k
{
1026
42.5k
    PyWrapperDescrObject *descr;
1027
1028
42.5k
    descr = (PyWrapperDescrObject *)descr_new(&PyWrapperDescr_Type,
1029
42.5k
                                             type, base->name);
1030
42.5k
    if (descr != NULL) {
1031
42.5k
        descr->d_base = base;
1032
42.5k
        descr->d_wrapped = wrapped;
1033
42.5k
    }
1034
42.5k
    return (PyObject *)descr;
1035
42.5k
}
1036
1037
int
1038
PyDescr_IsData(PyObject *ob)
1039
210M
{
1040
210M
    return Py_TYPE(ob)->tp_descr_set != NULL;
1041
210M
}
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
8.69k
{
1100
8.69k
    mappingproxyobject *pp = (mappingproxyobject *)self;
1101
8.69k
    if (PyDict_CheckExact(pp->mapping))
1102
3.30k
        return PyDict_Contains(pp->mapping, key);
1103
5.39k
    else
1104
5.39k
        return PySequence_Contains(pp->mapping, key);
1105
8.69k
}
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
8.60k
{
1123
8.60k
    mappingproxyobject *pp = (mappingproxyobject *)self;
1124
    /* newargs: mapping, key, default=None */
1125
8.60k
    PyObject *newargs[3];
1126
8.60k
    newargs[0] = pp->mapping;
1127
8.60k
    newargs[2] = Py_None;
1128
1129
8.60k
    if (!_PyArg_UnpackStack(args, nargs, "get", 1, 2,
1130
8.60k
                            &newargs[1], &newargs[2]))
1131
0
    {
1132
0
        return NULL;
1133
0
    }
1134
8.60k
    return PyObject_VectorcallMethod(&_Py_ID(get), newargs,
1135
8.60k
                                     3 | PY_VECTORCALL_ARGUMENTS_OFFSET,
1136
8.60k
                                     NULL);
1137
8.60k
}
1138
1139
static PyObject *
1140
mappingproxy_keys(PyObject *self, PyObject *Py_UNUSED(ignored))
1141
64.5k
{
1142
64.5k
    mappingproxyobject *pp = (mappingproxyobject *)self;
1143
64.5k
    return PyObject_CallMethodNoArgs(pp->mapping, &_Py_ID(keys));
1144
64.5k
}
1145
1146
static PyObject *
1147
mappingproxy_values(PyObject *self, PyObject *Py_UNUSED(ignored))
1148
54.6k
{
1149
54.6k
    mappingproxyobject *pp = (mappingproxyobject *)self;
1150
54.6k
    return PyObject_CallMethodNoArgs(pp->mapping, &_Py_ID(values));
1151
54.6k
}
1152
1153
static PyObject *
1154
mappingproxy_items(PyObject *self, PyObject *Py_UNUSED(ignored))
1155
2.33k
{
1156
2.33k
    mappingproxyobject *pp = (mappingproxyobject *)self;
1157
2.33k
    return PyObject_CallMethodNoArgs(pp->mapping, &_Py_ID(items));
1158
2.33k
}
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
97.0k
{
1199
97.0k
    mappingproxyobject *pp = (mappingproxyobject *)self;
1200
97.0k
    _PyObject_GC_UNTRACK(pp);
1201
97.0k
    Py_DECREF(pp->mapping);
1202
97.0k
    PyObject_GC_Del(pp);
1203
97.0k
}
1204
1205
static PyObject *
1206
mappingproxy_getiter(PyObject *self)
1207
10.9k
{
1208
10.9k
    mappingproxyobject *pp = (mappingproxyobject *)self;
1209
10.9k
    return PyObject_GetIter(pp->mapping);
1210
10.9k
}
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
450
{
1236
450
    mappingproxyobject *pp = (mappingproxyobject *)self;
1237
450
    Py_VISIT(pp->mapping);
1238
450
    return 0;
1239
450
}
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
97.0k
{
1275
97.0k
    if (!PyMapping_Check(mapping)
1276
97.0k
        || PyList_Check(mapping)
1277
97.0k
        || 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
97.0k
    return 0;
1284
97.0k
}
1285
1286
PyObject *
1287
PyDictProxy_New(PyObject *mapping)
1288
97.0k
{
1289
97.0k
    mappingproxyobject *pp;
1290
1291
97.0k
    if (mappingproxy_check_mapping(mapping) == -1)
1292
0
        return NULL;
1293
1294
97.0k
    pp = PyObject_GC_New(mappingproxyobject, &PyDictProxy_Type);
1295
97.0k
    if (pp != NULL) {
1296
97.0k
        pp->mapping = Py_NewRef(mapping);
1297
97.0k
        _PyObject_GC_TRACK(pp);
1298
97.0k
    }
1299
97.0k
    return (PyObject *)pp;
1300
97.0k
}
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
16.6k
{
1315
16.6k
    return PyDictProxy_New(mapping);
1316
16.6k
}
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
10.4M
{
1335
10.4M
    wrapperobject *wp = (wrapperobject *)self;
1336
10.4M
    PyObject_GC_UnTrack(wp);
1337
10.4M
    Py_XDECREF(wp->descr);
1338
10.4M
    Py_XDECREF(wp->self);
1339
10.4M
    PyObject_GC_Del(wp);
1340
10.4M
}
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
10.5M
{
1459
10.5M
    wrapperobject *wp = (wrapperobject *)self;
1460
10.5M
    return wrapperdescr_raw_call(wp->descr, wp->self, args, kwds);
1461
10.5M
}
1462
1463
static int
1464
wrapper_traverse(PyObject *self, visitproc visit, void *arg)
1465
5.59k
{
1466
5.59k
    wrapperobject *wp = (wrapperobject *)self;
1467
5.59k
    Py_VISIT(wp->descr);
1468
5.59k
    Py_VISIT(wp->self);
1469
5.59k
    return 0;
1470
5.59k
}
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
10.4M
{
1513
10.4M
    wrapperobject *wp;
1514
10.4M
    PyWrapperDescrObject *descr;
1515
1516
10.4M
    assert(PyObject_TypeCheck(d, &PyWrapperDescr_Type));
1517
10.4M
    descr = (PyWrapperDescrObject *)d;
1518
10.4M
    assert(_PyObject_RealIsSubclass((PyObject *)Py_TYPE(self),
1519
10.4M
                                    (PyObject *)PyDescr_TYPE(descr)));
1520
1521
10.4M
    wp = PyObject_GC_New(wrapperobject, &_PyMethodWrapper_Type);
1522
10.4M
    if (wp != NULL) {
1523
10.4M
        wp->descr = (PyWrapperDescrObject*)Py_NewRef(descr);
1524
10.4M
        wp->self = Py_NewRef(self);
1525
10.4M
        _PyObject_GC_TRACK(wp);
1526
10.4M
    }
1527
10.4M
    return (PyObject *)wp;
1528
10.4M
}
1529
1530
1531
/* A built-in 'property' type */
1532
1533
295
#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
228
{
1608
228
    return property_copy(self, NULL, setter, NULL);
1609
228
}
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
8
{
1618
8
    return property_copy(self, NULL, NULL, deleter);
1619
8
}
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.88k
{
1637
2.88k
    Py_XSETREF(self->prop_name, Py_XNewRef(name));
1638
1639
2.88k
    Py_RETURN_NONE;
1640
2.88k
}
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
236
{
1654
236
    propertyobject *gs = (propertyobject *)self;
1655
1656
236
    _PyObject_GC_UNTRACK(self);
1657
236
    Py_XDECREF(gs->prop_get);
1658
236
    Py_XDECREF(gs->prop_set);
1659
236
    Py_XDECREF(gs->prop_del);
1660
236
    Py_XDECREF(gs->prop_doc);
1661
236
    Py_XDECREF(gs->prop_name);
1662
236
    Py_TYPE(self)->tp_free(self);
1663
236
}
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
10.7M
{
1682
10.7M
    if (obj == NULL || obj == Py_None) {
1683
50
        return Py_NewRef(self);
1684
50
    }
1685
1686
10.7M
    propertyobject *gs = (propertyobject *)self;
1687
10.7M
    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
10.7M
    return PyObject_CallOneArg(gs->prop_get, obj);
1713
10.7M
}
1714
1715
static int
1716
property_descr_set(PyObject *self, PyObject *obj, PyObject *value)
1717
8.38k
{
1718
8.38k
    propertyobject *gs = (propertyobject *)self;
1719
8.38k
    PyObject *func, *res;
1720
1721
8.38k
    if (value == NULL) {
1722
0
        func = gs->prop_del;
1723
0
    }
1724
8.38k
    else {
1725
8.38k
        func = gs->prop_set;
1726
8.38k
    }
1727
1728
8.38k
    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.38k
    if (value == NULL) {
1764
0
        res = PyObject_CallOneArg(func, obj);
1765
0
    }
1766
8.38k
    else {
1767
8.38k
        EVAL_CALL_STAT_INC_IF_FUNCTION(EVAL_CALL_API, func);
1768
8.38k
        PyObject *args[] = { obj, value };
1769
8.38k
        res = PyObject_Vectorcall(func, args, 2, NULL);
1770
8.38k
    }
1771
1772
8.38k
    if (res == NULL) {
1773
0
        return -1;
1774
0
    }
1775
1776
8.38k
    Py_DECREF(res);
1777
8.38k
    return 0;
1778
8.38k
}
1779
1780
static PyObject *
1781
property_copy(PyObject *old, PyObject *get, PyObject *set, PyObject *del)
1782
236
{
1783
236
    propertyobject *pold = (propertyobject *)old;
1784
236
    PyObject *new, *type, *doc;
1785
1786
236
    type = PyObject_Type(old);
1787
236
    if (type == NULL)
1788
0
        return NULL;
1789
1790
236
    if (get == NULL || get == Py_None) {
1791
236
        get = pold->prop_get ? pold->prop_get : Py_None;
1792
236
    }
1793
236
    if (set == NULL || set == Py_None) {
1794
8
        set = pold->prop_set ? pold->prop_set : Py_None;
1795
8
    }
1796
236
    if (del == NULL || del == Py_None) {
1797
228
        del = pold->prop_del ? pold->prop_del : Py_None;
1798
228
    }
1799
236
    if (pold->getter_doc && get != Py_None) {
1800
        /* make _init use __doc__ from getter */
1801
78
        doc = Py_None;
1802
78
    }
1803
158
    else {
1804
158
        doc = pold->prop_doc ? pold->prop_doc : Py_None;
1805
158
    }
1806
1807
236
    new =  PyObject_CallFunctionObjArgs(type, get, set, del, doc, NULL);
1808
236
    Py_DECREF(type);
1809
236
    if (new == NULL)
1810
0
        return NULL;
1811
1812
236
    if (PyObject_TypeCheck((new), &PyProperty_Type)) {
1813
236
        Py_XSETREF(((propertyobject *) new)->prop_name, Py_XNewRef(pold->prop_name));
1814
236
    }
1815
236
    return new;
1816
236
}
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.90k
{
1860
2.90k
    if (fget == Py_None)
1861
0
        fget = NULL;
1862
2.90k
    if (fset == Py_None)
1863
0
        fset = NULL;
1864
2.90k
    if (fdel == Py_None)
1865
228
        fdel = NULL;
1866
1867
2.90k
    Py_XSETREF(self->prop_get, Py_XNewRef(fget));
1868
2.90k
    Py_XSETREF(self->prop_set, Py_XNewRef(fset));
1869
2.90k
    Py_XSETREF(self->prop_del, Py_XNewRef(fdel));
1870
2.90k
    Py_XSETREF(self->prop_doc, NULL);
1871
2.90k
    Py_XSETREF(self->prop_name, NULL);
1872
1873
2.90k
    self->getter_doc = 0;
1874
2.90k
    PyObject *prop_doc = NULL;
1875
1876
2.90k
    if (doc != NULL && doc != Py_None) {
1877
4
        prop_doc = Py_XNewRef(doc);
1878
4
    }
1879
    /* if no docstring given and the getter has one, use that one */
1880
2.89k
    else if (fget != NULL) {
1881
2.89k
        int rc = PyObject_GetOptionalAttr(fget, &_Py_ID(__doc__), &prop_doc);
1882
2.89k
        if (rc < 0) {
1883
0
            return rc;
1884
0
        }
1885
2.89k
        if (prop_doc == Py_None) {
1886
1.91k
            prop_doc = NULL;
1887
1.91k
            Py_DECREF(Py_None);
1888
1.91k
        }
1889
2.89k
        if (prop_doc != NULL){
1890
982
            self->getter_doc = 1;
1891
982
        }
1892
2.89k
    }
1893
1894
    /* At this point `prop_doc` is either NULL or
1895
       a non-None object with incremented ref counter */
1896
1897
2.90k
    if (Py_IS_TYPE(self, &PyProperty_Type)) {
1898
2.90k
        Py_XSETREF(self->prop_doc, prop_doc);
1899
2.90k
    } 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.90k
    return 0;
1931
2.90k
}
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
295
{
1959
295
    propertyobject *prop = _propertyobject_CAST(op);
1960
295
    int res = _PyObject_IsAbstract(prop->prop_get);
1961
295
    if (res == -1) {
1962
0
        return NULL;
1963
0
    }
1964
295
    else if (res) {
1965
20
        Py_RETURN_TRUE;
1966
20
    }
1967
1968
275
    res = _PyObject_IsAbstract(prop->prop_set);
1969
275
    if (res == -1) {
1970
0
        return NULL;
1971
0
    }
1972
275
    else if (res) {
1973
0
        Py_RETURN_TRUE;
1974
0
    }
1975
1976
275
    res = _PyObject_IsAbstract(prop->prop_del);
1977
275
    if (res == -1) {
1978
0
        return NULL;
1979
0
    }
1980
275
    else if (res) {
1981
0
        Py_RETURN_TRUE;
1982
0
    }
1983
275
    Py_RETURN_FALSE;
1984
275
}
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
92.7k
{
1997
92.7k
    propertyobject *pp = (propertyobject *)self;
1998
92.7k
    Py_VISIT(pp->prop_get);
1999
92.7k
    Py_VISIT(pp->prop_set);
2000
92.7k
    Py_VISIT(pp->prop_del);
2001
92.7k
    Py_VISIT(pp->prop_doc);
2002
92.7k
    Py_VISIT(pp->prop_name);
2003
92.7k
    return 0;
2004
92.7k
}
2005
2006
static int
2007
property_clear(PyObject *self)
2008
0
{
2009
0
    propertyobject *pp = (propertyobject *)self;
2010
    Py_CLEAR(pp->prop_doc);
2011
0
    return 0;
2012
0
}
2013
2014
2015
PyTypeObject PyDictProxy_Type = {
2016
    PyVarObject_HEAD_INIT(&PyType_Type, 0)
2017
    "mappingproxy",                             /* tp_name */
2018
    sizeof(mappingproxyobject),                 /* tp_basicsize */
2019
    0,                                          /* tp_itemsize */
2020
    /* methods */
2021
    mappingproxy_dealloc,                       /* tp_dealloc */
2022
    0,                                          /* tp_vectorcall_offset */
2023
    0,                                          /* tp_getattr */
2024
    0,                                          /* tp_setattr */
2025
    0,                                          /* tp_as_async */
2026
    mappingproxy_repr,                          /* tp_repr */
2027
    &mappingproxy_as_number,                    /* tp_as_number */
2028
    &mappingproxy_as_sequence,                  /* tp_as_sequence */
2029
    &mappingproxy_as_mapping,                   /* tp_as_mapping */
2030
    mappingproxy_hash,                          /* tp_hash */
2031
    0,                                          /* tp_call */
2032
    mappingproxy_str,                           /* tp_str */
2033
    PyObject_GenericGetAttr,                    /* tp_getattro */
2034
    0,                                          /* tp_setattro */
2035
    0,                                          /* tp_as_buffer */
2036
    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
2037
        Py_TPFLAGS_MAPPING,                     /* tp_flags */
2038
    mappingproxy_new__doc__,                    /* tp_doc */
2039
    mappingproxy_traverse,                      /* tp_traverse */
2040
    0,                                          /* tp_clear */
2041
    mappingproxy_richcompare,                   /* tp_richcompare */
2042
    0,                                          /* tp_weaklistoffset */
2043
    mappingproxy_getiter,                       /* tp_iter */
2044
    0,                                          /* tp_iternext */
2045
    mappingproxy_methods,                       /* tp_methods */
2046
    0,                                          /* tp_members */
2047
    0,                                          /* tp_getset */
2048
    0,                                          /* tp_base */
2049
    0,                                          /* tp_dict */
2050
    0,                                          /* tp_descr_get */
2051
    0,                                          /* tp_descr_set */
2052
    0,                                          /* tp_dictoffset */
2053
    0,                                          /* tp_init */
2054
    0,                                          /* tp_alloc */
2055
    mappingproxy_new,                           /* tp_new */
2056
};
2057
2058
PyTypeObject PyProperty_Type = {
2059
    PyVarObject_HEAD_INIT(&PyType_Type, 0)
2060
    "property",                                 /* tp_name */
2061
    sizeof(propertyobject),                     /* tp_basicsize */
2062
    0,                                          /* tp_itemsize */
2063
    /* methods */
2064
    property_dealloc,                           /* tp_dealloc */
2065
    0,                                          /* tp_vectorcall_offset */
2066
    0,                                          /* tp_getattr */
2067
    0,                                          /* tp_setattr */
2068
    0,                                          /* tp_as_async */
2069
    0,                                          /* tp_repr */
2070
    0,                                          /* tp_as_number */
2071
    0,                                          /* tp_as_sequence */
2072
    0,                                          /* tp_as_mapping */
2073
    0,                                          /* tp_hash */
2074
    0,                                          /* tp_call */
2075
    0,                                          /* tp_str */
2076
    PyObject_GenericGetAttr,                    /* tp_getattro */
2077
    0,                                          /* tp_setattro */
2078
    0,                                          /* tp_as_buffer */
2079
    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
2080
        Py_TPFLAGS_BASETYPE,                    /* tp_flags */
2081
    property_init__doc__,                       /* tp_doc */
2082
    property_traverse,                          /* tp_traverse */
2083
    property_clear,                             /* tp_clear */
2084
    0,                                          /* tp_richcompare */
2085
    0,                                          /* tp_weaklistoffset */
2086
    0,                                          /* tp_iter */
2087
    0,                                          /* tp_iternext */
2088
    property_methods,                           /* tp_methods */
2089
    property_members,                           /* tp_members */
2090
    property_getsetlist,                        /* tp_getset */
2091
    0,                                          /* tp_base */
2092
    0,                                          /* tp_dict */
2093
    property_descr_get,                         /* tp_descr_get */
2094
    property_descr_set,                         /* tp_descr_set */
2095
    0,                                          /* tp_dictoffset */
2096
    property_init,                              /* tp_init */
2097
    PyType_GenericAlloc,                        /* tp_alloc */
2098
    PyType_GenericNew,                          /* tp_new */
2099
    PyObject_GC_Del,                            /* tp_free */
2100
};