From 57f5a47f056355541cca6b88dcd2c9871b289ab2 Mon Sep 17 00:00:00 2001 From: Crowthebird <78076854+thatbirdguythatuknownot@users.noreply.github.com> Date: Mon, 19 Jun 2023 18:42:32 +0800 Subject: [PATCH 1/5] pass in a pointer of flags to `compiler_setup` instead of a struct --- Python/compile.c | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/Python/compile.c b/Python/compile.c index cfa945ba7603bb..a67006b8ba1b97 100644 --- a/Python/compile.c +++ b/Python/compile.c @@ -494,7 +494,7 @@ static PyCodeObject *optimize_and_assemble(struct compiler *, int addNone); static int compiler_setup(struct compiler *c, mod_ty mod, PyObject *filename, - PyCompilerFlags flags, int optimize, PyArena *arena) + PyCompilerFlags *flags, int optimize, PyArena *arena) { c->c_const_cache = PyDict_New(); if (!c->c_const_cache) { @@ -511,10 +511,13 @@ compiler_setup(struct compiler *c, mod_ty mod, PyObject *filename, if (!_PyFuture_FromAST(mod, filename, &c->c_future)) { return ERROR; } - int merged = c->c_future.ff_features | flags.cf_flags; + if (!flags) { + flags = &_PyCompilerFlags_INIT; + } + int merged = c->c_future.ff_features | flags->cf_flags; c->c_future.ff_features = merged; - flags.cf_flags = merged; - c->c_flags = flags; + flags->cf_flags = merged; + c->c_flags = *flags; c->c_optimize = (optimize == -1) ? _Py_GetConfig()->optimization_level : optimize; c->c_nestlevel = 0; @@ -535,12 +538,11 @@ static struct compiler* new_compiler(mod_ty mod, PyObject *filename, PyCompilerFlags *pflags, int optimize, PyArena *arena) { - PyCompilerFlags flags = pflags ? *pflags : _PyCompilerFlags_INIT; struct compiler *c = PyMem_Calloc(1, sizeof(struct compiler)); if (c == NULL) { return NULL; } - if (compiler_setup(c, mod, filename, flags, optimize, arena) < 0) { + if (compiler_setup(c, mod, filename, pflags, optimize, arena) < 0) { compiler_free(c); return NULL; } From 28aa0f837f751cebed7b49afba560b42ecbd6dea Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Mon, 19 Jun 2023 11:04:04 +0000 Subject: [PATCH 2/5] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../2023-06-19-11-04-01.gh-issue-105908.7oanny.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2023-06-19-11-04-01.gh-issue-105908.7oanny.rst diff --git a/Misc/NEWS.d/next/Core and Builtins/2023-06-19-11-04-01.gh-issue-105908.7oanny.rst b/Misc/NEWS.d/next/Core and Builtins/2023-06-19-11-04-01.gh-issue-105908.7oanny.rst new file mode 100644 index 00000000000000..fae07eaa0e20e8 --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2023-06-19-11-04-01.gh-issue-105908.7oanny.rst @@ -0,0 +1 @@ +Fixed bug where gh-99111 breaks future import `barry_as_FLUFL` in the Python REPL. From 0ce3aab6effffb5fa2b74bb47f43ea37150632ea Mon Sep 17 00:00:00 2001 From: Crowthebird <78076854+thatbirdguythatuknownot@users.noreply.github.com> Date: Mon, 19 Jun 2023 19:16:55 +0800 Subject: [PATCH 3/5] add checks for `barry_as_FLUFL` import in REPL --- Lib/test/test_future.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/Lib/test/test_future.py b/Lib/test/test_future.py index b8b591a1bcf2c6..4730bfafbd9cfe 100644 --- a/Lib/test/test_future.py +++ b/Lib/test/test_future.py @@ -4,6 +4,7 @@ import ast import unittest from test.support import import_helper +from test.support.script_helper import spawn_python, kill_python from textwrap import dedent import os import re @@ -121,6 +122,13 @@ def test_unicode_literals_exec(self): exec("from __future__ import unicode_literals; x = ''", {}, scope) self.assertIsInstance(scope["x"], str) + def test_syntactical_future_repl(self): + p = spawn_python('-i') + p.stdin.write(b"from __future__ import barry_as_FLUFL\n") + p.stdin.write(b"2 <> 3\n") + out = kill_python(p) + self.assertNotIn(b'SyntaxError: invalid syntax', out) + class AnnotationsFutureTestCase(unittest.TestCase): template = dedent( """ From 6815da7ff96fc503c1cf6a7d5ed8f5beaea38dd1 Mon Sep 17 00:00:00 2001 From: Crowthebird <78076854+thatbirdguythatuknownot@users.noreply.github.com> Date: Mon, 19 Jun 2023 19:30:13 +0800 Subject: [PATCH 4/5] fix news markdown Co-authored-by: Kirill Podoprigora --- .../2023-06-19-11-04-01.gh-issue-105908.7oanny.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Core and Builtins/2023-06-19-11-04-01.gh-issue-105908.7oanny.rst b/Misc/NEWS.d/next/Core and Builtins/2023-06-19-11-04-01.gh-issue-105908.7oanny.rst index fae07eaa0e20e8..03db3f064f503f 100644 --- a/Misc/NEWS.d/next/Core and Builtins/2023-06-19-11-04-01.gh-issue-105908.7oanny.rst +++ b/Misc/NEWS.d/next/Core and Builtins/2023-06-19-11-04-01.gh-issue-105908.7oanny.rst @@ -1 +1 @@ -Fixed bug where gh-99111 breaks future import `barry_as_FLUFL` in the Python REPL. +Fixed bug where :gh:`99111` breaks future import ``barry_as_FLUFL`` in the Python REPL. From f78b15165623631aaef98796dc196ee0f38bfc47 Mon Sep 17 00:00:00 2001 From: Crowthebird <78076854+thatbirdguythatuknownot@users.noreply.github.com> Date: Tue, 20 Jun 2023 05:03:10 +0800 Subject: [PATCH 5/5] add `local_flags` variable back to prevent stack use-after-free --- Python/compile.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Python/compile.c b/Python/compile.c index a67006b8ba1b97..69468f755a1d26 100644 --- a/Python/compile.c +++ b/Python/compile.c @@ -496,6 +496,8 @@ static int compiler_setup(struct compiler *c, mod_ty mod, PyObject *filename, PyCompilerFlags *flags, int optimize, PyArena *arena) { + PyCompilerFlags local_flags = _PyCompilerFlags_INIT; + c->c_const_cache = PyDict_New(); if (!c->c_const_cache) { return ERROR; @@ -512,7 +514,7 @@ compiler_setup(struct compiler *c, mod_ty mod, PyObject *filename, return ERROR; } if (!flags) { - flags = &_PyCompilerFlags_INIT; + flags = &local_flags; } int merged = c->c_future.ff_features | flags->cf_flags; c->c_future.ff_features = merged;