Commit 22fb559a authored by Kirill Smelkov's avatar Kirill Smelkov

gpython: Handle program on stdin through just read+exec

Not through interactive console, because it was printing prompts, while
`python <prog.py` does not emit anything:

    $ cat prog.py
    print 'Hello World'

    $ python <prog.py
    Hello World

    $ gpython <prog.py
    Python 2.7.18 (default, Apr 20 2020, 20:30:41)
    [GCC 9.3.0] [GPython 0.0.8] [gevent 20.9.0] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    (InteractiveConsole)
    Hello World

After the patch gpython output is the same as of python:

    $ gpython <prog.py
    Hello World

Test coverage for interactive mode is pending.
We'll add them soon in a follow-up patch after implementing -i.

For _interact - by this patch logic - we should be dropping custom
raw_input, since now _interact is called only when sys.stdin is tty.
But we'll soon be invoking _interact when stdin is not a tty (for
`gpython -i <prog.py`), so leave that logic in place as is.

/reviewed-by @jerome
/reviewed-on !15
parent 6cc4bf32
......@@ -88,7 +88,7 @@ def pymain(argv, init=None):
run = None # function to run according to -c/-m/file/interactive
run = None # function to run according to -c/-m/file/stdin/interactive
version = False # set if `-V`
warnoptions = [] # collected `-W arg`
reexec_with = [] # reexecute underlying python with those options (e.g. -O, -S, ...)
......@@ -175,13 +175,21 @@ def pymain(argv, init=None):
'__package__': None}
_execfile(filepath, g)
# interactive console
# interactive console / program on non-tty stdin
else:
sys.argv = [''] if len(argv) == 0 else argv # e.g. ['-']
sys.path.insert(0, '') # cwd
if sys.stdin.isatty():
def run():
_interact()
else:
def run():
import six
prog = sys.stdin.read()
g = {'__name__': '__main__',
'__file__': '<stdin>'}
six.exec_(prog, g)
# ---- options processed -> start the interpreter ----
......
# -*- coding: utf-8 -*-
# Copyright (C) 2019-2020 Nexedi SA and Contributors.
# Copyright (C) 2019-2021 Nexedi SA and Contributors.
# Kirill Smelkov <kirr@nexedi.com>
#
# This program is free software: you can Use, Study, Modify and Redistribute
......@@ -160,7 +160,7 @@ def test_executable(runtime):
def test_pymain():
from golang import b
# interactive
# stdin
_ = pyout([], stdin=b'import hello\n', cwd=testdata)
assert _ == b"hello\nworld\n['']\n"
_ = pyout(['-'], stdin=b'import hello\n', cwd=testdata)
......@@ -241,7 +241,7 @@ def test_pymain_syspath():
check_gpy_vs_py(argv, postprocessf=_, **kw)
check([], stdin=b'import print_syspath', cwd=testprog) # interactive
check([], stdin=b'import print_syspath', cwd=testprog) # stdin
check(['-c', 'import print_syspath'], cwd=testprog) # -c
check(['-m', 'print_syspath'], cwd=testprog, # -m
path0cwd2realpath=(PY2 or is_pypy))
......
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