Commit 4f28dddf authored by Kirill Smelkov's avatar Kirill Smelkov

*: Python3.9 switched __file__ to be always absolute

https://bugs.python.org/issue20443

This broke test_defer_excchain_dump because
testprog/golang_test_defer_excchain.txt is prepared with output where
`python file` shows that file name in traceback as it was specified on
the command line, e.g.

    .../pygolang/golang/testprog$ python golang_test_defer_excchain.py
    Traceback (most recent call last):
      File ".../pygolang/golang/__init__.py", line 103, in _
        return f(*argv, **kw)
      File "golang_test_defer_excchain.py", line 42, in main
        raise RuntimeError("err")
    RuntimeError: err

while with py39 it became

    .../pygolang/golang/testprog$ python golang_test_defer_excchain.py
    Traceback (most recent call last):
      File ".../pygolang/golang/__init__.py", line 103, in _
        return f(*argv, **kw)
      File ".../pygolang/golang/testprog/golang_test_defer_excchain.py", line 42, in main
        raise RuntimeError("err")
    RuntimeError: err

(notice the difference related to "line 42")

-> Fix it:

- amend the test to conditionally prefix golang_test_defer_excchain.py
  in expected output with PYGOLANG/golang/testprog/ if it is Python >= 3.9.
- amend `gpython file` to match behaviour of underlying `python file`,
  so that the test passes unconditionally whether it is run by python or
  gpython.

--------

@jerome also says (nexedi/pygolang!13 (comment 122826)):

FYI, buildout (and many zope packages) essentially use doctests for
testing and they had to deal with similar differences in output.
`zope.testing` comes with a doctest checker named "renormalizing" which
normalize output with regular expressions, see for example setup of some
buildout test [here](https://github.com/buildout/buildout/blob/db3d6e2fbf5d7ff2cc4b2507253c7a221cfc3e32/src/zc/buildout/tests.py#L3615-L3651).
We definitely don't need this here for the moment, but maybe one day it
can be useful.

/reviewed-on nexedi/pygolang!13
parent 32167853
......@@ -1892,7 +1892,12 @@ def assertDoc(want, got):
got = got.replace(dir_pygolang, "PYGOLANG") # /home/x/.../pygolang -> PYGOLANG
got = got.replace(udir_pygolang, "PYGOLANG") # ~/.../pygolang -> PYGOLANG
# ^$ -> <BLANKLINE>
# want: process conditionals
# PY39(...) -> ... if py39 else ø
py39 = sys.version_info >= (3, 9)
want = re.sub(r"PY39\((.*)\)", r"\1" if py39 else "", want)
# want: ^$ -> <BLANKLINE>
while "\n\n" in want:
want = want.replace("\n\n", "\n<BLANKLINE>\n")
......
Traceback (most recent call last):
File "PYGOLANG/golang/__init__.py", line ..., in _
return f(*argv, **kw)
File "golang_test_defer_excchain.py", line 42, in main
File "PY39(PYGOLANG/golang/testprog/)golang_test_defer_excchain.py", line 42, in main
raise RuntimeError("err")
RuntimeError: err
......@@ -10,7 +10,7 @@ During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "PYGOLANG/golang/__init__.py", line ..., in __exit__
...
File "golang_test_defer_excchain.py", line 31, in d1
File "PY39(PYGOLANG/golang/testprog/)golang_test_defer_excchain.py", line 31, in d1
raise RuntimeError("d1: aaa")
RuntimeError: d1: aaa
......@@ -19,14 +19,14 @@ During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "PYGOLANG/golang/__init__.py", line ..., in __exit__
...
File "golang_test_defer_excchain.py", line 33, in d2
File "PY39(PYGOLANG/golang/testprog/)golang_test_defer_excchain.py", line 33, in d2
1/0
ZeroDivisionError: ...
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
... "golang_test_defer_excchain.py", line 45, in <module>
... "PY39(PYGOLANG/golang/testprog/)golang_test_defer_excchain.py", line 45, in <module>
main()
...
File "PYGOLANG/golang/__init__.py", line ..., in _
......@@ -37,6 +37,6 @@ Traceback (most recent call last):
...
File "PYGOLANG/golang/__init__.py", line ..., in __exit__
...
File "golang_test_defer_excchain.py", line 35, in d3
File "PY39(PYGOLANG/golang/testprog/)golang_test_defer_excchain.py", line 35, in d3
raise RuntimeError("d3: bbb")
RuntimeError: d3: bbb
......@@ -160,6 +160,10 @@ def pymain(argv, init=None):
if len(argv) > 0 and argv[0] != '-':
sys.argv = argv
filepath = argv[0]
# starting from cpython 3.9 __file__ is always absolute
# https://bugs.python.org/issue20443
if sys.version_info >= (3, 9):
filepath = realpath(filepath)
sys.path.insert(0, realpath(dirname(filepath))) # not abspath -> see PySys_SetArgvEx
def run():
......
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