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,111 +49,120 @@ def pymain(argv): ...@@ -49,111 +49,120 @@ def pymain(argv):
from os.path import dirname from os.path import dirname
from six.moves import input as raw_input from six.moves import input as raw_input
# interactive console run = None # function to run according to -c/-m/file/interactive
if not argv:
sys.argv = ['']
sys.path.insert(0, '') # cwd
# like code.interact() but with overridden console.raw_input _and_ while len(argv) > 0:
# readline imported (code.interact mutually excludes those two). # -V / --version
try: if argv[0] in ('-V', '--version'):
import readline # enable interactive editing ver = []
except ImportError: if 'GPython' in sys.version:
pass golang = sys.modules['golang'] # must be already imported
gevent = sys.modules.get('gevent', None)
console = code.InteractiveConsole() gpyver = 'GPython %s' % golang.__version__
def _(prompt): if gevent is not None:
# python behaviour: don't print '>>>' if stdin is not a tty gpyver += ' [gevent %s]' % gevent.__version__
# (builtin raw_input always prints prompt) else:
if not sys.stdin.isatty(): gpyver += ' [threads]'
prompt='' ver.append(gpyver)
return raw_input(prompt)
console.raw_input = _ import platform
pyimpl = platform.python_implementation()
console.interact()
return v = _version_info_str
if pyimpl == 'CPython':
# -V / --version ver.append('CPython %s' % v(sys.version_info))
if argv[0] in ('-V', '--version'): elif pyimpl == 'PyPy':
ver = [] ver.append('PyPy %s' % v(sys.pypy_version_info))
if 'GPython' in sys.version: ver.append('Python %s' % v(sys.version_info))
golang = sys.modules['golang'] # must be already imported
gevent = sys.modules.get('gevent', None)
gpyver = 'GPython %s' % golang.__version__
if gevent is not None:
gpyver += ' [gevent %s]' % gevent.__version__
else: else:
gpyver += ' [threads]' ver = [] # unknown
ver.append(gpyver)
import platform
pyimpl = platform.python_implementation()
v = _version_info_str
if pyimpl == 'CPython':
ver.append('CPython %s' % v(sys.version_info))
elif pyimpl == 'PyPy':
ver.append('PyPy %s' % v(sys.pypy_version_info))
ver.append('Python %s' % v(sys.version_info))
else:
ver = [] # unknown
ver = ' / '.join(ver)
if ver == '':
# unknown implementation: just print full sys.version
ver = sys.version
print(ver, file=sys.stderr) ver = ' / '.join(ver)
if ver == '':
# unknown implementation: just print full sys.version
ver = sys.version
print(ver, file=sys.stderr)
return
# -c command # -c command
elif argv[0].startswith('-c'): elif argv[0].startswith('-c'):
cmd = argv[0][2:] # -c<command> also works cmd = argv[0][2:] # -c<command> also works
argv = argv[1:]
if cmd == '':
cmd = argv[0]
argv = argv[1:] argv = argv[1:]
sys.argv = ['-c'] + argv # python leaves '-c' as argv[0] if cmd == '':
sys.path.insert(0, '') # cwd cmd = argv[0]
argv = argv[1:]
# exec with the same globals `python -c ...` does sys.argv = ['-c'] + argv # python leaves '-c' as argv[0]
g = {'__name__': '__main__', sys.path.insert(0, '') # cwd
'__doc__': None, def run():
'__package__': None} # exec with the same globals `python -c ...` does
six.exec_(cmd, g) g = {'__name__': '__main__',
'__doc__': None,
# -m module '__package__': None}
elif argv[0].startswith('-m'): six.exec_(cmd, g)
mod = argv[0][2:] # -m<module> also works break
argv = argv[1:]
if mod == '': # -m module
mod = argv[0] elif argv[0].startswith('-m'):
mod = argv[0][2:] # -m<module> also works
argv = argv[1:] argv = argv[1:]
# search sys.path for module and run corresponding .py file as script if mod == '':
sys.argv = [mod] + argv mod = argv[0]
argv = argv[1:]
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)
sys.exit(2)
# file
else:
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__',
'__file__': filepath,
'__doc__': None,
'__package__': None}
_execfile(filepath, g)
break
# interactive console
if run is None:
sys.argv = ['']
sys.path.insert(0, '') # cwd sys.path.insert(0, '') # cwd
runpy.run_module(mod, init_globals={'__doc__': None},
run_name='__main__', alter_sys=True)
elif argv[0].startswith('-'): def run():
print("unknown option: '%s'" % argv[0], file=sys.stderr) # like code.interact() but with overridden console.raw_input _and_
sys.exit(2) # 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()
# file
else:
sys.argv = argv
filepath = argv[0]
sys.path.insert(0, dirname(filepath))
# exec with same globals `python file.py` does
# XXX use runpy.run_path() instead?
g = {'__name__': '__main__',
'__file__': filepath,
'__doc__': None,
'__package__': None}
_execfile(filepath, g)
return
# execfile was removed in py3 # execfile was removed in py3
def _execfile(path, globals=None, locals=None): 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