Skip to content

Commit 6d683d8

Browse files
authored
gh-87092: do not allocate PyFutureFeatures dynamically (GH-98913)
1 parent c76db37 commit 6d683d8

File tree

4 files changed

+22
-32
lines changed

4 files changed

+22
-32
lines changed

Include/internal/pycore_compile.h

+4-3
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,11 @@ PyAPI_FUNC(PyCodeObject*) _PyAST_Compile(
1818
PyCompilerFlags *flags,
1919
int optimize,
2020
struct _arena *arena);
21-
extern PyFutureFeatures* _PyFuture_FromAST(
21+
22+
int _PyFuture_FromAST(
2223
struct _mod * mod,
23-
PyObject *filename
24-
);
24+
PyObject *filename,
25+
PyFutureFeatures* futures);
2526

2627
extern PyObject* _Py_Mangle(PyObject *p, PyObject *name);
2728

Python/compile.c

+10-12
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,7 @@ handled by the symbol analysis pass.
417417
struct compiler {
418418
PyObject *c_filename;
419419
struct symtable *c_st;
420-
PyFutureFeatures *c_future; /* pointer to module's __future__ */
420+
PyFutureFeatures c_future; /* module's __future__ */
421421
PyCompilerFlags *c_flags;
422422

423423
int c_optimize; /* optimization level */
@@ -619,14 +619,14 @@ _PyAST_Compile(mod_ty mod, PyObject *filename, PyCompilerFlags *flags,
619619
Py_INCREF(filename);
620620
c.c_filename = filename;
621621
c.c_arena = arena;
622-
c.c_future = _PyFuture_FromAST(mod, filename);
623-
if (c.c_future == NULL)
622+
if (!_PyFuture_FromAST(mod, filename, &c.c_future)) {
624623
goto finally;
624+
}
625625
if (!flags) {
626626
flags = &local_flags;
627627
}
628-
merged = c.c_future->ff_features | flags->cf_flags;
629-
c.c_future->ff_features = merged;
628+
merged = c.c_future.ff_features | flags->cf_flags;
629+
c.c_future.ff_features = merged;
630630
flags->cf_flags = merged;
631631
c.c_flags = flags;
632632
c.c_optimize = (optimize == -1) ? _Py_GetConfig()->optimization_level : optimize;
@@ -640,7 +640,7 @@ _PyAST_Compile(mod_ty mod, PyObject *filename, PyCompilerFlags *flags,
640640
goto finally;
641641
}
642642

643-
c.c_st = _PySymtable_Build(mod, filename, c.c_future);
643+
c.c_st = _PySymtable_Build(mod, filename, &c.c_future);
644644
if (c.c_st == NULL) {
645645
if (!PyErr_Occurred())
646646
PyErr_SetString(PyExc_SystemError, "no symtable");
@@ -660,8 +660,6 @@ compiler_free(struct compiler *c)
660660
{
661661
if (c->c_st)
662662
_PySymtable_Free(c->c_st);
663-
if (c->c_future)
664-
PyObject_Free(c->c_future);
665663
Py_XDECREF(c->c_filename);
666664
Py_DECREF(c->c_const_cache);
667665
Py_DECREF(c->c_stack);
@@ -2404,7 +2402,7 @@ compiler_visit_argannotation(struct compiler *c, identifier id,
24042402
ADDOP_LOAD_CONST(c, loc, mangled);
24052403
Py_DECREF(mangled);
24062404

2407-
if (c->c_future->ff_features & CO_FUTURE_ANNOTATIONS) {
2405+
if (c->c_future.ff_features & CO_FUTURE_ANNOTATIONS) {
24082406
VISIT(c, annexpr, annotation);
24092407
}
24102408
else {
@@ -3927,7 +3925,7 @@ compiler_from_import(struct compiler *c, stmt_ty s)
39273925
PyTuple_SET_ITEM(names, i, alias->name);
39283926
}
39293927

3930-
if (location_is_after(LOC(s), c->c_future->ff_location) &&
3928+
if (location_is_after(LOC(s), c->c_future.ff_location) &&
39313929
s->v.ImportFrom.module &&
39323930
_PyUnicode_EqualToASCIIString(s->v.ImportFrom.module, "__future__"))
39333931
{
@@ -6056,7 +6054,7 @@ check_annotation(struct compiler *c, stmt_ty s)
60566054
{
60576055
/* Annotations of complex targets does not produce anything
60586056
under annotations future */
6059-
if (c->c_future->ff_features & CO_FUTURE_ANNOTATIONS) {
6057+
if (c->c_future.ff_features & CO_FUTURE_ANNOTATIONS) {
60606058
return 1;
60616059
}
60626060

@@ -6122,7 +6120,7 @@ compiler_annassign(struct compiler *c, stmt_ty s)
61226120
if (s->v.AnnAssign.simple &&
61236121
(c->u->u_scope_type == COMPILER_SCOPE_MODULE ||
61246122
c->u->u_scope_type == COMPILER_SCOPE_CLASS)) {
6125-
if (c->c_future->ff_features & CO_FUTURE_ANNOTATIONS) {
6123+
if (c->c_future.ff_features & CO_FUTURE_ANNOTATIONS) {
61266124
VISIT(c, annexpr, s->v.AnnAssign.annotation)
61276125
}
61286126
else {

Python/future.c

+4-12
Original file line numberDiff line numberDiff line change
@@ -96,22 +96,14 @@ future_parse(PyFutureFeatures *ff, mod_ty mod, PyObject *filename)
9696
}
9797

9898

99-
PyFutureFeatures *
100-
_PyFuture_FromAST(mod_ty mod, PyObject *filename)
99+
int
100+
_PyFuture_FromAST(mod_ty mod, PyObject *filename, PyFutureFeatures *ff)
101101
{
102-
PyFutureFeatures *ff;
103-
104-
ff = (PyFutureFeatures *)PyObject_Malloc(sizeof(PyFutureFeatures));
105-
if (ff == NULL) {
106-
PyErr_NoMemory();
107-
return NULL;
108-
}
109102
ff->ff_features = 0;
110103
ff->ff_location = (_PyCompilerSrcLocation){-1, -1, -1, -1};
111104

112105
if (!future_parse(ff, mod, filename)) {
113-
PyObject_Free(ff);
114-
return NULL;
106+
return 0;
115107
}
116-
return ff;
108+
return 1;
117109
}

Python/symtable.c

+4-5
Original file line numberDiff line numberDiff line change
@@ -2144,14 +2144,13 @@ _Py_SymtableStringObjectFlags(const char *str, PyObject *filename,
21442144
_PyArena_Free(arena);
21452145
return NULL;
21462146
}
2147-
PyFutureFeatures *future = _PyFuture_FromAST(mod, filename);
2148-
if (future == NULL) {
2147+
PyFutureFeatures future;
2148+
if (!_PyFuture_FromAST(mod, filename, &future)) {
21492149
_PyArena_Free(arena);
21502150
return NULL;
21512151
}
2152-
future->ff_features |= flags->cf_flags;
2153-
st = _PySymtable_Build(mod, filename, future);
2154-
PyObject_Free((void *)future);
2152+
future.ff_features |= flags->cf_flags;
2153+
st = _PySymtable_Build(mod, filename, &future);
21552154
_PyArena_Free(arena);
21562155
return st;
21572156
}

0 commit comments

Comments
 (0)