Skip to content

Commit 28187a9

Browse files
gh-105908: fix barry_as_FLUFL future import (#105909)
1 parent 1858db7 commit 28187a9

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
@@ -494,8 +494,10 @@ static PyCodeObject *optimize_and_assemble(struct compiler *, int addNone);
494494

495495
static int
496496
compiler_setup(struct compiler *c, mod_ty mod, PyObject *filename,
497-
PyCompilerFlags flags, int optimize, PyArena *arena)
497+
PyCompilerFlags *flags, int optimize, PyArena *arena)
498498
{
499+
PyCompilerFlags local_flags = _PyCompilerFlags_INIT;
500+
499501
c->c_const_cache = PyDict_New();
500502
if (!c->c_const_cache) {
501503
return ERROR;
@@ -511,10 +513,13 @@ compiler_setup(struct compiler *c, mod_ty mod, PyObject *filename,
511513
if (!_PyFuture_FromAST(mod, filename, &c->c_future)) {
512514
return ERROR;
513515
}
514-
int merged = c->c_future.ff_features | flags.cf_flags;
516+
if (!flags) {
517+
flags = &local_flags;
518+
}
519+
int merged = c->c_future.ff_features | flags->cf_flags;
515520
c->c_future.ff_features = merged;
516-
flags.cf_flags = merged;
517-
c->c_flags = flags;
521+
flags->cf_flags = merged;
522+
c->c_flags = *flags;
518523
c->c_optimize = (optimize == -1) ? _Py_GetConfig()->optimization_level : optimize;
519524
c->c_nestlevel = 0;
520525

@@ -535,12 +540,11 @@ static struct compiler*
535540
new_compiler(mod_ty mod, PyObject *filename, PyCompilerFlags *pflags,
536541
int optimize, PyArena *arena)
537542
{
538-
PyCompilerFlags flags = pflags ? *pflags : _PyCompilerFlags_INIT;
539543
struct compiler *c = PyMem_Calloc(1, sizeof(struct compiler));
540544
if (c == NULL) {
541545
return NULL;
542546
}
543-
if (compiler_setup(c, mod, filename, flags, optimize, arena) < 0) {
547+
if (compiler_setup(c, mod, filename, pflags, optimize, arena) < 0) {
544548
compiler_free(c);
545549
return NULL;
546550
}

0 commit comments

Comments
 (0)