.. versionadded:: 3.14
+.. data:: CO_METHOD
+
+ The flag is set when the code object is a function defined in class
+ scope.
+
+ .. versionadded:: 3.14
+
.. note::
The flags are specific to CPython, and may not be defined in other
Python implementations. Furthermore, the flags are an implementation
*/
#define CO_HAS_DOCSTRING 0x4000000
+/* A function defined in class scope */
+#define CO_METHOD 0x8000000
+
/* This should be defined if a future statement modifies the syntax.
For example, when a keyword is added.
*/
unsigned ste_can_see_class_scope : 1; /* true if this block can see names bound in an
enclosing class scope */
unsigned ste_has_docstring : 1; /* true if docstring present */
+ unsigned ste_method : 1; /* true if block is a function block defined in class scope */
int ste_comp_iter_expr; /* non-zero if visiting a comprehension range expression */
_Py_SourceLocation ste_loc; /* source location of block */
struct _symtable_entry *ste_annotation_block; /* symbol table entry for this entry's annotations */
256: "ITERABLE_COROUTINE",
512: "ASYNC_GENERATOR",
0x4000000: "HAS_DOCSTRING",
+ 0x8000000: "METHOD",
}
def pretty_flags(flags):
"CO_VARARGS",
"CO_VARKEYWORDS",
"CO_HAS_DOCSTRING",
+ "CO_METHOD",
"ClassFoundException",
"ClosureVars",
"EndOfBlock",
def __call__(self, code, offset, val):
self.events.append(("return", code.co_name, val))
-# gh-127274: CALL_ALLOC_AND_ENTER_INIT will only cache __init__ methods that
-# are deferred. We only defer functions defined at the top-level.
-class ValueErrorRaiser:
- def __init__(self):
- raise ValueError()
-
class ExceptionMonitoringTest(CheckEvents):
@requires_specialization_ft
def test_no_unwind_for_shim_frame(self):
+ class ValueErrorRaiser:
+ def __init__(self):
+ raise ValueError()
def f():
try:
self.assertFalse(f())
-# gh-127274: CALL_ALLOC_AND_ENTER_INIT will only cache __init__ methods that
-# are deferred. We only defer functions defined at the top-level.
-class MyClass:
- def __init__(self):
- pass
-
-
class InitTakesArg:
def __init__(self, arg):
self.arg = arg
@disabling_optimizer
@requires_specialization_ft
def test_assign_init_code(self):
+ class MyClass:
+ def __init__(self):
+ pass
+
def instantiate():
return MyClass()
--- /dev/null
+Add a new flag, ``CO_METHOD``, to :attr:`~codeobject.co_flags` that
+indicates whether the code object belongs to a function defined in class
+scope.
op->func_typeparams = NULL;
op->vectorcall = _PyFunction_Vectorcall;
op->func_version = FUNC_VERSION_UNSET;
- if ((code_obj->co_flags & CO_NESTED) == 0) {
+ if (((code_obj->co_flags & CO_NESTED) == 0) ||
+ (code_obj->co_flags & CO_METHOD)) {
// 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.
+ //
+ // Nested methods (functions defined in class scope) are also deferred,
+ // since they will likely be cleaned up by GC anyway.
_PyObject_SetDeferredRefcount((PyObject *)op);
}
_PyObject_GC_TRACK(op);
flags |= CO_VARKEYWORDS;
if (ste->ste_has_docstring)
flags |= CO_HAS_DOCSTRING;
+ if (ste->ste_method)
+ flags |= CO_METHOD;
}
if (ste->ste_coroutine && !ste->ste_generator) {
ste->ste_has_docstring = 0;
+ ste->ste_method = 0;
+ if (st->st_cur != NULL &&
+ st->st_cur->ste_type == ClassBlock &&
+ block == FunctionBlock) {
+ ste->ste_method = 1;
+ }
+
ste->ste_symbols = PyDict_New();
ste->ste_varnames = PyList_New(0);
ste->ste_children = PyList_New(0);