Skip to content

Commit 2be37ec

Browse files
authored
gh-121404: remove direct accesses to u_private from codegen functions (#121500)
1 parent 1d3cf79 commit 2be37ec

File tree

1 file changed

+42
-27
lines changed

1 file changed

+42
-27
lines changed

Python/compile.c

+42-27
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@
7474
typedef _Py_SourceLocation location;
7575
typedef struct _PyCfgBuilder cfg_builder;
7676

77+
struct compiler;
78+
79+
static PyObject *compiler_maybe_mangle(struct compiler *c, PyObject *name);
80+
7781
#define LOCATION(LNO, END_LNO, COL, END_COL) \
7882
((const _Py_SourceLocation){(LNO), (END_LNO), (COL), (END_COL)})
7983

@@ -887,10 +891,10 @@ compiler_addop_o(struct compiler_unit *u, location loc,
887891
#define LOAD_ZERO_SUPER_METHOD -4
888892

889893
static int
890-
compiler_addop_name(struct compiler_unit *u, location loc,
894+
compiler_addop_name(struct compiler *c, location loc,
891895
int opcode, PyObject *dict, PyObject *o)
892896
{
893-
PyObject *mangled = _Py_MaybeMangle(u->u_private, u->u_ste, o);
897+
PyObject *mangled = compiler_maybe_mangle(c, o);
894898
if (!mangled) {
895899
return ERROR;
896900
}
@@ -925,7 +929,7 @@ compiler_addop_name(struct compiler_unit *u, location loc,
925929
arg <<= 2;
926930
arg |= 1;
927931
}
928-
return codegen_addop_i(u->u_instr_sequence, opcode, arg, loc);
932+
return codegen_addop_i(INSTR_SEQUENCE(c), opcode, arg, loc);
929933
}
930934

931935
/* Add an opcode with an integer argument */
@@ -993,7 +997,7 @@ codegen_addop_j(instr_sequence *seq, location loc,
993997
}
994998

995999
#define ADDOP_NAME(C, LOC, OP, O, TYPE) \
996-
RETURN_IF_ERROR(compiler_addop_name((C)->u, (LOC), (OP), (C)->u->u_metadata.u_ ## TYPE, (O)))
1000+
RETURN_IF_ERROR(compiler_addop_name((C), (LOC), (OP), (C)->u->u_metadata.u_ ## TYPE, (O)))
9971001

9981002
#define ADDOP_I(C, LOC, OP, O) \
9991003
RETURN_IF_ERROR(codegen_addop_i(INSTR_SEQUENCE(C), (OP), (O), (LOC)))
@@ -1052,8 +1056,8 @@ codegen_addop_j(instr_sequence *seq, location loc,
10521056

10531057

10541058
static int
1055-
compiler_enter_scope(struct compiler *c, identifier name,
1056-
int scope_type, void *key, int lineno)
1059+
compiler_enter_scope(struct compiler *c, identifier name, int scope_type,
1060+
void *key, int lineno, PyObject *private)
10571061
{
10581062
location loc = LOCATION(lineno, lineno, 0, 0);
10591063

@@ -1132,7 +1136,6 @@ compiler_enter_scope(struct compiler *c, identifier name,
11321136
return ERROR;
11331137
}
11341138

1135-
u->u_private = NULL;
11361139
u->u_deferred_annotations = NULL;
11371140
if (scope_type == COMPILER_SCOPE_CLASS) {
11381141
u->u_static_attributes = PySet_New(0);
@@ -1146,6 +1149,10 @@ compiler_enter_scope(struct compiler *c, identifier name,
11461149
}
11471150

11481151
u->u_instr_sequence = (instr_sequence*)_PyInstructionSequence_New();
1152+
if (!u->u_instr_sequence) {
1153+
compiler_unit_free(u);
1154+
return ERROR;
1155+
}
11491156

11501157
/* Push the old compiler_unit on the stack. */
11511158
if (c->u) {
@@ -1156,8 +1163,13 @@ compiler_enter_scope(struct compiler *c, identifier name,
11561163
return ERROR;
11571164
}
11581165
Py_DECREF(capsule);
1159-
u->u_private = Py_XNewRef(c->u->u_private);
1166+
if (private == NULL) {
1167+
private = c->u->u_private;
1168+
}
11601169
}
1170+
1171+
u->u_private = Py_XNewRef(private);
1172+
11611173
c->u = u;
11621174

11631175
c->c_nestlevel++;
@@ -1436,7 +1448,7 @@ compiler_setup_annotations_scope(struct compiler *c, location loc,
14361448
void *key, PyObject *name)
14371449
{
14381450
if (compiler_enter_scope(c, name, COMPILER_SCOPE_ANNOTATIONS,
1439-
key, loc.lineno) == -1) {
1451+
key, loc.lineno, NULL) == -1) {
14401452
return ERROR;
14411453
}
14421454
c->u->u_metadata.u_posonlyargcount = 1;
@@ -1597,7 +1609,7 @@ compiler_enter_anonymous_scope(struct compiler* c, mod_ty mod)
15971609
_Py_DECLARE_STR(anon_module, "<module>");
15981610
RETURN_IF_ERROR(
15991611
compiler_enter_scope(c, &_Py_STR(anon_module), COMPILER_SCOPE_MODULE,
1600-
mod, 1));
1612+
mod, 1, NULL));
16011613
return SUCCESS;
16021614
}
16031615

@@ -1770,7 +1782,7 @@ compiler_kwonlydefaults(struct compiler *c, location loc,
17701782
arg_ty arg = asdl_seq_GET(kwonlyargs, i);
17711783
expr_ty default_ = asdl_seq_GET(kw_defaults, i);
17721784
if (default_) {
1773-
PyObject *mangled = _Py_MaybeMangle(c->u->u_private, c->u->u_ste, arg->arg);
1785+
PyObject *mangled = compiler_maybe_mangle(c, arg->arg);
17741786
if (!mangled) {
17751787
goto error;
17761788
}
@@ -1827,7 +1839,7 @@ compiler_argannotation(struct compiler *c, identifier id,
18271839
if (!annotation) {
18281840
return SUCCESS;
18291841
}
1830-
PyObject *mangled = _Py_MaybeMangle(c->u->u_private, c->u->u_ste, id);
1842+
PyObject *mangled = compiler_maybe_mangle(c, id);
18311843
if (!mangled) {
18321844
return ERROR;
18331845
}
@@ -2052,7 +2064,7 @@ compiler_type_param_bound_or_default(struct compiler *c, expr_ty e,
20522064
bool allow_starred)
20532065
{
20542066
if (compiler_enter_scope(c, name, COMPILER_SCOPE_ANNOTATIONS,
2055-
key, e->lineno) == -1) {
2067+
key, e->lineno, NULL) == -1) {
20562068
return ERROR;
20572069
}
20582070
if (allow_starred && e->kind == Starred_kind) {
@@ -2197,7 +2209,7 @@ compiler_function_body(struct compiler *c, stmt_ty s, int is_async, Py_ssize_t f
21972209
}
21982210

21992211
RETURN_IF_ERROR(
2200-
compiler_enter_scope(c, name, scope_type, (void *)s, firstlineno));
2212+
compiler_enter_scope(c, name, scope_type, (void *)s, firstlineno, NULL));
22012213

22022214
Py_ssize_t first_instr = 0;
22032215
PyObject *docstring = _PyAST_GetDocString(body);
@@ -2324,7 +2336,7 @@ compiler_function(struct compiler *c, stmt_ty s, int is_async)
23242336
return ERROR;
23252337
}
23262338
if (compiler_enter_scope(c, type_params_name, COMPILER_SCOPE_ANNOTATIONS,
2327-
(void *)type_params, firstlineno) == -1) {
2339+
(void *)type_params, firstlineno, NULL) == -1) {
23282340
Py_DECREF(type_params_name);
23292341
return ERROR;
23302342
}
@@ -2407,12 +2419,10 @@ compiler_class_body(struct compiler *c, stmt_ty s, int firstlineno)
24072419

24082420
/* 1. compile the class body into a code object */
24092421
RETURN_IF_ERROR(
2410-
compiler_enter_scope(c, s->v.ClassDef.name,
2411-
COMPILER_SCOPE_CLASS, (void *)s, firstlineno));
2422+
compiler_enter_scope(c, s->v.ClassDef.name, COMPILER_SCOPE_CLASS,
2423+
(void *)s, firstlineno, s->v.ClassDef.name));
24122424

24132425
location loc = LOCATION(firstlineno, firstlineno, 0, 0);
2414-
/* use the class name for name mangling */
2415-
Py_XSETREF(c->u->u_private, Py_NewRef(s->v.ClassDef.name));
24162426
/* load (global) __name__ ... */
24172427
if (compiler_nameop(c, loc, &_Py_ID(__name__), Load) < 0) {
24182428
compiler_exit_scope(c);
@@ -2558,12 +2568,11 @@ compiler_class(struct compiler *c, stmt_ty s)
25582568
return ERROR;
25592569
}
25602570
if (compiler_enter_scope(c, type_params_name, COMPILER_SCOPE_ANNOTATIONS,
2561-
(void *)type_params, firstlineno) == -1) {
2571+
(void *)type_params, firstlineno, s->v.ClassDef.name) == -1) {
25622572
Py_DECREF(type_params_name);
25632573
return ERROR;
25642574
}
25652575
Py_DECREF(type_params_name);
2566-
Py_XSETREF(c->u->u_private, Py_NewRef(s->v.ClassDef.name));
25672576
RETURN_IF_ERROR_IN_SCOPE(c, compiler_type_params(c, type_params));
25682577
_Py_DECLARE_STR(type_params, ".type_params");
25692578
RETURN_IF_ERROR_IN_SCOPE(c, compiler_nameop(c, loc, &_Py_STR(type_params), Store));
@@ -2643,7 +2652,7 @@ compiler_typealias_body(struct compiler *c, stmt_ty s)
26432652
location loc = LOC(s);
26442653
PyObject *name = s->v.TypeAlias.name->v.Name.id;
26452654
RETURN_IF_ERROR(
2646-
compiler_enter_scope(c, name, COMPILER_SCOPE_FUNCTION, s, loc.lineno));
2655+
compiler_enter_scope(c, name, COMPILER_SCOPE_FUNCTION, s, loc.lineno, NULL));
26472656
/* Make None the first constant, so the evaluate function can't have a
26482657
docstring. */
26492658
RETURN_IF_ERROR(compiler_add_const(c->c_const_cache, c->u, Py_None));
@@ -2678,7 +2687,7 @@ compiler_typealias(struct compiler *c, stmt_ty s)
26782687
return ERROR;
26792688
}
26802689
if (compiler_enter_scope(c, type_params_name, COMPILER_SCOPE_ANNOTATIONS,
2681-
(void *)type_params, loc.lineno) == -1) {
2690+
(void *)type_params, loc.lineno, NULL) == -1) {
26822691
Py_DECREF(type_params_name);
26832692
return ERROR;
26842693
}
@@ -2947,7 +2956,7 @@ compiler_lambda(struct compiler *c, expr_ty e)
29472956
_Py_DECLARE_STR(anon_lambda, "<lambda>");
29482957
RETURN_IF_ERROR(
29492958
compiler_enter_scope(c, &_Py_STR(anon_lambda), COMPILER_SCOPE_LAMBDA,
2950-
(void *)e, e->lineno));
2959+
(void *)e, e->lineno, NULL));
29512960

29522961
/* Make None the first constant, so the lambda can't have a
29532962
docstring. */
@@ -4115,7 +4124,7 @@ compiler_nameop(struct compiler *c, location loc,
41154124
return ERROR;
41164125
}
41174126

4118-
mangled = _Py_MaybeMangle(c->u->u_private, c->u->u_ste, name);
4127+
mangled = compiler_maybe_mangle(c, name);
41194128
if (!mangled) {
41204129
return ERROR;
41214130
}
@@ -5712,7 +5721,7 @@ compiler_comprehension(struct compiler *c, expr_ty e, int type,
57125721
}
57135722
else {
57145723
if (compiler_enter_scope(c, name, COMPILER_SCOPE_COMPREHENSION,
5715-
(void *)e, e->lineno) < 0)
5724+
(void *)e, e->lineno, NULL) < 0)
57165725
{
57175726
goto error;
57185727
}
@@ -6416,7 +6425,7 @@ compiler_annassign(struct compiler *c, stmt_ty s)
64166425
if (future_annotations) {
64176426
VISIT(c, annexpr, s->v.AnnAssign.annotation);
64186427
ADDOP_NAME(c, loc, LOAD_NAME, &_Py_ID(__annotations__), names);
6419-
mangled = _Py_MaybeMangle(c->u->u_private, c->u->u_ste, targ->v.Name.id);
6428+
mangled = compiler_maybe_mangle(c, targ->v.Name.id);
64206429
ADDOP_LOAD_CONST_NEW(c, loc, mangled);
64216430
ADDOP(c, loc, STORE_SUBSCR);
64226431
}
@@ -7458,6 +7467,12 @@ consts_dict_keys_inorder(PyObject *dict)
74587467
return consts;
74597468
}
74607469

7470+
static PyObject *
7471+
compiler_maybe_mangle(struct compiler *c, PyObject *name)
7472+
{
7473+
return _Py_MaybeMangle(c->u->u_private, c->u->u_ste, name);
7474+
}
7475+
74617476
static int
74627477
compute_code_flags(struct compiler *c)
74637478
{

0 commit comments

Comments
 (0)