Commit 076cdd8f authored by Kirill Smelkov's avatar Kirill Smelkov

gpython: Fix crash when invoked as e.g. ./bin/gpython

There was an assert because sys.path[0] is setup as realpath (real, not
relative), but if exe was given as relative path it was raising like
below:

    gpython/gpython_test.py::test_pymain_run_via_relpath GPython 0.0.7.post1 [gevent 20.9.0] / CPython 3.8.6
    Traceback (most recent call last):
      File "./__init__.py", line 482, in <module>
        main()
      File "./__init__.py", line 366, in main
        pymain(argv, init)
      File "./__init__.py", line 82, in pymain
        raise RuntimeError('pymain: internal error: sys.path[0] was not set by underlying python to dirname(exe):'
    RuntimeError: pymain: internal error: sys.path[0] was not set by underlying python to dirname(exe):

            exe:    ./__init__.py
            sys.path[0]:    /home/kirr/src/tools/go/pygolang-master/gpython
    FAILED

-> Fix it by setting up exe as realpath.
parent 11b367c6
......@@ -50,6 +50,7 @@ _pyopt_long = ('version',)
# init, if provided, is called after options are parsed, but before interpreter start.
def pymain(argv, init=None):
import sys
from os.path import dirname, realpath
# sys.executable
# on windows there are
......@@ -57,7 +58,7 @@ def pymain(argv, init=None):
# gpython.exe
# gpython.manifest
# and argv[0] is gpython-script.py
exe = argv[0]
exe = realpath(argv[0])
argv = argv[1:]
if exe.endswith('-script.py'):
exe = exe[:-len('-script.py')]
......@@ -65,14 +66,11 @@ def pymain(argv, init=None):
sys._gpy_underlying_executable = sys.executable
sys.executable = exe
import os
from os.path import dirname, realpath
# `python /path/to/gpython` adds /path/to to sys.path[0] - remove it.
# `gpython file` will add path-to-file to sys.path[0] by itself, and
# /path/to/gpython is unneccessary and would create difference in behaviour
# in between gpython and python.
exedir = exe[:exe.rindex(os.sep)] # dirname, but os.path cannot be imported yet
exedir = dirname(exe)
if sys.path[0] == exedir:
del sys.path[0]
else:
......
......@@ -274,6 +274,13 @@ def test_pymain_ver(runtime):
ret, out, err = _pyrun(['--version'], stdout=PIPE, stderr=PIPE, env=gpyenv(runtime))
assert (ret, out, b(err)) == (0, b'', b(vok))
# verify that ./bin/gpython runs ok.
@gpython_only
def test_pymain_run_via_relpath():
argv = ['-c', 'import sys; print(sys.version)']
out1 = pyout( argv, pyexe=sys.executable)
out2 = pyout(['./__init__.py'] + argv, pyexe=sys._gpy_underlying_executable, cwd=here)
assert out1 == out2
# verify -X gpython.runtime=...
@gpython_only
......
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