diff options
author | Irit Katriel <1055913+iritkatriel@users.noreply.github.com> | 2024-08-02 18:26:57 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-08-02 18:26:57 (GMT) |
commit | 545a328eae058b82d7279a31d7fdce08b6ff4f94 (patch) | |
tree | 77f9ab0a45c1073fba475439bc49f35181c9e1eb /Python/compile.c | |
parent | b20893b5c2dc999bbad5723c20f0ff2aea83a428 (diff) | |
download | cpython-545a328eae058b82d7279a31d7fdce08b6ff4f94.zip cpython-545a328eae058b82d7279a31d7fdce08b6ff4f94.tar.gz cpython-545a328eae058b82d7279a31d7fdce08b6ff4f94.tar.bz2 |
[3.13] gh-122445: populate only modified fields in __static_attributes__ (#122446) (#122621)
gh-122445: populate only modified fields in __static_attributes__ (#122446)
(cherry picked from commit 498376d7a7d6f704f22a2c963130cc15c17e7a6f)
Diffstat (limited to 'Python/compile.c')
-rw-r--r-- | Python/compile.c | 30 |
1 files changed, 15 insertions, 15 deletions
diff --git a/Python/compile.c b/Python/compile.c index 7456668..6c6260c 100644 --- a/Python/compile.c +++ b/Python/compile.c @@ -589,9 +589,17 @@ compiler_unit_free(struct compiler_unit *u) PyMem_Free(u); } -static struct compiler_unit * -get_class_compiler_unit(struct compiler *c) +static int +compiler_maybe_add_static_attribute_to_class(struct compiler *c, expr_ty e) { + assert(e->kind == Attribute_kind); + expr_ty attr_value = e->v.Attribute.value; + if (attr_value->kind != Name_kind || + e->v.Attribute.ctx != Store || + !_PyUnicode_EqualToASCIIString(attr_value->v.Name.id, "self")) + { + return SUCCESS; + } Py_ssize_t stack_size = PyList_GET_SIZE(c->c_stack); for (Py_ssize_t i = stack_size - 1; i >= 0; i--) { PyObject *capsule = PyList_GET_ITEM(c->c_stack, i); @@ -599,10 +607,12 @@ get_class_compiler_unit(struct compiler *c) capsule, CAPSULE_NAME); assert(u); if (u->u_scope_type == COMPILER_SCOPE_CLASS) { - return u; + assert(u->u_static_attributes); + RETURN_IF_ERROR(PySet_Add(u->u_static_attributes, e->v.Attribute.attr)); + break; } } - return NULL; + return SUCCESS; } static int @@ -6283,17 +6293,7 @@ compiler_visit_expr1(struct compiler *c, expr_ty e) ADDOP(c, loc, NOP); return SUCCESS; } - if (e->v.Attribute.value->kind == Name_kind && - _PyUnicode_EqualToASCIIString(e->v.Attribute.value->v.Name.id, "self")) - { - struct compiler_unit *class_u = get_class_compiler_unit(c); - if (class_u != NULL) { - assert(class_u->u_scope_type == COMPILER_SCOPE_CLASS); - assert(class_u->u_static_attributes); - RETURN_IF_ERROR( - PySet_Add(class_u->u_static_attributes, e->v.Attribute.attr)); - } - } + RETURN_IF_ERROR(compiler_maybe_add_static_attribute_to_class(c, e)); VISIT(c, expr, e->v.Attribute.value); loc = LOC(e); loc = update_start_location_to_match_attr(c, loc, e); |