/src/cpython3/Objects/funcobject.c
Line | Count | Source |
1 | | /* Function object implementation */ |
2 | | |
3 | | #include "Python.h" |
4 | | #include "pycore_code.h" // _PyCode_VerifyStateless() |
5 | | #include "pycore_dict.h" // _Py_INCREF_DICT() |
6 | | #include "pycore_function.h" // _PyFunction_Vectorcall |
7 | | #include "pycore_long.h" // _PyLong_GetOne() |
8 | | #include "pycore_modsupport.h" // _PyArg_NoKeywords() |
9 | | #include "pycore_object.h" // _PyObject_GC_UNTRACK() |
10 | | #include "pycore_object_deferred.h" // _PyObject_SetDeferredRefcount() |
11 | | #include "pycore_optimizer.h" // _Py_Executors_InvalidateDependency() |
12 | | #include "pycore_pyerrors.h" // _PyErr_Occurred() |
13 | | #include "pycore_setobject.h" // _PySet_NextEntry() |
14 | | #include "pycore_stats.h" |
15 | | #include "pycore_weakref.h" // FT_CLEAR_WEAKREFS() |
16 | | |
17 | | static const char * |
18 | 0 | func_event_name(PyFunction_WatchEvent event) { |
19 | 0 | switch (event) { |
20 | 0 | #define CASE(op) \ |
21 | 0 | case PyFunction_EVENT_##op: \ |
22 | 0 | return "PyFunction_EVENT_" #op; |
23 | 0 | PY_FOREACH_FUNC_EVENT(CASE) |
24 | 0 | #undef CASE |
25 | 0 | } |
26 | 0 | Py_UNREACHABLE(); |
27 | 0 | } |
28 | | |
29 | | static void |
30 | | notify_func_watchers(PyInterpreterState *interp, PyFunction_WatchEvent event, |
31 | | PyFunctionObject *func, PyObject *new_value) |
32 | 0 | { |
33 | 0 | uint8_t bits = interp->active_func_watchers; |
34 | 0 | int i = 0; |
35 | 0 | while (bits) { |
36 | 0 | assert(i < FUNC_MAX_WATCHERS); |
37 | 0 | if (bits & 1) { |
38 | 0 | PyFunction_WatchCallback cb = interp->func_watchers[i]; |
39 | | // callback must be non-null if the watcher bit is set |
40 | 0 | assert(cb != NULL); |
41 | 0 | if (cb(event, func, new_value) < 0) { |
42 | 0 | PyErr_FormatUnraisable( |
43 | 0 | "Exception ignored in %s watcher callback for function %U at %p", |
44 | 0 | func_event_name(event), func->func_qualname, func); |
45 | 0 | } |
46 | 0 | } |
47 | 0 | i++; |
48 | 0 | bits >>= 1; |
49 | 0 | } |
50 | 0 | } |
51 | | |
52 | | static inline void |
53 | | handle_func_event(PyFunction_WatchEvent event, PyFunctionObject *func, |
54 | | PyObject *new_value) |
55 | 2.34M | { |
56 | 2.34M | assert(Py_REFCNT(func) > 0); |
57 | 2.34M | PyInterpreterState *interp = _PyInterpreterState_GET(); |
58 | 2.34M | assert(interp->_initialized); |
59 | 2.34M | if (interp->active_func_watchers) { |
60 | 0 | notify_func_watchers(interp, event, func, new_value); |
61 | 0 | } |
62 | 2.34M | switch (event) { |
63 | 0 | case PyFunction_EVENT_MODIFY_CODE: |
64 | 0 | case PyFunction_EVENT_MODIFY_DEFAULTS: |
65 | 0 | case PyFunction_EVENT_MODIFY_KWDEFAULTS: |
66 | 289 | case PyFunction_EVENT_MODIFY_QUALNAME: |
67 | | #if _Py_TIER2 |
68 | | // Note: we only invalidate JIT code if a function version changes. |
69 | | // Not when the function is deallocated. |
70 | | // Function deallocation occurs frequently (think: lambdas), |
71 | | // so we want to minimize dependency invalidation there. |
72 | | _Py_Executors_InvalidateDependency(interp, func, 1); |
73 | | #endif |
74 | 289 | RARE_EVENT_INTERP_INC(interp, func_modification); |
75 | 289 | break; |
76 | 2.34M | default: |
77 | 2.34M | break; |
78 | 2.34M | } |
79 | 2.34M | } |
80 | | |
81 | | int |
82 | | PyFunction_AddWatcher(PyFunction_WatchCallback callback) |
83 | 0 | { |
84 | 0 | PyInterpreterState *interp = _PyInterpreterState_GET(); |
85 | 0 | assert(interp->_initialized); |
86 | 0 | for (int i = 0; i < FUNC_MAX_WATCHERS; i++) { |
87 | 0 | if (interp->func_watchers[i] == NULL) { |
88 | 0 | interp->func_watchers[i] = callback; |
89 | 0 | interp->active_func_watchers |= (1 << i); |
90 | 0 | return i; |
91 | 0 | } |
92 | 0 | } |
93 | 0 | PyErr_SetString(PyExc_RuntimeError, "no more func watcher IDs available"); |
94 | 0 | return -1; |
95 | 0 | } |
96 | | |
97 | | int |
98 | | PyFunction_ClearWatcher(int watcher_id) |
99 | 0 | { |
100 | 0 | PyInterpreterState *interp = _PyInterpreterState_GET(); |
101 | 0 | if (watcher_id < 0 || watcher_id >= FUNC_MAX_WATCHERS) { |
102 | 0 | PyErr_Format(PyExc_ValueError, "invalid func watcher ID %d", |
103 | 0 | watcher_id); |
104 | 0 | return -1; |
105 | 0 | } |
106 | 0 | if (!interp->func_watchers[watcher_id]) { |
107 | 0 | PyErr_Format(PyExc_ValueError, "no func watcher set for ID %d", |
108 | 0 | watcher_id); |
109 | 0 | return -1; |
110 | 0 | } |
111 | 0 | interp->func_watchers[watcher_id] = NULL; |
112 | 0 | interp->active_func_watchers &= ~(1 << watcher_id); |
113 | 0 | return 0; |
114 | 0 | } |
115 | | PyFunctionObject * |
116 | | _PyFunction_FromConstructor(PyFrameConstructor *constr) |
117 | 571 | { |
118 | 571 | PyObject *module; |
119 | 571 | if (PyDict_GetItemRef(constr->fc_globals, &_Py_ID(__name__), &module) < 0) { |
120 | 0 | return NULL; |
121 | 0 | } |
122 | | |
123 | 571 | PyFunctionObject *op = PyObject_GC_New(PyFunctionObject, &PyFunction_Type); |
124 | 571 | if (op == NULL) { |
125 | 0 | Py_XDECREF(module); |
126 | 0 | return NULL; |
127 | 0 | } |
128 | 571 | _Py_INCREF_DICT(constr->fc_globals); |
129 | 571 | op->func_globals = constr->fc_globals; |
130 | 571 | _Py_INCREF_BUILTINS(constr->fc_builtins); |
131 | 571 | op->func_builtins = constr->fc_builtins; |
132 | 571 | op->func_name = Py_NewRef(constr->fc_name); |
133 | 571 | op->func_qualname = Py_NewRef(constr->fc_qualname); |
134 | 571 | _Py_INCREF_CODE((PyCodeObject *)constr->fc_code); |
135 | 571 | op->func_code = constr->fc_code; |
136 | 571 | op->func_defaults = Py_XNewRef(constr->fc_defaults); |
137 | 571 | op->func_kwdefaults = Py_XNewRef(constr->fc_kwdefaults); |
138 | 571 | op->func_closure = Py_XNewRef(constr->fc_closure); |
139 | 571 | op->func_doc = Py_NewRef(Py_None); |
140 | 571 | op->func_dict = NULL; |
141 | 571 | op->func_weakreflist = NULL; |
142 | 571 | op->func_module = module; |
143 | 571 | op->func_annotations = NULL; |
144 | 571 | op->func_annotate = NULL; |
145 | 571 | op->func_typeparams = NULL; |
146 | 571 | op->vectorcall = _PyFunction_Vectorcall; |
147 | 571 | op->func_version = FUNC_VERSION_UNSET; |
148 | | // NOTE: functions created via FrameConstructor do not use deferred |
149 | | // reference counting because they are typically not part of cycles |
150 | | // nor accessed by multiple threads. |
151 | 571 | _PyObject_GC_TRACK(op); |
152 | 571 | handle_func_event(PyFunction_EVENT_CREATE, op, NULL); |
153 | 571 | return op; |
154 | 571 | } |
155 | | |
156 | | PyObject * |
157 | | PyFunction_NewWithQualName(PyObject *code, PyObject *globals, PyObject *qualname) |
158 | 1.18M | { |
159 | 1.18M | assert(globals != NULL); |
160 | 1.18M | assert(PyAnyDict_Check(globals)); |
161 | 1.18M | _Py_INCREF_DICT(globals); |
162 | | |
163 | 1.18M | PyCodeObject *code_obj = (PyCodeObject *)code; |
164 | 1.18M | _Py_INCREF_CODE(code_obj); |
165 | | |
166 | 1.18M | assert(code_obj->co_name != NULL); |
167 | 1.18M | PyObject *name = Py_NewRef(code_obj->co_name); |
168 | | |
169 | 1.18M | if (!qualname) { |
170 | 1.18M | qualname = code_obj->co_qualname; |
171 | 1.18M | } |
172 | 1.18M | assert(qualname != NULL); |
173 | 1.18M | Py_INCREF(qualname); |
174 | | |
175 | 1.18M | PyObject *consts = code_obj->co_consts; |
176 | 1.18M | assert(PyTuple_Check(consts)); |
177 | 1.18M | PyObject *doc; |
178 | 1.18M | if (code_obj->co_flags & CO_HAS_DOCSTRING) { |
179 | 9.08k | assert(PyTuple_Size(consts) >= 1); |
180 | 9.08k | doc = PyTuple_GetItem(consts, 0); |
181 | 9.08k | if (!PyUnicode_Check(doc)) { |
182 | 0 | doc = Py_None; |
183 | 0 | } |
184 | 9.08k | } |
185 | 1.17M | else { |
186 | 1.17M | doc = Py_None; |
187 | 1.17M | } |
188 | 1.18M | Py_INCREF(doc); |
189 | | |
190 | | // __module__: Use globals['__name__'] if it exists, or NULL. |
191 | 1.18M | PyObject *module; |
192 | 1.18M | PyObject *builtins = NULL; |
193 | 1.18M | if (PyDict_GetItemRef(globals, &_Py_ID(__name__), &module) < 0) { |
194 | 0 | goto error; |
195 | 0 | } |
196 | | |
197 | 1.18M | builtins = _PyDict_LoadBuiltinsFromGlobals(globals); |
198 | 1.18M | if (builtins == NULL) { |
199 | 0 | goto error; |
200 | 0 | } |
201 | | |
202 | 1.18M | PyFunctionObject *op = PyObject_GC_New(PyFunctionObject, &PyFunction_Type); |
203 | 1.18M | if (op == NULL) { |
204 | 0 | goto error; |
205 | 0 | } |
206 | | /* Note: No failures from this point on, since func_dealloc() does not |
207 | | expect a partially-created object. */ |
208 | | |
209 | 1.18M | op->func_globals = globals; |
210 | 1.18M | op->func_builtins = builtins; |
211 | 1.18M | op->func_name = name; |
212 | 1.18M | op->func_qualname = qualname; |
213 | 1.18M | op->func_code = (PyObject*)code_obj; |
214 | 1.18M | op->func_defaults = NULL; // No default positional arguments |
215 | 1.18M | op->func_kwdefaults = NULL; // No default keyword arguments |
216 | 1.18M | op->func_closure = NULL; |
217 | 1.18M | op->func_doc = doc; |
218 | 1.18M | op->func_dict = NULL; |
219 | 1.18M | op->func_weakreflist = NULL; |
220 | 1.18M | op->func_module = module; |
221 | 1.18M | op->func_annotations = NULL; |
222 | 1.18M | op->func_annotate = NULL; |
223 | 1.18M | op->func_typeparams = NULL; |
224 | 1.18M | op->vectorcall = _PyFunction_Vectorcall; |
225 | 1.18M | op->func_version = FUNC_VERSION_UNSET; |
226 | 1.18M | if (((code_obj->co_flags & CO_NESTED) == 0) || |
227 | 1.16M | (code_obj->co_flags & CO_METHOD)) { |
228 | | // Use deferred reference counting for top-level functions, but not |
229 | | // nested functions because they are more likely to capture variables, |
230 | | // which makes prompt deallocation more important. |
231 | | // |
232 | | // Nested methods (functions defined in class scope) are also deferred, |
233 | | // since they will likely be cleaned up by GC anyway. |
234 | 20.7k | _PyObject_SetDeferredRefcount((PyObject *)op); |
235 | 20.7k | } |
236 | 1.18M | _PyObject_GC_TRACK(op); |
237 | 1.18M | handle_func_event(PyFunction_EVENT_CREATE, op, NULL); |
238 | 1.18M | return (PyObject *)op; |
239 | | |
240 | 0 | error: |
241 | 0 | Py_DECREF(globals); |
242 | 0 | Py_DECREF(code_obj); |
243 | 0 | Py_DECREF(name); |
244 | 0 | Py_DECREF(qualname); |
245 | 0 | Py_DECREF(doc); |
246 | 0 | Py_XDECREF(module); |
247 | 0 | Py_XDECREF(builtins); |
248 | 0 | return NULL; |
249 | 1.18M | } |
250 | | |
251 | | /* |
252 | | (This is purely internal documentation. There are no public APIs here.) |
253 | | |
254 | | Function (and code) versions |
255 | | ---------------------------- |
256 | | |
257 | | The Tier 1 specializer generates CALL variants that can be invalidated |
258 | | by changes to critical function attributes: |
259 | | |
260 | | - __code__ |
261 | | - __defaults__ |
262 | | - __kwdefaults__ |
263 | | - __closure__ |
264 | | |
265 | | For this purpose function objects have a 32-bit func_version member |
266 | | that the specializer writes to the specialized instruction's inline |
267 | | cache and which is checked by a guard on the specialized instructions. |
268 | | |
269 | | The MAKE_FUNCTION bytecode sets func_version from the code object's |
270 | | co_version field. The latter is initialized from a counter in the |
271 | | interpreter state (interp->func_state.next_version) and never changes. |
272 | | When this counter overflows, it remains zero and the specializer loses |
273 | | the ability to specialize calls to new functions. |
274 | | |
275 | | The func_version is reset to zero when any of the critical attributes |
276 | | is modified; after this point the specializer will no longer specialize |
277 | | calls to this function, and the guard will always fail. |
278 | | |
279 | | The function and code version cache |
280 | | ----------------------------------- |
281 | | |
282 | | The Tier 2 optimizer now has a problem, since it needs to find the |
283 | | function and code objects given only the version number from the inline |
284 | | cache. Our solution is to maintain a cache mapping version numbers to |
285 | | function and code objects. To limit the cache size we could hash |
286 | | the version number, but for now we simply use it modulo the table size. |
287 | | |
288 | | There are some corner cases (e.g. generator expressions) where we will |
289 | | be unable to find the function object in the cache but we can still |
290 | | find the code object. For this reason the cache stores both the |
291 | | function object and the code object. |
292 | | |
293 | | The cache doesn't contain strong references; cache entries are |
294 | | invalidated whenever the function or code object is deallocated. |
295 | | |
296 | | Invariants |
297 | | ---------- |
298 | | |
299 | | These should hold at any time except when one of the cache-mutating |
300 | | functions is running. |
301 | | |
302 | | - For any slot s at index i: |
303 | | - s->func == NULL or s->func->func_version % FUNC_VERSION_CACHE_SIZE == i |
304 | | - s->code == NULL or s->code->co_version % FUNC_VERSION_CACHE_SIZE == i |
305 | | if s->func != NULL, then s->func->func_code == s->code |
306 | | |
307 | | */ |
308 | | |
309 | | #ifndef Py_GIL_DISABLED |
310 | | static inline struct _func_version_cache_item * |
311 | | get_cache_item(PyInterpreterState *interp, uint32_t version) |
312 | 2.40M | { |
313 | 2.40M | return interp->func_state.func_version_cache + |
314 | 2.40M | (version % FUNC_VERSION_CACHE_SIZE); |
315 | 2.40M | } |
316 | | #endif |
317 | | |
318 | | void |
319 | | _PyFunction_SetVersion(PyFunctionObject *func, uint32_t version) |
320 | 1.18M | { |
321 | 1.18M | assert(func->func_version == FUNC_VERSION_UNSET); |
322 | 1.18M | assert(version >= FUNC_VERSION_FIRST_VALID); |
323 | | // This should only be called from MAKE_FUNCTION. No code is specialized |
324 | | // based on the version, so we do not need to stop the world to set it. |
325 | 1.18M | func->func_version = version; |
326 | 1.18M | #ifndef Py_GIL_DISABLED |
327 | 1.18M | PyInterpreterState *interp = _PyInterpreterState_GET(); |
328 | 1.18M | struct _func_version_cache_item *slot = get_cache_item(interp, version); |
329 | 1.18M | slot->func = func; |
330 | 1.18M | slot->code = func->func_code; |
331 | 1.18M | #endif |
332 | 1.18M | } |
333 | | |
334 | | static void |
335 | | func_clear_version(PyInterpreterState *interp, PyFunctionObject *func) |
336 | 1.16M | { |
337 | 1.16M | if (func->func_version < FUNC_VERSION_FIRST_VALID) { |
338 | | // Version was never set or has already been cleared. |
339 | 796 | return; |
340 | 796 | } |
341 | 1.16M | #ifndef Py_GIL_DISABLED |
342 | 1.16M | struct _func_version_cache_item *slot = |
343 | 1.16M | get_cache_item(interp, func->func_version); |
344 | 1.16M | if (slot->func == func) { |
345 | 1.14M | slot->func = NULL; |
346 | | // Leave slot->code alone, there may be use for it. |
347 | 1.14M | } |
348 | 1.16M | #endif |
349 | 1.16M | func->func_version = FUNC_VERSION_CLEARED; |
350 | 1.16M | } |
351 | | |
352 | | void |
353 | | _PyFunction_ClearCodeByVersion(uint32_t version) |
354 | 57.2k | { |
355 | 57.2k | #ifndef Py_GIL_DISABLED |
356 | 57.2k | PyInterpreterState *interp = _PyInterpreterState_GET(); |
357 | 57.2k | struct _func_version_cache_item *slot = get_cache_item(interp, version); |
358 | 57.2k | if (slot->code) { |
359 | 21.9k | assert(PyCode_Check(slot->code)); |
360 | 21.9k | PyCodeObject *code = (PyCodeObject *)slot->code; |
361 | 21.9k | if (code->co_version == version) { |
362 | 3.35k | slot->code = NULL; |
363 | 3.35k | slot->func = NULL; |
364 | 3.35k | } |
365 | 21.9k | } |
366 | 57.2k | #endif |
367 | 57.2k | } |
368 | | |
369 | | uint32_t |
370 | | _PyFunction_GetVersionForCurrentState(PyFunctionObject *func) |
371 | 30.6k | { |
372 | 30.6k | return func->func_version; |
373 | 30.6k | } |
374 | | |
375 | | PyObject * |
376 | | PyFunction_New(PyObject *code, PyObject *globals) |
377 | 1.18M | { |
378 | 1.18M | return PyFunction_NewWithQualName(code, globals, NULL); |
379 | 1.18M | } |
380 | | |
381 | | PyObject * |
382 | | PyFunction_GetCode(PyObject *op) |
383 | 0 | { |
384 | 0 | if (!PyFunction_Check(op)) { |
385 | 0 | PyErr_BadInternalCall(); |
386 | 0 | return NULL; |
387 | 0 | } |
388 | 0 | return ((PyFunctionObject *) op) -> func_code; |
389 | 0 | } |
390 | | |
391 | | PyObject * |
392 | | PyFunction_GetGlobals(PyObject *op) |
393 | 0 | { |
394 | 0 | if (!PyFunction_Check(op)) { |
395 | 0 | PyErr_BadInternalCall(); |
396 | 0 | return NULL; |
397 | 0 | } |
398 | 0 | return ((PyFunctionObject *) op) -> func_globals; |
399 | 0 | } |
400 | | |
401 | | PyObject * |
402 | | PyFunction_GetModule(PyObject *op) |
403 | 15 | { |
404 | 15 | if (!PyFunction_Check(op)) { |
405 | 0 | PyErr_BadInternalCall(); |
406 | 0 | return NULL; |
407 | 0 | } |
408 | 15 | return ((PyFunctionObject *) op) -> func_module; |
409 | 15 | } |
410 | | |
411 | | PyObject * |
412 | | PyFunction_GetDefaults(PyObject *op) |
413 | 0 | { |
414 | 0 | if (!PyFunction_Check(op)) { |
415 | 0 | PyErr_BadInternalCall(); |
416 | 0 | return NULL; |
417 | 0 | } |
418 | 0 | return ((PyFunctionObject *) op) -> func_defaults; |
419 | 0 | } |
420 | | |
421 | | int |
422 | | PyFunction_SetDefaults(PyObject *op, PyObject *defaults) |
423 | 0 | { |
424 | 0 | if (!PyFunction_Check(op)) { |
425 | 0 | PyErr_BadInternalCall(); |
426 | 0 | return -1; |
427 | 0 | } |
428 | 0 | if (defaults == Py_None) |
429 | 0 | defaults = NULL; |
430 | 0 | else if (defaults && PyTuple_Check(defaults)) { |
431 | 0 | Py_INCREF(defaults); |
432 | 0 | } |
433 | 0 | else { |
434 | 0 | PyErr_SetString(PyExc_SystemError, "non-tuple default args"); |
435 | 0 | return -1; |
436 | 0 | } |
437 | 0 | PyFunctionObject *func = (PyFunctionObject *)op; |
438 | 0 | handle_func_event(PyFunction_EVENT_MODIFY_DEFAULTS, func, defaults); |
439 | 0 | PyInterpreterState *interp = _PyInterpreterState_GET(); |
440 | 0 | _PyEval_StopTheWorld(interp); |
441 | 0 | func_clear_version(interp, func); |
442 | 0 | PyObject *old_defaults = func->func_defaults; |
443 | 0 | func->func_defaults = defaults; |
444 | 0 | _PyEval_StartTheWorld(interp); |
445 | 0 | Py_XDECREF(old_defaults); |
446 | 0 | return 0; |
447 | 0 | } |
448 | | |
449 | | void |
450 | | PyFunction_SetVectorcall(PyFunctionObject *func, vectorcallfunc vectorcall) |
451 | 0 | { |
452 | 0 | assert(func != NULL); |
453 | 0 | PyInterpreterState *interp = _PyInterpreterState_GET(); |
454 | 0 | _PyEval_StopTheWorld(interp); |
455 | 0 | func_clear_version(interp, func); |
456 | 0 | func->vectorcall = vectorcall; |
457 | 0 | _PyEval_StartTheWorld(interp); |
458 | 0 | } |
459 | | |
460 | | PyObject * |
461 | | PyFunction_GetKwDefaults(PyObject *op) |
462 | 0 | { |
463 | 0 | if (!PyFunction_Check(op)) { |
464 | 0 | PyErr_BadInternalCall(); |
465 | 0 | return NULL; |
466 | 0 | } |
467 | 0 | return ((PyFunctionObject *) op) -> func_kwdefaults; |
468 | 0 | } |
469 | | |
470 | | int |
471 | | PyFunction_SetKwDefaults(PyObject *op, PyObject *defaults) |
472 | 0 | { |
473 | 0 | if (!PyFunction_Check(op)) { |
474 | 0 | PyErr_BadInternalCall(); |
475 | 0 | return -1; |
476 | 0 | } |
477 | 0 | if (defaults == Py_None) |
478 | 0 | defaults = NULL; |
479 | 0 | else if (defaults && PyDict_Check(defaults)) { |
480 | 0 | Py_INCREF(defaults); |
481 | 0 | } |
482 | 0 | else { |
483 | 0 | PyErr_SetString(PyExc_SystemError, |
484 | 0 | "non-dict keyword only default args"); |
485 | 0 | return -1; |
486 | 0 | } |
487 | 0 | PyFunctionObject *func = (PyFunctionObject *)op; |
488 | 0 | handle_func_event(PyFunction_EVENT_MODIFY_KWDEFAULTS, func, defaults); |
489 | 0 | PyInterpreterState *interp = _PyInterpreterState_GET(); |
490 | 0 | _PyEval_StopTheWorld(interp); |
491 | 0 | func_clear_version(interp, func); |
492 | 0 | PyObject *old_kwdefaults = func->func_kwdefaults; |
493 | 0 | func->func_kwdefaults = defaults; |
494 | 0 | _PyEval_StartTheWorld(interp); |
495 | 0 | Py_XDECREF(old_kwdefaults); |
496 | 0 | return 0; |
497 | 0 | } |
498 | | |
499 | | PyObject * |
500 | | PyFunction_GetClosure(PyObject *op) |
501 | 0 | { |
502 | 0 | if (!PyFunction_Check(op)) { |
503 | 0 | PyErr_BadInternalCall(); |
504 | 0 | return NULL; |
505 | 0 | } |
506 | 0 | return ((PyFunctionObject *) op) -> func_closure; |
507 | 0 | } |
508 | | |
509 | | int |
510 | | PyFunction_SetClosure(PyObject *op, PyObject *closure) |
511 | 0 | { |
512 | 0 | if (!PyFunction_Check(op)) { |
513 | 0 | PyErr_BadInternalCall(); |
514 | 0 | return -1; |
515 | 0 | } |
516 | 0 | if (closure == Py_None) |
517 | 0 | closure = NULL; |
518 | 0 | else if (PyTuple_Check(closure)) { |
519 | 0 | Py_INCREF(closure); |
520 | 0 | } |
521 | 0 | else { |
522 | 0 | PyErr_Format(PyExc_SystemError, |
523 | 0 | "expected tuple for closure, got '%.100s'", |
524 | 0 | Py_TYPE(closure)->tp_name); |
525 | 0 | return -1; |
526 | 0 | } |
527 | 0 | PyFunctionObject *func = (PyFunctionObject *)op; |
528 | 0 | PyInterpreterState *interp = _PyInterpreterState_GET(); |
529 | 0 | _PyEval_StopTheWorld(interp); |
530 | 0 | func_clear_version(interp, func); |
531 | 0 | PyObject *old_closure = func->func_closure; |
532 | 0 | func->func_closure = closure; |
533 | 0 | _PyEval_StartTheWorld(interp); |
534 | 0 | Py_XDECREF(old_closure); |
535 | 0 | return 0; |
536 | 0 | } |
537 | | |
538 | | static PyObject * |
539 | | func_get_annotation_dict(PyFunctionObject *op) |
540 | 0 | { |
541 | 0 | if (op->func_annotations == NULL) { |
542 | 0 | if (op->func_annotate == NULL || !PyCallable_Check(op->func_annotate)) { |
543 | 0 | Py_RETURN_NONE; |
544 | 0 | } |
545 | 0 | PyObject *one = _PyLong_GetOne(); |
546 | 0 | PyObject *ann_dict = _PyObject_CallOneArg(op->func_annotate, one); |
547 | 0 | if (ann_dict == NULL) { |
548 | 0 | return NULL; |
549 | 0 | } |
550 | 0 | if (!PyDict_Check(ann_dict)) { |
551 | 0 | PyErr_Format(PyExc_TypeError, |
552 | 0 | "__annotate__() must return a dict, not %T", |
553 | 0 | ann_dict); |
554 | 0 | Py_DECREF(ann_dict); |
555 | 0 | return NULL; |
556 | 0 | } |
557 | 0 | Py_XSETREF(op->func_annotations, ann_dict); |
558 | 0 | return ann_dict; |
559 | 0 | } |
560 | 0 | if (PyTuple_CheckExact(op->func_annotations)) { |
561 | 0 | PyObject *ann_tuple = op->func_annotations; |
562 | 0 | PyObject *ann_dict = PyDict_New(); |
563 | 0 | if (ann_dict == NULL) { |
564 | 0 | return NULL; |
565 | 0 | } |
566 | | |
567 | 0 | assert(PyTuple_GET_SIZE(ann_tuple) % 2 == 0); |
568 | | |
569 | 0 | for (Py_ssize_t i = 0; i < PyTuple_GET_SIZE(ann_tuple); i += 2) { |
570 | 0 | int err = PyDict_SetItem(ann_dict, |
571 | 0 | PyTuple_GET_ITEM(ann_tuple, i), |
572 | 0 | PyTuple_GET_ITEM(ann_tuple, i + 1)); |
573 | | |
574 | 0 | if (err < 0) { |
575 | 0 | Py_DECREF(ann_dict); |
576 | 0 | return NULL; |
577 | 0 | } |
578 | 0 | } |
579 | 0 | Py_SETREF(op->func_annotations, ann_dict); |
580 | 0 | } |
581 | 0 | assert(PyDict_Check(op->func_annotations)); |
582 | 0 | return op->func_annotations; |
583 | 0 | } |
584 | | |
585 | | PyObject * |
586 | | PyFunction_GetAnnotations(PyObject *op) |
587 | 0 | { |
588 | 0 | if (!PyFunction_Check(op)) { |
589 | 0 | PyErr_BadInternalCall(); |
590 | 0 | return NULL; |
591 | 0 | } |
592 | 0 | return func_get_annotation_dict((PyFunctionObject *)op); |
593 | 0 | } |
594 | | |
595 | | int |
596 | | PyFunction_SetAnnotations(PyObject *op, PyObject *annotations) |
597 | 0 | { |
598 | 0 | if (!PyFunction_Check(op)) { |
599 | 0 | PyErr_BadInternalCall(); |
600 | 0 | return -1; |
601 | 0 | } |
602 | 0 | if (annotations == Py_None) |
603 | 0 | annotations = NULL; |
604 | 0 | else if (annotations && PyDict_Check(annotations)) { |
605 | 0 | Py_INCREF(annotations); |
606 | 0 | } |
607 | 0 | else { |
608 | 0 | PyErr_SetString(PyExc_SystemError, |
609 | 0 | "non-dict annotations"); |
610 | 0 | return -1; |
611 | 0 | } |
612 | 0 | PyFunctionObject *func = (PyFunctionObject *)op; |
613 | 0 | PyInterpreterState *interp = _PyInterpreterState_GET(); |
614 | 0 | _PyEval_StopTheWorld(interp); |
615 | 0 | PyObject *old_annotations = func->func_annotations; |
616 | 0 | func->func_annotations = annotations; |
617 | 0 | PyObject *old_annotate = func->func_annotate; |
618 | 0 | func->func_annotate = NULL; |
619 | 0 | _PyEval_StartTheWorld(interp); |
620 | 0 | Py_XDECREF(old_annotations); |
621 | 0 | Py_XDECREF(old_annotate); |
622 | 0 | return 0; |
623 | 0 | } |
624 | | |
625 | | /* Methods */ |
626 | | |
627 | | #define OFF(x) offsetof(PyFunctionObject, x) |
628 | | |
629 | | static PyMemberDef func_memberlist[] = { |
630 | | {"__closure__", _Py_T_OBJECT, OFF(func_closure), Py_READONLY}, |
631 | | {"__globals__", _Py_T_OBJECT, OFF(func_globals), Py_READONLY}, |
632 | | {"__builtins__", _Py_T_OBJECT, OFF(func_builtins), Py_READONLY}, |
633 | | {NULL} /* Sentinel */ |
634 | | }; |
635 | | |
636 | | /*[clinic input] |
637 | | class function "PyFunctionObject *" "&PyFunction_Type" |
638 | | [clinic start generated code]*/ |
639 | | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=70af9c90aa2e71b0]*/ |
640 | | |
641 | | #include "clinic/funcobject.c.h" |
642 | | |
643 | | static PyObject * |
644 | | func_get_code(PyObject *self, void *Py_UNUSED(ignored)) |
645 | 42 | { |
646 | 42 | PyFunctionObject *op = _PyFunction_CAST(self); |
647 | 42 | if (PySys_Audit("object.__getattr__", "Os", op, "__code__") < 0) { |
648 | 0 | return NULL; |
649 | 0 | } |
650 | | |
651 | 42 | return Py_NewRef(op->func_code); |
652 | 42 | } |
653 | | |
654 | | static int |
655 | | func_set_code(PyObject *self, PyObject *value, void *Py_UNUSED(ignored)) |
656 | 0 | { |
657 | 0 | PyFunctionObject *op = _PyFunction_CAST(self); |
658 | | |
659 | | /* Not legal to del f.func_code or to set it to anything |
660 | | * other than a code object. */ |
661 | 0 | if (value == NULL || !PyCode_Check(value)) { |
662 | 0 | PyErr_SetString(PyExc_TypeError, |
663 | 0 | "__code__ must be set to a code object"); |
664 | 0 | return -1; |
665 | 0 | } |
666 | | |
667 | 0 | if (PySys_Audit("object.__setattr__", "OsO", |
668 | 0 | op, "__code__", value) < 0) { |
669 | 0 | return -1; |
670 | 0 | } |
671 | | |
672 | 0 | int nfree = ((PyCodeObject *)value)->co_nfreevars; |
673 | 0 | Py_ssize_t nclosure = (op->func_closure == NULL ? 0 : |
674 | 0 | PyTuple_GET_SIZE(op->func_closure)); |
675 | 0 | if (nclosure != nfree) { |
676 | 0 | PyErr_Format(PyExc_ValueError, |
677 | 0 | "%U() requires a code object with %zd free vars," |
678 | 0 | " not %d", |
679 | 0 | op->func_name, |
680 | 0 | nclosure, nfree); |
681 | 0 | return -1; |
682 | 0 | } |
683 | | |
684 | 0 | PyObject *func_code = PyFunction_GET_CODE(op); |
685 | 0 | int old_flags = ((PyCodeObject *)func_code)->co_flags; |
686 | 0 | int new_flags = ((PyCodeObject *)value)->co_flags; |
687 | 0 | int mask = CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR; |
688 | 0 | if ((old_flags & mask) != (new_flags & mask)) { |
689 | 0 | if (PyErr_Warn(PyExc_DeprecationWarning, |
690 | 0 | "Assigning a code object of non-matching type is deprecated " |
691 | 0 | "(e.g., from a generator to a plain function)") < 0) |
692 | 0 | { |
693 | 0 | return -1; |
694 | 0 | } |
695 | 0 | } |
696 | | |
697 | 0 | handle_func_event(PyFunction_EVENT_MODIFY_CODE, op, value); |
698 | 0 | PyInterpreterState *interp = _PyInterpreterState_GET(); |
699 | 0 | _PyEval_StopTheWorld(interp); |
700 | 0 | func_clear_version(interp, op); |
701 | 0 | PyObject *old_code = op->func_code; |
702 | 0 | op->func_code = Py_NewRef(value); |
703 | 0 | _PyEval_StartTheWorld(interp); |
704 | 0 | Py_XDECREF(old_code); |
705 | 0 | return 0; |
706 | 0 | } |
707 | | |
708 | | static PyObject * |
709 | | func_get_name(PyObject *self, void *Py_UNUSED(ignored)) |
710 | 1.76k | { |
711 | 1.76k | PyFunctionObject *op = _PyFunction_CAST(self); |
712 | 1.76k | return Py_NewRef(op->func_name); |
713 | 1.76k | } |
714 | | |
715 | | static int |
716 | | func_set_name(PyObject *self, PyObject *value, void *Py_UNUSED(ignored)) |
717 | 224 | { |
718 | 224 | PyFunctionObject *op = _PyFunction_CAST(self); |
719 | | /* Not legal to del f.func_name or to set it to anything |
720 | | * other than a string object. */ |
721 | 224 | if (value == NULL || !PyUnicode_Check(value)) { |
722 | 0 | PyErr_SetString(PyExc_TypeError, |
723 | 0 | "__name__ must be set to a string object"); |
724 | 0 | return -1; |
725 | 0 | } |
726 | 224 | PyInterpreterState *interp = _PyInterpreterState_GET(); |
727 | 224 | _PyEval_StopTheWorld(interp); |
728 | 224 | PyObject *old_name = op->func_name; |
729 | 224 | op->func_name = Py_NewRef(value); |
730 | 224 | _PyEval_StartTheWorld(interp); |
731 | 224 | Py_XDECREF(old_name); |
732 | 224 | return 0; |
733 | 224 | } |
734 | | |
735 | | static PyObject * |
736 | | func_get_qualname(PyObject *self, void *Py_UNUSED(ignored)) |
737 | 1.68k | { |
738 | 1.68k | PyFunctionObject *op = _PyFunction_CAST(self); |
739 | 1.68k | return Py_NewRef(op->func_qualname); |
740 | 1.68k | } |
741 | | |
742 | | static int |
743 | | func_set_qualname(PyObject *self, PyObject *value, void *Py_UNUSED(ignored)) |
744 | 289 | { |
745 | 289 | PyFunctionObject *op = _PyFunction_CAST(self); |
746 | | /* Not legal to del f.__qualname__ or to set it to anything |
747 | | * other than a string object. */ |
748 | 289 | if (value == NULL || !PyUnicode_Check(value)) { |
749 | 0 | PyErr_SetString(PyExc_TypeError, |
750 | 0 | "__qualname__ must be set to a string object"); |
751 | 0 | return -1; |
752 | 0 | } |
753 | 289 | handle_func_event(PyFunction_EVENT_MODIFY_QUALNAME, (PyFunctionObject *) op, value); |
754 | 289 | PyInterpreterState *interp = _PyInterpreterState_GET(); |
755 | 289 | _PyEval_StopTheWorld(interp); |
756 | 289 | PyObject *old_qualname = op->func_qualname; |
757 | 289 | op->func_qualname = Py_NewRef(value); |
758 | 289 | _PyEval_StartTheWorld(interp); |
759 | 289 | Py_XDECREF(old_qualname); |
760 | 289 | return 0; |
761 | 289 | } |
762 | | |
763 | | static PyObject * |
764 | | func_get_doc(PyObject *self, void *Py_UNUSED(ignored)) |
765 | 1.97k | { |
766 | 1.97k | PyFunctionObject *op = _PyFunction_CAST(self); |
767 | 0 | PyObject *doc = op->func_doc; |
768 | 1.97k | if (doc == NULL) { |
769 | 0 | doc = Py_None; |
770 | 0 | } |
771 | 1.97k | return Py_NewRef(doc); |
772 | 1.97k | } |
773 | | |
774 | | static int |
775 | | func_set_doc(PyObject *self, PyObject *value, void *Py_UNUSED(ignored)) |
776 | 271 | { |
777 | | /* Legal to del f.__doc__ or to set it to any object. */ |
778 | 271 | PyFunctionObject *op = _PyFunction_CAST(self); |
779 | 0 | PyInterpreterState *interp = _PyInterpreterState_GET(); |
780 | 271 | _PyEval_StopTheWorld(interp); |
781 | 271 | PyObject *old_doc = op->func_doc; |
782 | 271 | op->func_doc = Py_XNewRef(value); |
783 | 271 | _PyEval_StartTheWorld(interp); |
784 | 271 | Py_XDECREF(old_doc); |
785 | 271 | return 0; |
786 | 271 | } |
787 | | |
788 | | static PyObject * |
789 | | func_get_module(PyObject *self, void *Py_UNUSED(ignored)) |
790 | 1.68k | { |
791 | 1.68k | PyFunctionObject *op = _PyFunction_CAST(self); |
792 | 0 | PyObject *module = op->func_module; |
793 | 1.68k | if (module == NULL) { |
794 | 0 | module = Py_None; |
795 | 0 | } |
796 | 1.68k | return Py_NewRef(module); |
797 | 1.68k | } |
798 | | |
799 | | static int |
800 | | func_set_module(PyObject *self, PyObject *value, void *Py_UNUSED(ignored)) |
801 | 211 | { |
802 | | /* Legal to del f.__module__ or to set it to any object. */ |
803 | 211 | PyFunctionObject *op = _PyFunction_CAST(self); |
804 | 0 | PyInterpreterState *interp = _PyInterpreterState_GET(); |
805 | 211 | _PyEval_StopTheWorld(interp); |
806 | 211 | PyObject *old_module = op->func_module; |
807 | 211 | op->func_module = Py_XNewRef(value); |
808 | 211 | _PyEval_StartTheWorld(interp); |
809 | 211 | Py_XDECREF(old_module); |
810 | 211 | return 0; |
811 | 211 | } |
812 | | |
813 | | static PyObject * |
814 | | func_get_defaults(PyObject *self, void *Py_UNUSED(ignored)) |
815 | 0 | { |
816 | 0 | PyFunctionObject *op = _PyFunction_CAST(self); |
817 | 0 | if (PySys_Audit("object.__getattr__", "Os", op, "__defaults__") < 0) { |
818 | 0 | return NULL; |
819 | 0 | } |
820 | 0 | if (op->func_defaults == NULL) { |
821 | 0 | Py_RETURN_NONE; |
822 | 0 | } |
823 | 0 | return Py_NewRef(op->func_defaults); |
824 | 0 | } |
825 | | |
826 | | static int |
827 | | func_set_defaults(PyObject *self, PyObject *value, void *Py_UNUSED(ignored)) |
828 | 0 | { |
829 | | /* Legal to del f.func_defaults. |
830 | | * Can only set func_defaults to NULL or a tuple. */ |
831 | 0 | PyFunctionObject *op = _PyFunction_CAST(self); |
832 | 0 | if (value == Py_None) |
833 | 0 | value = NULL; |
834 | 0 | if (value != NULL && !PyTuple_Check(value)) { |
835 | 0 | PyErr_SetString(PyExc_TypeError, |
836 | 0 | "__defaults__ must be set to a tuple object"); |
837 | 0 | return -1; |
838 | 0 | } |
839 | 0 | if (value) { |
840 | 0 | if (PySys_Audit("object.__setattr__", "OsO", |
841 | 0 | op, "__defaults__", value) < 0) { |
842 | 0 | return -1; |
843 | 0 | } |
844 | 0 | } else if (PySys_Audit("object.__delattr__", "Os", |
845 | 0 | op, "__defaults__") < 0) { |
846 | 0 | return -1; |
847 | 0 | } |
848 | | |
849 | 0 | handle_func_event(PyFunction_EVENT_MODIFY_DEFAULTS, op, value); |
850 | 0 | PyInterpreterState *interp = _PyInterpreterState_GET(); |
851 | 0 | _PyEval_StopTheWorld(interp); |
852 | 0 | func_clear_version(interp, op); |
853 | 0 | PyObject *old_defaults = op->func_defaults; |
854 | 0 | op->func_defaults = Py_XNewRef(value); |
855 | 0 | _PyEval_StartTheWorld(interp); |
856 | 0 | Py_XDECREF(old_defaults); |
857 | 0 | return 0; |
858 | 0 | } |
859 | | |
860 | | static PyObject * |
861 | | func_get_kwdefaults(PyObject *self, void *Py_UNUSED(ignored)) |
862 | 0 | { |
863 | 0 | PyFunctionObject *op = _PyFunction_CAST(self); |
864 | 0 | if (PySys_Audit("object.__getattr__", "Os", |
865 | 0 | op, "__kwdefaults__") < 0) { |
866 | 0 | return NULL; |
867 | 0 | } |
868 | 0 | if (op->func_kwdefaults == NULL) { |
869 | 0 | Py_RETURN_NONE; |
870 | 0 | } |
871 | 0 | return Py_NewRef(op->func_kwdefaults); |
872 | 0 | } |
873 | | |
874 | | static int |
875 | | func_set_kwdefaults(PyObject *self, PyObject *value, void *Py_UNUSED(ignored)) |
876 | 0 | { |
877 | 0 | PyFunctionObject *op = _PyFunction_CAST(self); |
878 | 0 | if (value == Py_None) |
879 | 0 | value = NULL; |
880 | | /* Legal to del f.func_kwdefaults. |
881 | | * Can only set func_kwdefaults to NULL or a dict. */ |
882 | 0 | if (value != NULL && !PyDict_Check(value)) { |
883 | 0 | PyErr_SetString(PyExc_TypeError, |
884 | 0 | "__kwdefaults__ must be set to a dict object"); |
885 | 0 | return -1; |
886 | 0 | } |
887 | 0 | if (value) { |
888 | 0 | if (PySys_Audit("object.__setattr__", "OsO", |
889 | 0 | op, "__kwdefaults__", value) < 0) { |
890 | 0 | return -1; |
891 | 0 | } |
892 | 0 | } else if (PySys_Audit("object.__delattr__", "Os", |
893 | 0 | op, "__kwdefaults__") < 0) { |
894 | 0 | return -1; |
895 | 0 | } |
896 | | |
897 | 0 | handle_func_event(PyFunction_EVENT_MODIFY_KWDEFAULTS, op, value); |
898 | 0 | PyInterpreterState *interp = _PyInterpreterState_GET(); |
899 | 0 | _PyEval_StopTheWorld(interp); |
900 | 0 | func_clear_version(interp, op); |
901 | 0 | PyObject *old_kwdefaults = op->func_kwdefaults; |
902 | 0 | op->func_kwdefaults = Py_XNewRef(value); |
903 | 0 | _PyEval_StartTheWorld(interp); |
904 | 0 | Py_XDECREF(old_kwdefaults); |
905 | 0 | return 0; |
906 | 0 | } |
907 | | |
908 | | /*[clinic input] |
909 | | @critical_section |
910 | | @getter |
911 | | function.__annotate__ |
912 | | |
913 | | Get the code object for a function. |
914 | | [clinic start generated code]*/ |
915 | | |
916 | | static PyObject * |
917 | | function___annotate___get_impl(PyFunctionObject *self) |
918 | | /*[clinic end generated code: output=5ec7219ff2bda9e6 input=7f3db11e3c3329f3]*/ |
919 | 27 | { |
920 | 27 | if (self->func_annotate == NULL) { |
921 | 27 | Py_RETURN_NONE; |
922 | 27 | } |
923 | 0 | return Py_NewRef(self->func_annotate); |
924 | 27 | } |
925 | | |
926 | | /*[clinic input] |
927 | | @critical_section |
928 | | @setter |
929 | | @deleter |
930 | | function.__annotate__ |
931 | | [clinic start generated code]*/ |
932 | | |
933 | | static int |
934 | | function___annotate___set_impl(PyFunctionObject *self, PyObject *value) |
935 | | /*[clinic end generated code: output=05b7dfc07ada66cd input=4bcfad0bdcfec768]*/ |
936 | 18 | { |
937 | 18 | if (value == NULL) { |
938 | 0 | PyErr_SetString(PyExc_TypeError, |
939 | 0 | "__annotate__ cannot be deleted"); |
940 | 0 | return -1; |
941 | 0 | } |
942 | 18 | if (Py_IsNone(value)) { |
943 | 18 | Py_XSETREF(self->func_annotate, Py_NewRef(value)); |
944 | 18 | return 0; |
945 | 18 | } |
946 | 0 | else if (PyCallable_Check(value)) { |
947 | 0 | Py_XSETREF(self->func_annotate, Py_NewRef(value)); |
948 | 0 | Py_CLEAR(self->func_annotations); |
949 | 0 | return 0; |
950 | 0 | } |
951 | 0 | else { |
952 | 0 | PyErr_SetString(PyExc_TypeError, |
953 | 0 | "__annotate__ must be callable or None"); |
954 | 0 | return -1; |
955 | 0 | } |
956 | 18 | } |
957 | | |
958 | | /*[clinic input] |
959 | | @critical_section |
960 | | @getter |
961 | | function.__annotations__ |
962 | | |
963 | | Dict of annotations in a function object. |
964 | | [clinic start generated code]*/ |
965 | | |
966 | | static PyObject * |
967 | | function___annotations___get_impl(PyFunctionObject *self) |
968 | | /*[clinic end generated code: output=a4cf4c884c934cbb input=92643d7186c1ad0c]*/ |
969 | 0 | { |
970 | 0 | PyObject *d = NULL; |
971 | 0 | if (self->func_annotations == NULL && |
972 | 0 | (self->func_annotate == NULL || !PyCallable_Check(self->func_annotate))) { |
973 | 0 | self->func_annotations = PyDict_New(); |
974 | 0 | if (self->func_annotations == NULL) |
975 | 0 | return NULL; |
976 | 0 | } |
977 | 0 | d = func_get_annotation_dict(self); |
978 | 0 | return Py_XNewRef(d); |
979 | 0 | } |
980 | | |
981 | | /*[clinic input] |
982 | | @critical_section |
983 | | @setter |
984 | | @deleter |
985 | | function.__annotations__ |
986 | | [clinic start generated code]*/ |
987 | | |
988 | | static int |
989 | | function___annotations___set_impl(PyFunctionObject *self, PyObject *value) |
990 | | /*[clinic end generated code: output=a61795d4a95eede4 input=71f6a58c00ac6745]*/ |
991 | 0 | { |
992 | 0 | if (value == Py_None) |
993 | 0 | value = NULL; |
994 | | /* Legal to del f.func_annotations. |
995 | | * Can only set func_annotations to NULL (through C api) |
996 | | * or a dict. */ |
997 | 0 | if (value != NULL && !PyDict_Check(value)) { |
998 | 0 | PyErr_SetString(PyExc_TypeError, |
999 | 0 | "__annotations__ must be set to a dict object"); |
1000 | 0 | return -1; |
1001 | 0 | } |
1002 | 0 | Py_XSETREF(self->func_annotations, Py_XNewRef(value)); |
1003 | 0 | Py_CLEAR(self->func_annotate); |
1004 | 0 | return 0; |
1005 | 0 | } |
1006 | | |
1007 | | /*[clinic input] |
1008 | | @critical_section |
1009 | | @getter |
1010 | | function.__type_params__ |
1011 | | |
1012 | | Get the declared type parameters for a function. |
1013 | | [clinic start generated code]*/ |
1014 | | |
1015 | | static PyObject * |
1016 | | function___type_params___get_impl(PyFunctionObject *self) |
1017 | | /*[clinic end generated code: output=eb844d7ffca517a8 input=0864721484293724]*/ |
1018 | 27 | { |
1019 | 27 | if (self->func_typeparams == NULL) { |
1020 | 27 | return PyTuple_New(0); |
1021 | 27 | } |
1022 | | |
1023 | 27 | assert(PyTuple_Check(self->func_typeparams)); |
1024 | 0 | return Py_NewRef(self->func_typeparams); |
1025 | 0 | } |
1026 | | |
1027 | | /*[clinic input] |
1028 | | @critical_section |
1029 | | @setter |
1030 | | @deleter |
1031 | | function.__type_params__ |
1032 | | [clinic start generated code]*/ |
1033 | | |
1034 | | static int |
1035 | | function___type_params___set_impl(PyFunctionObject *self, PyObject *value) |
1036 | | /*[clinic end generated code: output=038b4cda220e56fb input=c0e33abc5901a2f5]*/ |
1037 | 18 | { |
1038 | | /* Not legal to del f.__type_params__ or to set it to anything |
1039 | | * other than a tuple object. */ |
1040 | 18 | if (value == NULL || !PyTuple_Check(value)) { |
1041 | 0 | PyErr_SetString(PyExc_TypeError, |
1042 | 0 | "__type_params__ must be set to a tuple"); |
1043 | 0 | return -1; |
1044 | 0 | } |
1045 | 18 | Py_XSETREF(self->func_typeparams, Py_NewRef(value)); |
1046 | 18 | return 0; |
1047 | 18 | } |
1048 | | |
1049 | | PyObject * |
1050 | | _Py_set_function_type_params(PyThreadState *Py_UNUSED(ignored), PyObject *func, |
1051 | | PyObject *type_params) |
1052 | 0 | { |
1053 | 0 | assert(PyFunction_Check(func)); |
1054 | 0 | assert(PyTuple_Check(type_params)); |
1055 | 0 | PyFunctionObject *f = (PyFunctionObject *)func; |
1056 | 0 | Py_XSETREF(f->func_typeparams, Py_NewRef(type_params)); |
1057 | 0 | return Py_NewRef(func); |
1058 | 0 | } |
1059 | | |
1060 | | static PyGetSetDef func_getsetlist[] = { |
1061 | | {"__code__", func_get_code, func_set_code}, |
1062 | | {"__defaults__", func_get_defaults, func_set_defaults}, |
1063 | | {"__kwdefaults__", func_get_kwdefaults, func_set_kwdefaults}, |
1064 | | FUNCTION___ANNOTATIONS___GETSETDEF |
1065 | | FUNCTION___ANNOTATE___GETSETDEF |
1066 | | {"__dict__", PyObject_GenericGetDict, PyObject_GenericSetDict}, |
1067 | | {"__doc__", func_get_doc, func_set_doc}, |
1068 | | {"__module__", func_get_module, func_set_module}, |
1069 | | {"__name__", func_get_name, func_set_name}, |
1070 | | {"__qualname__", func_get_qualname, func_set_qualname}, |
1071 | | FUNCTION___TYPE_PARAMS___GETSETDEF |
1072 | | {NULL} /* Sentinel */ |
1073 | | }; |
1074 | | |
1075 | | /* function.__new__() maintains the following invariants for closures. |
1076 | | The closure must correspond to the free variables of the code object. |
1077 | | |
1078 | | if len(code.co_freevars) == 0: |
1079 | | closure = NULL |
1080 | | else: |
1081 | | len(closure) == len(code.co_freevars) |
1082 | | for every elt in closure, type(elt) == cell |
1083 | | */ |
1084 | | |
1085 | | /*[clinic input] |
1086 | | @classmethod |
1087 | | function.__new__ as func_new |
1088 | | code: object(type="PyCodeObject *", subclass_of="&PyCode_Type") |
1089 | | a code object |
1090 | | globals: object(subclass_of="&PyDict_Type") |
1091 | | the globals dictionary |
1092 | | name: object = None |
1093 | | a string that overrides the name from the code object |
1094 | | argdefs as defaults: object = None |
1095 | | a tuple that specifies the default argument values |
1096 | | closure: object = None |
1097 | | a tuple that supplies the bindings for free variables |
1098 | | kwdefaults: object = None |
1099 | | a dictionary that specifies the default keyword argument values |
1100 | | |
1101 | | Create a function object. |
1102 | | [clinic start generated code]*/ |
1103 | | |
1104 | | static PyObject * |
1105 | | func_new_impl(PyTypeObject *type, PyCodeObject *code, PyObject *globals, |
1106 | | PyObject *name, PyObject *defaults, PyObject *closure, |
1107 | | PyObject *kwdefaults) |
1108 | | /*[clinic end generated code: output=de72f4c22ac57144 input=20c9c9f04ad2d3f2]*/ |
1109 | 0 | { |
1110 | 0 | PyFunctionObject *newfunc; |
1111 | 0 | Py_ssize_t nclosure; |
1112 | |
|
1113 | 0 | if (name != Py_None && !PyUnicode_Check(name)) { |
1114 | 0 | PyErr_SetString(PyExc_TypeError, |
1115 | 0 | "arg 3 (name) must be None or string"); |
1116 | 0 | return NULL; |
1117 | 0 | } |
1118 | 0 | if (defaults != Py_None && !PyTuple_Check(defaults)) { |
1119 | 0 | PyErr_SetString(PyExc_TypeError, |
1120 | 0 | "arg 4 (defaults) must be None or tuple"); |
1121 | 0 | return NULL; |
1122 | 0 | } |
1123 | 0 | if (!PyTuple_Check(closure)) { |
1124 | 0 | if (code->co_nfreevars && closure == Py_None) { |
1125 | 0 | PyErr_SetString(PyExc_TypeError, |
1126 | 0 | "arg 5 (closure) must be tuple"); |
1127 | 0 | return NULL; |
1128 | 0 | } |
1129 | 0 | else if (closure != Py_None) { |
1130 | 0 | PyErr_SetString(PyExc_TypeError, |
1131 | 0 | "arg 5 (closure) must be None or tuple"); |
1132 | 0 | return NULL; |
1133 | 0 | } |
1134 | 0 | } |
1135 | 0 | if (kwdefaults != Py_None && !PyDict_Check(kwdefaults)) { |
1136 | 0 | PyErr_SetString(PyExc_TypeError, |
1137 | 0 | "arg 6 (kwdefaults) must be None or dict"); |
1138 | 0 | return NULL; |
1139 | 0 | } |
1140 | | |
1141 | | /* check that the closure is well-formed */ |
1142 | 0 | nclosure = closure == Py_None ? 0 : PyTuple_GET_SIZE(closure); |
1143 | 0 | if (code->co_nfreevars != nclosure) |
1144 | 0 | return PyErr_Format(PyExc_ValueError, |
1145 | 0 | "%U requires closure of length %d, not %zd", |
1146 | 0 | code->co_name, code->co_nfreevars, nclosure); |
1147 | 0 | if (nclosure) { |
1148 | 0 | Py_ssize_t i; |
1149 | 0 | for (i = 0; i < nclosure; i++) { |
1150 | 0 | PyObject *o = PyTuple_GET_ITEM(closure, i); |
1151 | 0 | if (!PyCell_Check(o)) { |
1152 | 0 | return PyErr_Format(PyExc_TypeError, |
1153 | 0 | "arg 5 (closure) expected cell, found %s", |
1154 | 0 | Py_TYPE(o)->tp_name); |
1155 | 0 | } |
1156 | 0 | } |
1157 | 0 | } |
1158 | 0 | if (PySys_Audit("function.__new__", "O", code) < 0) { |
1159 | 0 | return NULL; |
1160 | 0 | } |
1161 | | |
1162 | 0 | newfunc = (PyFunctionObject *)PyFunction_New((PyObject *)code, |
1163 | 0 | globals); |
1164 | 0 | if (newfunc == NULL) { |
1165 | 0 | return NULL; |
1166 | 0 | } |
1167 | 0 | if (name != Py_None) { |
1168 | 0 | Py_SETREF(newfunc->func_name, Py_NewRef(name)); |
1169 | 0 | } |
1170 | 0 | if (defaults != Py_None) { |
1171 | 0 | newfunc->func_defaults = Py_NewRef(defaults); |
1172 | 0 | } |
1173 | 0 | if (closure != Py_None) { |
1174 | 0 | newfunc->func_closure = Py_NewRef(closure); |
1175 | 0 | } |
1176 | 0 | if (kwdefaults != Py_None) { |
1177 | 0 | newfunc->func_kwdefaults = Py_NewRef(kwdefaults); |
1178 | 0 | } |
1179 | |
|
1180 | 0 | return (PyObject *)newfunc; |
1181 | 0 | } |
1182 | | |
1183 | | static int |
1184 | | func_clear(PyObject *self) |
1185 | 1.16M | { |
1186 | 1.16M | PyFunctionObject *op = _PyFunction_CAST(self); |
1187 | 0 | func_clear_version(_PyInterpreterState_GET(), op); |
1188 | 1.16M | PyObject *globals = op->func_globals; |
1189 | 1.16M | op->func_globals = NULL; |
1190 | 1.16M | if (globals != NULL) { |
1191 | 1.16M | _Py_DECREF_DICT(globals); |
1192 | 1.16M | } |
1193 | 1.16M | PyObject *builtins = op->func_builtins; |
1194 | 1.16M | op->func_builtins = NULL; |
1195 | 1.16M | if (builtins != NULL) { |
1196 | 1.16M | _Py_DECREF_BUILTINS(builtins); |
1197 | 1.16M | } |
1198 | 1.16M | Py_CLEAR(op->func_module); |
1199 | 1.16M | Py_CLEAR(op->func_defaults); |
1200 | 1.16M | Py_CLEAR(op->func_kwdefaults); |
1201 | 1.16M | Py_CLEAR(op->func_doc); |
1202 | 1.16M | Py_CLEAR(op->func_dict); |
1203 | 1.16M | Py_CLEAR(op->func_closure); |
1204 | 1.16M | Py_CLEAR(op->func_annotations); |
1205 | 1.16M | Py_CLEAR(op->func_annotate); |
1206 | 1.16M | Py_CLEAR(op->func_typeparams); |
1207 | | // Don't Py_CLEAR(op->func_code), since code is always required |
1208 | | // to be non-NULL. Similarly, name and qualname shouldn't be NULL. |
1209 | | // However, name and qualname could be str subclasses, so they |
1210 | | // could have reference cycles. The solution is to replace them |
1211 | | // with a genuinely immutable string. |
1212 | 1.16M | Py_SETREF(op->func_name, &_Py_STR(empty)); |
1213 | 1.16M | Py_SETREF(op->func_qualname, &_Py_STR(empty)); |
1214 | 1.16M | return 0; |
1215 | 1.16M | } |
1216 | | |
1217 | | static void |
1218 | | func_dealloc(PyObject *self) |
1219 | 1.16M | { |
1220 | 1.16M | PyFunctionObject *op = _PyFunction_CAST(self); |
1221 | 0 | _PyObject_ResurrectStart(self); |
1222 | 1.16M | handle_func_event(PyFunction_EVENT_DESTROY, op, NULL); |
1223 | 1.16M | if (_PyObject_ResurrectEnd(self)) { |
1224 | 0 | return; |
1225 | 0 | } |
1226 | 1.16M | _PyObject_GC_UNTRACK(op); |
1227 | 1.16M | FT_CLEAR_WEAKREFS(self, op->func_weakreflist); |
1228 | 1.16M | (void)func_clear((PyObject*)op); |
1229 | | // These aren't cleared by func_clear(). |
1230 | 1.16M | _Py_DECREF_CODE((PyCodeObject *)op->func_code); |
1231 | 1.16M | Py_DECREF(op->func_name); |
1232 | 1.16M | Py_DECREF(op->func_qualname); |
1233 | 1.16M | PyObject_GC_Del(op); |
1234 | 1.16M | } |
1235 | | |
1236 | | static PyObject* |
1237 | | func_repr(PyObject *self) |
1238 | 0 | { |
1239 | 0 | PyFunctionObject *op = _PyFunction_CAST(self); |
1240 | 0 | return PyUnicode_FromFormat("<function %U at %p>", |
1241 | 0 | op->func_qualname, op); |
1242 | 0 | } |
1243 | | |
1244 | | static int |
1245 | | func_traverse(PyObject *self, visitproc visit, void *arg) |
1246 | 563k | { |
1247 | 563k | PyFunctionObject *f = _PyFunction_CAST(self); |
1248 | 563k | Py_VISIT(f->func_code); |
1249 | 563k | Py_VISIT(f->func_globals); |
1250 | 563k | Py_VISIT(f->func_builtins); |
1251 | 563k | Py_VISIT(f->func_module); |
1252 | 563k | Py_VISIT(f->func_defaults); |
1253 | 563k | Py_VISIT(f->func_kwdefaults); |
1254 | 563k | Py_VISIT(f->func_doc); |
1255 | 563k | Py_VISIT(f->func_name); |
1256 | 563k | Py_VISIT(f->func_dict); |
1257 | 563k | Py_VISIT(f->func_closure); |
1258 | 563k | Py_VISIT(f->func_annotations); |
1259 | 563k | Py_VISIT(f->func_annotate); |
1260 | 563k | Py_VISIT(f->func_typeparams); |
1261 | 563k | Py_VISIT(f->func_qualname); |
1262 | 563k | return 0; |
1263 | 563k | } |
1264 | | |
1265 | | /* Bind a function to an object */ |
1266 | | static PyObject * |
1267 | | func_descr_get(PyObject *func, PyObject *obj, PyObject *type) |
1268 | 12.7M | { |
1269 | 12.7M | if (obj == Py_None || obj == NULL) { |
1270 | 1.62k | return Py_NewRef(func); |
1271 | 1.62k | } |
1272 | 12.7M | return PyMethod_New(func, obj); |
1273 | 12.7M | } |
1274 | | |
1275 | | PyTypeObject PyFunction_Type = { |
1276 | | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
1277 | | "function", |
1278 | | sizeof(PyFunctionObject), |
1279 | | 0, |
1280 | | func_dealloc, /* tp_dealloc */ |
1281 | | offsetof(PyFunctionObject, vectorcall), /* tp_vectorcall_offset */ |
1282 | | 0, /* tp_getattr */ |
1283 | | 0, /* tp_setattr */ |
1284 | | 0, /* tp_as_async */ |
1285 | | func_repr, /* tp_repr */ |
1286 | | 0, /* tp_as_number */ |
1287 | | 0, /* tp_as_sequence */ |
1288 | | 0, /* tp_as_mapping */ |
1289 | | 0, /* tp_hash */ |
1290 | | PyVectorcall_Call, /* tp_call */ |
1291 | | 0, /* tp_str */ |
1292 | | 0, /* tp_getattro */ |
1293 | | 0, /* tp_setattro */ |
1294 | | 0, /* tp_as_buffer */ |
1295 | | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
1296 | | Py_TPFLAGS_HAVE_VECTORCALL | |
1297 | | Py_TPFLAGS_METHOD_DESCRIPTOR, /* tp_flags */ |
1298 | | func_new__doc__, /* tp_doc */ |
1299 | | func_traverse, /* tp_traverse */ |
1300 | | func_clear, /* tp_clear */ |
1301 | | 0, /* tp_richcompare */ |
1302 | | offsetof(PyFunctionObject, func_weakreflist), /* tp_weaklistoffset */ |
1303 | | 0, /* tp_iter */ |
1304 | | 0, /* tp_iternext */ |
1305 | | 0, /* tp_methods */ |
1306 | | func_memberlist, /* tp_members */ |
1307 | | func_getsetlist, /* tp_getset */ |
1308 | | 0, /* tp_base */ |
1309 | | 0, /* tp_dict */ |
1310 | | func_descr_get, /* tp_descr_get */ |
1311 | | 0, /* tp_descr_set */ |
1312 | | offsetof(PyFunctionObject, func_dict), /* tp_dictoffset */ |
1313 | | 0, /* tp_init */ |
1314 | | 0, /* tp_alloc */ |
1315 | | func_new, /* tp_new */ |
1316 | | }; |
1317 | | |
1318 | | |
1319 | | int |
1320 | | _PyFunction_VerifyStateless(PyThreadState *tstate, PyObject *func) |
1321 | 0 | { |
1322 | 0 | assert(!PyErr_Occurred()); |
1323 | 0 | assert(PyFunction_Check(func)); |
1324 | | |
1325 | | // Check the globals. |
1326 | 0 | PyObject *globalsns = PyFunction_GET_GLOBALS(func); |
1327 | 0 | if (globalsns != NULL && !PyDict_Check(globalsns)) { |
1328 | 0 | _PyErr_Format(tstate, PyExc_TypeError, |
1329 | 0 | "unsupported globals %R", globalsns); |
1330 | 0 | return -1; |
1331 | 0 | } |
1332 | | // Check the builtins. |
1333 | 0 | PyObject *builtinsns = _PyFunction_GET_BUILTINS(func); |
1334 | 0 | if (builtinsns != NULL && !PyDict_Check(builtinsns)) { |
1335 | 0 | _PyErr_Format(tstate, PyExc_TypeError, |
1336 | 0 | "unsupported builtins %R", builtinsns); |
1337 | 0 | return -1; |
1338 | 0 | } |
1339 | | // Disallow __defaults__. |
1340 | 0 | PyObject *defaults = PyFunction_GET_DEFAULTS(func); |
1341 | 0 | if (defaults != NULL) { |
1342 | 0 | assert(PyTuple_Check(defaults)); // per PyFunction_New() |
1343 | 0 | if (PyTuple_GET_SIZE(defaults) > 0) { |
1344 | 0 | _PyErr_SetString(tstate, PyExc_ValueError, |
1345 | 0 | "defaults not supported"); |
1346 | 0 | return -1; |
1347 | 0 | } |
1348 | 0 | } |
1349 | | // Disallow __kwdefaults__. |
1350 | 0 | PyObject *kwdefaults = PyFunction_GET_KW_DEFAULTS(func); |
1351 | 0 | if (kwdefaults != NULL) { |
1352 | 0 | assert(PyDict_Check(kwdefaults)); // per PyFunction_New() |
1353 | 0 | if (PyDict_Size(kwdefaults) > 0) { |
1354 | 0 | _PyErr_SetString(tstate, PyExc_ValueError, |
1355 | 0 | "keyword defaults not supported"); |
1356 | 0 | return -1; |
1357 | 0 | } |
1358 | 0 | } |
1359 | | // Disallow __closure__. |
1360 | 0 | PyObject *closure = PyFunction_GET_CLOSURE(func); |
1361 | 0 | if (closure != NULL) { |
1362 | 0 | assert(PyTuple_Check(closure)); // per PyFunction_New() |
1363 | 0 | if (PyTuple_GET_SIZE(closure) > 0) { |
1364 | 0 | _PyErr_SetString(tstate, PyExc_ValueError, "closures not supported"); |
1365 | 0 | return -1; |
1366 | 0 | } |
1367 | 0 | } |
1368 | | // Check the code. |
1369 | 0 | PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func); |
1370 | 0 | if (_PyCode_VerifyStateless(tstate, co, NULL, globalsns, builtinsns) < 0) { |
1371 | 0 | return -1; |
1372 | 0 | } |
1373 | 0 | return 0; |
1374 | 0 | } |
1375 | | |
1376 | | |
1377 | | static int |
1378 | | functools_copy_attr(PyObject *wrapper, PyObject *wrapped, PyObject *name) |
1379 | 6.06k | { |
1380 | 6.06k | PyObject *value; |
1381 | 6.06k | int res = PyObject_GetOptionalAttr(wrapped, name, &value); |
1382 | 6.06k | if (value != NULL) { |
1383 | 6.06k | res = PyObject_SetAttr(wrapper, name, value); |
1384 | 6.06k | Py_DECREF(value); |
1385 | 6.06k | } |
1386 | 6.06k | return res; |
1387 | 6.06k | } |
1388 | | |
1389 | | // Similar to functools.wraps(wrapper, wrapped) |
1390 | | static int |
1391 | | functools_wraps(PyObject *wrapper, PyObject *wrapped) |
1392 | 1.51k | { |
1393 | 1.51k | #define COPY_ATTR(ATTR) \ |
1394 | 6.06k | do { \ |
1395 | 6.06k | if (functools_copy_attr(wrapper, wrapped, &_Py_ID(ATTR)) < 0) { \ |
1396 | 0 | return -1; \ |
1397 | 0 | } \ |
1398 | 6.06k | } while (0) \ |
1399 | 1.51k | |
1400 | 1.51k | COPY_ATTR(__module__); |
1401 | 1.51k | COPY_ATTR(__name__); |
1402 | 1.51k | COPY_ATTR(__qualname__); |
1403 | 1.51k | COPY_ATTR(__doc__); |
1404 | 1.51k | return 0; |
1405 | | |
1406 | 1.51k | #undef COPY_ATTR |
1407 | 1.51k | } |
1408 | | |
1409 | | // Used for wrapping __annotations__ and __annotate__ on classmethod |
1410 | | // and staticmethod objects. |
1411 | | static PyObject * |
1412 | | descriptor_get_wrapped_attribute(PyObject *wrapped, PyObject *obj, PyObject *name) |
1413 | 0 | { |
1414 | 0 | PyObject *dict = PyObject_GenericGetDict(obj, NULL); |
1415 | 0 | if (dict == NULL) { |
1416 | 0 | return NULL; |
1417 | 0 | } |
1418 | 0 | PyObject *res; |
1419 | 0 | if (PyDict_GetItemRef(dict, name, &res) < 0) { |
1420 | 0 | Py_DECREF(dict); |
1421 | 0 | return NULL; |
1422 | 0 | } |
1423 | 0 | if (res != NULL) { |
1424 | 0 | Py_DECREF(dict); |
1425 | 0 | return res; |
1426 | 0 | } |
1427 | 0 | res = PyObject_GetAttr(wrapped, name); |
1428 | 0 | if (res == NULL) { |
1429 | 0 | Py_DECREF(dict); |
1430 | 0 | return NULL; |
1431 | 0 | } |
1432 | 0 | if (PyDict_SetItem(dict, name, res) < 0) { |
1433 | 0 | Py_DECREF(dict); |
1434 | 0 | Py_DECREF(res); |
1435 | 0 | return NULL; |
1436 | 0 | } |
1437 | 0 | Py_DECREF(dict); |
1438 | 0 | return res; |
1439 | 0 | } |
1440 | | |
1441 | | static int |
1442 | | descriptor_set_wrapped_attribute(PyObject *oobj, PyObject *name, PyObject *value, |
1443 | | char *type_name) |
1444 | 0 | { |
1445 | 0 | PyObject *dict = PyObject_GenericGetDict(oobj, NULL); |
1446 | 0 | if (dict == NULL) { |
1447 | 0 | return -1; |
1448 | 0 | } |
1449 | 0 | if (value == NULL) { |
1450 | 0 | if (PyDict_DelItem(dict, name) < 0) { |
1451 | 0 | if (PyErr_ExceptionMatches(PyExc_KeyError)) { |
1452 | 0 | PyErr_Clear(); |
1453 | 0 | PyErr_Format(PyExc_AttributeError, |
1454 | 0 | "'%.200s' object has no attribute '%U'", |
1455 | 0 | type_name, name); |
1456 | 0 | Py_DECREF(dict); |
1457 | 0 | return -1; |
1458 | 0 | } |
1459 | 0 | else { |
1460 | 0 | Py_DECREF(dict); |
1461 | 0 | return -1; |
1462 | 0 | } |
1463 | 0 | } |
1464 | 0 | Py_DECREF(dict); |
1465 | 0 | return 0; |
1466 | 0 | } |
1467 | 0 | else { |
1468 | 0 | Py_DECREF(dict); |
1469 | 0 | return PyDict_SetItem(dict, name, value); |
1470 | 0 | } |
1471 | 0 | } |
1472 | | |
1473 | | |
1474 | | /* Class method object */ |
1475 | | |
1476 | | /* A class method receives the class as implicit first argument, |
1477 | | just like an instance method receives the instance. |
1478 | | To declare a class method, use this idiom: |
1479 | | |
1480 | | class C: |
1481 | | @classmethod |
1482 | | def f(cls, arg1, arg2, argN): |
1483 | | ... |
1484 | | |
1485 | | It can be called either on the class (e.g. C.f()) or on an instance |
1486 | | (e.g. C().f()); the instance is ignored except for its class. |
1487 | | If a class method is called for a derived class, the derived class |
1488 | | object is passed as the implied first argument. |
1489 | | |
1490 | | Class methods are different than C++ or Java static methods. |
1491 | | If you want those, see static methods below. |
1492 | | */ |
1493 | | |
1494 | | typedef struct { |
1495 | | PyObject_HEAD |
1496 | | PyObject *cm_callable; |
1497 | | PyObject *cm_dict; |
1498 | | } classmethod; |
1499 | | |
1500 | | #define _PyClassMethod_CAST(cm) \ |
1501 | 24.1k | (assert(PyObject_TypeCheck((cm), &PyClassMethod_Type)), \ |
1502 | 24.1k | _Py_CAST(classmethod*, cm)) |
1503 | | |
1504 | | static void |
1505 | | cm_dealloc(PyObject *self) |
1506 | 15 | { |
1507 | 15 | classmethod *cm = _PyClassMethod_CAST(self); |
1508 | 15 | _PyObject_GC_UNTRACK((PyObject *)cm); |
1509 | 15 | Py_XDECREF(cm->cm_callable); |
1510 | 15 | Py_XDECREF(cm->cm_dict); |
1511 | 15 | Py_TYPE(cm)->tp_free((PyObject *)cm); |
1512 | 15 | } |
1513 | | |
1514 | | static int |
1515 | | cm_traverse(PyObject *self, visitproc visit, void *arg) |
1516 | 23.5k | { |
1517 | 23.5k | classmethod *cm = _PyClassMethod_CAST(self); |
1518 | 23.5k | Py_VISIT(cm->cm_callable); |
1519 | 23.5k | Py_VISIT(cm->cm_dict); |
1520 | 23.5k | return 0; |
1521 | 23.5k | } |
1522 | | |
1523 | | static int |
1524 | | cm_clear(PyObject *self) |
1525 | 10 | { |
1526 | 10 | classmethod *cm = _PyClassMethod_CAST(self); |
1527 | 10 | Py_CLEAR(cm->cm_callable); |
1528 | 10 | Py_CLEAR(cm->cm_dict); |
1529 | 10 | return 0; |
1530 | 10 | } |
1531 | | |
1532 | | |
1533 | | static PyObject * |
1534 | | cm_descr_get(PyObject *self, PyObject *obj, PyObject *type) |
1535 | 5.64k | { |
1536 | 5.64k | classmethod *cm = (classmethod *)self; |
1537 | 5.64k | if (type == NULL) |
1538 | 0 | type = (PyObject *)(Py_TYPE(obj)); |
1539 | 5.64k | return PyMethod_New(cm->cm_callable, type); |
1540 | 5.64k | } |
1541 | | |
1542 | | static int |
1543 | | cm_set_callable(classmethod *cm, PyObject *callable) |
1544 | 2.09k | { |
1545 | 2.09k | assert(callable != NULL); |
1546 | 2.09k | if (cm->cm_callable == callable) { |
1547 | | // cm_init() sets the same callable than cm_new() |
1548 | 1.04k | return 0; |
1549 | 1.04k | } |
1550 | | |
1551 | 1.05k | Py_XSETREF(cm->cm_callable, Py_NewRef(callable)); |
1552 | 1.05k | return functools_wraps((PyObject *)cm, cm->cm_callable); |
1553 | 2.09k | } |
1554 | | |
1555 | | static PyObject * |
1556 | | cm_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
1557 | 1.04k | { |
1558 | 1.04k | if (!_PyArg_NoKeywords("classmethod", kwds)) { |
1559 | 0 | return NULL; |
1560 | 0 | } |
1561 | 1.04k | PyObject *callable; // borrowed ref |
1562 | 1.04k | if (!PyArg_UnpackTuple(args, "classmethod", 1, 1, &callable)) { |
1563 | 0 | return NULL; |
1564 | 0 | } |
1565 | | |
1566 | 1.04k | classmethod *cm = (classmethod *)PyType_GenericAlloc(type, 0); |
1567 | 1.04k | if (cm == NULL) { |
1568 | 0 | return NULL; |
1569 | 0 | } |
1570 | 1.04k | _PyObject_SetDeferredRefcount((PyObject *)cm); |
1571 | 1.04k | if (cm_set_callable(cm, callable) < 0) { |
1572 | 0 | Py_DECREF(cm); |
1573 | 0 | return NULL; |
1574 | 0 | } |
1575 | 1.04k | return (PyObject *)cm; |
1576 | 1.04k | } |
1577 | | |
1578 | | static int |
1579 | | cm_init(PyObject *self, PyObject *args, PyObject *kwds) |
1580 | 1.04k | { |
1581 | 1.04k | if (!_PyArg_NoKeywords("classmethod", kwds)) { |
1582 | 0 | return -1; |
1583 | 0 | } |
1584 | 1.04k | PyObject *callable; // borrowed ref |
1585 | 1.04k | if (!PyArg_UnpackTuple(args, "classmethod", 1, 1, &callable)) { |
1586 | 0 | return -1; |
1587 | 0 | } |
1588 | | |
1589 | 1.04k | classmethod *cm = (classmethod *)self; |
1590 | 1.04k | return cm_set_callable(cm, callable); |
1591 | 1.04k | } |
1592 | | |
1593 | | static PyMemberDef cm_memberlist[] = { |
1594 | | {"__func__", _Py_T_OBJECT, offsetof(classmethod, cm_callable), Py_READONLY}, |
1595 | | {"__wrapped__", _Py_T_OBJECT, offsetof(classmethod, cm_callable), Py_READONLY}, |
1596 | | {NULL} /* Sentinel */ |
1597 | | }; |
1598 | | |
1599 | | static PyObject * |
1600 | | cm_get___isabstractmethod__(PyObject *self, void *closure) |
1601 | 574 | { |
1602 | 574 | classmethod *cm = _PyClassMethod_CAST(self); |
1603 | 0 | int res = _PyObject_IsAbstract(cm->cm_callable); |
1604 | 574 | if (res == -1) { |
1605 | 0 | return NULL; |
1606 | 0 | } |
1607 | 574 | else if (res) { |
1608 | 0 | Py_RETURN_TRUE; |
1609 | 0 | } |
1610 | 574 | Py_RETURN_FALSE; |
1611 | 574 | } |
1612 | | |
1613 | | static PyObject * |
1614 | | cm_get___annotations__(PyObject *self, void *closure) |
1615 | 0 | { |
1616 | 0 | classmethod *cm = _PyClassMethod_CAST(self); |
1617 | 0 | return descriptor_get_wrapped_attribute(cm->cm_callable, self, &_Py_ID(__annotations__)); |
1618 | 0 | } |
1619 | | |
1620 | | static int |
1621 | | cm_set___annotations__(PyObject *self, PyObject *value, void *closure) |
1622 | 0 | { |
1623 | 0 | return descriptor_set_wrapped_attribute(self, &_Py_ID(__annotations__), value, "classmethod"); |
1624 | 0 | } |
1625 | | |
1626 | | static PyObject * |
1627 | | cm_get___annotate__(PyObject *self, void *closure) |
1628 | 0 | { |
1629 | 0 | classmethod *cm = _PyClassMethod_CAST(self); |
1630 | 0 | return descriptor_get_wrapped_attribute(cm->cm_callable, self, &_Py_ID(__annotate__)); |
1631 | 0 | } |
1632 | | |
1633 | | static int |
1634 | | cm_set___annotate__(PyObject *self, PyObject *value, void *closure) |
1635 | 0 | { |
1636 | 0 | return descriptor_set_wrapped_attribute(self, &_Py_ID(__annotate__), value, "classmethod"); |
1637 | 0 | } |
1638 | | |
1639 | | |
1640 | | static PyGetSetDef cm_getsetlist[] = { |
1641 | | {"__isabstractmethod__", cm_get___isabstractmethod__, NULL, NULL, NULL}, |
1642 | | {"__dict__", PyObject_GenericGetDict, PyObject_GenericSetDict, NULL, NULL}, |
1643 | | {"__annotations__", cm_get___annotations__, cm_set___annotations__, NULL, NULL}, |
1644 | | {"__annotate__", cm_get___annotate__, cm_set___annotate__, NULL, NULL}, |
1645 | | {NULL} /* Sentinel */ |
1646 | | }; |
1647 | | |
1648 | | static PyMethodDef cm_methodlist[] = { |
1649 | | {"__class_getitem__", Py_GenericAlias, METH_O|METH_CLASS, NULL}, |
1650 | | {NULL} /* Sentinel */ |
1651 | | }; |
1652 | | |
1653 | | static PyObject* |
1654 | | cm_repr(PyObject *self) |
1655 | 0 | { |
1656 | 0 | classmethod *cm = _PyClassMethod_CAST(self); |
1657 | 0 | return PyUnicode_FromFormat("<classmethod(%R)>", cm->cm_callable); |
1658 | 0 | } |
1659 | | |
1660 | | PyDoc_STRVAR(classmethod_doc, |
1661 | | "classmethod(function, /)\n\ |
1662 | | --\n\ |
1663 | | \n\ |
1664 | | Convert a function to be a class method.\n\ |
1665 | | \n\ |
1666 | | A class method receives the class as implicit first argument,\n\ |
1667 | | just like an instance method receives the instance.\n\ |
1668 | | To declare a class method, use this idiom:\n\ |
1669 | | \n\ |
1670 | | class C:\n\ |
1671 | | @classmethod\n\ |
1672 | | def f(cls, arg1, arg2, argN):\n\ |
1673 | | ...\n\ |
1674 | | \n\ |
1675 | | It can be called either on the class (e.g. C.f()) or on an instance\n\ |
1676 | | (e.g. C().f()). The instance is ignored except for its class.\n\ |
1677 | | If a class method is called for a derived class, the derived class\n\ |
1678 | | object is passed as the implied first argument.\n\ |
1679 | | \n\ |
1680 | | Class methods are different than C++ or Java static methods.\n\ |
1681 | | If you want those, see the staticmethod builtin."); |
1682 | | |
1683 | | PyTypeObject PyClassMethod_Type = { |
1684 | | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
1685 | | "classmethod", |
1686 | | sizeof(classmethod), |
1687 | | 0, |
1688 | | cm_dealloc, /* tp_dealloc */ |
1689 | | 0, /* tp_vectorcall_offset */ |
1690 | | 0, /* tp_getattr */ |
1691 | | 0, /* tp_setattr */ |
1692 | | 0, /* tp_as_async */ |
1693 | | cm_repr, /* tp_repr */ |
1694 | | 0, /* tp_as_number */ |
1695 | | 0, /* tp_as_sequence */ |
1696 | | 0, /* tp_as_mapping */ |
1697 | | 0, /* tp_hash */ |
1698 | | 0, /* tp_call */ |
1699 | | 0, /* tp_str */ |
1700 | | 0, /* tp_getattro */ |
1701 | | 0, /* tp_setattro */ |
1702 | | 0, /* tp_as_buffer */ |
1703 | | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, |
1704 | | classmethod_doc, /* tp_doc */ |
1705 | | cm_traverse, /* tp_traverse */ |
1706 | | cm_clear, /* tp_clear */ |
1707 | | 0, /* tp_richcompare */ |
1708 | | 0, /* tp_weaklistoffset */ |
1709 | | 0, /* tp_iter */ |
1710 | | 0, /* tp_iternext */ |
1711 | | cm_methodlist, /* tp_methods */ |
1712 | | cm_memberlist, /* tp_members */ |
1713 | | cm_getsetlist, /* tp_getset */ |
1714 | | 0, /* tp_base */ |
1715 | | 0, /* tp_dict */ |
1716 | | cm_descr_get, /* tp_descr_get */ |
1717 | | 0, /* tp_descr_set */ |
1718 | | offsetof(classmethod, cm_dict), /* tp_dictoffset */ |
1719 | | cm_init, /* tp_init */ |
1720 | | PyType_GenericAlloc, /* tp_alloc */ |
1721 | | cm_new, /* tp_new */ |
1722 | | PyObject_GC_Del, /* tp_free */ |
1723 | | }; |
1724 | | |
1725 | | PyObject * |
1726 | | PyClassMethod_New(PyObject *callable) |
1727 | 6 | { |
1728 | 6 | classmethod *cm = (classmethod *) |
1729 | 6 | PyType_GenericAlloc(&PyClassMethod_Type, 0); |
1730 | 6 | if (cm == NULL) { |
1731 | 0 | return NULL; |
1732 | 0 | } |
1733 | 6 | if (cm_set_callable(cm, callable) < 0) { |
1734 | 0 | Py_DECREF(cm); |
1735 | 0 | return NULL; |
1736 | 0 | } |
1737 | 6 | return (PyObject *)cm; |
1738 | 6 | } |
1739 | | |
1740 | | |
1741 | | /* Static method object */ |
1742 | | |
1743 | | /* A static method does not receive an implicit first argument. |
1744 | | To declare a static method, use this idiom: |
1745 | | |
1746 | | class C: |
1747 | | @staticmethod |
1748 | | def f(arg1, arg2, argN): |
1749 | | ... |
1750 | | |
1751 | | It can be called either on the class (e.g. C.f()) or on an instance |
1752 | | (e.g. C().f()). Both the class and the instance are ignored, and |
1753 | | neither is passed implicitly as the first argument to the method. |
1754 | | |
1755 | | Static methods in Python are similar to those found in Java or C++. |
1756 | | For a more advanced concept, see class methods above. |
1757 | | */ |
1758 | | |
1759 | | typedef struct { |
1760 | | PyObject_HEAD |
1761 | | PyObject *sm_callable; |
1762 | | PyObject *sm_dict; |
1763 | | } staticmethod; |
1764 | | |
1765 | | #define _PyStaticMethod_CAST(cm) \ |
1766 | 17.5k | (assert(PyObject_TypeCheck((cm), &PyStaticMethod_Type)), \ |
1767 | 17.5k | _Py_CAST(staticmethod*, cm)) |
1768 | | |
1769 | | static void |
1770 | | sm_dealloc(PyObject *self) |
1771 | 21 | { |
1772 | 21 | staticmethod *sm = _PyStaticMethod_CAST(self); |
1773 | 21 | _PyObject_GC_UNTRACK((PyObject *)sm); |
1774 | 21 | Py_XDECREF(sm->sm_callable); |
1775 | 21 | Py_XDECREF(sm->sm_dict); |
1776 | 21 | Py_TYPE(sm)->tp_free((PyObject *)sm); |
1777 | 21 | } |
1778 | | |
1779 | | static int |
1780 | | sm_traverse(PyObject *self, visitproc visit, void *arg) |
1781 | 11.4k | { |
1782 | 11.4k | staticmethod *sm = _PyStaticMethod_CAST(self); |
1783 | 11.4k | Py_VISIT(sm->sm_callable); |
1784 | 11.4k | Py_VISIT(sm->sm_dict); |
1785 | 11.4k | return 0; |
1786 | 11.4k | } |
1787 | | |
1788 | | static int |
1789 | | sm_clear(PyObject *self) |
1790 | 0 | { |
1791 | 0 | staticmethod *sm = _PyStaticMethod_CAST(self); |
1792 | 0 | Py_CLEAR(sm->sm_callable); |
1793 | 0 | Py_CLEAR(sm->sm_dict); |
1794 | 0 | return 0; |
1795 | 0 | } |
1796 | | |
1797 | | static PyObject * |
1798 | | sm_descr_get(PyObject *self, PyObject *obj, PyObject *type) |
1799 | 1.26k | { |
1800 | 1.26k | staticmethod *sm = (staticmethod *)self; |
1801 | 1.26k | return Py_NewRef(sm->sm_callable); |
1802 | 1.26k | } |
1803 | | |
1804 | | static int |
1805 | | sm_set_callable(staticmethod *sm, PyObject *callable) |
1806 | 697 | { |
1807 | 697 | assert(callable != NULL); |
1808 | 697 | if (sm->sm_callable == callable) { |
1809 | | // sm_init() sets the same callable than sm_new() |
1810 | 231 | return 0; |
1811 | 231 | } |
1812 | | |
1813 | 466 | Py_XSETREF(sm->sm_callable, Py_NewRef(callable)); |
1814 | 466 | return functools_wraps((PyObject *)sm, sm->sm_callable); |
1815 | 697 | } |
1816 | | |
1817 | | static PyObject * |
1818 | | sm_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
1819 | 231 | { |
1820 | 231 | if (!_PyArg_NoKeywords("staticmethod", kwds)) { |
1821 | 0 | return NULL; |
1822 | 0 | } |
1823 | 231 | PyObject *callable; // borrowed ref |
1824 | 231 | if (!PyArg_UnpackTuple(args, "staticmethod", 1, 1, &callable)) { |
1825 | 0 | return NULL; |
1826 | 0 | } |
1827 | | |
1828 | 231 | staticmethod *sm = (staticmethod *)PyType_GenericAlloc(type, 0); |
1829 | 231 | if (sm == NULL) { |
1830 | 0 | return NULL; |
1831 | 0 | } |
1832 | 231 | _PyObject_SetDeferredRefcount((PyObject *)sm); |
1833 | 231 | if (sm_set_callable(sm, callable) < 0) { |
1834 | 0 | Py_DECREF(sm); |
1835 | 0 | return NULL; |
1836 | 0 | } |
1837 | 231 | return (PyObject *)sm; |
1838 | 231 | } |
1839 | | |
1840 | | static int |
1841 | | sm_init(PyObject *self, PyObject *args, PyObject *kwds) |
1842 | 231 | { |
1843 | 231 | if (!_PyArg_NoKeywords("staticmethod", kwds)) { |
1844 | 0 | return -1; |
1845 | 0 | } |
1846 | 231 | PyObject *callable; // borrowed ref |
1847 | 231 | if (!PyArg_UnpackTuple(args, "staticmethod", 1, 1, &callable)) { |
1848 | 0 | return -1; |
1849 | 0 | } |
1850 | | |
1851 | 231 | staticmethod *sm = (staticmethod *)self; |
1852 | 231 | return sm_set_callable(sm, callable); |
1853 | 231 | } |
1854 | | |
1855 | | static PyObject* |
1856 | | sm_call(PyObject *callable, PyObject *args, PyObject *kwargs) |
1857 | 0 | { |
1858 | 0 | staticmethod *sm = (staticmethod *)callable; |
1859 | 0 | return PyObject_Call(sm->sm_callable, args, kwargs); |
1860 | 0 | } |
1861 | | |
1862 | | static PyMemberDef sm_memberlist[] = { |
1863 | | {"__func__", _Py_T_OBJECT, offsetof(staticmethod, sm_callable), Py_READONLY}, |
1864 | | {"__wrapped__", _Py_T_OBJECT, offsetof(staticmethod, sm_callable), Py_READONLY}, |
1865 | | {NULL} /* Sentinel */ |
1866 | | }; |
1867 | | |
1868 | | static PyObject * |
1869 | | sm_get___isabstractmethod__(PyObject *self, void *closure) |
1870 | 0 | { |
1871 | 0 | staticmethod *sm = _PyStaticMethod_CAST(self); |
1872 | 0 | int res = _PyObject_IsAbstract(sm->sm_callable); |
1873 | 0 | if (res == -1) { |
1874 | 0 | return NULL; |
1875 | 0 | } |
1876 | 0 | else if (res) { |
1877 | 0 | Py_RETURN_TRUE; |
1878 | 0 | } |
1879 | 0 | Py_RETURN_FALSE; |
1880 | 0 | } |
1881 | | |
1882 | | static PyObject * |
1883 | | sm_get___annotations__(PyObject *self, void *closure) |
1884 | 0 | { |
1885 | 0 | staticmethod *sm = _PyStaticMethod_CAST(self); |
1886 | 0 | return descriptor_get_wrapped_attribute(sm->sm_callable, self, &_Py_ID(__annotations__)); |
1887 | 0 | } |
1888 | | |
1889 | | static int |
1890 | | sm_set___annotations__(PyObject *self, PyObject *value, void *closure) |
1891 | 0 | { |
1892 | 0 | return descriptor_set_wrapped_attribute(self, &_Py_ID(__annotations__), value, "staticmethod"); |
1893 | 0 | } |
1894 | | |
1895 | | static PyObject * |
1896 | | sm_get___annotate__(PyObject *self, void *closure) |
1897 | 0 | { |
1898 | 0 | staticmethod *sm = _PyStaticMethod_CAST(self); |
1899 | 0 | return descriptor_get_wrapped_attribute(sm->sm_callable, self, &_Py_ID(__annotate__)); |
1900 | 0 | } |
1901 | | |
1902 | | static int |
1903 | | sm_set___annotate__(PyObject *self, PyObject *value, void *closure) |
1904 | 0 | { |
1905 | 0 | return descriptor_set_wrapped_attribute(self, &_Py_ID(__annotate__), value, "staticmethod"); |
1906 | 0 | } |
1907 | | |
1908 | | static PyGetSetDef sm_getsetlist[] = { |
1909 | | {"__isabstractmethod__", sm_get___isabstractmethod__, NULL, NULL, NULL}, |
1910 | | {"__dict__", PyObject_GenericGetDict, PyObject_GenericSetDict, NULL, NULL}, |
1911 | | {"__annotations__", sm_get___annotations__, sm_set___annotations__, NULL, NULL}, |
1912 | | {"__annotate__", sm_get___annotate__, sm_set___annotate__, NULL, NULL}, |
1913 | | {NULL} /* Sentinel */ |
1914 | | }; |
1915 | | |
1916 | | static PyMethodDef sm_methodlist[] = { |
1917 | | {"__class_getitem__", Py_GenericAlias, METH_O|METH_CLASS, NULL}, |
1918 | | {NULL} /* Sentinel */ |
1919 | | }; |
1920 | | |
1921 | | static PyObject* |
1922 | | sm_repr(PyObject *self) |
1923 | 0 | { |
1924 | 0 | staticmethod *sm = _PyStaticMethod_CAST(self); |
1925 | 0 | return PyUnicode_FromFormat("<staticmethod(%R)>", sm->sm_callable); |
1926 | 0 | } |
1927 | | |
1928 | | PyDoc_STRVAR(staticmethod_doc, |
1929 | | "staticmethod(function, /)\n\ |
1930 | | --\n\ |
1931 | | \n\ |
1932 | | Convert a function to be a static method.\n\ |
1933 | | \n\ |
1934 | | A static method does not receive an implicit first argument.\n\ |
1935 | | To declare a static method, use this idiom:\n\ |
1936 | | \n\ |
1937 | | class C:\n\ |
1938 | | @staticmethod\n\ |
1939 | | def f(arg1, arg2, argN):\n\ |
1940 | | ...\n\ |
1941 | | \n\ |
1942 | | It can be called either on the class (e.g. C.f()) or on an instance\n\ |
1943 | | (e.g. C().f()). Both the class and the instance are ignored, and\n\ |
1944 | | neither is passed implicitly as the first argument to the method.\n\ |
1945 | | \n\ |
1946 | | Static methods in Python are similar to those found in Java or C++.\n\ |
1947 | | For a more advanced concept, see the classmethod builtin."); |
1948 | | |
1949 | | PyTypeObject PyStaticMethod_Type = { |
1950 | | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
1951 | | "staticmethod", |
1952 | | sizeof(staticmethod), |
1953 | | 0, |
1954 | | sm_dealloc, /* tp_dealloc */ |
1955 | | 0, /* tp_vectorcall_offset */ |
1956 | | 0, /* tp_getattr */ |
1957 | | 0, /* tp_setattr */ |
1958 | | 0, /* tp_as_async */ |
1959 | | sm_repr, /* tp_repr */ |
1960 | | 0, /* tp_as_number */ |
1961 | | 0, /* tp_as_sequence */ |
1962 | | 0, /* tp_as_mapping */ |
1963 | | 0, /* tp_hash */ |
1964 | | sm_call, /* tp_call */ |
1965 | | 0, /* tp_str */ |
1966 | | 0, /* tp_getattro */ |
1967 | | 0, /* tp_setattro */ |
1968 | | 0, /* tp_as_buffer */ |
1969 | | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, |
1970 | | staticmethod_doc, /* tp_doc */ |
1971 | | sm_traverse, /* tp_traverse */ |
1972 | | sm_clear, /* tp_clear */ |
1973 | | 0, /* tp_richcompare */ |
1974 | | 0, /* tp_weaklistoffset */ |
1975 | | 0, /* tp_iter */ |
1976 | | 0, /* tp_iternext */ |
1977 | | sm_methodlist, /* tp_methods */ |
1978 | | sm_memberlist, /* tp_members */ |
1979 | | sm_getsetlist, /* tp_getset */ |
1980 | | 0, /* tp_base */ |
1981 | | 0, /* tp_dict */ |
1982 | | sm_descr_get, /* tp_descr_get */ |
1983 | | 0, /* tp_descr_set */ |
1984 | | offsetof(staticmethod, sm_dict), /* tp_dictoffset */ |
1985 | | sm_init, /* tp_init */ |
1986 | | PyType_GenericAlloc, /* tp_alloc */ |
1987 | | sm_new, /* tp_new */ |
1988 | | PyObject_GC_Del, /* tp_free */ |
1989 | | }; |
1990 | | |
1991 | | PyObject * |
1992 | | PyStaticMethod_New(PyObject *callable) |
1993 | 235 | { |
1994 | 235 | staticmethod *sm = (staticmethod *) |
1995 | 235 | PyType_GenericAlloc(&PyStaticMethod_Type, 0); |
1996 | 235 | if (sm == NULL) { |
1997 | 0 | return NULL; |
1998 | 0 | } |
1999 | 235 | _PyObject_SetDeferredRefcount((PyObject *)sm); |
2000 | 235 | if (sm_set_callable(sm, callable) < 0) { |
2001 | 0 | Py_DECREF(sm); |
2002 | 0 | return NULL; |
2003 | 0 | } |
2004 | 235 | return (PyObject *)sm; |
2005 | 235 | } |
2006 | | |
2007 | | PyObject * |
2008 | | _PyClassMethod_GetFunc(PyObject *self) |
2009 | 0 | { |
2010 | 0 | classmethod *cm = _PyClassMethod_CAST(self); |
2011 | 0 | return cm->cm_callable; |
2012 | 0 | } |
2013 | | |
2014 | | PyObject * |
2015 | | _PyStaticMethod_GetFunc(PyObject *self) |
2016 | 6.12k | { |
2017 | 6.12k | staticmethod *sm = _PyStaticMethod_CAST(self); |
2018 | 0 | return sm->sm_callable; |
2019 | 6.12k | } |