Commit b47edf42 authored by Kirill Smelkov's avatar Kirill Smelkov

gpython: Reorganize options parsing

Reorganize parsiong of command line options so that first all options are
parsed in a loop, and only after that a module/file/command is executed.

This is needed as preparatory step for next patch: there we'll add
support for -W, and `-W arg` can be given multiple times and has to be
processed multiple times by creating multiple corresponding warning
filters. Because those warning filters has to be applied uniformly to
all 4 codepaths of execution phase (-m/-c/file/interactive console), it
makes sense to move execution phase to after options parsing and inject
common runtime preparatory steps right before that.

This logic generally applies not only to -W, but to all other python
options - e.g. -Q,-u,...
parent 49bb8dcd
......@@ -49,30 +49,9 @@ def pymain(argv):
from os.path import dirname
from six.moves import input as raw_input
# interactive console
if not argv:
sys.argv = ['']
sys.path.insert(0, '') # cwd
# like code.interact() but with overridden console.raw_input _and_
# readline imported (code.interact mutually excludes those two).
try:
import readline # enable interactive editing
except ImportError:
pass
console = code.InteractiveConsole()
def _(prompt):
# python behaviour: don't print '>>>' if stdin is not a tty
# (builtin raw_input always prints prompt)
if not sys.stdin.isatty():
prompt=''
return raw_input(prompt)
console.raw_input = _
console.interact()
return
run = None # function to run according to -c/-m/file/interactive
while len(argv) > 0:
# -V / --version
if argv[0] in ('-V', '--version'):
ver = []
......@@ -104,7 +83,7 @@ def pymain(argv):
ver = sys.version
print(ver, file=sys.stderr)
return
# -c command
elif argv[0].startswith('-c'):
......@@ -115,12 +94,13 @@ def pymain(argv):
argv = argv[1:]
sys.argv = ['-c'] + argv # python leaves '-c' as argv[0]
sys.path.insert(0, '') # cwd
def run():
# exec with the same globals `python -c ...` does
g = {'__name__': '__main__',
'__doc__': None,
'__package__': None}
six.exec_(cmd, g)
break
# -m module
elif argv[0].startswith('-m'):
......@@ -129,11 +109,13 @@ def pymain(argv):
if mod == '':
mod = argv[0]
argv = argv[1:]
# search sys.path for module and run corresponding .py file as script
sys.argv = [mod] + argv
sys.path.insert(0, '') # cwd
def run():
# search sys.path for module and run corresponding .py file as script
runpy.run_module(mod, init_globals={'__doc__': None},
run_name='__main__', alter_sys=True)
break
elif argv[0].startswith('-'):
print("unknown option: '%s'" % argv[0], file=sys.stderr)
......@@ -144,7 +126,7 @@ def pymain(argv):
sys.argv = argv
filepath = argv[0]
sys.path.insert(0, dirname(filepath))
def run():
# exec with same globals `python file.py` does
# XXX use runpy.run_path() instead?
g = {'__name__': '__main__',
......@@ -152,8 +134,35 @@ def pymain(argv):
'__doc__': None,
'__package__': None}
_execfile(filepath, g)
break
# interactive console
if run is None:
sys.argv = ['']
sys.path.insert(0, '') # cwd
def run():
# like code.interact() but with overridden console.raw_input _and_
# readline imported (code.interact mutually excludes those two).
try:
import readline # enable interactive editing
except ImportError:
pass
console = code.InteractiveConsole()
def _(prompt):
# python behaviour: don't print '>>>' if stdin is not a tty
# (builtin raw_input always prints prompt)
if not sys.stdin.isatty():
prompt=''
return raw_input(prompt)
console.raw_input = _
console.interact()
# execute -m/-c/file/interactive
run()
return
# execfile was removed in py3
def _execfile(path, globals=None, locals=None):
......
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