Commit 64088e8a authored by Jérome Perrin's avatar Jérome Perrin Committed by Kirill Smelkov

gpython: Don't inherit __future__ when executing scripts

Because we use execfile to emulate "python script.py" invocation, future
enabled in gpython main script are also enabled in "script.py" and because
we use print_function feature, it was also applied in "script.py".
On python 2 when "script.py" uses print statements they were considered as
SyntaxError.

Hopefuly, compile has a `dont_inherit` flag which does exactly what we need
here: compile the script without inheriting futures enabled, so we use
compile with this flag.

/reviewed-by @kirr
/reviewed-on !8
parent d5b1eca0
...@@ -212,7 +212,7 @@ def _execfile(path, globals=None, locals=None): ...@@ -212,7 +212,7 @@ def _execfile(path, globals=None, locals=None):
import six import six
with open(path, "rb") as f: with open(path, "rb") as f:
src = f.read() src = f.read()
code = compile(src, path, 'exec') code = compile(src, path, 'exec', dont_inherit=True)
six.exec_(code, globals, locals) six.exec_(code, globals, locals)
# _version_info_str converts version_info -> str. # _version_info_str converts version_info -> str.
......
...@@ -208,6 +208,16 @@ def test_pymain(): ...@@ -208,6 +208,16 @@ def test_pymain():
b"- error::SyntaxWarning::*\n" + \ b"- error::SyntaxWarning::*\n" + \
b"- ignore::Warning::*\n"), _ b"- ignore::Warning::*\n"), _
def test_pymain_print_function_future():
if PY2:
_ = pyout([], stdin=b'print "print", "is", "a", "statement"\n')
assert _ == b"print is a statement\n"
_ = pyout(['print_statement.py'], cwd=testprog)
assert _ == b"print is a statement\n"
_ = pyout(['future_print_function.py'], cwd=testprog)
assert _ == b"print is a function with print_function future\n"
# verify that pymain sets sys.path in exactly the same way as underlying python does. # verify that pymain sets sys.path in exactly the same way as underlying python does.
@gpython_only @gpython_only
def test_pymain_syspath(): def test_pymain_syspath():
......
# -*- coding: utf-8 -*-
# Copyright (C) 2020 Nexedi SA and Contributors.
# Jérome Perrin <jerome@nexedi.com>
#
# This program is free software: you can Use, Study, Modify and Redistribute
# it under the terms of the GNU General Public License version 3, or (at your
# option) any later version, as published by the Free Software Foundation.
#
# You can also Link and Combine this program with other software covered by
# the terms of any of the Free Software licenses or any of the Open Source
# Initiative approved licenses and Convey the resulting work. Corresponding
# source of such a combination shall include the source code for all other
# software used.
#
# This program is distributed WITHOUT ANY WARRANTY; without even the implied
# warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
#
# See COPYING file for full licensing terms.
# See https://www.nexedi.com/licensing for rationale and options.
"""Program future_print_function enable print_function future and use print
as a function."""
from __future__ import print_function
print("print", "is", "a", "function", "with", "print_function", "future")
# -*- coding: utf-8 -*-
# Copyright (C) 2020 Nexedi SA and Contributors.
# Jérome Perrin <jerome@nexedi.com>
#
# This program is free software: you can Use, Study, Modify and Redistribute
# it under the terms of the GNU General Public License version 3, or (at your
# option) any later version, as published by the Free Software Foundation.
#
# You can also Link and Combine this program with other software covered by
# the terms of any of the Free Software licenses or any of the Open Source
# Initiative approved licenses and Convey the resulting work. Corresponding
# source of such a combination shall include the source code for all other
# software used.
#
# This program is distributed WITHOUT ANY WARRANTY; without even the implied
# warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
#
# See COPYING file for full licensing terms.
# See https://www.nexedi.com/licensing for rationale and options.
"""Program print_statement uses print as a statement, which is allowed on
python2 when not using print_statement future."""
print "print", "is", "a", "statement"
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment