Commit e6714e49 authored by Kirill Smelkov's avatar Kirill Smelkov

gpython: Support -c<command>, not only -c <command>

And same for -m<module> - becuase Python supports it this way.

Before the patch:

	$ python '-cprint "hello world"'
	hello world
	$ gpython '-cprint "hello world"'
	unknown option: '-cprint "hello world"'

After the patch:

	$ python '-cprint "hello world"'
	hello world
	$ gpython '-cprint "hello world"'
	hello world

/reviewed-on nexedi/pygolang!5
parent 1f184095
...@@ -71,22 +71,32 @@ def pymain(argv): ...@@ -71,22 +71,32 @@ def pymain(argv):
return return
# -c command # -c command
if argv[0] == '-c': if argv[0].startswith('-c'):
sys.argv = argv[0:1] + argv[2:] # python leaves '-c' as argv[0] cmd = argv[0][2:] # -c<command> also works
sys.path.insert(0, '') # cwd argv = argv[1:]
if cmd == '':
cmd = argv[0]
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 # exec with the same globals `python -c ...` does
g = {'__name__': '__main__', g = {'__name__': '__main__',
'__doc__': None, '__doc__': None,
'__package__': None} '__package__': None}
six.exec_(argv[1], g) six.exec_(cmd, g)
# -m module # -m module
elif argv[0] == '-m': elif argv[0].startswith('-m'):
mod = argv[0][2:] # -m<module> also works
argv = argv[1:]
if mod == '':
mod = argv[0]
argv = argv[1:]
# search sys.path for module and run corresponding .py file as script # search sys.path for module and run corresponding .py file as script
sys.argv = argv[1:] sys.argv = [mod] + argv
sys.path.insert(0, '') # cwd sys.path.insert(0, '') # cwd
runpy.run_module(sys.argv[0], init_globals={'__doc__': None}, runpy.run_module(mod, init_globals={'__doc__': None},
run_name='__main__', alter_sys=True) run_name='__main__', alter_sys=True)
elif argv[0].startswith('-'): elif argv[0].startswith('-'):
......
...@@ -111,15 +111,21 @@ def test_pymain(): ...@@ -111,15 +111,21 @@ def test_pymain():
_ = pyout([], stdin=b'import hello\n', cwd=testdata) _ = pyout([], stdin=b'import hello\n', cwd=testdata)
assert _ == b"hello\nworld\n['']\n" assert _ == b"hello\nworld\n['']\n"
# -c # -c <command>
_ = pyout(['-c', 'import hello', 'abc', 'def'], cwd=testdata) _ = pyout(['-c', 'import hello', 'abc', 'def'], cwd=testdata)
assert _ == b"hello\nworld\n['-c', 'abc', 'def']\n" assert _ == b"hello\nworld\n['-c', 'abc', 'def']\n"
# -c<command> should also work
__ = pyout(['-cimport hello', 'abc', 'def'], cwd=testdata)
assert __ == _
# -m # -m <module>
_ = pyout(['-m', 'hello', 'abc', 'def'], cwd=testdata) _ = pyout(['-m', 'hello', 'abc', 'def'], cwd=testdata)
# realpath rewrites e.g. `local/lib -> lib` if local/lib is symlink # realpath rewrites e.g. `local/lib -> lib` if local/lib is symlink
hellopy = realpath(join(testdata, 'hello.py')) hellopy = realpath(join(testdata, 'hello.py'))
assert _ == b"hello\nworld\n['%s', 'abc', 'def']\n" % b(hellopy) assert _ == b"hello\nworld\n['%s', 'abc', 'def']\n" % b(hellopy)
# -m<module>
__ = pyout(['-mhello', 'abc', 'def'], cwd=testdata)
assert __ == _
# file # file
_ = pyout(['testdata/hello.py', 'abc', 'def'], cwd=here) _ = pyout(['testdata/hello.py', 'abc', 'def'], cwd=here)
......
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