Commit 70c4c82f authored by Kirill Smelkov's avatar Kirill Smelkov

gpython: Add support for -V / --version

On CPython:

	$ python -V
	Python 2.7.18

	$ gpython -V
	GPython 0.0.6.post2 [gevent 20.6.2] / CPython 2.7.18

On PyPy:

	$ python -V
        Python 3.6.9 (2ad108f17bdb, Apr 07 2020, 02:59:05)
        [PyPy 7.3.1 with GCC 7.3.1 20180303 (Red Hat 7.3.1-5)]

	$ gpython -V
	GPython 0.0.6.post2 [gevent 20.5.0] / PyPy 7.3.1 / Python 3.6.9

/reviewed-on nexedi/pygolang!5
parent e6714e49
......@@ -70,8 +70,37 @@ def pymain(argv):
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['gevent'] # must be already imported
gpyver = 'GPython %s [gevent %s]' % (golang.__version__, gevent.__version__)
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)
# -c command
if argv[0].startswith('-c'):
elif argv[0].startswith('-c'):
cmd = argv[0][2:] # -c<command> also works
argv = argv[1:]
if cmd == '':
......@@ -127,6 +156,14 @@ def _execfile(path, globals=None, locals=None):
code = compile(src, path, 'exec')
six.exec_(code, globals, locals)
# _version_info_str converts version_info -> str.
def _version_info_str(vi):
major, minor, micro, release, serial = vi
v = '%d.%d.%d' % (major, minor, micro)
if (release, serial) != ('final', 0):
v += '.%s%s' % (release, serial)
return v
def main():
# set UTF-8 as default encoding.
......
......@@ -20,12 +20,16 @@
from __future__ import print_function, absolute_import
import sys, os, golang
from golang.golang_test import pyout
import sys, os, platform, golang
from golang.golang_test import pyout, _pyrun
from subprocess import PIPE
from six import PY2
from six.moves import builtins
import pytest
is_pypy = (platform.python_implementation() == 'PyPy')
is_cpython = (platform.python_implementation() == 'CPython')
# @gpython_only is marker to run a test only under gpython
gpython_only = pytest.mark.skipif('GPython' not in sys.version, reason="gpython-only test")
......@@ -130,3 +134,27 @@ def test_pymain():
# file
_ = pyout(['testdata/hello.py', 'abc', 'def'], cwd=here)
assert _ == b"hello\nworld\n['testdata/hello.py', 'abc', 'def']\n"
# pymain -V/--version
# gpython_only because output differs from !gpython.
@gpython_only
def test_pymain_ver():
from golang import b
from gpython import _version_info_str as V
import gevent
vok = 'GPython %s [gevent %s]' % (golang.__version__, gevent.__version__)
if is_cpython:
vok += ' / CPython %s' % platform.python_version()
elif is_pypy:
vok += ' / PyPy %s / Python %s' % (V(sys.pypy_version_info), V(sys.version_info))
else:
vok = sys.version
vok += '\n'
ret, out, err = _pyrun(['-V'], stdout=PIPE, stderr=PIPE)
assert (ret, out, b(err)) == (0, b'', b(vok))
ret, out, err = _pyrun(['--version'], stdout=PIPE, stderr=PIPE)
assert (ret, out, b(err)) == (0, b'', b(vok))
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