gh-87092: do not allocate PyFutureFeatures dynamically (GH-98913)

This commit is contained in:
Irit Katriel 2022-11-02 15:13:07 +00:00 committed by GitHub
parent c76db37c0d
commit 6d683d8525
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 22 additions and 32 deletions

View file

@ -18,10 +18,11 @@ PyAPI_FUNC(PyCodeObject*) _PyAST_Compile(
PyCompilerFlags *flags, PyCompilerFlags *flags,
int optimize, int optimize,
struct _arena *arena); struct _arena *arena);
extern PyFutureFeatures* _PyFuture_FromAST(
int _PyFuture_FromAST(
struct _mod * mod, struct _mod * mod,
PyObject *filename PyObject *filename,
); PyFutureFeatures* futures);
extern PyObject* _Py_Mangle(PyObject *p, PyObject *name); extern PyObject* _Py_Mangle(PyObject *p, PyObject *name);

View file

@ -417,7 +417,7 @@ handled by the symbol analysis pass.
struct compiler { struct compiler {
PyObject *c_filename; PyObject *c_filename;
struct symtable *c_st; struct symtable *c_st;
PyFutureFeatures *c_future; /* pointer to module's __future__ */ PyFutureFeatures c_future; /* module's __future__ */
PyCompilerFlags *c_flags; PyCompilerFlags *c_flags;
int c_optimize; /* optimization level */ int c_optimize; /* optimization level */
@ -619,14 +619,14 @@ _PyAST_Compile(mod_ty mod, PyObject *filename, PyCompilerFlags *flags,
Py_INCREF(filename); Py_INCREF(filename);
c.c_filename = filename; c.c_filename = filename;
c.c_arena = arena; c.c_arena = arena;
c.c_future = _PyFuture_FromAST(mod, filename); if (!_PyFuture_FromAST(mod, filename, &c.c_future)) {
if (c.c_future == NULL)
goto finally; goto finally;
}
if (!flags) { if (!flags) {
flags = &local_flags; flags = &local_flags;
} }
merged = c.c_future->ff_features | flags->cf_flags; merged = c.c_future.ff_features | flags->cf_flags;
c.c_future->ff_features = merged; c.c_future.ff_features = merged;
flags->cf_flags = merged; flags->cf_flags = merged;
c.c_flags = flags; c.c_flags = flags;
c.c_optimize = (optimize == -1) ? _Py_GetConfig()->optimization_level : optimize; c.c_optimize = (optimize == -1) ? _Py_GetConfig()->optimization_level : optimize;
@ -640,7 +640,7 @@ _PyAST_Compile(mod_ty mod, PyObject *filename, PyCompilerFlags *flags,
goto finally; goto finally;
} }
c.c_st = _PySymtable_Build(mod, filename, c.c_future); c.c_st = _PySymtable_Build(mod, filename, &c.c_future);
if (c.c_st == NULL) { if (c.c_st == NULL) {
if (!PyErr_Occurred()) if (!PyErr_Occurred())
PyErr_SetString(PyExc_SystemError, "no symtable"); PyErr_SetString(PyExc_SystemError, "no symtable");
@ -660,8 +660,6 @@ compiler_free(struct compiler *c)
{ {
if (c->c_st) if (c->c_st)
_PySymtable_Free(c->c_st); _PySymtable_Free(c->c_st);
if (c->c_future)
PyObject_Free(c->c_future);
Py_XDECREF(c->c_filename); Py_XDECREF(c->c_filename);
Py_DECREF(c->c_const_cache); Py_DECREF(c->c_const_cache);
Py_DECREF(c->c_stack); Py_DECREF(c->c_stack);
@ -2404,7 +2402,7 @@ compiler_visit_argannotation(struct compiler *c, identifier id,
ADDOP_LOAD_CONST(c, loc, mangled); ADDOP_LOAD_CONST(c, loc, mangled);
Py_DECREF(mangled); Py_DECREF(mangled);
if (c->c_future->ff_features & CO_FUTURE_ANNOTATIONS) { if (c->c_future.ff_features & CO_FUTURE_ANNOTATIONS) {
VISIT(c, annexpr, annotation); VISIT(c, annexpr, annotation);
} }
else { else {
@ -3927,7 +3925,7 @@ compiler_from_import(struct compiler *c, stmt_ty s)
PyTuple_SET_ITEM(names, i, alias->name); PyTuple_SET_ITEM(names, i, alias->name);
} }
if (location_is_after(LOC(s), c->c_future->ff_location) && if (location_is_after(LOC(s), c->c_future.ff_location) &&
s->v.ImportFrom.module && s->v.ImportFrom.module &&
_PyUnicode_EqualToASCIIString(s->v.ImportFrom.module, "__future__")) _PyUnicode_EqualToASCIIString(s->v.ImportFrom.module, "__future__"))
{ {
@ -6056,7 +6054,7 @@ check_annotation(struct compiler *c, stmt_ty s)
{ {
/* Annotations of complex targets does not produce anything /* Annotations of complex targets does not produce anything
under annotations future */ under annotations future */
if (c->c_future->ff_features & CO_FUTURE_ANNOTATIONS) { if (c->c_future.ff_features & CO_FUTURE_ANNOTATIONS) {
return 1; return 1;
} }
@ -6122,7 +6120,7 @@ compiler_annassign(struct compiler *c, stmt_ty s)
if (s->v.AnnAssign.simple && if (s->v.AnnAssign.simple &&
(c->u->u_scope_type == COMPILER_SCOPE_MODULE || (c->u->u_scope_type == COMPILER_SCOPE_MODULE ||
c->u->u_scope_type == COMPILER_SCOPE_CLASS)) { c->u->u_scope_type == COMPILER_SCOPE_CLASS)) {
if (c->c_future->ff_features & CO_FUTURE_ANNOTATIONS) { if (c->c_future.ff_features & CO_FUTURE_ANNOTATIONS) {
VISIT(c, annexpr, s->v.AnnAssign.annotation) VISIT(c, annexpr, s->v.AnnAssign.annotation)
} }
else { else {

View file

@ -96,22 +96,14 @@ future_parse(PyFutureFeatures *ff, mod_ty mod, PyObject *filename)
} }
PyFutureFeatures * int
_PyFuture_FromAST(mod_ty mod, PyObject *filename) _PyFuture_FromAST(mod_ty mod, PyObject *filename, PyFutureFeatures *ff)
{ {
PyFutureFeatures *ff;
ff = (PyFutureFeatures *)PyObject_Malloc(sizeof(PyFutureFeatures));
if (ff == NULL) {
PyErr_NoMemory();
return NULL;
}
ff->ff_features = 0; ff->ff_features = 0;
ff->ff_location = (_PyCompilerSrcLocation){-1, -1, -1, -1}; ff->ff_location = (_PyCompilerSrcLocation){-1, -1, -1, -1};
if (!future_parse(ff, mod, filename)) { if (!future_parse(ff, mod, filename)) {
PyObject_Free(ff); return 0;
return NULL;
} }
return ff; return 1;
} }

View file

@ -2144,14 +2144,13 @@ _Py_SymtableStringObjectFlags(const char *str, PyObject *filename,
_PyArena_Free(arena); _PyArena_Free(arena);
return NULL; return NULL;
} }
PyFutureFeatures *future = _PyFuture_FromAST(mod, filename); PyFutureFeatures future;
if (future == NULL) { if (!_PyFuture_FromAST(mod, filename, &future)) {
_PyArena_Free(arena); _PyArena_Free(arena);
return NULL; return NULL;
} }
future->ff_features |= flags->cf_flags; future.ff_features |= flags->cf_flags;
st = _PySymtable_Build(mod, filename, future); st = _PySymtable_Build(mod, filename, &future);
PyObject_Free((void *)future);
_PyArena_Free(arena); _PyArena_Free(arena);
return st; return st;
} }