1
2 /* Function object implementation */
3
4 #include "Python.h"
5 #include "pycore_ceval.h" // _PyEval_BuiltinsFromGlobals()
6 #include "pycore_code.h" // _Py_next_func_version
7 #include "pycore_object.h" // _PyObject_GC_UNTRACK()
8 #include "pycore_pyerrors.h" // _PyErr_Occurred()
9 #include "structmember.h" // PyMemberDef
10
11 static PyObject* func_repr(PyFunctionObject *op);
12
13 static const char *
14 func_event_name(PyFunction_WatchEvent event) {
15 switch (event) {
16 #define CASE(op) \
17 case PyFunction_EVENT_##op: \
18 return "PyFunction_EVENT_" #op;
19 PY_FOREACH_FUNC_EVENT(CASE)
20 #undef CASE
21 }
22 Py_UNREACHABLE();
23 }
24
25 static void
26 notify_func_watchers(PyInterpreterState *interp, PyFunction_WatchEvent event,
27 PyFunctionObject *func, PyObject *new_value)
28 {
29 uint8_t bits = interp->active_func_watchers;
30 int i = 0;
31 while (bits) {
32 assert(i < FUNC_MAX_WATCHERS);
33 if (bits & 1) {
34 PyFunction_WatchCallback cb = interp->func_watchers[i];
35 // callback must be non-null if the watcher bit is set
36 assert(cb != NULL);
37 if (cb(event, func, new_value) < 0) {
38 // Don't risk resurrecting the func if an unraisablehook keeps a
39 // reference; pass a string as context.
40 PyObject *context = NULL;
41 PyObject *repr = func_repr(func);
42 if (repr != NULL) {
43 context = PyUnicode_FromFormat(
44 "%s watcher callback for %U",
45 func_event_name(event), repr);
46 Py_DECREF(repr);
47 }
48 if (context == NULL) {
49 context = Py_NewRef(Py_None);
50 }
51 PyErr_WriteUnraisable(context);
52 Py_DECREF(context);
53 }
54 }
55 i++;
56 bits >>= 1;
57 }
58 }
59
60 static inline void
61 handle_func_event(PyFunction_WatchEvent event, PyFunctionObject *func,
62 PyObject *new_value)
63 {
64 assert(Py_REFCNT(func) > 0);
65 PyInterpreterState *interp = _PyInterpreterState_GET();
66 assert(interp->_initialized);
67 if (interp->active_func_watchers) {
68 notify_func_watchers(interp, event, func, new_value);
69 }
70 }
71
72 int
73 PyFunction_AddWatcher(PyFunction_WatchCallback callback)
74 {
75 PyInterpreterState *interp = _PyInterpreterState_GET();
76 assert(interp->_initialized);
77 for (int i = 0; i < FUNC_MAX_WATCHERS; i++) {
78 if (interp->func_watchers[i] == NULL) {
79 interp->func_watchers[i] = callback;
80 interp->active_func_watchers |= (1 << i);
81 return i;
82 }
83 }
84 PyErr_SetString(PyExc_RuntimeError, "no more func watcher IDs available");
85 return -1;
86 }
87
88 int
89 PyFunction_ClearWatcher(int watcher_id)
90 {
91 PyInterpreterState *interp = _PyInterpreterState_GET();
92 if (watcher_id < 0 || watcher_id >= FUNC_MAX_WATCHERS) {
93 PyErr_Format(PyExc_ValueError, "invalid func watcher ID %d",
94 watcher_id);
95 return -1;
96 }
97 if (!interp->func_watchers[watcher_id]) {
98 PyErr_Format(PyExc_ValueError, "no func watcher set for ID %d",
99 watcher_id);
100 return -1;
101 }
102 interp->func_watchers[watcher_id] = NULL;
103 interp->active_func_watchers &= ~(1 << watcher_id);
104 return 0;
105 }
106 PyFunctionObject *
107 _PyFunction_FromConstructor(PyFrameConstructor *constr)
108 {
109 PyObject *module = Py_XNewRef(PyDict_GetItemWithError(constr->fc_globals, &_Py_ID(__name__)));
110 if (!module && PyErr_Occurred()) {
111 return NULL;
112 }
113
114 PyFunctionObject *op = PyObject_GC_New(PyFunctionObject, &PyFunction_Type);
115 if (op == NULL) {
116 Py_XDECREF(module);
117 return NULL;
118 }
119 op->func_globals = Py_NewRef(constr->fc_globals);
120 op->func_builtins = Py_NewRef(constr->fc_builtins);
121 op->func_name = Py_NewRef(constr->fc_name);
122 op->func_qualname = Py_NewRef(constr->fc_qualname);
123 op->func_code = Py_NewRef(constr->fc_code);
124 op->func_defaults = Py_XNewRef(constr->fc_defaults);
125 op->func_kwdefaults = Py_XNewRef(constr->fc_kwdefaults);
126 op->func_closure = Py_XNewRef(constr->fc_closure);
127 op->func_doc = Py_NewRef(Py_None);
128 op->func_dict = NULL;
129 op->func_weakreflist = NULL;
130 op->func_module = module;
131 op->func_annotations = NULL;
132 op->func_typeparams = NULL;
133 op->vectorcall = _PyFunction_Vectorcall;
134 op->func_version = 0;
135 _PyObject_GC_TRACK(op);
136 handle_func_event(PyFunction_EVENT_CREATE, op, NULL);
137 return op;
138 }
139
140 PyObject *
141 PyFunction_NewWithQualName(PyObject *code, PyObject *globals, PyObject *qualname)
142 {
143 assert(globals != NULL);
144 assert(PyDict_Check(globals));
145 Py_INCREF(globals);
146
147 PyThreadState *tstate = _PyThreadState_GET();
148
149 PyCodeObject *code_obj = (PyCodeObject *)Py_NewRef(code);
150
151 assert(code_obj->co_name != NULL);
152 PyObject *name = Py_NewRef(code_obj->co_name);
153
154 if (!qualname) {
155 qualname = code_obj->co_qualname;
156 }
157 assert(qualname != NULL);
158 Py_INCREF(qualname);
159
160 PyObject *consts = code_obj->co_consts;
161 assert(PyTuple_Check(consts));
162 PyObject *doc;
163 if (PyTuple_Size(consts) >= 1) {
164 doc = PyTuple_GetItem(consts, 0);
165 if (!PyUnicode_Check(doc)) {
166 doc = Py_None;
167 }
168 }
169 else {
170 doc = Py_None;
171 }
172 Py_INCREF(doc);
173
174 // __module__: Use globals['__name__'] if it exists, or NULL.
175 PyObject *module = PyDict_GetItemWithError(globals, &_Py_ID(__name__));
176 PyObject *builtins = NULL;
177 if (module == NULL && _PyErr_Occurred(tstate)) {
178 goto error;
179 }
180 Py_XINCREF(module);
181
182 builtins = _PyEval_BuiltinsFromGlobals(tstate, globals); // borrowed ref
183 if (builtins == NULL) {
184 goto error;
185 }
186 Py_INCREF(builtins);
187
188 PyFunctionObject *op = PyObject_GC_New(PyFunctionObject, &PyFunction_Type);
189 if (op == NULL) {
190 goto error;
191 }
192 /* Note: No failures from this point on, since func_dealloc() does not
193 expect a partially-created object. */
194
195 op->func_globals = globals;
196 op->func_builtins = builtins;
197 op->func_name = name;
198 op->func_qualname = qualname;
199 op->func_code = (PyObject*)code_obj;
200 op->func_defaults = NULL; // No default positional arguments
201 op->func_kwdefaults = NULL; // No default keyword arguments
202 op->func_closure = NULL;
203 op->func_doc = doc;
204 op->func_dict = NULL;
205 op->func_weakreflist = NULL;
206 op->func_module = module;
207 op->func_annotations = NULL;
208 op->func_typeparams = NULL;
209 op->vectorcall = _PyFunction_Vectorcall;
210 op->func_version = 0;
211 _PyObject_GC_TRACK(op);
212 handle_func_event(PyFunction_EVENT_CREATE, op, NULL);
213 return (PyObject *)op;
214
215 error:
216 Py_DECREF(globals);
217 Py_DECREF(code_obj);
218 Py_DECREF(name);
219 Py_DECREF(qualname);
220 Py_DECREF(doc);
221 Py_XDECREF(module);
222 Py_XDECREF(builtins);
223 return NULL;
224 }
225
226 uint32_t _PyFunction_GetVersionForCurrentState(PyFunctionObject *func)
227 {
228 if (func->func_version != 0) {
229 return func->func_version;
230 }
231 if (func->vectorcall != _PyFunction_Vectorcall) {
232 return 0;
233 }
234 PyInterpreterState *interp = _PyInterpreterState_GET();
235 if (interp->func_state.next_version == 0) {
236 return 0;
237 }
238 uint32_t v = interp->func_state.next_version++;
239 func->func_version = v;
240 return v;
241 }
242
243 PyObject *
244 PyFunction_New(PyObject *code, PyObject *globals)
245 {
246 return PyFunction_NewWithQualName(code, globals, NULL);
247 }
248
249 PyObject *
250 PyFunction_GetCode(PyObject *op)
251 {
252 if (!PyFunction_Check(op)) {
253 PyErr_BadInternalCall();
254 return NULL;
255 }
256 return ((PyFunctionObject *) op) -> func_code;
257 }
258
259 PyObject *
260 PyFunction_GetGlobals(PyObject *op)
261 {
262 if (!PyFunction_Check(op)) {
263 PyErr_BadInternalCall();
264 return NULL;
265 }
266 return ((PyFunctionObject *) op) -> func_globals;
267 }
268
269 PyObject *
270 PyFunction_GetModule(PyObject *op)
271 {
272 if (!PyFunction_Check(op)) {
273 PyErr_BadInternalCall();
274 return NULL;
275 }
276 return ((PyFunctionObject *) op) -> func_module;
277 }
278
279 PyObject *
280 PyFunction_GetDefaults(PyObject *op)
281 {
282 if (!PyFunction_Check(op)) {
283 PyErr_BadInternalCall();
284 return NULL;
285 }
286 return ((PyFunctionObject *) op) -> func_defaults;
287 }
288
289 int
290 PyFunction_SetDefaults(PyObject *op, PyObject *defaults)
291 {
292 if (!PyFunction_Check(op)) {
293 PyErr_BadInternalCall();
294 return -1;
295 }
296 if (defaults == Py_None)
297 defaults = NULL;
298 else if (defaults && PyTuple_Check(defaults)) {
299 Py_INCREF(defaults);
300 }
301 else {
302 PyErr_SetString(PyExc_SystemError, "non-tuple default args");
303 return -1;
304 }
305 handle_func_event(PyFunction_EVENT_MODIFY_DEFAULTS,
306 (PyFunctionObject *) op, defaults);
307 ((PyFunctionObject *)op)->func_version = 0;
308 Py_XSETREF(((PyFunctionObject *)op)->func_defaults, defaults);
309 return 0;
310 }
311
312 void
313 PyFunction_SetVectorcall(PyFunctionObject *func, vectorcallfunc vectorcall)
314 {
315 assert(func != NULL);
316 func->func_version = 0;
317 func->vectorcall = vectorcall;
318 }
319
320 PyObject *
321 PyFunction_GetKwDefaults(PyObject *op)
322 {
323 if (!PyFunction_Check(op)) {
324 PyErr_BadInternalCall();
325 return NULL;
326 }
327 return ((PyFunctionObject *) op) -> func_kwdefaults;
328 }
329
330 int
331 PyFunction_SetKwDefaults(PyObject *op, PyObject *defaults)
332 {
333 if (!PyFunction_Check(op)) {
334 PyErr_BadInternalCall();
335 return -1;
336 }
337 if (defaults == Py_None)
338 defaults = NULL;
339 else if (defaults && PyDict_Check(defaults)) {
340 Py_INCREF(defaults);
341 }
342 else {
343 PyErr_SetString(PyExc_SystemError,
344 "non-dict keyword only default args");
345 return -1;
346 }
347 handle_func_event(PyFunction_EVENT_MODIFY_KWDEFAULTS,
348 (PyFunctionObject *) op, defaults);
349 ((PyFunctionObject *)op)->func_version = 0;
350 Py_XSETREF(((PyFunctionObject *)op)->func_kwdefaults, defaults);
351 return 0;
352 }
353
354 PyObject *
355 PyFunction_GetClosure(PyObject *op)
356 {
357 if (!PyFunction_Check(op)) {
358 PyErr_BadInternalCall();
359 return NULL;
360 }
361 return ((PyFunctionObject *) op) -> func_closure;
362 }
363
364 int
365 PyFunction_SetClosure(PyObject *op, PyObject *closure)
366 {
367 if (!PyFunction_Check(op)) {
368 PyErr_BadInternalCall();
369 return -1;
370 }
371 if (closure == Py_None)
372 closure = NULL;
373 else if (PyTuple_Check(closure)) {
374 Py_INCREF(closure);
375 }
376 else {
377 PyErr_Format(PyExc_SystemError,
378 "expected tuple for closure, got '%.100s'",
379 Py_TYPE(closure)->tp_name);
380 return -1;
381 }
382 ((PyFunctionObject *)op)->func_version = 0;
383 Py_XSETREF(((PyFunctionObject *)op)->func_closure, closure);
384 return 0;
385 }
386
387 static PyObject *
388 func_get_annotation_dict(PyFunctionObject *op)
389 {
390 if (op->func_annotations == NULL) {
391 return NULL;
392 }
393 if (PyTuple_CheckExact(op->func_annotations)) {
394 PyObject *ann_tuple = op->func_annotations;
395 PyObject *ann_dict = PyDict_New();
396 if (ann_dict == NULL) {
397 return NULL;
398 }
399
400 assert(PyTuple_GET_SIZE(ann_tuple) % 2 == 0);
401
402 for (Py_ssize_t i = 0; i < PyTuple_GET_SIZE(ann_tuple); i += 2) {
403 int err = PyDict_SetItem(ann_dict,
404 PyTuple_GET_ITEM(ann_tuple, i),
405 PyTuple_GET_ITEM(ann_tuple, i + 1));
406
407 if (err < 0) {
408 return NULL;
409 }
410 }
411 Py_SETREF(op->func_annotations, ann_dict);
412 }
413 assert(PyDict_Check(op->func_annotations));
414 return op->func_annotations;
415 }
416
417 PyObject *
418 PyFunction_GetAnnotations(PyObject *op)
419 {
420 if (!PyFunction_Check(op)) {
421 PyErr_BadInternalCall();
422 return NULL;
423 }
424 return func_get_annotation_dict((PyFunctionObject *)op);
425 }
426
427 int
428 PyFunction_SetAnnotations(PyObject *op, PyObject *annotations)
429 {
430 if (!PyFunction_Check(op)) {
431 PyErr_BadInternalCall();
432 return -1;
433 }
434 if (annotations == Py_None)
435 annotations = NULL;
436 else if (annotations && PyDict_Check(annotations)) {
437 Py_INCREF(annotations);
438 }
439 else {
440 PyErr_SetString(PyExc_SystemError,
441 "non-dict annotations");
442 return -1;
443 }
444 ((PyFunctionObject *)op)->func_version = 0;
445 Py_XSETREF(((PyFunctionObject *)op)->func_annotations, annotations);
446 return 0;
447 }
448
449 /* Methods */
450
451 #define OFF(x) offsetof(PyFunctionObject, x)
452
453 static PyMemberDef func_memberlist[] = {
454 {"__closure__", T_OBJECT, OFF(func_closure), READONLY},
455 {"__doc__", T_OBJECT, OFF(func_doc), 0},
456 {"__globals__", T_OBJECT, OFF(func_globals), READONLY},
457 {"__module__", T_OBJECT, OFF(func_module), 0},
458 {"__builtins__", T_OBJECT, OFF(func_builtins), READONLY},
459 {NULL} /* Sentinel */
460 };
461
462 static PyObject *
463 func_get_code(PyFunctionObject *op, void *Py_UNUSED(ignored))
464 {
465 if (PySys_Audit("object.__getattr__", "Os", op, "__code__") < 0) {
466 return NULL;
467 }
468
469 return Py_NewRef(op->func_code);
470 }
471
472 static int
473 func_set_code(PyFunctionObject *op, PyObject *value, void *Py_UNUSED(ignored))
474 {
475 Py_ssize_t nclosure;
476 int nfree;
477
478 /* Not legal to del f.func_code or to set it to anything
479 * other than a code object. */
480 if (value == NULL || !PyCode_Check(value)) {
481 PyErr_SetString(PyExc_TypeError,
482 "__code__ must be set to a code object");
483 return -1;
484 }
485
486 if (PySys_Audit("object.__setattr__", "OsO",
487 op, "__code__", value) < 0) {
488 return -1;
489 }
490
491 nfree = ((PyCodeObject *)value)->co_nfreevars;
492 nclosure = (op->func_closure == NULL ? 0 :
493 PyTuple_GET_SIZE(op->func_closure));
494 if (nclosure != nfree) {
495 PyErr_Format(PyExc_ValueError,
496 "%U() requires a code object with %zd free vars,"
497 " not %zd",
498 op->func_name,
499 nclosure, nfree);
500 return -1;
501 }
502 handle_func_event(PyFunction_EVENT_MODIFY_CODE, op, value);
503 op->func_version = 0;
504 Py_XSETREF(op->func_code, Py_NewRef(value));
505 return 0;
506 }
507
508 static PyObject *
509 func_get_name(PyFunctionObject *op, void *Py_UNUSED(ignored))
510 {
511 return Py_NewRef(op->func_name);
512 }
513
514 static int
515 func_set_name(PyFunctionObject *op, PyObject *value, void *Py_UNUSED(ignored))
516 {
517 /* Not legal to del f.func_name or to set it to anything
518 * other than a string object. */
519 if (value == NULL || !PyUnicode_Check(value)) {
520 PyErr_SetString(PyExc_TypeError,
521 "__name__ must be set to a string object");
522 return -1;
523 }
524 Py_XSETREF(op->func_name, Py_NewRef(value));
525 return 0;
526 }
527
528 static PyObject *
529 func_get_qualname(PyFunctionObject *op, void *Py_UNUSED(ignored))
530 {
531 return Py_NewRef(op->func_qualname);
532 }
533
534 static int
535 func_set_qualname(PyFunctionObject *op, PyObject *value, void *Py_UNUSED(ignored))
536 {
537 /* Not legal to del f.__qualname__ or to set it to anything
538 * other than a string object. */
539 if (value == NULL || !PyUnicode_Check(value)) {
540 PyErr_SetString(PyExc_TypeError,
541 "__qualname__ must be set to a string object");
542 return -1;
543 }
544 Py_XSETREF(op->func_qualname, Py_NewRef(value));
545 return 0;
546 }
547
548 static PyObject *
549 func_get_defaults(PyFunctionObject *op, void *Py_UNUSED(ignored))
550 {
551 if (PySys_Audit("object.__getattr__", "Os", op, "__defaults__") < 0) {
552 return NULL;
553 }
554 if (op->func_defaults == NULL) {
555 Py_RETURN_NONE;
556 }
557 return Py_NewRef(op->func_defaults);
558 }
559
560 static int
561 func_set_defaults(PyFunctionObject *op, PyObject *value, void *Py_UNUSED(ignored))
562 {
563 /* Legal to del f.func_defaults.
564 * Can only set func_defaults to NULL or a tuple. */
565 if (value == Py_None)
566 value = NULL;
567 if (value != NULL && !PyTuple_Check(value)) {
568 PyErr_SetString(PyExc_TypeError,
569 "__defaults__ must be set to a tuple object");
570 return -1;
571 }
572 if (value) {
573 if (PySys_Audit("object.__setattr__", "OsO",
574 op, "__defaults__", value) < 0) {
575 return -1;
576 }
577 } else if (PySys_Audit("object.__delattr__", "Os",
578 op, "__defaults__") < 0) {
579 return -1;
580 }
581
582 handle_func_event(PyFunction_EVENT_MODIFY_DEFAULTS, op, value);
583 op->func_version = 0;
584 Py_XSETREF(op->func_defaults, Py_XNewRef(value));
585 return 0;
586 }
587
588 static PyObject *
589 func_get_kwdefaults(PyFunctionObject *op, void *Py_UNUSED(ignored))
590 {
591 if (PySys_Audit("object.__getattr__", "Os",
592 op, "__kwdefaults__") < 0) {
593 return NULL;
594 }
595 if (op->func_kwdefaults == NULL) {
596 Py_RETURN_NONE;
597 }
598 return Py_NewRef(op->func_kwdefaults);
599 }
600
601 static int
602 func_set_kwdefaults(PyFunctionObject *op, PyObject *value, void *Py_UNUSED(ignored))
603 {
604 if (value == Py_None)
605 value = NULL;
606 /* Legal to del f.func_kwdefaults.
607 * Can only set func_kwdefaults to NULL or a dict. */
608 if (value != NULL && !PyDict_Check(value)) {
609 PyErr_SetString(PyExc_TypeError,
610 "__kwdefaults__ must be set to a dict object");
611 return -1;
612 }
613 if (value) {
614 if (PySys_Audit("object.__setattr__", "OsO",
615 op, "__kwdefaults__", value) < 0) {
616 return -1;
617 }
618 } else if (PySys_Audit("object.__delattr__", "Os",
619 op, "__kwdefaults__") < 0) {
620 return -1;
621 }
622
623 handle_func_event(PyFunction_EVENT_MODIFY_KWDEFAULTS, op, value);
624 op->func_version = 0;
625 Py_XSETREF(op->func_kwdefaults, Py_XNewRef(value));
626 return 0;
627 }
628
629 static PyObject *
630 func_get_annotations(PyFunctionObject *op, void *Py_UNUSED(ignored))
631 {
632 if (op->func_annotations == NULL) {
633 op->func_annotations = PyDict_New();
634 if (op->func_annotations == NULL)
635 return NULL;
636 }
637 PyObject *d = func_get_annotation_dict(op);
638 return Py_XNewRef(d);
639 }
640
641 static int
642 func_set_annotations(PyFunctionObject *op, PyObject *value, void *Py_UNUSED(ignored))
643 {
644 if (value == Py_None)
645 value = NULL;
646 /* Legal to del f.func_annotations.
647 * Can only set func_annotations to NULL (through C api)
648 * or a dict. */
649 if (value != NULL && !PyDict_Check(value)) {
650 PyErr_SetString(PyExc_TypeError,
651 "__annotations__ must be set to a dict object");
652 return -1;
653 }
654 op->func_version = 0;
655 Py_XSETREF(op->func_annotations, Py_XNewRef(value));
656 return 0;
657 }
658
659 static PyObject *
660 func_get_type_params(PyFunctionObject *op, void *Py_UNUSED(ignored))
661 {
662 if (op->func_typeparams == NULL) {
663 return PyTuple_New(0);
664 }
665
666 assert(PyTuple_Check(op->func_typeparams));
667 return Py_NewRef(op->func_typeparams);
668 }
669
670 static int
671 func_set_type_params(PyFunctionObject *op, PyObject *value, void *Py_UNUSED(ignored))
672 {
673 /* Not legal to del f.__type_params__ or to set it to anything
674 * other than a tuple object. */
675 if (value == NULL || !PyTuple_Check(value)) {
676 PyErr_SetString(PyExc_TypeError,
677 "__type_params__ must be set to a tuple");
678 return -1;
679 }
680 Py_XSETREF(op->func_typeparams, Py_NewRef(value));
681 return 0;
682 }
683
684 PyObject *
685 _Py_set_function_type_params(PyThreadState *Py_UNUSED(ignored), PyObject *func,
686 PyObject *type_params)
687 {
688 assert(PyFunction_Check(func));
689 assert(PyTuple_Check(type_params));
690 PyFunctionObject *f = (PyFunctionObject *)func;
691 Py_XSETREF(f->func_typeparams, Py_NewRef(type_params));
692 return Py_NewRef(func);
693 }
694
695 static PyGetSetDef func_getsetlist[] = {
696 {"__code__", (getter)func_get_code, (setter)func_set_code},
697 {"__defaults__", (getter)func_get_defaults,
698 (setter)func_set_defaults},
699 {"__kwdefaults__", (getter)func_get_kwdefaults,
700 (setter)func_set_kwdefaults},
701 {"__annotations__", (getter)func_get_annotations,
702 (setter)func_set_annotations},
703 {"__dict__", PyObject_GenericGetDict, PyObject_GenericSetDict},
704 {"__name__", (getter)func_get_name, (setter)func_set_name},
705 {"__qualname__", (getter)func_get_qualname, (setter)func_set_qualname},
706 {"__type_params__", (getter)func_get_type_params,
707 (setter)func_set_type_params},
708 {NULL} /* Sentinel */
709 };
710
711 /*[clinic input]
712 class function "PyFunctionObject *" "&PyFunction_Type"
713 [clinic start generated code]*/
714 /*[clinic end generated code: output=da39a3ee5e6b4b0d input=70af9c90aa2e71b0]*/
715
716 #include "clinic/funcobject.c.h"
717
718 /* function.__new__() maintains the following invariants for closures.
719 The closure must correspond to the free variables of the code object.
720
721 if len(code.co_freevars) == 0:
722 closure = NULL
723 else:
724 len(closure) == len(code.co_freevars)
725 for every elt in closure, type(elt) == cell
726 */
727
728 /*[clinic input]
729 @classmethod
730 function.__new__ as func_new
731 code: object(type="PyCodeObject *", subclass_of="&PyCode_Type")
732 a code object
733 globals: object(subclass_of="&PyDict_Type")
734 the globals dictionary
735 name: object = None
736 a string that overrides the name from the code object
737 argdefs as defaults: object = None
738 a tuple that specifies the default argument values
739 closure: object = None
740 a tuple that supplies the bindings for free variables
741
742 Create a function object.
743 [clinic start generated code]*/
744
745 static PyObject *
746 func_new_impl(PyTypeObject *type, PyCodeObject *code, PyObject *globals,
747 PyObject *name, PyObject *defaults, PyObject *closure)
748 /*[clinic end generated code: output=99c6d9da3a24e3be input=93611752fc2daf11]*/
749 {
750 PyFunctionObject *newfunc;
751 Py_ssize_t nclosure;
752
753 if (name != Py_None && !PyUnicode_Check(name)) {
754 PyErr_SetString(PyExc_TypeError,
755 "arg 3 (name) must be None or string");
756 return NULL;
757 }
758 if (defaults != Py_None && !PyTuple_Check(defaults)) {
759 PyErr_SetString(PyExc_TypeError,
760 "arg 4 (defaults) must be None or tuple");
761 return NULL;
762 }
763 if (!PyTuple_Check(closure)) {
764 if (code->co_nfreevars && closure == Py_None) {
765 PyErr_SetString(PyExc_TypeError,
766 "arg 5 (closure) must be tuple");
767 return NULL;
768 }
769 else if (closure != Py_None) {
770 PyErr_SetString(PyExc_TypeError,
771 "arg 5 (closure) must be None or tuple");
772 return NULL;
773 }
774 }
775
776 /* check that the closure is well-formed */
777 nclosure = closure == Py_None ? 0 : PyTuple_GET_SIZE(closure);
778 if (code->co_nfreevars != nclosure)
779 return PyErr_Format(PyExc_ValueError,
780 "%U requires closure of length %zd, not %zd",
781 code->co_name, code->co_nfreevars, nclosure);
782 if (nclosure) {
783 Py_ssize_t i;
784 for (i = 0; i < nclosure; i++) {
785 PyObject *o = PyTuple_GET_ITEM(closure, i);
786 if (!PyCell_Check(o)) {
787 return PyErr_Format(PyExc_TypeError,
788 "arg 5 (closure) expected cell, found %s",
789 Py_TYPE(o)->tp_name);
790 }
791 }
792 }
793 if (PySys_Audit("function.__new__", "O", code) < 0) {
794 return NULL;
795 }
796
797 newfunc = (PyFunctionObject *)PyFunction_New((PyObject *)code,
798 globals);
799 if (newfunc == NULL) {
800 return NULL;
801 }
802 if (name != Py_None) {
803 Py_SETREF(newfunc->func_name, Py_NewRef(name));
804 }
805 if (defaults != Py_None) {
806 newfunc->func_defaults = Py_NewRef(defaults);
807 }
808 if (closure != Py_None) {
809 newfunc->func_closure = Py_NewRef(closure);
810 }
811
812 return (PyObject *)newfunc;
813 }
814
815 static int
816 func_clear(PyFunctionObject *op)
817 {
818 op->func_version = 0;
819 Py_CLEAR(op->func_globals);
820 Py_CLEAR(op->func_builtins);
821 Py_CLEAR(op->func_module);
822 Py_CLEAR(op->func_defaults);
823 Py_CLEAR(op->func_kwdefaults);
824 Py_CLEAR(op->func_doc);
825 Py_CLEAR(op->func_dict);
826 Py_CLEAR(op->func_closure);
827 Py_CLEAR(op->func_annotations);
828 Py_CLEAR(op->func_typeparams);
829 // Don't Py_CLEAR(op->func_code), since code is always required
830 // to be non-NULL. Similarly, name and qualname shouldn't be NULL.
831 // However, name and qualname could be str subclasses, so they
832 // could have reference cycles. The solution is to replace them
833 // with a genuinely immutable string.
834 Py_SETREF(op->func_name, Py_NewRef(&_Py_STR(empty)));
835 Py_SETREF(op->func_qualname, Py_NewRef(&_Py_STR(empty)));
836 return 0;
837 }
838
839 static void
840 func_dealloc(PyFunctionObject *op)
841 {
842 assert(Py_REFCNT(op) == 0);
843 Py_SET_REFCNT(op, 1);
844 handle_func_event(PyFunction_EVENT_DESTROY, op, NULL);
845 if (Py_REFCNT(op) > 1) {
846 Py_SET_REFCNT(op, Py_REFCNT(op) - 1);
847 return;
848 }
849 Py_SET_REFCNT(op, 0);
850 _PyObject_GC_UNTRACK(op);
851 if (op->func_weakreflist != NULL) {
852 PyObject_ClearWeakRefs((PyObject *) op);
853 }
854 (void)func_clear(op);
855 // These aren't cleared by func_clear().
856 Py_DECREF(op->func_code);
857 Py_DECREF(op->func_name);
858 Py_DECREF(op->func_qualname);
859 PyObject_GC_Del(op);
860 }
861
862 static PyObject*
863 func_repr(PyFunctionObject *op)
864 {
865 return PyUnicode_FromFormat("<function %U at %p>",
866 op->func_qualname, op);
867 }
868
869 static int
870 func_traverse(PyFunctionObject *f, visitproc visit, void *arg)
871 {
872 Py_VISIT(f->func_code);
873 Py_VISIT(f->func_globals);
874 Py_VISIT(f->func_builtins);
875 Py_VISIT(f->func_module);
876 Py_VISIT(f->func_defaults);
877 Py_VISIT(f->func_kwdefaults);
878 Py_VISIT(f->func_doc);
879 Py_VISIT(f->func_name);
880 Py_VISIT(f->func_dict);
881 Py_VISIT(f->func_closure);
882 Py_VISIT(f->func_annotations);
883 Py_VISIT(f->func_typeparams);
884 Py_VISIT(f->func_qualname);
885 return 0;
886 }
887
888 /* Bind a function to an object */
889 static PyObject *
890 func_descr_get(PyObject *func, PyObject *obj, PyObject *type)
891 {
892 if (obj == Py_None || obj == NULL) {
893 return Py_NewRef(func);
894 }
895 return PyMethod_New(func, obj);
896 }
897
898 PyTypeObject PyFunction_Type = {
899 PyVarObject_HEAD_INIT(&PyType_Type, 0)
900 "function",
901 sizeof(PyFunctionObject),
902 0,
903 (destructor)func_dealloc, /* tp_dealloc */
904 offsetof(PyFunctionObject, vectorcall), /* tp_vectorcall_offset */
905 0, /* tp_getattr */
906 0, /* tp_setattr */
907 0, /* tp_as_async */
908 (reprfunc)func_repr, /* tp_repr */
909 0, /* tp_as_number */
910 0, /* tp_as_sequence */
911 0, /* tp_as_mapping */
912 0, /* tp_hash */
913 PyVectorcall_Call, /* tp_call */
914 0, /* tp_str */
915 0, /* tp_getattro */
916 0, /* tp_setattro */
917 0, /* tp_as_buffer */
918 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
919 Py_TPFLAGS_HAVE_VECTORCALL |
920 Py_TPFLAGS_METHOD_DESCRIPTOR, /* tp_flags */
921 func_new__doc__, /* tp_doc */
922 (traverseproc)func_traverse, /* tp_traverse */
923 (inquiry)func_clear, /* tp_clear */
924 0, /* tp_richcompare */
925 offsetof(PyFunctionObject, func_weakreflist), /* tp_weaklistoffset */
926 0, /* tp_iter */
927 0, /* tp_iternext */
928 0, /* tp_methods */
929 func_memberlist, /* tp_members */
930 func_getsetlist, /* tp_getset */
931 0, /* tp_base */
932 0, /* tp_dict */
933 func_descr_get, /* tp_descr_get */
934 0, /* tp_descr_set */
935 offsetof(PyFunctionObject, func_dict), /* tp_dictoffset */
936 0, /* tp_init */
937 0, /* tp_alloc */
938 func_new, /* tp_new */
939 };
940
941
942 static int
943 functools_copy_attr(PyObject *wrapper, PyObject *wrapped, PyObject *name)
944 {
945 PyObject *value = PyObject_GetAttr(wrapped, name);
946 if (value == NULL) {
947 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
948 PyErr_Clear();
949 return 0;
950 }
951 return -1;
952 }
953
954 int res = PyObject_SetAttr(wrapper, name, value);
955 Py_DECREF(value);
956 return res;
957 }
958
959 // Similar to functools.wraps(wrapper, wrapped)
960 static int
961 functools_wraps(PyObject *wrapper, PyObject *wrapped)
962 {
963 #define COPY_ATTR(ATTR) \
964 do { \
965 if (functools_copy_attr(wrapper, wrapped, &_Py_ID(ATTR)) < 0) { \
966 return -1; \
967 } \
968 } while (0) \
969
970 COPY_ATTR(__module__);
971 COPY_ATTR(__name__);
972 COPY_ATTR(__qualname__);
973 COPY_ATTR(__doc__);
974 COPY_ATTR(__annotations__);
975 return 0;
976
977 #undef COPY_ATTR
978 }
979
980
981 /* Class method object */
982
983 /* A class method receives the class as implicit first argument,
984 just like an instance method receives the instance.
985 To declare a class method, use this idiom:
986
987 class C:
988 @classmethod
989 def f(cls, arg1, arg2, argN):
990 ...
991
992 It can be called either on the class (e.g. C.f()) or on an instance
993 (e.g. C().f()); the instance is ignored except for its class.
994 If a class method is called for a derived class, the derived class
995 object is passed as the implied first argument.
996
997 Class methods are different than C++ or Java static methods.
998 If you want those, see static methods below.
999 */
1000
1001 typedef struct {
1002 PyObject_HEAD
1003 PyObject *cm_callable;
1004 PyObject *cm_dict;
1005 } classmethod;
1006
1007 static void
1008 cm_dealloc(classmethod *cm)
1009 {
1010 _PyObject_GC_UNTRACK((PyObject *)cm);
1011 Py_XDECREF(cm->cm_callable);
1012 Py_XDECREF(cm->cm_dict);
1013 Py_TYPE(cm)->tp_free((PyObject *)cm);
1014 }
1015
1016 static int
1017 cm_traverse(classmethod *cm, visitproc visit, void *arg)
1018 {
1019 Py_VISIT(cm->cm_callable);
1020 Py_VISIT(cm->cm_dict);
1021 return 0;
1022 }
1023
1024 static int
1025 cm_clear(classmethod *cm)
1026 {
1027 Py_CLEAR(cm->cm_callable);
1028 Py_CLEAR(cm->cm_dict);
1029 return 0;
1030 }
1031
1032
1033 static PyObject *
1034 cm_descr_get(PyObject *self, PyObject *obj, PyObject *type)
1035 {
1036 classmethod *cm = (classmethod *)self;
1037
1038 if (cm->cm_callable == NULL) {
1039 PyErr_SetString(PyExc_RuntimeError,
1040 "uninitialized classmethod object");
1041 return NULL;
1042 }
1043 if (type == NULL)
1044 type = (PyObject *)(Py_TYPE(obj));
1045 if (Py_TYPE(cm->cm_callable)->tp_descr_get != NULL) {
1046 return Py_TYPE(cm->cm_callable)->tp_descr_get(cm->cm_callable, type,
1047 type);
1048 }
1049 return PyMethod_New(cm->cm_callable, type);
1050 }
1051
1052 static int
1053 cm_init(PyObject *self, PyObject *args, PyObject *kwds)
1054 {
1055 classmethod *cm = (classmethod *)self;
1056 PyObject *callable;
1057
1058 if (!_PyArg_NoKeywords("classmethod", kwds))
1059 return -1;
1060 if (!PyArg_UnpackTuple(args, "classmethod", 1, 1, &callable))
1061 return -1;
1062 Py_XSETREF(cm->cm_callable, Py_NewRef(callable));
1063
1064 if (functools_wraps((PyObject *)cm, cm->cm_callable) < 0) {
1065 return -1;
1066 }
1067 return 0;
1068 }
1069
1070 static PyMemberDef cm_memberlist[] = {
1071 {"__func__", T_OBJECT, offsetof(classmethod, cm_callable), READONLY},
1072 {"__wrapped__", T_OBJECT, offsetof(classmethod, cm_callable), READONLY},
1073 {NULL} /* Sentinel */
1074 };
1075
1076 static PyObject *
1077 cm_get___isabstractmethod__(classmethod *cm, void *closure)
1078 {
1079 int res = _PyObject_IsAbstract(cm->cm_callable);
1080 if (res == -1) {
1081 return NULL;
1082 }
1083 else if (res) {
1084 Py_RETURN_TRUE;
1085 }
1086 Py_RETURN_FALSE;
1087 }
1088
1089 static PyGetSetDef cm_getsetlist[] = {
1090 {"__isabstractmethod__",
1091 (getter)cm_get___isabstractmethod__, NULL, NULL, NULL},
1092 {"__dict__", PyObject_GenericGetDict, PyObject_GenericSetDict, NULL, NULL},
1093 {NULL} /* Sentinel */
1094 };
1095
1096 static PyObject*
1097 cm_repr(classmethod *cm)
1098 {
1099 return PyUnicode_FromFormat("<classmethod(%R)>", cm->cm_callable);
1100 }
1101
1102 PyDoc_STRVAR(classmethod_doc,
1103 "classmethod(function) -> method\n\
1104 \n\
1105 Convert a function to be a class method.\n\
1106 \n\
1107 A class method receives the class as implicit first argument,\n\
1108 just like an instance method receives the instance.\n\
1109 To declare a class method, use this idiom:\n\
1110 \n\
1111 class C:\n\
1112 @classmethod\n\
1113 def f(cls, arg1, arg2, argN):\n\
1114 ...\n\
1115 \n\
1116 It can be called either on the class (e.g. C.f()) or on an instance\n\
1117 (e.g. C().f()). The instance is ignored except for its class.\n\
1118 If a class method is called for a derived class, the derived class\n\
1119 object is passed as the implied first argument.\n\
1120 \n\
1121 Class methods are different than C++ or Java static methods.\n\
1122 If you want those, see the staticmethod builtin.");
1123
1124 PyTypeObject PyClassMethod_Type = {
1125 PyVarObject_HEAD_INIT(&PyType_Type, 0)
1126 "classmethod",
1127 sizeof(classmethod),
1128 0,
1129 (destructor)cm_dealloc, /* tp_dealloc */
1130 0, /* tp_vectorcall_offset */
1131 0, /* tp_getattr */
1132 0, /* tp_setattr */
1133 0, /* tp_as_async */
1134 (reprfunc)cm_repr, /* tp_repr */
1135 0, /* tp_as_number */
1136 0, /* tp_as_sequence */
1137 0, /* tp_as_mapping */
1138 0, /* tp_hash */
1139 0, /* tp_call */
1140 0, /* tp_str */
1141 0, /* tp_getattro */
1142 0, /* tp_setattro */
1143 0, /* tp_as_buffer */
1144 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
1145 classmethod_doc, /* tp_doc */
1146 (traverseproc)cm_traverse, /* tp_traverse */
1147 (inquiry)cm_clear, /* tp_clear */
1148 0, /* tp_richcompare */
1149 0, /* tp_weaklistoffset */
1150 0, /* tp_iter */
1151 0, /* tp_iternext */
1152 0, /* tp_methods */
1153 cm_memberlist, /* tp_members */
1154 cm_getsetlist, /* tp_getset */
1155 0, /* tp_base */
1156 0, /* tp_dict */
1157 cm_descr_get, /* tp_descr_get */
1158 0, /* tp_descr_set */
1159 offsetof(classmethod, cm_dict), /* tp_dictoffset */
1160 cm_init, /* tp_init */
1161 PyType_GenericAlloc, /* tp_alloc */
1162 PyType_GenericNew, /* tp_new */
1163 PyObject_GC_Del, /* tp_free */
1164 };
1165
1166 PyObject *
1167 PyClassMethod_New(PyObject *callable)
1168 {
1169 classmethod *cm = (classmethod *)
1170 PyType_GenericAlloc(&PyClassMethod_Type, 0);
1171 if (cm != NULL) {
1172 cm->cm_callable = Py_NewRef(callable);
1173 }
1174 return (PyObject *)cm;
1175 }
1176
1177
1178 /* Static method object */
1179
1180 /* A static method does not receive an implicit first argument.
1181 To declare a static method, use this idiom:
1182
1183 class C:
1184 @staticmethod
1185 def f(arg1, arg2, argN):
1186 ...
1187
1188 It can be called either on the class (e.g. C.f()) or on an instance
1189 (e.g. C().f()). Both the class and the instance are ignored, and
1190 neither is passed implicitly as the first argument to the method.
1191
1192 Static methods in Python are similar to those found in Java or C++.
1193 For a more advanced concept, see class methods above.
1194 */
1195
1196 typedef struct {
1197 PyObject_HEAD
1198 PyObject *sm_callable;
1199 PyObject *sm_dict;
1200 } staticmethod;
1201
1202 static void
1203 sm_dealloc(staticmethod *sm)
1204 {
1205 _PyObject_GC_UNTRACK((PyObject *)sm);
1206 Py_XDECREF(sm->sm_callable);
1207 Py_XDECREF(sm->sm_dict);
1208 Py_TYPE(sm)->tp_free((PyObject *)sm);
1209 }
1210
1211 static int
1212 sm_traverse(staticmethod *sm, visitproc visit, void *arg)
1213 {
1214 Py_VISIT(sm->sm_callable);
1215 Py_VISIT(sm->sm_dict);
1216 return 0;
1217 }
1218
1219 static int
1220 sm_clear(staticmethod *sm)
1221 {
1222 Py_CLEAR(sm->sm_callable);
1223 Py_CLEAR(sm->sm_dict);
1224 return 0;
1225 }
1226
1227 static PyObject *
1228 sm_descr_get(PyObject *self, PyObject *obj, PyObject *type)
1229 {
1230 staticmethod *sm = (staticmethod *)self;
1231
1232 if (sm->sm_callable == NULL) {
1233 PyErr_SetString(PyExc_RuntimeError,
1234 "uninitialized staticmethod object");
1235 return NULL;
1236 }
1237 return Py_NewRef(sm->sm_callable);
1238 }
1239
1240 static int
1241 sm_init(PyObject *self, PyObject *args, PyObject *kwds)
1242 {
1243 staticmethod *sm = (staticmethod *)self;
1244 PyObject *callable;
1245
1246 if (!_PyArg_NoKeywords("staticmethod", kwds))
1247 return -1;
1248 if (!PyArg_UnpackTuple(args, "staticmethod", 1, 1, &callable))
1249 return -1;
1250 Py_XSETREF(sm->sm_callable, Py_NewRef(callable));
1251
1252 if (functools_wraps((PyObject *)sm, sm->sm_callable) < 0) {
1253 return -1;
1254 }
1255 return 0;
1256 }
1257
1258 static PyObject*
1259 sm_call(PyObject *callable, PyObject *args, PyObject *kwargs)
1260 {
1261 staticmethod *sm = (staticmethod *)callable;
1262 return PyObject_Call(sm->sm_callable, args, kwargs);
1263 }
1264
1265 static PyMemberDef sm_memberlist[] = {
1266 {"__func__", T_OBJECT, offsetof(staticmethod, sm_callable), READONLY},
1267 {"__wrapped__", T_OBJECT, offsetof(staticmethod, sm_callable), READONLY},
1268 {NULL} /* Sentinel */
1269 };
1270
1271 static PyObject *
1272 sm_get___isabstractmethod__(staticmethod *sm, void *closure)
1273 {
1274 int res = _PyObject_IsAbstract(sm->sm_callable);
1275 if (res == -1) {
1276 return NULL;
1277 }
1278 else if (res) {
1279 Py_RETURN_TRUE;
1280 }
1281 Py_RETURN_FALSE;
1282 }
1283
1284 static PyGetSetDef sm_getsetlist[] = {
1285 {"__isabstractmethod__",
1286 (getter)sm_get___isabstractmethod__, NULL, NULL, NULL},
1287 {"__dict__", PyObject_GenericGetDict, PyObject_GenericSetDict, NULL, NULL},
1288 {NULL} /* Sentinel */
1289 };
1290
1291 static PyObject*
1292 sm_repr(staticmethod *sm)
1293 {
1294 return PyUnicode_FromFormat("<staticmethod(%R)>", sm->sm_callable);
1295 }
1296
1297 PyDoc_STRVAR(staticmethod_doc,
1298 "staticmethod(function) -> method\n\
1299 \n\
1300 Convert a function to be a static method.\n\
1301 \n\
1302 A static method does not receive an implicit first argument.\n\
1303 To declare a static method, use this idiom:\n\
1304 \n\
1305 class C:\n\
1306 @staticmethod\n\
1307 def f(arg1, arg2, argN):\n\
1308 ...\n\
1309 \n\
1310 It can be called either on the class (e.g. C.f()) or on an instance\n\
1311 (e.g. C().f()). Both the class and the instance are ignored, and\n\
1312 neither is passed implicitly as the first argument to the method.\n\
1313 \n\
1314 Static methods in Python are similar to those found in Java or C++.\n\
1315 For a more advanced concept, see the classmethod builtin.");
1316
1317 PyTypeObject PyStaticMethod_Type = {
1318 PyVarObject_HEAD_INIT(&PyType_Type, 0)
1319 "staticmethod",
1320 sizeof(staticmethod),
1321 0,
1322 (destructor)sm_dealloc, /* tp_dealloc */
1323 0, /* tp_vectorcall_offset */
1324 0, /* tp_getattr */
1325 0, /* tp_setattr */
1326 0, /* tp_as_async */
1327 (reprfunc)sm_repr, /* tp_repr */
1328 0, /* tp_as_number */
1329 0, /* tp_as_sequence */
1330 0, /* tp_as_mapping */
1331 0, /* tp_hash */
1332 sm_call, /* tp_call */
1333 0, /* tp_str */
1334 0, /* tp_getattro */
1335 0, /* tp_setattro */
1336 0, /* tp_as_buffer */
1337 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
1338 staticmethod_doc, /* tp_doc */
1339 (traverseproc)sm_traverse, /* tp_traverse */
1340 (inquiry)sm_clear, /* tp_clear */
1341 0, /* tp_richcompare */
1342 0, /* tp_weaklistoffset */
1343 0, /* tp_iter */
1344 0, /* tp_iternext */
1345 0, /* tp_methods */
1346 sm_memberlist, /* tp_members */
1347 sm_getsetlist, /* tp_getset */
1348 0, /* tp_base */
1349 0, /* tp_dict */
1350 sm_descr_get, /* tp_descr_get */
1351 0, /* tp_descr_set */
1352 offsetof(staticmethod, sm_dict), /* tp_dictoffset */
1353 sm_init, /* tp_init */
1354 PyType_GenericAlloc, /* tp_alloc */
1355 PyType_GenericNew, /* tp_new */
1356 PyObject_GC_Del, /* tp_free */
1357 };
1358
1359 PyObject *
1360 PyStaticMethod_New(PyObject *callable)
1361 {
1362 staticmethod *sm = (staticmethod *)
1363 PyType_GenericAlloc(&PyStaticMethod_Type, 0);
1364 if (sm != NULL) {
1365 sm->sm_callable = Py_NewRef(callable);
1366 }
1367 return (PyObject *)sm;
1368 }