Skip to content

Commit cc18a8b

Browse files
[3.12] gh-105908: fix barry_as_FLUFL future import (GH-105909) (#105930)
(cherry picked from commit 28187a9) Co-authored-by: Crowthebird <[email protected]>
1 parent 225cc4c commit cc18a8b

File tree

3 files changed

+19
-6
lines changed

3 files changed

+19
-6
lines changed

Lib/test/test_future.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import ast
55
import unittest
66
from test.support import import_helper
7+
from test.support.script_helper import spawn_python, kill_python
78
from textwrap import dedent
89
import os
910
import re
@@ -121,6 +122,13 @@ def test_unicode_literals_exec(self):
121122
exec("from __future__ import unicode_literals; x = ''", {}, scope)
122123
self.assertIsInstance(scope["x"], str)
123124

125+
def test_syntactical_future_repl(self):
126+
p = spawn_python('-i')
127+
p.stdin.write(b"from __future__ import barry_as_FLUFL\n")
128+
p.stdin.write(b"2 <> 3\n")
129+
out = kill_python(p)
130+
self.assertNotIn(b'SyntaxError: invalid syntax', out)
131+
124132
class AnnotationsFutureTestCase(unittest.TestCase):
125133
template = dedent(
126134
"""
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fixed bug where :gh:`99111` breaks future import ``barry_as_FLUFL`` in the Python REPL.

Python/compile.c

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -510,8 +510,10 @@ static PyCodeObject *optimize_and_assemble(struct compiler *, int addNone);
510510

511511
static int
512512
compiler_setup(struct compiler *c, mod_ty mod, PyObject *filename,
513-
PyCompilerFlags flags, int optimize, PyArena *arena)
513+
PyCompilerFlags *flags, int optimize, PyArena *arena)
514514
{
515+
PyCompilerFlags local_flags = _PyCompilerFlags_INIT;
516+
515517
c->c_const_cache = PyDict_New();
516518
if (!c->c_const_cache) {
517519
return ERROR;
@@ -527,10 +529,13 @@ compiler_setup(struct compiler *c, mod_ty mod, PyObject *filename,
527529
if (!_PyFuture_FromAST(mod, filename, &c->c_future)) {
528530
return ERROR;
529531
}
530-
int merged = c->c_future.ff_features | flags.cf_flags;
532+
if (!flags) {
533+
flags = &local_flags;
534+
}
535+
int merged = c->c_future.ff_features | flags->cf_flags;
531536
c->c_future.ff_features = merged;
532-
flags.cf_flags = merged;
533-
c->c_flags = flags;
537+
flags->cf_flags = merged;
538+
c->c_flags = *flags;
534539
c->c_optimize = (optimize == -1) ? _Py_GetConfig()->optimization_level : optimize;
535540
c->c_nestlevel = 0;
536541

@@ -555,12 +560,11 @@ static struct compiler*
555560
new_compiler(mod_ty mod, PyObject *filename, PyCompilerFlags *pflags,
556561
int optimize, PyArena *arena)
557562
{
558-
PyCompilerFlags flags = pflags ? *pflags : _PyCompilerFlags_INIT;
559563
struct compiler *c = PyMem_Calloc(1, sizeof(struct compiler));
560564
if (c == NULL) {
561565
return NULL;
562566
}
563-
if (compiler_setup(c, mod, filename, flags, optimize, arena) < 0) {
567+
if (compiler_setup(c, mod, filename, pflags, optimize, arena) < 0) {
564568
compiler_free(c);
565569
return NULL;
566570
}

0 commit comments

Comments
 (0)