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):
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
run = None # function to run according to -c/-m/file/interactive
# 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
# -V / --version
if argv[0] in ('-V', '--version'):
ver = []
if 'GPython' in sys.version:
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__
while len(argv) > 0:
# -V / --version
if argv[0] in ('-V', '--version'):
ver = []
if 'GPython' in sys.version:
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:
gpyver += ' [threads]'
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:
gpyver += ' [threads]'
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
ver = [] # unknown
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
elif argv[0].startswith('-c'):
cmd = argv[0][2:] # -c<command> also works
argv = argv[1:]
if cmd == '':
cmd = argv[0]
# -c command
elif argv[0].startswith('-c'):
cmd = argv[0][2:] # -c<command> also works
argv = argv[1:]
sys.argv = ['-c'] + argv # python leaves '-c' as argv[0]
sys.path.insert(0, '') # cwd
# exec with the same globals `python -c ...` does
g = {'__name__': '__main__',
'__doc__': None,
'__package__': None}
six.exec_(cmd, g)
# -m module
elif argv[0].startswith('-m'):
mod = argv[0][2:] # -m<module> also works
argv = argv[1:]
if mod == '':
mod = argv[0]
if cmd == '':
cmd = argv[0]
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'):
mod = argv[0][2:] # -m<module> also works
argv = argv[1:]
# search sys.path for module and run corresponding .py file as script
sys.argv = [mod] + argv
if mod == '':
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
runpy.run_module(mod, init_globals={'__doc__': None},
run_name='__main__', alter_sys=True)
elif argv[0].startswith('-'):
print("unknown option: '%s'" % argv[0], file=sys.stderr)
sys.exit(2)
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()
# 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
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