Skip to main content
 首页 » 编程设计

python之CPython:为什么 3 行脚本需要在解释器中执行远远超过 3 个周期才能执行

2024年09月07日8cyq1162

我刚看了this Youtube lecture关于 Philip Guo 的 CPython Internals,我对一件事感到困惑。

在 25:55,他修改了 CPython 的 C 源代码,在运行所有字节码指令的无限循环的开头插入 printf(“hello\n”);您可以通过以下方式执行相同的操作:

  • 下载 Python 2.7 C 源代码
  • 打开文件 Python/ceval.c
  • 找到无限评估循环的开始,for (;;) {
  • 添加行 printf('hello\n'); 作为无限循环的第一行。
  • 运行 configure 和 make 构建 Python 二进制文件。

他写了一个 3 行的 test.py:

X = 1 
Y = 2 
print X + Y 

令人不解的是,当他用修改后的解释器运行test.py时,为什么在我们看到“3”之前有这么多“hello”?

那 3 行代码应该编译成几个字节码指令,加载值 1,加载值 2 和调用 print 的指令,所以我可以想象当执行从 test.py 编译的字节码时,我们应该只看到几个“你好”。

所以编译器在编译外部Python脚本之前实际上生成了许多内部字节码指令?

请您参考如下方法:

您看到如此多的 hello 有两个原因:

  • Python 并没有为每个可能的 Python 语句提供特殊的字节码。相反,语句将使用字节码的组合。
  • Python 解释器导入一系列 Python 模块只是为了开始运行。您可以使用 -v 开关运行常规 Python 解释器,以查看每次导入的内容。每个模块都包含多个语句,因此在您开始运行小脚本之前,需要经过相当多的字节码。

如果我将这 3 行放入 test.py 并使用我未修改的 Python 2.7 二进制文件运行它,并使用 -v 开关,我看到:

$ python2.7 -v test.py 
# installing zipimport hook 
import zipimport # builtin 
# installed zipimport hook 
# /..../lib/python2.7/site.pyc matches /..../lib/python2.7/site.py 
import site # precompiled from /..../lib/python2.7/site.pyc 
# /..../lib/python2.7/os.pyc matches /..../lib/python2.7/os.py 
import os # precompiled from /..../lib/python2.7/os.pyc 
import errno # builtin 
import posix # builtin 
# /..../lib/python2.7/posixpath.pyc matches /..../lib/python2.7/posixpath.py 
import posixpath # precompiled from /..../lib/python2.7/posixpath.pyc 
# /..../lib/python2.7/stat.pyc matches /..../lib/python2.7/stat.py 
import stat # precompiled from /..../lib/python2.7/stat.pyc 
# /..../lib/python2.7/genericpath.pyc matches /..../lib/python2.7/genericpath.py 
import genericpath # precompiled from /..../lib/python2.7/genericpath.pyc 
# /..../lib/python2.7/warnings.pyc matches /..../lib/python2.7/warnings.py 
import warnings # precompiled from /..../lib/python2.7/warnings.pyc 
# /..../lib/python2.7/linecache.pyc matches /..../lib/python2.7/linecache.py 
import linecache # precompiled from /..../lib/python2.7/linecache.pyc 
# /..../lib/python2.7/types.pyc matches /..../lib/python2.7/types.py 
import types # precompiled from /..../lib/python2.7/types.pyc 
# /..../lib/python2.7/UserDict.pyc matches /..../lib/python2.7/UserDict.py 
import UserDict # precompiled from /..../lib/python2.7/UserDict.pyc 
# /..../lib/python2.7/_abcoll.pyc matches /..../lib/python2.7/_abcoll.py 
import _abcoll # precompiled from /..../lib/python2.7/_abcoll.pyc 
# /..../lib/python2.7/abc.pyc matches /..../lib/python2.7/abc.py 
import abc # precompiled from /..../lib/python2.7/abc.pyc 
# /..../lib/python2.7/_weakrefset.pyc matches /..../lib/python2.7/_weakrefset.py 
import _weakrefset # precompiled from /..../lib/python2.7/_weakrefset.pyc 
import _weakref # builtin 
# /..../lib/python2.7/copy_reg.pyc matches /..../lib/python2.7/copy_reg.py 
import copy_reg # precompiled from /..../lib/python2.7/copy_reg.pyc 
import encodings # directory /..../lib/python2.7/encodings 
# /..../lib/python2.7/encodings/__init__.pyc matches /..../lib/python2.7/encodings/__init__.py 
import encodings # precompiled from /..../lib/python2.7/encodings/__init__.pyc 
# /..../lib/python2.7/codecs.pyc matches /..../lib/python2.7/codecs.py 
import codecs # precompiled from /..../lib/python2.7/codecs.pyc 
import _codecs # builtin 
# /..../lib/python2.7/encodings/aliases.pyc matches /..../lib/python2.7/encodings/aliases.py 
import encodings.aliases # precompiled from /..../lib/python2.7/encodings/aliases.pyc 
# /..../lib/python2.7/encodings/utf_8.pyc matches /..../lib/python2.7/encodings/utf_8.py 
import encodings.utf_8 # precompiled from /..../lib/python2.7/encodings/utf_8.pyc 
Python 2.7.15 (default, May  7 2018, 17:08:03) 
[GCC 4.2.1 Compatible Apple LLVM 9.1.0 (clang-902.0.39.1)] on darwin 
Type "help", "copyright", "credits" or "license" for more information. 
3 
# -- clean-up output omitted -- 

其中的每个 import ... 行引用一个内置模块(Python 二进制文件的一部分,用 C 实现)或一个 .pyc 字节码缓存文件.有 17 个这样的文件被导入甚至在脚本代码运行之前。

主脚本中的 3 行代码转换为另外 9 条字节码指令:

>>> import dis 
>>> dis.dis(compile(r'''\ 
... X = 1 
... Y = 2 
... print X + Y 
... ''', '', 'exec')) 
  2           0 LOAD_CONST               0 (1) 
              3 STORE_NAME               0 (X) 
 
  3           6 LOAD_CONST               1 (2) 
              9 STORE_NAME               1 (Y) 
 
  4          12 LOAD_NAME                0 (X) 
             15 LOAD_NAME                1 (Y) 
             18 BINARY_ADD 
             19 PRINT_ITEM 
             20 PRINT_NEWLINE 
             21 LOAD_CONST               2 (None) 
             24 RETURN_VALUE 

(我忽略了末尾的 2 个字节码,编码了一个额外的 return None,这并不真正适用于模块)。