我有一个奇怪的行为,恐怕不容易重现。也许你可以给我一个提示..
在 Ubuntu 18.04 机器上和 venv 中,我正在使用 Python 3.8.0 在使用
pytest --doctest-modules
的模块上运行
__future__.annotations
pipenv run pytest --doctest-modules my_module.py
没有问题。
现在我在 Docker 环境中运行相同的命令(使用
ubuntu:20.04
),映射包含 .venv
文件夹的目录。安装了相同的 Python 版本并且 pipenv graph
的输出没有差异我得到一个异常:
pipenv run pytest --doctest-modules my_module.py
____________________ [doctest] my_module _____________________________________
[some doctest code]
UNEXPECTED EXCEPTION: ValueError('compile(): unrecognised flags')
Traceback (most recent call last):
File "/usr/lib/python3.8/doctest.py", line 1336, in __run
exec(compile(example.source, filename, "single",
ValueError: compile(): unrecognised flags
/path/to/my_module.py:1234: UnexpectedException
我尝试比较
/usr/lib/python3.8/__future__.py
和在 Docker 环境内部和外部使用的 doctest.py
,但它们是相同的(而且它们也包含在 .venv
目录中......)。
然后我尝试调查 Doctests
compileflags
调用中使用的 exec(compile(...))
,发现在一种情况下(在 Docker 之外)它是 2^20
,在另一种情况下它是 2^24
.
深入挖掘,我发现 inside Docker 环境的
str()
值 __future__.annotations
是
_Feature((3, 7, 0, 'beta', 1), (3, 10, 0, 'alpha', 0), 16777216)
虽然它是
_Feature((3, 7, 0, 'beta', 1), (4, 0, 0, 'alpha', 0), 1048576)
在 Docker 容器之外。
为什么我会有这些差异?我怎样才能让这个 Pytest-Test 运行?
这也许不应该在cpython 3.8中被反向移植——https://github.com/python/cpython/pull/19835
标志在 3.8.2 和 3.8.3 之间以不兼容的方式更改,这意味着如果您有来自较新版本的 pyc 文件,它将无法与旧版本一起正常工作(尽管被标记为与 cpython 3.8 兼容)
上面链接的补丁调整了标志以避免与其他编译器标志冲突