@@ -563,8 +563,16 @@ compiler_unit_free(struct compiler_unit *u)
563563}
564564
565565static int
566- compiler_add_static_attribute_to_class (struct compiler * c , PyObject * attr )
566+ compiler_maybe_add_static_attribute_to_class (struct compiler * c , expr_ty e )
567567{
568+ assert (e -> kind == Attribute_kind );
569+ expr_ty attr_value = e -> v .Attribute .value ;
570+ if (attr_value -> kind != Name_kind ||
571+ e -> v .Attribute .ctx != Store ||
572+ !_PyUnicode_EqualToASCIIString (attr_value -> v .Name .id , "self" ))
573+ {
574+ return SUCCESS ;
575+ }
568576 Py_ssize_t stack_size = PyList_GET_SIZE (c -> c_stack );
569577 for (Py_ssize_t i = stack_size - 1 ; i >= 0 ; i -- ) {
570578 PyObject * capsule = PyList_GET_ITEM (c -> c_stack , i );
@@ -573,7 +581,7 @@ compiler_add_static_attribute_to_class(struct compiler *c, PyObject *attr)
573581 assert (u );
574582 if (u -> u_scope_type == COMPILER_SCOPE_CLASS ) {
575583 assert (u -> u_static_attributes );
576- RETURN_IF_ERROR (PySet_Add (u -> u_static_attributes , attr ));
584+ RETURN_IF_ERROR (PySet_Add (u -> u_static_attributes , e -> v . Attribute . attr ));
577585 break ;
578586 }
579587 }
@@ -6065,11 +6073,7 @@ compiler_visit_expr(struct compiler *c, expr_ty e)
60656073 ADDOP (c , loc , NOP );
60666074 return SUCCESS ;
60676075 }
6068- if (e -> v .Attribute .value -> kind == Name_kind &&
6069- _PyUnicode_EqualToASCIIString (e -> v .Attribute .value -> v .Name .id , "self" ))
6070- {
6071- RETURN_IF_ERROR (compiler_add_static_attribute_to_class (c , e -> v .Attribute .attr ));
6072- }
6076+ RETURN_IF_ERROR (compiler_maybe_add_static_attribute_to_class (c , e ));
60736077 VISIT (c , expr , e -> v .Attribute .value );
60746078 loc = LOC (e );
60756079 loc = update_start_location_to_match_attr (c , loc , e );
0 commit comments