1991-02-19 08:39:46 -04:00
|
|
|
|
1990-10-14 09:07:46 -03:00
|
|
|
/* Function object implementation */
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
#include "Python.h"
|
2021-02-18 14:20:16 -04:00
|
|
|
#include "pycore_ceval.h" // _PyEval_BuiltinsFromGlobals()
|
2024-05-21 23:38:12 -03:00
|
|
|
#include "pycore_long.h" // _PyLong_GetOne()
|
2023-10-17 09:30:31 -03:00
|
|
|
#include "pycore_modsupport.h" // _PyArg_NoKeywords()
|
2021-02-18 14:20:16 -04:00
|
|
|
#include "pycore_object.h" // _PyObject_GC_UNTRACK()
|
2021-02-20 10:17:18 -04:00
|
|
|
#include "pycore_pyerrors.h" // _PyErr_Occurred()
|
2023-07-25 10:28:30 -03:00
|
|
|
|
1990-10-14 09:07:46 -03:00
|
|
|
|
2023-03-07 20:10:58 -04:00
|
|
|
static const char *
|
|
|
|
func_event_name(PyFunction_WatchEvent event) {
|
|
|
|
switch (event) {
|
|
|
|
#define CASE(op) \
|
|
|
|
case PyFunction_EVENT_##op: \
|
|
|
|
return "PyFunction_EVENT_" #op;
|
|
|
|
PY_FOREACH_FUNC_EVENT(CASE)
|
|
|
|
#undef CASE
|
|
|
|
}
|
|
|
|
Py_UNREACHABLE();
|
|
|
|
}
|
|
|
|
|
2022-11-22 08:06:44 -04:00
|
|
|
static void
|
|
|
|
notify_func_watchers(PyInterpreterState *interp, PyFunction_WatchEvent event,
|
|
|
|
PyFunctionObject *func, PyObject *new_value)
|
|
|
|
{
|
2022-12-14 15:14:16 -04:00
|
|
|
uint8_t bits = interp->active_func_watchers;
|
|
|
|
int i = 0;
|
|
|
|
while (bits) {
|
|
|
|
assert(i < FUNC_MAX_WATCHERS);
|
|
|
|
if (bits & 1) {
|
|
|
|
PyFunction_WatchCallback cb = interp->func_watchers[i];
|
|
|
|
// callback must be non-null if the watcher bit is set
|
|
|
|
assert(cb != NULL);
|
|
|
|
if (cb(event, func, new_value) < 0) {
|
2023-11-02 06:16:34 -03:00
|
|
|
PyErr_FormatUnraisable(
|
|
|
|
"Exception ignored in %s watcher callback for function %U at %p",
|
|
|
|
func_event_name(event), func->func_qualname, func);
|
2022-12-14 15:14:16 -04:00
|
|
|
}
|
2022-11-22 08:06:44 -04:00
|
|
|
}
|
2022-12-14 15:14:16 -04:00
|
|
|
i++;
|
|
|
|
bits >>= 1;
|
2022-11-22 08:06:44 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void
|
|
|
|
handle_func_event(PyFunction_WatchEvent event, PyFunctionObject *func,
|
|
|
|
PyObject *new_value)
|
|
|
|
{
|
2023-03-07 20:10:58 -04:00
|
|
|
assert(Py_REFCNT(func) > 0);
|
2022-11-22 08:06:44 -04:00
|
|
|
PyInterpreterState *interp = _PyInterpreterState_GET();
|
2022-12-14 15:14:16 -04:00
|
|
|
assert(interp->_initialized);
|
2022-11-22 08:06:44 -04:00
|
|
|
if (interp->active_func_watchers) {
|
|
|
|
notify_func_watchers(interp, event, func, new_value);
|
|
|
|
}
|
2024-01-25 07:10:51 -04:00
|
|
|
switch (event) {
|
|
|
|
case PyFunction_EVENT_MODIFY_CODE:
|
|
|
|
case PyFunction_EVENT_MODIFY_DEFAULTS:
|
|
|
|
case PyFunction_EVENT_MODIFY_KWDEFAULTS:
|
|
|
|
RARE_EVENT_INTERP_INC(interp, func_modification);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2022-11-22 08:06:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
PyFunction_AddWatcher(PyFunction_WatchCallback callback)
|
|
|
|
{
|
|
|
|
PyInterpreterState *interp = _PyInterpreterState_GET();
|
|
|
|
assert(interp->_initialized);
|
|
|
|
for (int i = 0; i < FUNC_MAX_WATCHERS; i++) {
|
|
|
|
if (interp->func_watchers[i] == NULL) {
|
|
|
|
interp->func_watchers[i] = callback;
|
|
|
|
interp->active_func_watchers |= (1 << i);
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
PyErr_SetString(PyExc_RuntimeError, "no more func watcher IDs available");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
PyFunction_ClearWatcher(int watcher_id)
|
|
|
|
{
|
|
|
|
PyInterpreterState *interp = _PyInterpreterState_GET();
|
|
|
|
if (watcher_id < 0 || watcher_id >= FUNC_MAX_WATCHERS) {
|
|
|
|
PyErr_Format(PyExc_ValueError, "invalid func watcher ID %d",
|
|
|
|
watcher_id);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (!interp->func_watchers[watcher_id]) {
|
|
|
|
PyErr_Format(PyExc_ValueError, "no func watcher set for ID %d",
|
|
|
|
watcher_id);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
interp->func_watchers[watcher_id] = NULL;
|
|
|
|
interp->active_func_watchers &= ~(1 << watcher_id);
|
|
|
|
return 0;
|
|
|
|
}
|
2021-11-23 05:53:24 -04:00
|
|
|
PyFunctionObject *
|
|
|
|
_PyFunction_FromConstructor(PyFrameConstructor *constr)
|
|
|
|
{
|
2023-11-14 05:25:39 -04:00
|
|
|
PyObject *module;
|
|
|
|
if (PyDict_GetItemRef(constr->fc_globals, &_Py_ID(__name__), &module) < 0) {
|
2023-06-29 06:31:08 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
2021-11-23 05:53:24 -04:00
|
|
|
|
|
|
|
PyFunctionObject *op = PyObject_GC_New(PyFunctionObject, &PyFunction_Type);
|
|
|
|
if (op == NULL) {
|
2023-06-29 06:31:08 -03:00
|
|
|
Py_XDECREF(module);
|
2021-11-23 05:53:24 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
2022-11-10 17:22:02 -04:00
|
|
|
op->func_globals = Py_NewRef(constr->fc_globals);
|
|
|
|
op->func_builtins = Py_NewRef(constr->fc_builtins);
|
|
|
|
op->func_name = Py_NewRef(constr->fc_name);
|
|
|
|
op->func_qualname = Py_NewRef(constr->fc_qualname);
|
|
|
|
op->func_code = Py_NewRef(constr->fc_code);
|
2023-02-07 05:34:21 -04:00
|
|
|
op->func_defaults = Py_XNewRef(constr->fc_defaults);
|
|
|
|
op->func_kwdefaults = Py_XNewRef(constr->fc_kwdefaults);
|
2022-11-10 17:22:02 -04:00
|
|
|
op->func_closure = Py_XNewRef(constr->fc_closure);
|
|
|
|
op->func_doc = Py_NewRef(Py_None);
|
2021-11-23 05:53:24 -04:00
|
|
|
op->func_dict = NULL;
|
|
|
|
op->func_weakreflist = NULL;
|
2023-06-29 06:31:08 -03:00
|
|
|
op->func_module = module;
|
2021-11-23 05:53:24 -04:00
|
|
|
op->func_annotations = NULL;
|
2024-05-21 23:38:12 -03:00
|
|
|
op->func_annotate = NULL;
|
2023-05-16 00:36:23 -03:00
|
|
|
op->func_typeparams = NULL;
|
2021-11-23 05:53:24 -04:00
|
|
|
op->vectorcall = _PyFunction_Vectorcall;
|
2023-08-23 19:36:19 -03:00
|
|
|
op->func_version = 0;
|
2024-04-12 14:36:20 -03:00
|
|
|
// NOTE: functions created via FrameConstructor do not use deferred
|
|
|
|
// reference counting because they are typically not part of cycles
|
|
|
|
// nor accessed by multiple threads.
|
2021-11-23 05:53:24 -04:00
|
|
|
_PyObject_GC_TRACK(op);
|
2022-11-22 08:06:44 -04:00
|
|
|
handle_func_event(PyFunction_EVENT_CREATE, op, NULL);
|
2021-11-23 05:53:24 -04:00
|
|
|
return op;
|
|
|
|
}
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
PyObject *
|
2011-11-25 13:56:07 -04:00
|
|
|
PyFunction_NewWithQualName(PyObject *code, PyObject *globals, PyObject *qualname)
|
1990-10-14 09:07:46 -03:00
|
|
|
{
|
2021-02-18 14:20:16 -04:00
|
|
|
assert(globals != NULL);
|
|
|
|
assert(PyDict_Check(globals));
|
|
|
|
Py_INCREF(globals);
|
|
|
|
|
2021-02-20 10:17:18 -04:00
|
|
|
PyThreadState *tstate = _PyThreadState_GET();
|
|
|
|
|
2022-11-16 05:39:47 -04:00
|
|
|
PyCodeObject *code_obj = (PyCodeObject *)Py_NewRef(code);
|
2021-02-18 14:20:16 -04:00
|
|
|
|
2022-11-16 13:34:24 -04:00
|
|
|
assert(code_obj->co_name != NULL);
|
|
|
|
PyObject *name = Py_NewRef(code_obj->co_name);
|
2021-07-07 08:21:51 -03:00
|
|
|
|
2021-02-18 14:20:16 -04:00
|
|
|
if (!qualname) {
|
2021-07-07 08:21:51 -03:00
|
|
|
qualname = code_obj->co_qualname;
|
2013-07-22 18:04:55 -03:00
|
|
|
}
|
2021-07-07 08:21:51 -03:00
|
|
|
assert(qualname != NULL);
|
2021-02-18 14:20:16 -04:00
|
|
|
Py_INCREF(qualname);
|
2013-07-22 18:04:55 -03:00
|
|
|
|
2021-02-18 14:20:16 -04:00
|
|
|
PyObject *consts = code_obj->co_consts;
|
|
|
|
assert(PyTuple_Check(consts));
|
|
|
|
PyObject *doc;
|
|
|
|
if (PyTuple_Size(consts) >= 1) {
|
|
|
|
doc = PyTuple_GetItem(consts, 0);
|
|
|
|
if (!PyUnicode_Check(doc)) {
|
|
|
|
doc = Py_None;
|
|
|
|
}
|
2020-10-29 06:58:52 -03:00
|
|
|
}
|
2021-02-18 14:20:16 -04:00
|
|
|
else {
|
|
|
|
doc = Py_None;
|
2020-10-29 06:58:52 -03:00
|
|
|
}
|
2021-02-18 14:20:16 -04:00
|
|
|
Py_INCREF(doc);
|
2020-10-29 06:58:52 -03:00
|
|
|
|
2021-02-18 14:20:16 -04:00
|
|
|
// __module__: Use globals['__name__'] if it exists, or NULL.
|
2023-11-14 05:25:39 -04:00
|
|
|
PyObject *module;
|
2021-02-18 14:20:16 -04:00
|
|
|
PyObject *builtins = NULL;
|
2023-11-14 05:25:39 -04:00
|
|
|
if (PyDict_GetItemRef(globals, &_Py_ID(__name__), &module) < 0) {
|
2021-02-18 14:20:16 -04:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2021-03-18 10:51:24 -03:00
|
|
|
builtins = _PyEval_BuiltinsFromGlobals(tstate, globals); // borrowed ref
|
2021-02-18 14:20:16 -04:00
|
|
|
if (builtins == NULL) {
|
|
|
|
goto error;
|
|
|
|
}
|
2021-02-20 10:17:18 -04:00
|
|
|
Py_INCREF(builtins);
|
2021-02-18 14:20:16 -04:00
|
|
|
|
|
|
|
PyFunctionObject *op = PyObject_GC_New(PyFunctionObject, &PyFunction_Type);
|
2020-10-29 06:58:52 -03:00
|
|
|
if (op == NULL) {
|
2021-02-18 14:20:16 -04:00
|
|
|
goto error;
|
2020-10-29 06:58:52 -03:00
|
|
|
}
|
|
|
|
/* Note: No failures from this point on, since func_dealloc() does not
|
|
|
|
expect a partially-created object. */
|
2013-07-22 18:02:05 -03:00
|
|
|
|
|
|
|
op->func_globals = globals;
|
2021-01-29 09:24:55 -04:00
|
|
|
op->func_builtins = builtins;
|
2021-02-18 14:20:16 -04:00
|
|
|
op->func_name = name;
|
|
|
|
op->func_qualname = qualname;
|
|
|
|
op->func_code = (PyObject*)code_obj;
|
|
|
|
op->func_defaults = NULL; // No default positional arguments
|
|
|
|
op->func_kwdefaults = NULL; // No default keyword arguments
|
2013-07-22 18:02:05 -03:00
|
|
|
op->func_closure = NULL;
|
|
|
|
op->func_doc = doc;
|
|
|
|
op->func_dict = NULL;
|
2021-02-18 14:20:16 -04:00
|
|
|
op->func_weakreflist = NULL;
|
|
|
|
op->func_module = module;
|
2013-07-22 18:02:05 -03:00
|
|
|
op->func_annotations = NULL;
|
2024-05-21 23:38:12 -03:00
|
|
|
op->func_annotate = NULL;
|
2023-05-16 00:36:23 -03:00
|
|
|
op->func_typeparams = NULL;
|
2021-02-18 14:20:16 -04:00
|
|
|
op->vectorcall = _PyFunction_Vectorcall;
|
2023-08-23 19:36:19 -03:00
|
|
|
op->func_version = 0;
|
2024-04-12 14:36:20 -03:00
|
|
|
if ((code_obj->co_flags & CO_NESTED) == 0) {
|
|
|
|
// Use deferred reference counting for top-level functions, but not
|
|
|
|
// nested functions because they are more likely to capture variables,
|
|
|
|
// which makes prompt deallocation more important.
|
|
|
|
_PyObject_SetDeferredRefcount((PyObject *)op);
|
|
|
|
}
|
2010-05-09 12:52:27 -03:00
|
|
|
_PyObject_GC_TRACK(op);
|
2022-11-22 08:06:44 -04:00
|
|
|
handle_func_event(PyFunction_EVENT_CREATE, op, NULL);
|
2010-05-09 12:52:27 -03:00
|
|
|
return (PyObject *)op;
|
2021-02-18 14:20:16 -04:00
|
|
|
|
|
|
|
error:
|
|
|
|
Py_DECREF(globals);
|
|
|
|
Py_DECREF(code_obj);
|
|
|
|
Py_DECREF(name);
|
|
|
|
Py_DECREF(qualname);
|
|
|
|
Py_DECREF(doc);
|
|
|
|
Py_XDECREF(module);
|
|
|
|
Py_XDECREF(builtins);
|
|
|
|
return NULL;
|
1990-10-14 09:07:46 -03:00
|
|
|
}
|
|
|
|
|
2023-08-17 15:29:58 -03:00
|
|
|
/*
|
gh-117045: Add code object to function version cache (#117028)
Changes to the function version cache:
- In addition to the function object, also store the code object,
and allow the latter to be retrieved even if the function has been evicted.
- Stop assigning new function versions after a critical attribute (e.g. `__code__`)
has been modified; the version is permanently reset to zero in this case.
- Changes to `__annotations__` are no longer considered critical. (This fixes gh-109998.)
Changes to the Tier 2 optimization machinery:
- If we cannot map a function version to a function, but it is still mapped to a code object,
we continue projecting the trace.
The operand of the `_PUSH_FRAME` and `_POP_FRAME` opcodes can be either NULL,
a function object, or a code object with the lowest bit set.
This allows us to trace through code that calls an ephemeral function,
i.e., a function that may not be alive when we are constructing the executor,
e.g. a generator expression or certain nested functions.
We will lose globals removal inside such functions,
but we can still do other peephole operations
(and even possibly [call inlining](https://github.com/python/cpython/pull/116290),
if we decide to do it), which only need the code object.
As before, if we cannot retrieve the code object from the cache, we stop projecting.
2024-03-21 16:37:41 -03:00
|
|
|
(This is purely internal documentation. There are no public APIs here.)
|
2023-08-17 15:29:58 -03:00
|
|
|
|
gh-117045: Add code object to function version cache (#117028)
Changes to the function version cache:
- In addition to the function object, also store the code object,
and allow the latter to be retrieved even if the function has been evicted.
- Stop assigning new function versions after a critical attribute (e.g. `__code__`)
has been modified; the version is permanently reset to zero in this case.
- Changes to `__annotations__` are no longer considered critical. (This fixes gh-109998.)
Changes to the Tier 2 optimization machinery:
- If we cannot map a function version to a function, but it is still mapped to a code object,
we continue projecting the trace.
The operand of the `_PUSH_FRAME` and `_POP_FRAME` opcodes can be either NULL,
a function object, or a code object with the lowest bit set.
This allows us to trace through code that calls an ephemeral function,
i.e., a function that may not be alive when we are constructing the executor,
e.g. a generator expression or certain nested functions.
We will lose globals removal inside such functions,
but we can still do other peephole operations
(and even possibly [call inlining](https://github.com/python/cpython/pull/116290),
if we decide to do it), which only need the code object.
As before, if we cannot retrieve the code object from the cache, we stop projecting.
2024-03-21 16:37:41 -03:00
|
|
|
Function (and code) versions
|
|
|
|
----------------------------
|
2023-08-17 15:29:58 -03:00
|
|
|
|
gh-117045: Add code object to function version cache (#117028)
Changes to the function version cache:
- In addition to the function object, also store the code object,
and allow the latter to be retrieved even if the function has been evicted.
- Stop assigning new function versions after a critical attribute (e.g. `__code__`)
has been modified; the version is permanently reset to zero in this case.
- Changes to `__annotations__` are no longer considered critical. (This fixes gh-109998.)
Changes to the Tier 2 optimization machinery:
- If we cannot map a function version to a function, but it is still mapped to a code object,
we continue projecting the trace.
The operand of the `_PUSH_FRAME` and `_POP_FRAME` opcodes can be either NULL,
a function object, or a code object with the lowest bit set.
This allows us to trace through code that calls an ephemeral function,
i.e., a function that may not be alive when we are constructing the executor,
e.g. a generator expression or certain nested functions.
We will lose globals removal inside such functions,
but we can still do other peephole operations
(and even possibly [call inlining](https://github.com/python/cpython/pull/116290),
if we decide to do it), which only need the code object.
As before, if we cannot retrieve the code object from the cache, we stop projecting.
2024-03-21 16:37:41 -03:00
|
|
|
The Tier 1 specializer generates CALL variants that can be invalidated
|
|
|
|
by changes to critical function attributes:
|
2023-08-17 15:29:58 -03:00
|
|
|
|
gh-117045: Add code object to function version cache (#117028)
Changes to the function version cache:
- In addition to the function object, also store the code object,
and allow the latter to be retrieved even if the function has been evicted.
- Stop assigning new function versions after a critical attribute (e.g. `__code__`)
has been modified; the version is permanently reset to zero in this case.
- Changes to `__annotations__` are no longer considered critical. (This fixes gh-109998.)
Changes to the Tier 2 optimization machinery:
- If we cannot map a function version to a function, but it is still mapped to a code object,
we continue projecting the trace.
The operand of the `_PUSH_FRAME` and `_POP_FRAME` opcodes can be either NULL,
a function object, or a code object with the lowest bit set.
This allows us to trace through code that calls an ephemeral function,
i.e., a function that may not be alive when we are constructing the executor,
e.g. a generator expression or certain nested functions.
We will lose globals removal inside such functions,
but we can still do other peephole operations
(and even possibly [call inlining](https://github.com/python/cpython/pull/116290),
if we decide to do it), which only need the code object.
As before, if we cannot retrieve the code object from the cache, we stop projecting.
2024-03-21 16:37:41 -03:00
|
|
|
- __code__
|
|
|
|
- __defaults__
|
|
|
|
- __kwdefaults__
|
|
|
|
- __closure__
|
2023-08-17 15:29:58 -03:00
|
|
|
|
gh-117045: Add code object to function version cache (#117028)
Changes to the function version cache:
- In addition to the function object, also store the code object,
and allow the latter to be retrieved even if the function has been evicted.
- Stop assigning new function versions after a critical attribute (e.g. `__code__`)
has been modified; the version is permanently reset to zero in this case.
- Changes to `__annotations__` are no longer considered critical. (This fixes gh-109998.)
Changes to the Tier 2 optimization machinery:
- If we cannot map a function version to a function, but it is still mapped to a code object,
we continue projecting the trace.
The operand of the `_PUSH_FRAME` and `_POP_FRAME` opcodes can be either NULL,
a function object, or a code object with the lowest bit set.
This allows us to trace through code that calls an ephemeral function,
i.e., a function that may not be alive when we are constructing the executor,
e.g. a generator expression or certain nested functions.
We will lose globals removal inside such functions,
but we can still do other peephole operations
(and even possibly [call inlining](https://github.com/python/cpython/pull/116290),
if we decide to do it), which only need the code object.
As before, if we cannot retrieve the code object from the cache, we stop projecting.
2024-03-21 16:37:41 -03:00
|
|
|
For this purpose function objects have a 32-bit func_version member
|
|
|
|
that the specializer writes to the specialized instruction's inline
|
|
|
|
cache and which is checked by a guard on the specialized instructions.
|
2023-08-17 15:29:58 -03:00
|
|
|
|
gh-117045: Add code object to function version cache (#117028)
Changes to the function version cache:
- In addition to the function object, also store the code object,
and allow the latter to be retrieved even if the function has been evicted.
- Stop assigning new function versions after a critical attribute (e.g. `__code__`)
has been modified; the version is permanently reset to zero in this case.
- Changes to `__annotations__` are no longer considered critical. (This fixes gh-109998.)
Changes to the Tier 2 optimization machinery:
- If we cannot map a function version to a function, but it is still mapped to a code object,
we continue projecting the trace.
The operand of the `_PUSH_FRAME` and `_POP_FRAME` opcodes can be either NULL,
a function object, or a code object with the lowest bit set.
This allows us to trace through code that calls an ephemeral function,
i.e., a function that may not be alive when we are constructing the executor,
e.g. a generator expression or certain nested functions.
We will lose globals removal inside such functions,
but we can still do other peephole operations
(and even possibly [call inlining](https://github.com/python/cpython/pull/116290),
if we decide to do it), which only need the code object.
As before, if we cannot retrieve the code object from the cache, we stop projecting.
2024-03-21 16:37:41 -03:00
|
|
|
The MAKE_FUNCTION bytecode sets func_version from the code object's
|
|
|
|
co_version field. The latter is initialized from a counter in the
|
|
|
|
interpreter state (interp->func_state.next_version) and never changes.
|
|
|
|
When this counter overflows, it remains zero and the specializer loses
|
|
|
|
the ability to specialize calls to new functions.
|
2023-08-17 15:29:58 -03:00
|
|
|
|
gh-117045: Add code object to function version cache (#117028)
Changes to the function version cache:
- In addition to the function object, also store the code object,
and allow the latter to be retrieved even if the function has been evicted.
- Stop assigning new function versions after a critical attribute (e.g. `__code__`)
has been modified; the version is permanently reset to zero in this case.
- Changes to `__annotations__` are no longer considered critical. (This fixes gh-109998.)
Changes to the Tier 2 optimization machinery:
- If we cannot map a function version to a function, but it is still mapped to a code object,
we continue projecting the trace.
The operand of the `_PUSH_FRAME` and `_POP_FRAME` opcodes can be either NULL,
a function object, or a code object with the lowest bit set.
This allows us to trace through code that calls an ephemeral function,
i.e., a function that may not be alive when we are constructing the executor,
e.g. a generator expression or certain nested functions.
We will lose globals removal inside such functions,
but we can still do other peephole operations
(and even possibly [call inlining](https://github.com/python/cpython/pull/116290),
if we decide to do it), which only need the code object.
As before, if we cannot retrieve the code object from the cache, we stop projecting.
2024-03-21 16:37:41 -03:00
|
|
|
The func_version is reset to zero when any of the critical attributes
|
|
|
|
is modified; after this point the specializer will no longer specialize
|
|
|
|
calls to this function, and the guard will always fail.
|
2023-08-17 15:29:58 -03:00
|
|
|
|
gh-117045: Add code object to function version cache (#117028)
Changes to the function version cache:
- In addition to the function object, also store the code object,
and allow the latter to be retrieved even if the function has been evicted.
- Stop assigning new function versions after a critical attribute (e.g. `__code__`)
has been modified; the version is permanently reset to zero in this case.
- Changes to `__annotations__` are no longer considered critical. (This fixes gh-109998.)
Changes to the Tier 2 optimization machinery:
- If we cannot map a function version to a function, but it is still mapped to a code object,
we continue projecting the trace.
The operand of the `_PUSH_FRAME` and `_POP_FRAME` opcodes can be either NULL,
a function object, or a code object with the lowest bit set.
This allows us to trace through code that calls an ephemeral function,
i.e., a function that may not be alive when we are constructing the executor,
e.g. a generator expression or certain nested functions.
We will lose globals removal inside such functions,
but we can still do other peephole operations
(and even possibly [call inlining](https://github.com/python/cpython/pull/116290),
if we decide to do it), which only need the code object.
As before, if we cannot retrieve the code object from the cache, we stop projecting.
2024-03-21 16:37:41 -03:00
|
|
|
The function and code version cache
|
|
|
|
-----------------------------------
|
2023-08-17 15:29:58 -03:00
|
|
|
|
gh-117045: Add code object to function version cache (#117028)
Changes to the function version cache:
- In addition to the function object, also store the code object,
and allow the latter to be retrieved even if the function has been evicted.
- Stop assigning new function versions after a critical attribute (e.g. `__code__`)
has been modified; the version is permanently reset to zero in this case.
- Changes to `__annotations__` are no longer considered critical. (This fixes gh-109998.)
Changes to the Tier 2 optimization machinery:
- If we cannot map a function version to a function, but it is still mapped to a code object,
we continue projecting the trace.
The operand of the `_PUSH_FRAME` and `_POP_FRAME` opcodes can be either NULL,
a function object, or a code object with the lowest bit set.
This allows us to trace through code that calls an ephemeral function,
i.e., a function that may not be alive when we are constructing the executor,
e.g. a generator expression or certain nested functions.
We will lose globals removal inside such functions,
but we can still do other peephole operations
(and even possibly [call inlining](https://github.com/python/cpython/pull/116290),
if we decide to do it), which only need the code object.
As before, if we cannot retrieve the code object from the cache, we stop projecting.
2024-03-21 16:37:41 -03:00
|
|
|
The Tier 2 optimizer now has a problem, since it needs to find the
|
|
|
|
function and code objects given only the version number from the inline
|
|
|
|
cache. Our solution is to maintain a cache mapping version numbers to
|
|
|
|
function and code objects. To limit the cache size we could hash
|
|
|
|
the version number, but for now we simply use it modulo the table size.
|
|
|
|
|
|
|
|
There are some corner cases (e.g. generator expressions) where we will
|
|
|
|
be unable to find the function object in the cache but we can still
|
|
|
|
find the code object. For this reason the cache stores both the
|
|
|
|
function object and the code object.
|
|
|
|
|
|
|
|
The cache doesn't contain strong references; cache entries are
|
|
|
|
invalidated whenever the function or code object is deallocated.
|
|
|
|
|
|
|
|
Invariants
|
|
|
|
----------
|
|
|
|
|
|
|
|
These should hold at any time except when one of the cache-mutating
|
|
|
|
functions is running.
|
|
|
|
|
|
|
|
- For any slot s at index i:
|
|
|
|
- s->func == NULL or s->func->func_version % FUNC_VERSION_CACHE_SIZE == i
|
|
|
|
- s->code == NULL or s->code->co_version % FUNC_VERSION_CACHE_SIZE == i
|
|
|
|
if s->func != NULL, then s->func->func_code == s->code
|
2023-08-17 15:29:58 -03:00
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
void
|
|
|
|
_PyFunction_SetVersion(PyFunctionObject *func, uint32_t version)
|
|
|
|
{
|
2024-05-03 17:21:04 -03:00
|
|
|
#ifndef Py_GIL_DISABLED
|
2023-08-22 12:29:49 -03:00
|
|
|
PyInterpreterState *interp = _PyInterpreterState_GET();
|
|
|
|
if (func->func_version != 0) {
|
gh-117045: Add code object to function version cache (#117028)
Changes to the function version cache:
- In addition to the function object, also store the code object,
and allow the latter to be retrieved even if the function has been evicted.
- Stop assigning new function versions after a critical attribute (e.g. `__code__`)
has been modified; the version is permanently reset to zero in this case.
- Changes to `__annotations__` are no longer considered critical. (This fixes gh-109998.)
Changes to the Tier 2 optimization machinery:
- If we cannot map a function version to a function, but it is still mapped to a code object,
we continue projecting the trace.
The operand of the `_PUSH_FRAME` and `_POP_FRAME` opcodes can be either NULL,
a function object, or a code object with the lowest bit set.
This allows us to trace through code that calls an ephemeral function,
i.e., a function that may not be alive when we are constructing the executor,
e.g. a generator expression or certain nested functions.
We will lose globals removal inside such functions,
but we can still do other peephole operations
(and even possibly [call inlining](https://github.com/python/cpython/pull/116290),
if we decide to do it), which only need the code object.
As before, if we cannot retrieve the code object from the cache, we stop projecting.
2024-03-21 16:37:41 -03:00
|
|
|
struct _func_version_cache_item *slot =
|
2023-08-22 12:29:49 -03:00
|
|
|
interp->func_state.func_version_cache
|
|
|
|
+ (func->func_version % FUNC_VERSION_CACHE_SIZE);
|
gh-117045: Add code object to function version cache (#117028)
Changes to the function version cache:
- In addition to the function object, also store the code object,
and allow the latter to be retrieved even if the function has been evicted.
- Stop assigning new function versions after a critical attribute (e.g. `__code__`)
has been modified; the version is permanently reset to zero in this case.
- Changes to `__annotations__` are no longer considered critical. (This fixes gh-109998.)
Changes to the Tier 2 optimization machinery:
- If we cannot map a function version to a function, but it is still mapped to a code object,
we continue projecting the trace.
The operand of the `_PUSH_FRAME` and `_POP_FRAME` opcodes can be either NULL,
a function object, or a code object with the lowest bit set.
This allows us to trace through code that calls an ephemeral function,
i.e., a function that may not be alive when we are constructing the executor,
e.g. a generator expression or certain nested functions.
We will lose globals removal inside such functions,
but we can still do other peephole operations
(and even possibly [call inlining](https://github.com/python/cpython/pull/116290),
if we decide to do it), which only need the code object.
As before, if we cannot retrieve the code object from the cache, we stop projecting.
2024-03-21 16:37:41 -03:00
|
|
|
if (slot->func == func) {
|
|
|
|
slot->func = NULL;
|
|
|
|
// Leave slot->code alone, there may be use for it.
|
2023-08-22 12:29:49 -03:00
|
|
|
}
|
|
|
|
}
|
2024-05-03 17:21:04 -03:00
|
|
|
#endif
|
2023-08-17 15:29:58 -03:00
|
|
|
func->func_version = version;
|
2024-05-03 17:21:04 -03:00
|
|
|
#ifndef Py_GIL_DISABLED
|
2023-08-17 15:29:58 -03:00
|
|
|
if (version != 0) {
|
gh-117045: Add code object to function version cache (#117028)
Changes to the function version cache:
- In addition to the function object, also store the code object,
and allow the latter to be retrieved even if the function has been evicted.
- Stop assigning new function versions after a critical attribute (e.g. `__code__`)
has been modified; the version is permanently reset to zero in this case.
- Changes to `__annotations__` are no longer considered critical. (This fixes gh-109998.)
Changes to the Tier 2 optimization machinery:
- If we cannot map a function version to a function, but it is still mapped to a code object,
we continue projecting the trace.
The operand of the `_PUSH_FRAME` and `_POP_FRAME` opcodes can be either NULL,
a function object, or a code object with the lowest bit set.
This allows us to trace through code that calls an ephemeral function,
i.e., a function that may not be alive when we are constructing the executor,
e.g. a generator expression or certain nested functions.
We will lose globals removal inside such functions,
but we can still do other peephole operations
(and even possibly [call inlining](https://github.com/python/cpython/pull/116290),
if we decide to do it), which only need the code object.
As before, if we cannot retrieve the code object from the cache, we stop projecting.
2024-03-21 16:37:41 -03:00
|
|
|
struct _func_version_cache_item *slot =
|
|
|
|
interp->func_state.func_version_cache
|
|
|
|
+ (version % FUNC_VERSION_CACHE_SIZE);
|
|
|
|
slot->func = func;
|
|
|
|
slot->code = func->func_code;
|
|
|
|
}
|
2024-05-03 17:21:04 -03:00
|
|
|
#endif
|
gh-117045: Add code object to function version cache (#117028)
Changes to the function version cache:
- In addition to the function object, also store the code object,
and allow the latter to be retrieved even if the function has been evicted.
- Stop assigning new function versions after a critical attribute (e.g. `__code__`)
has been modified; the version is permanently reset to zero in this case.
- Changes to `__annotations__` are no longer considered critical. (This fixes gh-109998.)
Changes to the Tier 2 optimization machinery:
- If we cannot map a function version to a function, but it is still mapped to a code object,
we continue projecting the trace.
The operand of the `_PUSH_FRAME` and `_POP_FRAME` opcodes can be either NULL,
a function object, or a code object with the lowest bit set.
This allows us to trace through code that calls an ephemeral function,
i.e., a function that may not be alive when we are constructing the executor,
e.g. a generator expression or certain nested functions.
We will lose globals removal inside such functions,
but we can still do other peephole operations
(and even possibly [call inlining](https://github.com/python/cpython/pull/116290),
if we decide to do it), which only need the code object.
As before, if we cannot retrieve the code object from the cache, we stop projecting.
2024-03-21 16:37:41 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
_PyFunction_ClearCodeByVersion(uint32_t version)
|
|
|
|
{
|
2024-05-03 17:21:04 -03:00
|
|
|
#ifndef Py_GIL_DISABLED
|
gh-117045: Add code object to function version cache (#117028)
Changes to the function version cache:
- In addition to the function object, also store the code object,
and allow the latter to be retrieved even if the function has been evicted.
- Stop assigning new function versions after a critical attribute (e.g. `__code__`)
has been modified; the version is permanently reset to zero in this case.
- Changes to `__annotations__` are no longer considered critical. (This fixes gh-109998.)
Changes to the Tier 2 optimization machinery:
- If we cannot map a function version to a function, but it is still mapped to a code object,
we continue projecting the trace.
The operand of the `_PUSH_FRAME` and `_POP_FRAME` opcodes can be either NULL,
a function object, or a code object with the lowest bit set.
This allows us to trace through code that calls an ephemeral function,
i.e., a function that may not be alive when we are constructing the executor,
e.g. a generator expression or certain nested functions.
We will lose globals removal inside such functions,
but we can still do other peephole operations
(and even possibly [call inlining](https://github.com/python/cpython/pull/116290),
if we decide to do it), which only need the code object.
As before, if we cannot retrieve the code object from the cache, we stop projecting.
2024-03-21 16:37:41 -03:00
|
|
|
PyInterpreterState *interp = _PyInterpreterState_GET();
|
|
|
|
struct _func_version_cache_item *slot =
|
|
|
|
interp->func_state.func_version_cache
|
|
|
|
+ (version % FUNC_VERSION_CACHE_SIZE);
|
|
|
|
if (slot->code) {
|
|
|
|
assert(PyCode_Check(slot->code));
|
|
|
|
PyCodeObject *code = (PyCodeObject *)slot->code;
|
|
|
|
if (code->co_version == version) {
|
|
|
|
slot->code = NULL;
|
|
|
|
slot->func = NULL;
|
|
|
|
}
|
2023-08-17 15:29:58 -03:00
|
|
|
}
|
2024-05-03 17:21:04 -03:00
|
|
|
#endif
|
2023-08-17 15:29:58 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
PyFunctionObject *
|
gh-117045: Add code object to function version cache (#117028)
Changes to the function version cache:
- In addition to the function object, also store the code object,
and allow the latter to be retrieved even if the function has been evicted.
- Stop assigning new function versions after a critical attribute (e.g. `__code__`)
has been modified; the version is permanently reset to zero in this case.
- Changes to `__annotations__` are no longer considered critical. (This fixes gh-109998.)
Changes to the Tier 2 optimization machinery:
- If we cannot map a function version to a function, but it is still mapped to a code object,
we continue projecting the trace.
The operand of the `_PUSH_FRAME` and `_POP_FRAME` opcodes can be either NULL,
a function object, or a code object with the lowest bit set.
This allows us to trace through code that calls an ephemeral function,
i.e., a function that may not be alive when we are constructing the executor,
e.g. a generator expression or certain nested functions.
We will lose globals removal inside such functions,
but we can still do other peephole operations
(and even possibly [call inlining](https://github.com/python/cpython/pull/116290),
if we decide to do it), which only need the code object.
As before, if we cannot retrieve the code object from the cache, we stop projecting.
2024-03-21 16:37:41 -03:00
|
|
|
_PyFunction_LookupByVersion(uint32_t version, PyObject **p_code)
|
2023-08-17 15:29:58 -03:00
|
|
|
{
|
2024-05-03 17:21:04 -03:00
|
|
|
#ifdef Py_GIL_DISABLED
|
|
|
|
return NULL;
|
|
|
|
#else
|
2023-08-17 15:29:58 -03:00
|
|
|
PyInterpreterState *interp = _PyInterpreterState_GET();
|
gh-117045: Add code object to function version cache (#117028)
Changes to the function version cache:
- In addition to the function object, also store the code object,
and allow the latter to be retrieved even if the function has been evicted.
- Stop assigning new function versions after a critical attribute (e.g. `__code__`)
has been modified; the version is permanently reset to zero in this case.
- Changes to `__annotations__` are no longer considered critical. (This fixes gh-109998.)
Changes to the Tier 2 optimization machinery:
- If we cannot map a function version to a function, but it is still mapped to a code object,
we continue projecting the trace.
The operand of the `_PUSH_FRAME` and `_POP_FRAME` opcodes can be either NULL,
a function object, or a code object with the lowest bit set.
This allows us to trace through code that calls an ephemeral function,
i.e., a function that may not be alive when we are constructing the executor,
e.g. a generator expression or certain nested functions.
We will lose globals removal inside such functions,
but we can still do other peephole operations
(and even possibly [call inlining](https://github.com/python/cpython/pull/116290),
if we decide to do it), which only need the code object.
As before, if we cannot retrieve the code object from the cache, we stop projecting.
2024-03-21 16:37:41 -03:00
|
|
|
struct _func_version_cache_item *slot =
|
|
|
|
interp->func_state.func_version_cache
|
|
|
|
+ (version % FUNC_VERSION_CACHE_SIZE);
|
|
|
|
if (slot->code) {
|
|
|
|
assert(PyCode_Check(slot->code));
|
|
|
|
PyCodeObject *code = (PyCodeObject *)slot->code;
|
|
|
|
if (code->co_version == version) {
|
|
|
|
*p_code = slot->code;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
*p_code = NULL;
|
|
|
|
}
|
|
|
|
if (slot->func && slot->func->func_version == version) {
|
|
|
|
assert(slot->func->func_code == slot->code);
|
|
|
|
return slot->func;
|
2023-08-17 15:29:58 -03:00
|
|
|
}
|
|
|
|
return NULL;
|
2024-05-03 17:21:04 -03:00
|
|
|
#endif
|
2023-08-17 15:29:58 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t
|
|
|
|
_PyFunction_GetVersionForCurrentState(PyFunctionObject *func)
|
2021-07-12 06:01:01 -03:00
|
|
|
{
|
gh-117045: Add code object to function version cache (#117028)
Changes to the function version cache:
- In addition to the function object, also store the code object,
and allow the latter to be retrieved even if the function has been evicted.
- Stop assigning new function versions after a critical attribute (e.g. `__code__`)
has been modified; the version is permanently reset to zero in this case.
- Changes to `__annotations__` are no longer considered critical. (This fixes gh-109998.)
Changes to the Tier 2 optimization machinery:
- If we cannot map a function version to a function, but it is still mapped to a code object,
we continue projecting the trace.
The operand of the `_PUSH_FRAME` and `_POP_FRAME` opcodes can be either NULL,
a function object, or a code object with the lowest bit set.
This allows us to trace through code that calls an ephemeral function,
i.e., a function that may not be alive when we are constructing the executor,
e.g. a generator expression or certain nested functions.
We will lose globals removal inside such functions,
but we can still do other peephole operations
(and even possibly [call inlining](https://github.com/python/cpython/pull/116290),
if we decide to do it), which only need the code object.
As before, if we cannot retrieve the code object from the cache, we stop projecting.
2024-03-21 16:37:41 -03:00
|
|
|
return func->func_version;
|
2021-07-12 06:01:01 -03:00
|
|
|
}
|
|
|
|
|
2011-11-25 13:56:07 -04:00
|
|
|
PyObject *
|
|
|
|
PyFunction_New(PyObject *code, PyObject *globals)
|
|
|
|
{
|
|
|
|
return PyFunction_NewWithQualName(code, globals, NULL);
|
|
|
|
}
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
PyObject *
|
2000-07-09 03:03:25 -03:00
|
|
|
PyFunction_GetCode(PyObject *op)
|
1990-10-14 09:07:46 -03:00
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
if (!PyFunction_Check(op)) {
|
|
|
|
PyErr_BadInternalCall();
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return ((PyFunctionObject *) op) -> func_code;
|
1990-10-14 09:07:46 -03:00
|
|
|
}
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
PyObject *
|
2000-07-09 03:03:25 -03:00
|
|
|
PyFunction_GetGlobals(PyObject *op)
|
1990-10-14 09:07:46 -03:00
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
if (!PyFunction_Check(op)) {
|
|
|
|
PyErr_BadInternalCall();
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return ((PyFunctionObject *) op) -> func_globals;
|
1990-10-14 09:07:46 -03:00
|
|
|
}
|
|
|
|
|
2003-01-31 14:33:18 -04:00
|
|
|
PyObject *
|
|
|
|
PyFunction_GetModule(PyObject *op)
|
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
if (!PyFunction_Check(op)) {
|
|
|
|
PyErr_BadInternalCall();
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return ((PyFunctionObject *) op) -> func_module;
|
2003-01-31 14:33:18 -04:00
|
|
|
}
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
PyObject *
|
2000-07-09 03:03:25 -03:00
|
|
|
PyFunction_GetDefaults(PyObject *op)
|
1994-08-30 05:27:36 -03:00
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
if (!PyFunction_Check(op)) {
|
|
|
|
PyErr_BadInternalCall();
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return ((PyFunctionObject *) op) -> func_defaults;
|
1994-08-30 05:27:36 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2000-07-09 03:03:25 -03:00
|
|
|
PyFunction_SetDefaults(PyObject *op, PyObject *defaults)
|
1994-08-30 05:27:36 -03:00
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
if (!PyFunction_Check(op)) {
|
|
|
|
PyErr_BadInternalCall();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (defaults == Py_None)
|
|
|
|
defaults = NULL;
|
|
|
|
else if (defaults && PyTuple_Check(defaults)) {
|
|
|
|
Py_INCREF(defaults);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
PyErr_SetString(PyExc_SystemError, "non-tuple default args");
|
|
|
|
return -1;
|
|
|
|
}
|
2022-11-22 08:06:44 -04:00
|
|
|
handle_func_event(PyFunction_EVENT_MODIFY_DEFAULTS,
|
|
|
|
(PyFunctionObject *) op, defaults);
|
2023-08-22 12:29:49 -03:00
|
|
|
_PyFunction_SetVersion((PyFunctionObject *)op, 0);
|
2016-04-06 03:45:48 -03:00
|
|
|
Py_XSETREF(((PyFunctionObject *)op)->func_defaults, defaults);
|
2010-05-09 12:52:27 -03:00
|
|
|
return 0;
|
1994-08-30 05:27:36 -03:00
|
|
|
}
|
|
|
|
|
2022-09-15 12:42:37 -03:00
|
|
|
void
|
|
|
|
PyFunction_SetVectorcall(PyFunctionObject *func, vectorcallfunc vectorcall)
|
|
|
|
{
|
|
|
|
assert(func != NULL);
|
2023-08-22 12:29:49 -03:00
|
|
|
_PyFunction_SetVersion(func, 0);
|
2022-09-15 12:42:37 -03:00
|
|
|
func->vectorcall = vectorcall;
|
|
|
|
}
|
|
|
|
|
2006-10-27 20:31:49 -03:00
|
|
|
PyObject *
|
|
|
|
PyFunction_GetKwDefaults(PyObject *op)
|
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
if (!PyFunction_Check(op)) {
|
|
|
|
PyErr_BadInternalCall();
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return ((PyFunctionObject *) op) -> func_kwdefaults;
|
2006-10-27 20:31:49 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
PyFunction_SetKwDefaults(PyObject *op, PyObject *defaults)
|
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
if (!PyFunction_Check(op)) {
|
|
|
|
PyErr_BadInternalCall();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (defaults == Py_None)
|
|
|
|
defaults = NULL;
|
|
|
|
else if (defaults && PyDict_Check(defaults)) {
|
|
|
|
Py_INCREF(defaults);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
PyErr_SetString(PyExc_SystemError,
|
|
|
|
"non-dict keyword only default args");
|
|
|
|
return -1;
|
|
|
|
}
|
2022-11-22 08:06:44 -04:00
|
|
|
handle_func_event(PyFunction_EVENT_MODIFY_KWDEFAULTS,
|
|
|
|
(PyFunctionObject *) op, defaults);
|
2023-08-22 12:29:49 -03:00
|
|
|
_PyFunction_SetVersion((PyFunctionObject *)op, 0);
|
2016-04-06 03:45:48 -03:00
|
|
|
Py_XSETREF(((PyFunctionObject *)op)->func_kwdefaults, defaults);
|
2010-05-09 12:52:27 -03:00
|
|
|
return 0;
|
2006-10-27 20:31:49 -03:00
|
|
|
}
|
|
|
|
|
PEP 227 implementation
The majority of the changes are in the compiler. The mainloop changes
primarily to implement the new opcodes and to pass a function's
closure to eval_code2(). Frames and functions got new slots to hold
the closure.
Include/compile.h
Add co_freevars and co_cellvars slots to code objects.
Update PyCode_New() to take freevars and cellvars as arguments
Include/funcobject.h
Add func_closure slot to function objects.
Add GetClosure()/SetClosure() functions (and corresponding
macros) for getting at the closure.
Include/frameobject.h
PyFrame_New() now takes a closure.
Include/opcode.h
Add four new opcodes: MAKE_CLOSURE, LOAD_CLOSURE, LOAD_DEREF,
STORE_DEREF.
Remove comment about old requirement for opcodes to fit in 7
bits.
compile.c
Implement changes to code objects for co_freevars and co_cellvars.
Modify symbol table to use st_cur_name (string object for the name
of the current scope) and st_cur_children (list of nested blocks).
Also define st_nested, which might more properly be called
st_cur_nested. Add several DEF_XXX flags to track def-use
information for free variables.
New or modified functions of note:
com_make_closure(struct compiling *, PyCodeObject *)
Emit LOAD_CLOSURE opcodes as needed to pass cells for free
variables into nested scope.
com_addop_varname(struct compiling *, int, char *)
Emits opcodes for LOAD_DEREF and STORE_DEREF.
get_ref_type(struct compiling *, char *name)
Return NAME_CLOSURE if ref type is FREE or CELL
symtable_load_symbols(struct compiling *)
Decides what variables are cell or free based on def-use info.
Can now raise SyntaxError if nested scopes are mixed with
exec or from blah import *.
make_scope_info(PyObject *, PyObject *, int, int)
Helper functions for symtable scope stack.
symtable_update_free_vars(struct symtable *)
After a code block has been analyzed, it must check each of
its children for free variables that are not defined in the
block. If a variable is free in a child and not defined in
the parent, then it is defined by block the enclosing the
current one or it is a global. This does the right logic.
symtable_add_use() is now a macro for symtable_add_def()
symtable_assign(struct symtable *, node *)
Use goto instead of for (;;)
Fixed bug in symtable where name of keyword argument in function
call was treated as assignment in the scope of the call site. Ex:
def f():
g(a=2) # a was considered a local of f
ceval.c
eval_code2() now take one more argument, a closure.
Implement LOAD_CLOSURE, LOAD_DEREF, STORE_DEREF, MAKE_CLOSURE>
Also: When name error occurs for global variable, report that the
name was global in the error mesage.
Objects/frameobject.c
Initialize f_closure to be a tuple containing space for cellvars
and freevars. f_closure is NULL if neither are present.
Objects/funcobject.c
Add support for func_closure.
Python/import.c
Change the magic number.
Python/marshal.c
Track changes to code objects.
2001-01-25 16:06:59 -04:00
|
|
|
PyObject *
|
|
|
|
PyFunction_GetClosure(PyObject *op)
|
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
if (!PyFunction_Check(op)) {
|
|
|
|
PyErr_BadInternalCall();
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return ((PyFunctionObject *) op) -> func_closure;
|
PEP 227 implementation
The majority of the changes are in the compiler. The mainloop changes
primarily to implement the new opcodes and to pass a function's
closure to eval_code2(). Frames and functions got new slots to hold
the closure.
Include/compile.h
Add co_freevars and co_cellvars slots to code objects.
Update PyCode_New() to take freevars and cellvars as arguments
Include/funcobject.h
Add func_closure slot to function objects.
Add GetClosure()/SetClosure() functions (and corresponding
macros) for getting at the closure.
Include/frameobject.h
PyFrame_New() now takes a closure.
Include/opcode.h
Add four new opcodes: MAKE_CLOSURE, LOAD_CLOSURE, LOAD_DEREF,
STORE_DEREF.
Remove comment about old requirement for opcodes to fit in 7
bits.
compile.c
Implement changes to code objects for co_freevars and co_cellvars.
Modify symbol table to use st_cur_name (string object for the name
of the current scope) and st_cur_children (list of nested blocks).
Also define st_nested, which might more properly be called
st_cur_nested. Add several DEF_XXX flags to track def-use
information for free variables.
New or modified functions of note:
com_make_closure(struct compiling *, PyCodeObject *)
Emit LOAD_CLOSURE opcodes as needed to pass cells for free
variables into nested scope.
com_addop_varname(struct compiling *, int, char *)
Emits opcodes for LOAD_DEREF and STORE_DEREF.
get_ref_type(struct compiling *, char *name)
Return NAME_CLOSURE if ref type is FREE or CELL
symtable_load_symbols(struct compiling *)
Decides what variables are cell or free based on def-use info.
Can now raise SyntaxError if nested scopes are mixed with
exec or from blah import *.
make_scope_info(PyObject *, PyObject *, int, int)
Helper functions for symtable scope stack.
symtable_update_free_vars(struct symtable *)
After a code block has been analyzed, it must check each of
its children for free variables that are not defined in the
block. If a variable is free in a child and not defined in
the parent, then it is defined by block the enclosing the
current one or it is a global. This does the right logic.
symtable_add_use() is now a macro for symtable_add_def()
symtable_assign(struct symtable *, node *)
Use goto instead of for (;;)
Fixed bug in symtable where name of keyword argument in function
call was treated as assignment in the scope of the call site. Ex:
def f():
g(a=2) # a was considered a local of f
ceval.c
eval_code2() now take one more argument, a closure.
Implement LOAD_CLOSURE, LOAD_DEREF, STORE_DEREF, MAKE_CLOSURE>
Also: When name error occurs for global variable, report that the
name was global in the error mesage.
Objects/frameobject.c
Initialize f_closure to be a tuple containing space for cellvars
and freevars. f_closure is NULL if neither are present.
Objects/funcobject.c
Add support for func_closure.
Python/import.c
Change the magic number.
Python/marshal.c
Track changes to code objects.
2001-01-25 16:06:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
PyFunction_SetClosure(PyObject *op, PyObject *closure)
|
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
if (!PyFunction_Check(op)) {
|
|
|
|
PyErr_BadInternalCall();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (closure == Py_None)
|
|
|
|
closure = NULL;
|
|
|
|
else if (PyTuple_Check(closure)) {
|
|
|
|
Py_INCREF(closure);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
PyErr_Format(PyExc_SystemError,
|
|
|
|
"expected tuple for closure, got '%.100s'",
|
2020-02-06 22:04:21 -04:00
|
|
|
Py_TYPE(closure)->tp_name);
|
2010-05-09 12:52:27 -03:00
|
|
|
return -1;
|
|
|
|
}
|
2023-08-22 12:29:49 -03:00
|
|
|
_PyFunction_SetVersion((PyFunctionObject *)op, 0);
|
2016-04-06 03:45:48 -03:00
|
|
|
Py_XSETREF(((PyFunctionObject *)op)->func_closure, closure);
|
2010-05-09 12:52:27 -03:00
|
|
|
return 0;
|
PEP 227 implementation
The majority of the changes are in the compiler. The mainloop changes
primarily to implement the new opcodes and to pass a function's
closure to eval_code2(). Frames and functions got new slots to hold
the closure.
Include/compile.h
Add co_freevars and co_cellvars slots to code objects.
Update PyCode_New() to take freevars and cellvars as arguments
Include/funcobject.h
Add func_closure slot to function objects.
Add GetClosure()/SetClosure() functions (and corresponding
macros) for getting at the closure.
Include/frameobject.h
PyFrame_New() now takes a closure.
Include/opcode.h
Add four new opcodes: MAKE_CLOSURE, LOAD_CLOSURE, LOAD_DEREF,
STORE_DEREF.
Remove comment about old requirement for opcodes to fit in 7
bits.
compile.c
Implement changes to code objects for co_freevars and co_cellvars.
Modify symbol table to use st_cur_name (string object for the name
of the current scope) and st_cur_children (list of nested blocks).
Also define st_nested, which might more properly be called
st_cur_nested. Add several DEF_XXX flags to track def-use
information for free variables.
New or modified functions of note:
com_make_closure(struct compiling *, PyCodeObject *)
Emit LOAD_CLOSURE opcodes as needed to pass cells for free
variables into nested scope.
com_addop_varname(struct compiling *, int, char *)
Emits opcodes for LOAD_DEREF and STORE_DEREF.
get_ref_type(struct compiling *, char *name)
Return NAME_CLOSURE if ref type is FREE or CELL
symtable_load_symbols(struct compiling *)
Decides what variables are cell or free based on def-use info.
Can now raise SyntaxError if nested scopes are mixed with
exec or from blah import *.
make_scope_info(PyObject *, PyObject *, int, int)
Helper functions for symtable scope stack.
symtable_update_free_vars(struct symtable *)
After a code block has been analyzed, it must check each of
its children for free variables that are not defined in the
block. If a variable is free in a child and not defined in
the parent, then it is defined by block the enclosing the
current one or it is a global. This does the right logic.
symtable_add_use() is now a macro for symtable_add_def()
symtable_assign(struct symtable *, node *)
Use goto instead of for (;;)
Fixed bug in symtable where name of keyword argument in function
call was treated as assignment in the scope of the call site. Ex:
def f():
g(a=2) # a was considered a local of f
ceval.c
eval_code2() now take one more argument, a closure.
Implement LOAD_CLOSURE, LOAD_DEREF, STORE_DEREF, MAKE_CLOSURE>
Also: When name error occurs for global variable, report that the
name was global in the error mesage.
Objects/frameobject.c
Initialize f_closure to be a tuple containing space for cellvars
and freevars. f_closure is NULL if neither are present.
Objects/funcobject.c
Add support for func_closure.
Python/import.c
Change the magic number.
Python/marshal.c
Track changes to code objects.
2001-01-25 16:06:59 -04:00
|
|
|
}
|
|
|
|
|
2022-01-05 08:25:54 -04:00
|
|
|
static PyObject *
|
|
|
|
func_get_annotation_dict(PyFunctionObject *op)
|
|
|
|
{
|
|
|
|
if (op->func_annotations == NULL) {
|
2024-05-21 23:38:12 -03:00
|
|
|
if (op->func_annotate == NULL || !PyCallable_Check(op->func_annotate)) {
|
|
|
|
Py_RETURN_NONE;
|
|
|
|
}
|
|
|
|
PyObject *one = _PyLong_GetOne();
|
|
|
|
PyObject *ann_dict = _PyObject_CallOneArg(op->func_annotate, one);
|
|
|
|
if (ann_dict == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (!PyDict_Check(ann_dict)) {
|
|
|
|
PyErr_Format(PyExc_TypeError, "__annotate__ returned non-dict of type '%.100s'",
|
|
|
|
Py_TYPE(ann_dict)->tp_name);
|
|
|
|
Py_DECREF(ann_dict);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
Py_XSETREF(op->func_annotations, ann_dict);
|
|
|
|
return ann_dict;
|
2022-01-05 08:25:54 -04:00
|
|
|
}
|
|
|
|
if (PyTuple_CheckExact(op->func_annotations)) {
|
|
|
|
PyObject *ann_tuple = op->func_annotations;
|
|
|
|
PyObject *ann_dict = PyDict_New();
|
|
|
|
if (ann_dict == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(PyTuple_GET_SIZE(ann_tuple) % 2 == 0);
|
|
|
|
|
|
|
|
for (Py_ssize_t i = 0; i < PyTuple_GET_SIZE(ann_tuple); i += 2) {
|
|
|
|
int err = PyDict_SetItem(ann_dict,
|
|
|
|
PyTuple_GET_ITEM(ann_tuple, i),
|
|
|
|
PyTuple_GET_ITEM(ann_tuple, i + 1));
|
|
|
|
|
|
|
|
if (err < 0) {
|
2024-07-24 09:47:52 -03:00
|
|
|
Py_DECREF(ann_dict);
|
2022-01-05 08:25:54 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Py_SETREF(op->func_annotations, ann_dict);
|
|
|
|
}
|
|
|
|
assert(PyDict_Check(op->func_annotations));
|
|
|
|
return op->func_annotations;
|
|
|
|
}
|
|
|
|
|
2006-12-28 02:47:50 -04:00
|
|
|
PyObject *
|
|
|
|
PyFunction_GetAnnotations(PyObject *op)
|
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
if (!PyFunction_Check(op)) {
|
|
|
|
PyErr_BadInternalCall();
|
|
|
|
return NULL;
|
|
|
|
}
|
2022-01-05 08:25:54 -04:00
|
|
|
return func_get_annotation_dict((PyFunctionObject *)op);
|
2006-12-28 02:47:50 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
PyFunction_SetAnnotations(PyObject *op, PyObject *annotations)
|
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
if (!PyFunction_Check(op)) {
|
|
|
|
PyErr_BadInternalCall();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (annotations == Py_None)
|
|
|
|
annotations = NULL;
|
|
|
|
else if (annotations && PyDict_Check(annotations)) {
|
|
|
|
Py_INCREF(annotations);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
PyErr_SetString(PyExc_SystemError,
|
|
|
|
"non-dict annotations");
|
|
|
|
return -1;
|
|
|
|
}
|
2024-05-21 23:38:12 -03:00
|
|
|
PyFunctionObject *func = (PyFunctionObject *)op;
|
|
|
|
Py_XSETREF(func->func_annotations, annotations);
|
|
|
|
Py_CLEAR(func->func_annotate);
|
2010-05-09 12:52:27 -03:00
|
|
|
return 0;
|
2006-12-28 02:47:50 -04:00
|
|
|
}
|
|
|
|
|
1990-10-14 09:07:46 -03:00
|
|
|
/* Methods */
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
#define OFF(x) offsetof(PyFunctionObject, x)
|
1990-12-20 11:06:42 -04:00
|
|
|
|
2001-09-20 17:46:19 -03:00
|
|
|
static PyMemberDef func_memberlist[] = {
|
2023-07-25 10:28:30 -03:00
|
|
|
{"__closure__", _Py_T_OBJECT, OFF(func_closure), Py_READONLY},
|
|
|
|
{"__doc__", _Py_T_OBJECT, OFF(func_doc), 0},
|
|
|
|
{"__globals__", _Py_T_OBJECT, OFF(func_globals), Py_READONLY},
|
|
|
|
{"__module__", _Py_T_OBJECT, OFF(func_module), 0},
|
|
|
|
{"__builtins__", _Py_T_OBJECT, OFF(func_builtins), Py_READONLY},
|
2010-05-09 12:52:27 -03:00
|
|
|
{NULL} /* Sentinel */
|
1990-12-20 11:06:42 -04:00
|
|
|
};
|
|
|
|
|
2001-09-17 20:46:56 -03:00
|
|
|
static PyObject *
|
2018-11-27 13:34:35 -04:00
|
|
|
func_get_code(PyFunctionObject *op, void *Py_UNUSED(ignored))
|
2001-09-17 20:46:56 -03:00
|
|
|
{
|
2019-05-23 12:45:22 -03:00
|
|
|
if (PySys_Audit("object.__getattr__", "Os", op, "__code__") < 0) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2022-11-10 17:22:02 -04:00
|
|
|
return Py_NewRef(op->func_code);
|
2001-09-17 20:46:56 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2018-11-27 13:34:35 -04:00
|
|
|
func_set_code(PyFunctionObject *op, PyObject *value, void *Py_UNUSED(ignored))
|
2001-09-17 20:46:56 -03:00
|
|
|
{
|
2021-05-26 16:15:40 -03:00
|
|
|
Py_ssize_t nclosure;
|
|
|
|
int nfree;
|
2010-05-09 12:52:27 -03:00
|
|
|
|
|
|
|
/* Not legal to del f.func_code or to set it to anything
|
|
|
|
* other than a code object. */
|
|
|
|
if (value == NULL || !PyCode_Check(value)) {
|
|
|
|
PyErr_SetString(PyExc_TypeError,
|
|
|
|
"__code__ must be set to a code object");
|
|
|
|
return -1;
|
|
|
|
}
|
2019-05-23 12:45:22 -03:00
|
|
|
|
|
|
|
if (PySys_Audit("object.__setattr__", "OsO",
|
|
|
|
op, "__code__", value) < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2021-05-26 16:15:40 -03:00
|
|
|
nfree = ((PyCodeObject *)value)->co_nfreevars;
|
2010-05-09 12:52:27 -03:00
|
|
|
nclosure = (op->func_closure == NULL ? 0 :
|
|
|
|
PyTuple_GET_SIZE(op->func_closure));
|
|
|
|
if (nclosure != nfree) {
|
|
|
|
PyErr_Format(PyExc_ValueError,
|
|
|
|
"%U() requires a code object with %zd free vars,"
|
|
|
|
" not %zd",
|
|
|
|
op->func_name,
|
|
|
|
nclosure, nfree);
|
|
|
|
return -1;
|
|
|
|
}
|
2023-11-07 14:54:36 -04:00
|
|
|
|
|
|
|
PyObject *func_code = PyFunction_GET_CODE(op);
|
|
|
|
int old_flags = ((PyCodeObject *)func_code)->co_flags;
|
|
|
|
int new_flags = ((PyCodeObject *)value)->co_flags;
|
|
|
|
int mask = CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR;
|
|
|
|
if ((old_flags & mask) != (new_flags & mask)) {
|
|
|
|
if (PyErr_Warn(PyExc_DeprecationWarning,
|
|
|
|
"Assigning a code object of non-matching type is deprecated "
|
|
|
|
"(e.g., from a generator to a plain function)") < 0)
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-22 08:06:44 -04:00
|
|
|
handle_func_event(PyFunction_EVENT_MODIFY_CODE, op, value);
|
2023-08-22 12:29:49 -03:00
|
|
|
_PyFunction_SetVersion(op, 0);
|
2022-11-10 17:22:02 -04:00
|
|
|
Py_XSETREF(op->func_code, Py_NewRef(value));
|
2010-05-09 12:52:27 -03:00
|
|
|
return 0;
|
2001-09-17 20:46:56 -03:00
|
|
|
}
|
|
|
|
|
2004-08-12 15:12:44 -03:00
|
|
|
static PyObject *
|
2018-11-27 13:34:35 -04:00
|
|
|
func_get_name(PyFunctionObject *op, void *Py_UNUSED(ignored))
|
2004-08-12 15:12:44 -03:00
|
|
|
{
|
2022-11-10 17:22:02 -04:00
|
|
|
return Py_NewRef(op->func_name);
|
2004-08-12 15:12:44 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2018-11-27 13:34:35 -04:00
|
|
|
func_set_name(PyFunctionObject *op, PyObject *value, void *Py_UNUSED(ignored))
|
2004-08-12 15:12:44 -03:00
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
/* Not legal to del f.func_name or to set it to anything
|
|
|
|
* other than a string object. */
|
|
|
|
if (value == NULL || !PyUnicode_Check(value)) {
|
|
|
|
PyErr_SetString(PyExc_TypeError,
|
|
|
|
"__name__ must be set to a string object");
|
|
|
|
return -1;
|
|
|
|
}
|
2022-11-10 17:22:02 -04:00
|
|
|
Py_XSETREF(op->func_name, Py_NewRef(value));
|
2010-05-09 12:52:27 -03:00
|
|
|
return 0;
|
2004-08-12 15:12:44 -03:00
|
|
|
}
|
|
|
|
|
2011-11-25 13:56:07 -04:00
|
|
|
static PyObject *
|
2018-11-27 13:34:35 -04:00
|
|
|
func_get_qualname(PyFunctionObject *op, void *Py_UNUSED(ignored))
|
2011-11-25 13:56:07 -04:00
|
|
|
{
|
2022-11-10 17:22:02 -04:00
|
|
|
return Py_NewRef(op->func_qualname);
|
2011-11-25 13:56:07 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2018-11-27 13:34:35 -04:00
|
|
|
func_set_qualname(PyFunctionObject *op, PyObject *value, void *Py_UNUSED(ignored))
|
2011-11-25 13:56:07 -04:00
|
|
|
{
|
|
|
|
/* Not legal to del f.__qualname__ or to set it to anything
|
|
|
|
* other than a string object. */
|
|
|
|
if (value == NULL || !PyUnicode_Check(value)) {
|
|
|
|
PyErr_SetString(PyExc_TypeError,
|
|
|
|
"__qualname__ must be set to a string object");
|
|
|
|
return -1;
|
|
|
|
}
|
2022-11-10 17:22:02 -04:00
|
|
|
Py_XSETREF(op->func_qualname, Py_NewRef(value));
|
2011-11-25 13:56:07 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2001-09-17 20:46:56 -03:00
|
|
|
static PyObject *
|
2018-11-27 13:34:35 -04:00
|
|
|
func_get_defaults(PyFunctionObject *op, void *Py_UNUSED(ignored))
|
2001-09-17 20:46:56 -03:00
|
|
|
{
|
2019-05-23 12:45:22 -03:00
|
|
|
if (PySys_Audit("object.__getattr__", "Os", op, "__defaults__") < 0) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2010-05-09 12:52:27 -03:00
|
|
|
if (op->func_defaults == NULL) {
|
2017-01-23 03:47:21 -04:00
|
|
|
Py_RETURN_NONE;
|
2010-05-09 12:52:27 -03:00
|
|
|
}
|
2022-11-10 17:22:02 -04:00
|
|
|
return Py_NewRef(op->func_defaults);
|
2001-09-17 20:46:56 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2018-11-27 13:34:35 -04:00
|
|
|
func_set_defaults(PyFunctionObject *op, PyObject *value, void *Py_UNUSED(ignored))
|
2001-09-17 20:46:56 -03:00
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
/* Legal to del f.func_defaults.
|
|
|
|
* Can only set func_defaults to NULL or a tuple. */
|
|
|
|
if (value == Py_None)
|
|
|
|
value = NULL;
|
|
|
|
if (value != NULL && !PyTuple_Check(value)) {
|
|
|
|
PyErr_SetString(PyExc_TypeError,
|
|
|
|
"__defaults__ must be set to a tuple object");
|
|
|
|
return -1;
|
|
|
|
}
|
2019-05-23 12:45:22 -03:00
|
|
|
if (value) {
|
|
|
|
if (PySys_Audit("object.__setattr__", "OsO",
|
|
|
|
op, "__defaults__", value) < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
} else if (PySys_Audit("object.__delattr__", "Os",
|
|
|
|
op, "__defaults__") < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2022-11-22 08:06:44 -04:00
|
|
|
handle_func_event(PyFunction_EVENT_MODIFY_DEFAULTS, op, value);
|
2023-08-22 12:29:49 -03:00
|
|
|
_PyFunction_SetVersion(op, 0);
|
2022-11-10 17:22:02 -04:00
|
|
|
Py_XSETREF(op->func_defaults, Py_XNewRef(value));
|
2010-05-09 12:52:27 -03:00
|
|
|
return 0;
|
1998-05-21 21:55:34 -03:00
|
|
|
}
|
|
|
|
|
2006-10-27 20:31:49 -03:00
|
|
|
static PyObject *
|
2018-11-27 13:34:35 -04:00
|
|
|
func_get_kwdefaults(PyFunctionObject *op, void *Py_UNUSED(ignored))
|
2006-10-27 20:31:49 -03:00
|
|
|
{
|
2019-05-23 12:45:22 -03:00
|
|
|
if (PySys_Audit("object.__getattr__", "Os",
|
|
|
|
op, "__kwdefaults__") < 0) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2010-05-09 12:52:27 -03:00
|
|
|
if (op->func_kwdefaults == NULL) {
|
2017-01-23 03:47:21 -04:00
|
|
|
Py_RETURN_NONE;
|
2010-05-09 12:52:27 -03:00
|
|
|
}
|
2022-11-10 17:22:02 -04:00
|
|
|
return Py_NewRef(op->func_kwdefaults);
|
2006-10-27 20:31:49 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2018-11-27 13:34:35 -04:00
|
|
|
func_set_kwdefaults(PyFunctionObject *op, PyObject *value, void *Py_UNUSED(ignored))
|
2006-10-27 20:31:49 -03:00
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
if (value == Py_None)
|
|
|
|
value = NULL;
|
|
|
|
/* Legal to del f.func_kwdefaults.
|
|
|
|
* Can only set func_kwdefaults to NULL or a dict. */
|
|
|
|
if (value != NULL && !PyDict_Check(value)) {
|
|
|
|
PyErr_SetString(PyExc_TypeError,
|
|
|
|
"__kwdefaults__ must be set to a dict object");
|
|
|
|
return -1;
|
|
|
|
}
|
2019-05-23 12:45:22 -03:00
|
|
|
if (value) {
|
|
|
|
if (PySys_Audit("object.__setattr__", "OsO",
|
|
|
|
op, "__kwdefaults__", value) < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
} else if (PySys_Audit("object.__delattr__", "Os",
|
|
|
|
op, "__kwdefaults__") < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2022-11-22 08:06:44 -04:00
|
|
|
handle_func_event(PyFunction_EVENT_MODIFY_KWDEFAULTS, op, value);
|
2023-08-22 12:29:49 -03:00
|
|
|
_PyFunction_SetVersion(op, 0);
|
2022-11-10 17:22:02 -04:00
|
|
|
Py_XSETREF(op->func_kwdefaults, Py_XNewRef(value));
|
2010-05-09 12:52:27 -03:00
|
|
|
return 0;
|
2006-10-27 20:31:49 -03:00
|
|
|
}
|
|
|
|
|
2024-05-21 23:38:12 -03:00
|
|
|
static PyObject *
|
|
|
|
func_get_annotate(PyFunctionObject *op, void *Py_UNUSED(ignored))
|
|
|
|
{
|
|
|
|
if (op->func_annotate == NULL) {
|
|
|
|
Py_RETURN_NONE;
|
|
|
|
}
|
|
|
|
return Py_NewRef(op->func_annotate);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
func_set_annotate(PyFunctionObject *op, PyObject *value, void *Py_UNUSED(ignored))
|
|
|
|
{
|
|
|
|
if (value == NULL) {
|
|
|
|
PyErr_SetString(PyExc_TypeError,
|
|
|
|
"__annotate__ cannot be deleted");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (Py_IsNone(value)) {
|
|
|
|
Py_XSETREF(op->func_annotate, value);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else if (PyCallable_Check(value)) {
|
|
|
|
Py_XSETREF(op->func_annotate, Py_XNewRef(value));
|
|
|
|
Py_CLEAR(op->func_annotations);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
PyErr_SetString(PyExc_TypeError,
|
|
|
|
"__annotate__ must be callable or None");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-12-28 02:47:50 -04:00
|
|
|
static PyObject *
|
2018-11-27 13:34:35 -04:00
|
|
|
func_get_annotations(PyFunctionObject *op, void *Py_UNUSED(ignored))
|
2006-12-28 02:47:50 -04:00
|
|
|
{
|
2024-05-21 23:38:12 -03:00
|
|
|
if (op->func_annotations == NULL &&
|
|
|
|
(op->func_annotate == NULL || !PyCallable_Check(op->func_annotate))) {
|
2010-05-09 12:52:27 -03:00
|
|
|
op->func_annotations = PyDict_New();
|
|
|
|
if (op->func_annotations == NULL)
|
|
|
|
return NULL;
|
|
|
|
}
|
2022-10-06 16:23:20 -03:00
|
|
|
PyObject *d = func_get_annotation_dict(op);
|
2022-11-16 13:34:24 -04:00
|
|
|
return Py_XNewRef(d);
|
2006-12-28 02:47:50 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2018-11-27 13:34:35 -04:00
|
|
|
func_set_annotations(PyFunctionObject *op, PyObject *value, void *Py_UNUSED(ignored))
|
2006-12-28 02:47:50 -04:00
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
if (value == Py_None)
|
|
|
|
value = NULL;
|
|
|
|
/* Legal to del f.func_annotations.
|
|
|
|
* Can only set func_annotations to NULL (through C api)
|
|
|
|
* or a dict. */
|
|
|
|
if (value != NULL && !PyDict_Check(value)) {
|
|
|
|
PyErr_SetString(PyExc_TypeError,
|
|
|
|
"__annotations__ must be set to a dict object");
|
|
|
|
return -1;
|
|
|
|
}
|
2022-11-10 17:22:02 -04:00
|
|
|
Py_XSETREF(op->func_annotations, Py_XNewRef(value));
|
2024-05-21 23:38:12 -03:00
|
|
|
Py_CLEAR(op->func_annotate);
|
2010-05-09 12:52:27 -03:00
|
|
|
return 0;
|
2006-12-28 02:47:50 -04:00
|
|
|
}
|
|
|
|
|
2023-05-16 00:36:23 -03:00
|
|
|
static PyObject *
|
|
|
|
func_get_type_params(PyFunctionObject *op, void *Py_UNUSED(ignored))
|
|
|
|
{
|
|
|
|
if (op->func_typeparams == NULL) {
|
|
|
|
return PyTuple_New(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(PyTuple_Check(op->func_typeparams));
|
|
|
|
return Py_NewRef(op->func_typeparams);
|
|
|
|
}
|
|
|
|
|
2023-05-18 20:45:37 -03:00
|
|
|
static int
|
|
|
|
func_set_type_params(PyFunctionObject *op, PyObject *value, void *Py_UNUSED(ignored))
|
|
|
|
{
|
|
|
|
/* Not legal to del f.__type_params__ or to set it to anything
|
|
|
|
* other than a tuple object. */
|
|
|
|
if (value == NULL || !PyTuple_Check(value)) {
|
|
|
|
PyErr_SetString(PyExc_TypeError,
|
|
|
|
"__type_params__ must be set to a tuple");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
Py_XSETREF(op->func_typeparams, Py_NewRef(value));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-05-16 00:36:23 -03:00
|
|
|
PyObject *
|
|
|
|
_Py_set_function_type_params(PyThreadState *Py_UNUSED(ignored), PyObject *func,
|
|
|
|
PyObject *type_params)
|
|
|
|
{
|
|
|
|
assert(PyFunction_Check(func));
|
|
|
|
assert(PyTuple_Check(type_params));
|
|
|
|
PyFunctionObject *f = (PyFunctionObject *)func;
|
|
|
|
Py_XSETREF(f->func_typeparams, Py_NewRef(type_params));
|
|
|
|
return Py_NewRef(func);
|
|
|
|
}
|
|
|
|
|
2001-09-20 18:45:26 -03:00
|
|
|
static PyGetSetDef func_getsetlist[] = {
|
2010-05-09 12:52:27 -03:00
|
|
|
{"__code__", (getter)func_get_code, (setter)func_set_code},
|
|
|
|
{"__defaults__", (getter)func_get_defaults,
|
|
|
|
(setter)func_set_defaults},
|
|
|
|
{"__kwdefaults__", (getter)func_get_kwdefaults,
|
|
|
|
(setter)func_set_kwdefaults},
|
|
|
|
{"__annotations__", (getter)func_get_annotations,
|
|
|
|
(setter)func_set_annotations},
|
2024-05-21 23:38:12 -03:00
|
|
|
{"__annotate__", (getter)func_get_annotate, (setter)func_set_annotate},
|
2012-02-19 21:02:57 -04:00
|
|
|
{"__dict__", PyObject_GenericGetDict, PyObject_GenericSetDict},
|
2010-05-09 12:52:27 -03:00
|
|
|
{"__name__", (getter)func_get_name, (setter)func_set_name},
|
2011-11-25 13:56:07 -04:00
|
|
|
{"__qualname__", (getter)func_get_qualname, (setter)func_set_qualname},
|
2023-05-18 20:45:37 -03:00
|
|
|
{"__type_params__", (getter)func_get_type_params,
|
|
|
|
(setter)func_set_type_params},
|
2010-05-09 12:52:27 -03:00
|
|
|
{NULL} /* Sentinel */
|
2001-09-17 20:46:56 -03:00
|
|
|
};
|
|
|
|
|
2017-03-19 03:51:07 -03:00
|
|
|
/*[clinic input]
|
|
|
|
class function "PyFunctionObject *" "&PyFunction_Type"
|
|
|
|
[clinic start generated code]*/
|
|
|
|
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=70af9c90aa2e71b0]*/
|
2002-07-11 15:30:27 -03:00
|
|
|
|
2017-03-19 03:51:07 -03:00
|
|
|
#include "clinic/funcobject.c.h"
|
|
|
|
|
|
|
|
/* function.__new__() maintains the following invariants for closures.
|
|
|
|
The closure must correspond to the free variables of the code object.
|
2010-05-09 12:52:27 -03:00
|
|
|
|
|
|
|
if len(code.co_freevars) == 0:
|
|
|
|
closure = NULL
|
2002-07-11 15:30:27 -03:00
|
|
|
else:
|
2010-05-09 12:52:27 -03:00
|
|
|
len(closure) == len(code.co_freevars)
|
2002-07-11 15:30:27 -03:00
|
|
|
for every elt in closure, type(elt) == cell
|
|
|
|
*/
|
2002-06-14 17:41:17 -03:00
|
|
|
|
2017-03-19 03:51:07 -03:00
|
|
|
/*[clinic input]
|
|
|
|
@classmethod
|
|
|
|
function.__new__ as func_new
|
|
|
|
code: object(type="PyCodeObject *", subclass_of="&PyCode_Type")
|
|
|
|
a code object
|
|
|
|
globals: object(subclass_of="&PyDict_Type")
|
|
|
|
the globals dictionary
|
|
|
|
name: object = None
|
|
|
|
a string that overrides the name from the code object
|
|
|
|
argdefs as defaults: object = None
|
|
|
|
a tuple that specifies the default argument values
|
|
|
|
closure: object = None
|
|
|
|
a tuple that supplies the bindings for free variables
|
2024-01-11 04:42:30 -04:00
|
|
|
kwdefaults: object = None
|
|
|
|
a dictionary that specifies the default keyword argument values
|
2017-03-19 03:51:07 -03:00
|
|
|
|
|
|
|
Create a function object.
|
|
|
|
[clinic start generated code]*/
|
|
|
|
|
2002-06-14 17:41:17 -03:00
|
|
|
static PyObject *
|
2017-03-19 03:51:07 -03:00
|
|
|
func_new_impl(PyTypeObject *type, PyCodeObject *code, PyObject *globals,
|
2024-01-11 04:42:30 -04:00
|
|
|
PyObject *name, PyObject *defaults, PyObject *closure,
|
|
|
|
PyObject *kwdefaults)
|
|
|
|
/*[clinic end generated code: output=de72f4c22ac57144 input=20c9c9f04ad2d3f2]*/
|
2002-06-14 17:41:17 -03:00
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
PyFunctionObject *newfunc;
|
2021-05-26 16:15:40 -03:00
|
|
|
Py_ssize_t nclosure;
|
2017-03-19 03:51:07 -03:00
|
|
|
|
2010-05-09 12:52:27 -03:00
|
|
|
if (name != Py_None && !PyUnicode_Check(name)) {
|
|
|
|
PyErr_SetString(PyExc_TypeError,
|
|
|
|
"arg 3 (name) must be None or string");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (defaults != Py_None && !PyTuple_Check(defaults)) {
|
|
|
|
PyErr_SetString(PyExc_TypeError,
|
|
|
|
"arg 4 (defaults) must be None or tuple");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (!PyTuple_Check(closure)) {
|
2021-05-26 16:15:40 -03:00
|
|
|
if (code->co_nfreevars && closure == Py_None) {
|
2010-05-09 12:52:27 -03:00
|
|
|
PyErr_SetString(PyExc_TypeError,
|
|
|
|
"arg 5 (closure) must be tuple");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
else if (closure != Py_None) {
|
|
|
|
PyErr_SetString(PyExc_TypeError,
|
|
|
|
"arg 5 (closure) must be None or tuple");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
2024-01-11 04:42:30 -04:00
|
|
|
if (kwdefaults != Py_None && !PyDict_Check(kwdefaults)) {
|
|
|
|
PyErr_SetString(PyExc_TypeError,
|
|
|
|
"arg 6 (kwdefaults) must be None or dict");
|
|
|
|
return NULL;
|
|
|
|
}
|
2010-05-09 12:52:27 -03:00
|
|
|
|
|
|
|
/* check that the closure is well-formed */
|
|
|
|
nclosure = closure == Py_None ? 0 : PyTuple_GET_SIZE(closure);
|
2021-05-26 16:15:40 -03:00
|
|
|
if (code->co_nfreevars != nclosure)
|
2010-05-09 12:52:27 -03:00
|
|
|
return PyErr_Format(PyExc_ValueError,
|
|
|
|
"%U requires closure of length %zd, not %zd",
|
2021-05-26 16:15:40 -03:00
|
|
|
code->co_name, code->co_nfreevars, nclosure);
|
2010-05-09 12:52:27 -03:00
|
|
|
if (nclosure) {
|
|
|
|
Py_ssize_t i;
|
|
|
|
for (i = 0; i < nclosure; i++) {
|
|
|
|
PyObject *o = PyTuple_GET_ITEM(closure, i);
|
|
|
|
if (!PyCell_Check(o)) {
|
|
|
|
return PyErr_Format(PyExc_TypeError,
|
|
|
|
"arg 5 (closure) expected cell, found %s",
|
2020-02-06 22:04:21 -04:00
|
|
|
Py_TYPE(o)->tp_name);
|
2010-05-09 12:52:27 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-05-23 12:45:22 -03:00
|
|
|
if (PySys_Audit("function.__new__", "O", code) < 0) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2010-05-09 12:52:27 -03:00
|
|
|
|
|
|
|
newfunc = (PyFunctionObject *)PyFunction_New((PyObject *)code,
|
|
|
|
globals);
|
2021-02-01 06:42:03 -04:00
|
|
|
if (newfunc == NULL) {
|
2010-05-09 12:52:27 -03:00
|
|
|
return NULL;
|
2021-02-01 06:42:03 -04:00
|
|
|
}
|
2010-05-09 12:52:27 -03:00
|
|
|
if (name != Py_None) {
|
2022-11-10 17:22:02 -04:00
|
|
|
Py_SETREF(newfunc->func_name, Py_NewRef(name));
|
2010-05-09 12:52:27 -03:00
|
|
|
}
|
|
|
|
if (defaults != Py_None) {
|
2022-11-10 17:22:02 -04:00
|
|
|
newfunc->func_defaults = Py_NewRef(defaults);
|
2010-05-09 12:52:27 -03:00
|
|
|
}
|
|
|
|
if (closure != Py_None) {
|
2022-11-10 17:22:02 -04:00
|
|
|
newfunc->func_closure = Py_NewRef(closure);
|
2010-05-09 12:52:27 -03:00
|
|
|
}
|
2024-01-11 04:42:30 -04:00
|
|
|
if (kwdefaults != Py_None) {
|
|
|
|
newfunc->func_kwdefaults = Py_NewRef(kwdefaults);
|
|
|
|
}
|
2010-05-09 12:52:27 -03:00
|
|
|
|
|
|
|
return (PyObject *)newfunc;
|
2002-06-14 17:41:17 -03:00
|
|
|
}
|
|
|
|
|
2018-07-03 23:15:50 -03:00
|
|
|
static int
|
|
|
|
func_clear(PyFunctionObject *op)
|
|
|
|
{
|
2023-08-22 12:29:49 -03:00
|
|
|
_PyFunction_SetVersion(op, 0);
|
2018-07-03 23:15:50 -03:00
|
|
|
Py_CLEAR(op->func_globals);
|
2021-01-29 09:24:55 -04:00
|
|
|
Py_CLEAR(op->func_builtins);
|
|
|
|
Py_CLEAR(op->func_module);
|
2018-07-03 23:15:50 -03:00
|
|
|
Py_CLEAR(op->func_defaults);
|
|
|
|
Py_CLEAR(op->func_kwdefaults);
|
|
|
|
Py_CLEAR(op->func_doc);
|
|
|
|
Py_CLEAR(op->func_dict);
|
|
|
|
Py_CLEAR(op->func_closure);
|
|
|
|
Py_CLEAR(op->func_annotations);
|
2024-05-21 23:38:12 -03:00
|
|
|
Py_CLEAR(op->func_annotate);
|
2023-05-16 00:36:23 -03:00
|
|
|
Py_CLEAR(op->func_typeparams);
|
2022-04-21 03:06:35 -03:00
|
|
|
// Don't Py_CLEAR(op->func_code), since code is always required
|
|
|
|
// to be non-NULL. Similarly, name and qualname shouldn't be NULL.
|
|
|
|
// However, name and qualname could be str subclasses, so they
|
|
|
|
// could have reference cycles. The solution is to replace them
|
|
|
|
// with a genuinely immutable string.
|
2023-08-04 20:24:50 -03:00
|
|
|
Py_SETREF(op->func_name, &_Py_STR(empty));
|
|
|
|
Py_SETREF(op->func_qualname, &_Py_STR(empty));
|
2018-07-03 23:15:50 -03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
1990-10-14 09:07:46 -03:00
|
|
|
static void
|
2000-07-09 03:03:25 -03:00
|
|
|
func_dealloc(PyFunctionObject *op)
|
1990-10-14 09:07:46 -03:00
|
|
|
{
|
2023-03-07 20:10:58 -04:00
|
|
|
assert(Py_REFCNT(op) == 0);
|
|
|
|
Py_SET_REFCNT(op, 1);
|
2022-11-22 08:06:44 -04:00
|
|
|
handle_func_event(PyFunction_EVENT_DESTROY, op, NULL);
|
2023-03-07 20:10:58 -04:00
|
|
|
if (Py_REFCNT(op) > 1) {
|
|
|
|
Py_SET_REFCNT(op, Py_REFCNT(op) - 1);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Py_SET_REFCNT(op, 0);
|
2010-05-09 12:52:27 -03:00
|
|
|
_PyObject_GC_UNTRACK(op);
|
2018-07-03 23:15:50 -03:00
|
|
|
if (op->func_weakreflist != NULL) {
|
2010-05-09 12:52:27 -03:00
|
|
|
PyObject_ClearWeakRefs((PyObject *) op);
|
2018-07-03 23:15:50 -03:00
|
|
|
}
|
2023-08-22 12:29:49 -03:00
|
|
|
_PyFunction_SetVersion(op, 0);
|
2018-07-03 23:15:50 -03:00
|
|
|
(void)func_clear(op);
|
2022-04-21 03:06:35 -03:00
|
|
|
// These aren't cleared by func_clear().
|
|
|
|
Py_DECREF(op->func_code);
|
|
|
|
Py_DECREF(op->func_name);
|
|
|
|
Py_DECREF(op->func_qualname);
|
2010-05-09 12:52:27 -03:00
|
|
|
PyObject_GC_Del(op);
|
1990-10-14 09:07:46 -03:00
|
|
|
}
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
static PyObject*
|
2000-07-09 03:03:25 -03:00
|
|
|
func_repr(PyFunctionObject *op)
|
1993-03-29 06:43:31 -04:00
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
return PyUnicode_FromFormat("<function %U at %p>",
|
2021-04-09 12:51:22 -03:00
|
|
|
op->func_qualname, op);
|
1993-03-29 06:43:31 -04:00
|
|
|
}
|
|
|
|
|
2000-06-23 11:18:11 -03:00
|
|
|
static int
|
|
|
|
func_traverse(PyFunctionObject *f, visitproc visit, void *arg)
|
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
Py_VISIT(f->func_code);
|
|
|
|
Py_VISIT(f->func_globals);
|
2021-01-29 09:24:55 -04:00
|
|
|
Py_VISIT(f->func_builtins);
|
2010-05-09 12:52:27 -03:00
|
|
|
Py_VISIT(f->func_module);
|
|
|
|
Py_VISIT(f->func_defaults);
|
|
|
|
Py_VISIT(f->func_kwdefaults);
|
|
|
|
Py_VISIT(f->func_doc);
|
|
|
|
Py_VISIT(f->func_name);
|
|
|
|
Py_VISIT(f->func_dict);
|
|
|
|
Py_VISIT(f->func_closure);
|
|
|
|
Py_VISIT(f->func_annotations);
|
2024-05-21 23:38:12 -03:00
|
|
|
Py_VISIT(f->func_annotate);
|
2023-05-16 00:36:23 -03:00
|
|
|
Py_VISIT(f->func_typeparams);
|
2011-11-25 13:56:07 -04:00
|
|
|
Py_VISIT(f->func_qualname);
|
2010-05-09 12:52:27 -03:00
|
|
|
return 0;
|
2000-06-23 11:18:11 -03:00
|
|
|
}
|
|
|
|
|
2001-08-02 01:15:00 -03:00
|
|
|
/* Bind a function to an object */
|
|
|
|
static PyObject *
|
|
|
|
func_descr_get(PyObject *func, PyObject *obj, PyObject *type)
|
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
if (obj == Py_None || obj == NULL) {
|
2022-11-10 17:22:02 -04:00
|
|
|
return Py_NewRef(func);
|
2010-05-09 12:52:27 -03:00
|
|
|
}
|
|
|
|
return PyMethod_New(func, obj);
|
2001-08-02 01:15:00 -03:00
|
|
|
}
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
PyTypeObject PyFunction_Type = {
|
2010-05-09 12:52:27 -03:00
|
|
|
PyVarObject_HEAD_INIT(&PyType_Type, 0)
|
|
|
|
"function",
|
|
|
|
sizeof(PyFunctionObject),
|
|
|
|
0,
|
|
|
|
(destructor)func_dealloc, /* tp_dealloc */
|
2019-05-29 15:31:52 -03:00
|
|
|
offsetof(PyFunctionObject, vectorcall), /* tp_vectorcall_offset */
|
2010-05-09 12:52:27 -03:00
|
|
|
0, /* tp_getattr */
|
|
|
|
0, /* tp_setattr */
|
2019-05-30 23:13:39 -03:00
|
|
|
0, /* tp_as_async */
|
2010-05-09 12:52:27 -03:00
|
|
|
(reprfunc)func_repr, /* tp_repr */
|
|
|
|
0, /* tp_as_number */
|
|
|
|
0, /* tp_as_sequence */
|
|
|
|
0, /* tp_as_mapping */
|
|
|
|
0, /* tp_hash */
|
2019-06-18 08:05:41 -03:00
|
|
|
PyVectorcall_Call, /* tp_call */
|
2010-05-09 12:52:27 -03:00
|
|
|
0, /* tp_str */
|
2012-02-19 02:16:13 -04:00
|
|
|
0, /* tp_getattro */
|
|
|
|
0, /* tp_setattro */
|
2010-05-09 12:52:27 -03:00
|
|
|
0, /* tp_as_buffer */
|
2019-05-28 09:42:53 -03:00
|
|
|
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
|
2020-02-11 12:46:57 -04:00
|
|
|
Py_TPFLAGS_HAVE_VECTORCALL |
|
2019-05-28 09:42:53 -03:00
|
|
|
Py_TPFLAGS_METHOD_DESCRIPTOR, /* tp_flags */
|
2017-03-19 03:51:07 -03:00
|
|
|
func_new__doc__, /* tp_doc */
|
2010-05-09 12:52:27 -03:00
|
|
|
(traverseproc)func_traverse, /* tp_traverse */
|
2018-07-03 23:15:50 -03:00
|
|
|
(inquiry)func_clear, /* tp_clear */
|
2010-05-09 12:52:27 -03:00
|
|
|
0, /* tp_richcompare */
|
|
|
|
offsetof(PyFunctionObject, func_weakreflist), /* tp_weaklistoffset */
|
|
|
|
0, /* tp_iter */
|
|
|
|
0, /* tp_iternext */
|
|
|
|
0, /* tp_methods */
|
|
|
|
func_memberlist, /* tp_members */
|
|
|
|
func_getsetlist, /* tp_getset */
|
|
|
|
0, /* tp_base */
|
|
|
|
0, /* tp_dict */
|
|
|
|
func_descr_get, /* tp_descr_get */
|
|
|
|
0, /* tp_descr_set */
|
|
|
|
offsetof(PyFunctionObject, func_dict), /* tp_dictoffset */
|
|
|
|
0, /* tp_init */
|
|
|
|
0, /* tp_alloc */
|
|
|
|
func_new, /* tp_new */
|
2001-08-02 01:15:00 -03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2021-04-09 12:51:22 -03:00
|
|
|
static int
|
|
|
|
functools_copy_attr(PyObject *wrapper, PyObject *wrapped, PyObject *name)
|
|
|
|
{
|
2023-07-09 09:27:03 -03:00
|
|
|
PyObject *value;
|
2023-07-12 02:57:10 -03:00
|
|
|
int res = PyObject_GetOptionalAttr(wrapped, name, &value);
|
2023-07-09 09:27:03 -03:00
|
|
|
if (value != NULL) {
|
|
|
|
res = PyObject_SetAttr(wrapper, name, value);
|
|
|
|
Py_DECREF(value);
|
2021-04-09 12:51:22 -03:00
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Similar to functools.wraps(wrapper, wrapped)
|
|
|
|
static int
|
|
|
|
functools_wraps(PyObject *wrapper, PyObject *wrapped)
|
|
|
|
{
|
|
|
|
#define COPY_ATTR(ATTR) \
|
|
|
|
do { \
|
2022-02-08 16:39:07 -04:00
|
|
|
if (functools_copy_attr(wrapper, wrapped, &_Py_ID(ATTR)) < 0) { \
|
2021-04-09 12:51:22 -03:00
|
|
|
return -1; \
|
|
|
|
} \
|
|
|
|
} while (0) \
|
|
|
|
|
|
|
|
COPY_ATTR(__module__);
|
|
|
|
COPY_ATTR(__name__);
|
|
|
|
COPY_ATTR(__qualname__);
|
|
|
|
COPY_ATTR(__doc__);
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
#undef COPY_ATTR
|
|
|
|
}
|
|
|
|
|
2024-05-31 18:05:51 -03:00
|
|
|
// Used for wrapping __annotations__ and __annotate__ on classmethod
|
|
|
|
// and staticmethod objects.
|
|
|
|
static PyObject *
|
|
|
|
descriptor_get_wrapped_attribute(PyObject *wrapped, PyObject *dict, PyObject *name)
|
|
|
|
{
|
|
|
|
PyObject *res;
|
|
|
|
if (PyDict_GetItemRef(dict, name, &res) < 0) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (res != NULL) {
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
res = PyObject_GetAttr(wrapped, name);
|
|
|
|
if (res == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (PyDict_SetItem(dict, name, res) < 0) {
|
|
|
|
Py_DECREF(res);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
descriptor_set_wrapped_attribute(PyObject *dict, PyObject *name, PyObject *value,
|
|
|
|
char *type_name)
|
|
|
|
{
|
|
|
|
if (value == NULL) {
|
|
|
|
if (PyDict_DelItem(dict, name) < 0) {
|
|
|
|
if (PyErr_ExceptionMatches(PyExc_KeyError)) {
|
|
|
|
PyErr_Clear();
|
|
|
|
PyErr_Format(PyExc_AttributeError,
|
|
|
|
"'%.200s' object has no attribute '%U'",
|
|
|
|
type_name, name);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return PyDict_SetItem(dict, name, value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-09 12:51:22 -03:00
|
|
|
|
2001-08-02 01:15:00 -03:00
|
|
|
/* Class method object */
|
|
|
|
|
|
|
|
/* A class method receives the class as implicit first argument,
|
|
|
|
just like an instance method receives the instance.
|
|
|
|
To declare a class method, use this idiom:
|
|
|
|
|
|
|
|
class C:
|
2016-09-17 00:26:16 -03:00
|
|
|
@classmethod
|
2023-04-11 10:50:25 -03:00
|
|
|
def f(cls, arg1, arg2, argN):
|
2016-09-17 00:26:16 -03:00
|
|
|
...
|
2010-05-09 12:52:27 -03:00
|
|
|
|
2001-08-02 01:15:00 -03:00
|
|
|
It can be called either on the class (e.g. C.f()) or on an instance
|
|
|
|
(e.g. C().f()); the instance is ignored except for its class.
|
|
|
|
If a class method is called for a derived class, the derived class
|
|
|
|
object is passed as the implied first argument.
|
|
|
|
|
|
|
|
Class methods are different than C++ or Java static methods.
|
|
|
|
If you want those, see static methods below.
|
|
|
|
*/
|
|
|
|
|
|
|
|
typedef struct {
|
2010-05-09 12:52:27 -03:00
|
|
|
PyObject_HEAD
|
|
|
|
PyObject *cm_callable;
|
2012-02-19 02:10:25 -04:00
|
|
|
PyObject *cm_dict;
|
2001-08-02 01:15:00 -03:00
|
|
|
} classmethod;
|
|
|
|
|
|
|
|
static void
|
|
|
|
cm_dealloc(classmethod *cm)
|
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
_PyObject_GC_UNTRACK((PyObject *)cm);
|
|
|
|
Py_XDECREF(cm->cm_callable);
|
2012-02-19 02:10:25 -04:00
|
|
|
Py_XDECREF(cm->cm_dict);
|
2010-05-09 12:52:27 -03:00
|
|
|
Py_TYPE(cm)->tp_free((PyObject *)cm);
|
2001-08-02 01:15:00 -03:00
|
|
|
}
|
|
|
|
|
2003-04-08 18:28:47 -03:00
|
|
|
static int
|
|
|
|
cm_traverse(classmethod *cm, visitproc visit, void *arg)
|
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
Py_VISIT(cm->cm_callable);
|
2012-02-19 02:10:25 -04:00
|
|
|
Py_VISIT(cm->cm_dict);
|
2010-05-09 12:52:27 -03:00
|
|
|
return 0;
|
2003-04-08 18:28:47 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
cm_clear(classmethod *cm)
|
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
Py_CLEAR(cm->cm_callable);
|
2012-02-19 02:10:25 -04:00
|
|
|
Py_CLEAR(cm->cm_dict);
|
2010-05-09 12:52:27 -03:00
|
|
|
return 0;
|
2003-04-08 18:28:47 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-08-02 01:15:00 -03:00
|
|
|
static PyObject *
|
|
|
|
cm_descr_get(PyObject *self, PyObject *obj, PyObject *type)
|
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
classmethod *cm = (classmethod *)self;
|
2001-08-02 01:15:00 -03:00
|
|
|
|
2010-05-09 12:52:27 -03:00
|
|
|
if (cm->cm_callable == NULL) {
|
|
|
|
PyErr_SetString(PyExc_RuntimeError,
|
|
|
|
"uninitialized classmethod object");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (type == NULL)
|
|
|
|
type = (PyObject *)(Py_TYPE(obj));
|
|
|
|
return PyMethod_New(cm->cm_callable, type);
|
2001-08-02 01:15:00 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
cm_init(PyObject *self, PyObject *args, PyObject *kwds)
|
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
classmethod *cm = (classmethod *)self;
|
|
|
|
PyObject *callable;
|
2001-08-02 01:15:00 -03:00
|
|
|
|
2010-05-09 12:52:27 -03:00
|
|
|
if (!_PyArg_NoKeywords("classmethod", kwds))
|
|
|
|
return -1;
|
2017-07-09 00:45:06 -03:00
|
|
|
if (!PyArg_UnpackTuple(args, "classmethod", 1, 1, &callable))
|
|
|
|
return -1;
|
2022-11-10 17:22:02 -04:00
|
|
|
Py_XSETREF(cm->cm_callable, Py_NewRef(callable));
|
2021-04-09 12:51:22 -03:00
|
|
|
|
|
|
|
if (functools_wraps((PyObject *)cm, cm->cm_callable) < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
2010-05-09 12:52:27 -03:00
|
|
|
return 0;
|
2001-08-02 01:15:00 -03:00
|
|
|
}
|
|
|
|
|
2009-05-29 01:52:27 -03:00
|
|
|
static PyMemberDef cm_memberlist[] = {
|
2023-07-25 10:28:30 -03:00
|
|
|
{"__func__", _Py_T_OBJECT, offsetof(classmethod, cm_callable), Py_READONLY},
|
|
|
|
{"__wrapped__", _Py_T_OBJECT, offsetof(classmethod, cm_callable), Py_READONLY},
|
2010-05-09 12:52:27 -03:00
|
|
|
{NULL} /* Sentinel */
|
2009-05-29 01:52:27 -03:00
|
|
|
};
|
|
|
|
|
2011-12-15 16:34:02 -04:00
|
|
|
static PyObject *
|
|
|
|
cm_get___isabstractmethod__(classmethod *cm, void *closure)
|
|
|
|
{
|
|
|
|
int res = _PyObject_IsAbstract(cm->cm_callable);
|
|
|
|
if (res == -1) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
else if (res) {
|
|
|
|
Py_RETURN_TRUE;
|
|
|
|
}
|
|
|
|
Py_RETURN_FALSE;
|
|
|
|
}
|
|
|
|
|
2024-05-31 18:05:51 -03:00
|
|
|
static PyObject *
|
|
|
|
cm_get___annotations__(classmethod *cm, void *closure)
|
|
|
|
{
|
|
|
|
return descriptor_get_wrapped_attribute(cm->cm_callable, cm->cm_dict, &_Py_ID(__annotations__));
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
cm_set___annotations__(classmethod *cm, PyObject *value, void *closure)
|
|
|
|
{
|
|
|
|
return descriptor_set_wrapped_attribute(cm->cm_dict, &_Py_ID(__annotations__), value, "classmethod");
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
cm_get___annotate__(classmethod *cm, void *closure)
|
|
|
|
{
|
|
|
|
return descriptor_get_wrapped_attribute(cm->cm_callable, cm->cm_dict, &_Py_ID(__annotate__));
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
cm_set___annotate__(classmethod *cm, PyObject *value, void *closure)
|
|
|
|
{
|
|
|
|
return descriptor_set_wrapped_attribute(cm->cm_dict, &_Py_ID(__annotate__), value, "classmethod");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-12-15 16:34:02 -04:00
|
|
|
static PyGetSetDef cm_getsetlist[] = {
|
|
|
|
{"__isabstractmethod__",
|
2021-04-09 12:51:22 -03:00
|
|
|
(getter)cm_get___isabstractmethod__, NULL, NULL, NULL},
|
2012-02-19 21:02:57 -04:00
|
|
|
{"__dict__", PyObject_GenericGetDict, PyObject_GenericSetDict, NULL, NULL},
|
2024-05-31 18:05:51 -03:00
|
|
|
{"__annotations__", (getter)cm_get___annotations__, (setter)cm_set___annotations__, NULL, NULL},
|
|
|
|
{"__annotate__", (getter)cm_get___annotate__, (setter)cm_set___annotate__, NULL, NULL},
|
2011-12-15 16:34:02 -04:00
|
|
|
{NULL} /* Sentinel */
|
|
|
|
};
|
|
|
|
|
2021-04-09 12:51:22 -03:00
|
|
|
static PyObject*
|
|
|
|
cm_repr(classmethod *cm)
|
|
|
|
{
|
|
|
|
return PyUnicode_FromFormat("<classmethod(%R)>", cm->cm_callable);
|
|
|
|
}
|
|
|
|
|
2002-06-13 17:33:02 -03:00
|
|
|
PyDoc_STRVAR(classmethod_doc,
|
2023-11-13 03:13:49 -04:00
|
|
|
"classmethod(function, /)\n\
|
|
|
|
--\n\
|
2001-12-16 22:53:53 -04:00
|
|
|
\n\
|
|
|
|
Convert a function to be a class method.\n\
|
|
|
|
\n\
|
|
|
|
A class method receives the class as implicit first argument,\n\
|
|
|
|
just like an instance method receives the instance.\n\
|
|
|
|
To declare a class method, use this idiom:\n\
|
|
|
|
\n\
|
|
|
|
class C:\n\
|
2016-09-17 00:26:16 -03:00
|
|
|
@classmethod\n\
|
2023-04-11 10:50:25 -03:00
|
|
|
def f(cls, arg1, arg2, argN):\n\
|
2016-09-17 00:26:16 -03:00
|
|
|
...\n\
|
2001-12-16 22:53:53 -04:00
|
|
|
\n\
|
|
|
|
It can be called either on the class (e.g. C.f()) or on an instance\n\
|
|
|
|
(e.g. C().f()). The instance is ignored except for its class.\n\
|
|
|
|
If a class method is called for a derived class, the derived class\n\
|
|
|
|
object is passed as the implied first argument.\n\
|
2001-12-17 07:39:56 -04:00
|
|
|
\n\
|
2001-12-16 22:53:53 -04:00
|
|
|
Class methods are different than C++ or Java static methods.\n\
|
2002-06-13 17:33:02 -03:00
|
|
|
If you want those, see the staticmethod builtin.");
|
2001-12-16 22:53:53 -04:00
|
|
|
|
2001-08-02 01:15:00 -03:00
|
|
|
PyTypeObject PyClassMethod_Type = {
|
2010-05-09 12:52:27 -03:00
|
|
|
PyVarObject_HEAD_INIT(&PyType_Type, 0)
|
|
|
|
"classmethod",
|
|
|
|
sizeof(classmethod),
|
|
|
|
0,
|
|
|
|
(destructor)cm_dealloc, /* tp_dealloc */
|
2019-05-30 23:13:39 -03:00
|
|
|
0, /* tp_vectorcall_offset */
|
2010-05-09 12:52:27 -03:00
|
|
|
0, /* tp_getattr */
|
|
|
|
0, /* tp_setattr */
|
2019-05-30 23:13:39 -03:00
|
|
|
0, /* tp_as_async */
|
2021-04-09 12:51:22 -03:00
|
|
|
(reprfunc)cm_repr, /* tp_repr */
|
2010-05-09 12:52:27 -03:00
|
|
|
0, /* tp_as_number */
|
|
|
|
0, /* tp_as_sequence */
|
|
|
|
0, /* tp_as_mapping */
|
|
|
|
0, /* tp_hash */
|
|
|
|
0, /* tp_call */
|
|
|
|
0, /* tp_str */
|
2012-02-19 02:16:13 -04:00
|
|
|
0, /* tp_getattro */
|
2010-05-09 12:52:27 -03:00
|
|
|
0, /* tp_setattro */
|
|
|
|
0, /* tp_as_buffer */
|
|
|
|
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
|
|
|
|
classmethod_doc, /* tp_doc */
|
|
|
|
(traverseproc)cm_traverse, /* tp_traverse */
|
|
|
|
(inquiry)cm_clear, /* tp_clear */
|
|
|
|
0, /* tp_richcompare */
|
|
|
|
0, /* tp_weaklistoffset */
|
|
|
|
0, /* tp_iter */
|
|
|
|
0, /* tp_iternext */
|
|
|
|
0, /* tp_methods */
|
|
|
|
cm_memberlist, /* tp_members */
|
2011-12-15 16:34:02 -04:00
|
|
|
cm_getsetlist, /* tp_getset */
|
2010-05-09 12:52:27 -03:00
|
|
|
0, /* tp_base */
|
|
|
|
0, /* tp_dict */
|
|
|
|
cm_descr_get, /* tp_descr_get */
|
|
|
|
0, /* tp_descr_set */
|
2012-02-19 02:10:25 -04:00
|
|
|
offsetof(classmethod, cm_dict), /* tp_dictoffset */
|
2010-05-09 12:52:27 -03:00
|
|
|
cm_init, /* tp_init */
|
|
|
|
PyType_GenericAlloc, /* tp_alloc */
|
|
|
|
PyType_GenericNew, /* tp_new */
|
|
|
|
PyObject_GC_Del, /* tp_free */
|
2001-08-02 01:15:00 -03:00
|
|
|
};
|
|
|
|
|
|
|
|
PyObject *
|
|
|
|
PyClassMethod_New(PyObject *callable)
|
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
classmethod *cm = (classmethod *)
|
|
|
|
PyType_GenericAlloc(&PyClassMethod_Type, 0);
|
|
|
|
if (cm != NULL) {
|
2022-11-10 17:22:02 -04:00
|
|
|
cm->cm_callable = Py_NewRef(callable);
|
2010-05-09 12:52:27 -03:00
|
|
|
}
|
|
|
|
return (PyObject *)cm;
|
2001-08-02 01:15:00 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Static method object */
|
|
|
|
|
|
|
|
/* A static method does not receive an implicit first argument.
|
|
|
|
To declare a static method, use this idiom:
|
|
|
|
|
|
|
|
class C:
|
2016-09-17 00:26:16 -03:00
|
|
|
@staticmethod
|
2023-04-11 10:50:25 -03:00
|
|
|
def f(arg1, arg2, argN):
|
2016-09-17 00:26:16 -03:00
|
|
|
...
|
2001-08-02 01:15:00 -03:00
|
|
|
|
|
|
|
It can be called either on the class (e.g. C.f()) or on an instance
|
2018-12-24 03:47:38 -04:00
|
|
|
(e.g. C().f()). Both the class and the instance are ignored, and
|
|
|
|
neither is passed implicitly as the first argument to the method.
|
2001-08-02 01:15:00 -03:00
|
|
|
|
|
|
|
Static methods in Python are similar to those found in Java or C++.
|
|
|
|
For a more advanced concept, see class methods above.
|
|
|
|
*/
|
|
|
|
|
|
|
|
typedef struct {
|
2010-05-09 12:52:27 -03:00
|
|
|
PyObject_HEAD
|
|
|
|
PyObject *sm_callable;
|
2012-02-19 02:10:25 -04:00
|
|
|
PyObject *sm_dict;
|
2001-08-02 01:15:00 -03:00
|
|
|
} staticmethod;
|
|
|
|
|
|
|
|
static void
|
|
|
|
sm_dealloc(staticmethod *sm)
|
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
_PyObject_GC_UNTRACK((PyObject *)sm);
|
|
|
|
Py_XDECREF(sm->sm_callable);
|
2012-02-19 02:10:25 -04:00
|
|
|
Py_XDECREF(sm->sm_dict);
|
2010-05-09 12:52:27 -03:00
|
|
|
Py_TYPE(sm)->tp_free((PyObject *)sm);
|
2001-08-02 01:15:00 -03:00
|
|
|
}
|
|
|
|
|
2003-04-08 18:28:47 -03:00
|
|
|
static int
|
|
|
|
sm_traverse(staticmethod *sm, visitproc visit, void *arg)
|
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
Py_VISIT(sm->sm_callable);
|
2012-02-19 02:10:25 -04:00
|
|
|
Py_VISIT(sm->sm_dict);
|
2010-05-09 12:52:27 -03:00
|
|
|
return 0;
|
2003-04-08 18:28:47 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
sm_clear(staticmethod *sm)
|
|
|
|
{
|
2012-02-19 02:11:56 -04:00
|
|
|
Py_CLEAR(sm->sm_callable);
|
2012-02-19 02:10:25 -04:00
|
|
|
Py_CLEAR(sm->sm_dict);
|
2010-05-09 12:52:27 -03:00
|
|
|
return 0;
|
2003-04-08 18:28:47 -03:00
|
|
|
}
|
|
|
|
|
2001-08-02 01:15:00 -03:00
|
|
|
static PyObject *
|
|
|
|
sm_descr_get(PyObject *self, PyObject *obj, PyObject *type)
|
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
staticmethod *sm = (staticmethod *)self;
|
2001-08-02 01:15:00 -03:00
|
|
|
|
2010-05-09 12:52:27 -03:00
|
|
|
if (sm->sm_callable == NULL) {
|
|
|
|
PyErr_SetString(PyExc_RuntimeError,
|
|
|
|
"uninitialized staticmethod object");
|
|
|
|
return NULL;
|
|
|
|
}
|
2022-11-10 17:22:02 -04:00
|
|
|
return Py_NewRef(sm->sm_callable);
|
2001-08-02 01:15:00 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
sm_init(PyObject *self, PyObject *args, PyObject *kwds)
|
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
staticmethod *sm = (staticmethod *)self;
|
|
|
|
PyObject *callable;
|
2001-08-02 01:15:00 -03:00
|
|
|
|
2010-05-09 12:52:27 -03:00
|
|
|
if (!_PyArg_NoKeywords("staticmethod", kwds))
|
|
|
|
return -1;
|
2017-07-09 00:45:06 -03:00
|
|
|
if (!PyArg_UnpackTuple(args, "staticmethod", 1, 1, &callable))
|
|
|
|
return -1;
|
2022-11-10 17:22:02 -04:00
|
|
|
Py_XSETREF(sm->sm_callable, Py_NewRef(callable));
|
2021-04-09 12:51:22 -03:00
|
|
|
|
|
|
|
if (functools_wraps((PyObject *)sm, sm->sm_callable) < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
2010-05-09 12:52:27 -03:00
|
|
|
return 0;
|
2001-08-02 01:15:00 -03:00
|
|
|
}
|
|
|
|
|
2021-04-11 19:21:22 -03:00
|
|
|
static PyObject*
|
|
|
|
sm_call(PyObject *callable, PyObject *args, PyObject *kwargs)
|
|
|
|
{
|
|
|
|
staticmethod *sm = (staticmethod *)callable;
|
|
|
|
return PyObject_Call(sm->sm_callable, args, kwargs);
|
|
|
|
}
|
|
|
|
|
2009-05-29 01:52:27 -03:00
|
|
|
static PyMemberDef sm_memberlist[] = {
|
2023-07-25 10:28:30 -03:00
|
|
|
{"__func__", _Py_T_OBJECT, offsetof(staticmethod, sm_callable), Py_READONLY},
|
|
|
|
{"__wrapped__", _Py_T_OBJECT, offsetof(staticmethod, sm_callable), Py_READONLY},
|
2010-05-09 12:52:27 -03:00
|
|
|
{NULL} /* Sentinel */
|
2009-05-29 01:52:27 -03:00
|
|
|
};
|
|
|
|
|
2011-12-15 16:34:02 -04:00
|
|
|
static PyObject *
|
|
|
|
sm_get___isabstractmethod__(staticmethod *sm, void *closure)
|
|
|
|
{
|
|
|
|
int res = _PyObject_IsAbstract(sm->sm_callable);
|
|
|
|
if (res == -1) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
else if (res) {
|
|
|
|
Py_RETURN_TRUE;
|
|
|
|
}
|
|
|
|
Py_RETURN_FALSE;
|
|
|
|
}
|
|
|
|
|
2024-05-31 18:05:51 -03:00
|
|
|
static PyObject *
|
|
|
|
sm_get___annotations__(staticmethod *sm, void *closure)
|
|
|
|
{
|
|
|
|
return descriptor_get_wrapped_attribute(sm->sm_callable, sm->sm_dict, &_Py_ID(__annotations__));
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
sm_set___annotations__(staticmethod *sm, PyObject *value, void *closure)
|
|
|
|
{
|
|
|
|
return descriptor_set_wrapped_attribute(sm->sm_dict, &_Py_ID(__annotations__), value, "staticmethod");
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
sm_get___annotate__(staticmethod *sm, void *closure)
|
|
|
|
{
|
|
|
|
return descriptor_get_wrapped_attribute(sm->sm_callable, sm->sm_dict, &_Py_ID(__annotate__));
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
sm_set___annotate__(staticmethod *sm, PyObject *value, void *closure)
|
|
|
|
{
|
|
|
|
return descriptor_set_wrapped_attribute(sm->sm_dict, &_Py_ID(__annotate__), value, "staticmethod");
|
|
|
|
}
|
|
|
|
|
2011-12-15 16:34:02 -04:00
|
|
|
static PyGetSetDef sm_getsetlist[] = {
|
|
|
|
{"__isabstractmethod__",
|
2021-04-09 12:51:22 -03:00
|
|
|
(getter)sm_get___isabstractmethod__, NULL, NULL, NULL},
|
2012-02-19 21:02:57 -04:00
|
|
|
{"__dict__", PyObject_GenericGetDict, PyObject_GenericSetDict, NULL, NULL},
|
2024-05-31 18:05:51 -03:00
|
|
|
{"__annotations__", (getter)sm_get___annotations__, (setter)sm_set___annotations__, NULL, NULL},
|
|
|
|
{"__annotate__", (getter)sm_get___annotate__, (setter)sm_set___annotate__, NULL, NULL},
|
2011-12-15 16:34:02 -04:00
|
|
|
{NULL} /* Sentinel */
|
|
|
|
};
|
|
|
|
|
2021-04-09 12:51:22 -03:00
|
|
|
static PyObject*
|
|
|
|
sm_repr(staticmethod *sm)
|
|
|
|
{
|
|
|
|
return PyUnicode_FromFormat("<staticmethod(%R)>", sm->sm_callable);
|
|
|
|
}
|
|
|
|
|
2002-06-13 17:33:02 -03:00
|
|
|
PyDoc_STRVAR(staticmethod_doc,
|
2023-11-13 03:13:49 -04:00
|
|
|
"staticmethod(function, /)\n\
|
|
|
|
--\n\
|
2001-12-16 22:53:53 -04:00
|
|
|
\n\
|
|
|
|
Convert a function to be a static method.\n\
|
|
|
|
\n\
|
|
|
|
A static method does not receive an implicit first argument.\n\
|
|
|
|
To declare a static method, use this idiom:\n\
|
|
|
|
\n\
|
|
|
|
class C:\n\
|
2016-09-17 00:26:16 -03:00
|
|
|
@staticmethod\n\
|
2023-04-11 10:50:25 -03:00
|
|
|
def f(arg1, arg2, argN):\n\
|
2016-09-17 00:26:16 -03:00
|
|
|
...\n\
|
2001-12-16 22:53:53 -04:00
|
|
|
\n\
|
|
|
|
It can be called either on the class (e.g. C.f()) or on an instance\n\
|
2018-12-24 03:47:38 -04:00
|
|
|
(e.g. C().f()). Both the class and the instance are ignored, and\n\
|
|
|
|
neither is passed implicitly as the first argument to the method.\n\
|
2001-12-16 22:53:53 -04:00
|
|
|
\n\
|
|
|
|
Static methods in Python are similar to those found in Java or C++.\n\
|
2002-06-13 17:33:02 -03:00
|
|
|
For a more advanced concept, see the classmethod builtin.");
|
2001-12-16 22:53:53 -04:00
|
|
|
|
2001-08-02 01:15:00 -03:00
|
|
|
PyTypeObject PyStaticMethod_Type = {
|
2010-05-09 12:52:27 -03:00
|
|
|
PyVarObject_HEAD_INIT(&PyType_Type, 0)
|
|
|
|
"staticmethod",
|
|
|
|
sizeof(staticmethod),
|
|
|
|
0,
|
|
|
|
(destructor)sm_dealloc, /* tp_dealloc */
|
2019-05-30 23:13:39 -03:00
|
|
|
0, /* tp_vectorcall_offset */
|
2010-05-09 12:52:27 -03:00
|
|
|
0, /* tp_getattr */
|
|
|
|
0, /* tp_setattr */
|
2019-05-30 23:13:39 -03:00
|
|
|
0, /* tp_as_async */
|
2021-04-09 12:51:22 -03:00
|
|
|
(reprfunc)sm_repr, /* tp_repr */
|
2010-05-09 12:52:27 -03:00
|
|
|
0, /* tp_as_number */
|
|
|
|
0, /* tp_as_sequence */
|
|
|
|
0, /* tp_as_mapping */
|
|
|
|
0, /* tp_hash */
|
2021-04-11 19:21:22 -03:00
|
|
|
sm_call, /* tp_call */
|
2010-05-09 12:52:27 -03:00
|
|
|
0, /* tp_str */
|
2012-02-19 02:10:25 -04:00
|
|
|
0, /* tp_getattro */
|
2010-05-09 12:52:27 -03:00
|
|
|
0, /* tp_setattro */
|
|
|
|
0, /* tp_as_buffer */
|
|
|
|
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
|
|
|
|
staticmethod_doc, /* tp_doc */
|
|
|
|
(traverseproc)sm_traverse, /* tp_traverse */
|
|
|
|
(inquiry)sm_clear, /* tp_clear */
|
|
|
|
0, /* tp_richcompare */
|
|
|
|
0, /* tp_weaklistoffset */
|
|
|
|
0, /* tp_iter */
|
|
|
|
0, /* tp_iternext */
|
|
|
|
0, /* tp_methods */
|
|
|
|
sm_memberlist, /* tp_members */
|
2011-12-15 16:34:02 -04:00
|
|
|
sm_getsetlist, /* tp_getset */
|
2010-05-09 12:52:27 -03:00
|
|
|
0, /* tp_base */
|
|
|
|
0, /* tp_dict */
|
|
|
|
sm_descr_get, /* tp_descr_get */
|
|
|
|
0, /* tp_descr_set */
|
2012-02-19 02:10:25 -04:00
|
|
|
offsetof(staticmethod, sm_dict), /* tp_dictoffset */
|
2010-05-09 12:52:27 -03:00
|
|
|
sm_init, /* tp_init */
|
|
|
|
PyType_GenericAlloc, /* tp_alloc */
|
|
|
|
PyType_GenericNew, /* tp_new */
|
|
|
|
PyObject_GC_Del, /* tp_free */
|
1990-10-14 09:07:46 -03:00
|
|
|
};
|
2001-08-02 01:15:00 -03:00
|
|
|
|
|
|
|
PyObject *
|
|
|
|
PyStaticMethod_New(PyObject *callable)
|
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
staticmethod *sm = (staticmethod *)
|
|
|
|
PyType_GenericAlloc(&PyStaticMethod_Type, 0);
|
|
|
|
if (sm != NULL) {
|
2022-11-10 17:22:02 -04:00
|
|
|
sm->sm_callable = Py_NewRef(callable);
|
2010-05-09 12:52:27 -03:00
|
|
|
}
|
|
|
|
return (PyObject *)sm;
|
2001-08-02 01:15:00 -03:00
|
|
|
}
|