]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-105908: fix `barry_as_FLUFL` future import (#105909)
authorCrowthebird <78076854+thatbirdguythatuknownot@users.noreply.github.com>
Mon, 19 Jun 2023 21:50:57 +0000 (05:50 +0800)
committerGitHub <noreply@github.com>
Mon, 19 Jun 2023 21:50:57 +0000 (22:50 +0100)
Lib/test/test_future.py
Misc/NEWS.d/next/Core and Builtins/2023-06-19-11-04-01.gh-issue-105908.7oanny.rst [new file with mode: 0644]
Python/compile.c

index b8b591a1bcf2c6ef36011dd2e662555fcee032a1..4730bfafbd9cfe27b88058e196e9e9ee07046ad1 100644 (file)
@@ -4,6 +4,7 @@ import __future__
 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 @@ class FutureTest(unittest.TestCase):
         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(
         """
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 (file)
index 0000000..03db3f0
--- /dev/null
@@ -0,0 +1 @@
+Fixed bug where :gh:`99111` breaks future import ``barry_as_FLUFL`` in the Python REPL.
index cfa945ba7603bbac0e64079a5037cc3027f6ec92..69468f755a1d26606bdcde1b7216a1e6fd903860 100644 (file)
@@ -494,8 +494,10 @@ 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)
 {
+    PyCompilerFlags local_flags = _PyCompilerFlags_INIT;
+
     c->c_const_cache = PyDict_New();
     if (!c->c_const_cache) {
         return ERROR;
@@ -511,10 +513,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 = &local_flags;
+    }
+    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 +540,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;
     }