在 Python 3.x 中,为什么磁盘上没有 itertools 共享对象?

In Python 3.x, why is there not an itertools shared-object on disk?

itertools C module 是否以某种方式包含在 3.x 的主要 Python 二进制文件中?

假设已构建并包含 C 模块,它看起来是:

>>> import inspect
>>> import itertools
>>>
>>> inspect.getsourcefile(itertools)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/Cellar/python3/3.4.3_2/Frameworks/Python.framework/Versions/3.4/lib/python3.4/inspect.py", line 571, in getsourcefile
    filename = getfile(object)
  File "/usr/local/Cellar/python3/3.4.3_2/Frameworks/Python.framework/Versions/3.4/lib/python3.4/inspect.py", line 518, in getfile
    raise TypeError('{!r} is a built-in module'.format(object))
TypeError: <module 'itertools' (built-in)> is a built-in module

我在我的系统上找不到 Python 3.x 的 itertools.so,但是 2.7 有一个。

我注意到一些其他 C 模块作为共享对象(locate '.so' | grep -E '^/usr/local/' | grep '.so' 例如 mmap.so)存在于磁盘上,那么 itertools 是怎么回事?没有共享库怎么用?

inspect.py 的 Python 包装器附近的 makefile 中有提示:

/usr/local/Cellar/python3/3.4.3_2/Frameworks/Python.framework/Versions/3.4/lib/python3.4/config-3.4m/Makefile

我们可以看到 itertools.c 来源的构建规则:

1668 Modules/itertoolsmodule.o: $(srcdir)/Modules/itertoolsmodule.c; $(CC) $(PY_CORE_CFLAGS)  -c $(srcdir)/Modules/itertoolsmodule.c -o      Modules/itertoolsmodule.o

然后稍微跟踪一下,看看它被捆绑在:

 24 MODOBJS= ..  Modules/itertoolsmodule.o  ... Modules/xxsubtype.o

 462 # objects that get linked into the Python library
 463 LIBRARY_OBJS_OMIT_FROZEN=   \
...
 470         $(MODOBJS)
 471
 472 LIBRARY_OBJS=   \
 473         $(LIBRARY_OBJS_OMIT_FROZEN) \
 474         Python/frozen.o
 ...
 553 # Build the interpreter
 554 $(BUILDPYTHON): Modules/python.o $(LIBRARY) $(LDLIBRARY) $(PY3LIBRARY)
 555     $(LINKCC) $(PY_LDFLAGS) $(LINKFORSHARED) -o $@ Modules/python.o $(BLDLIBRARY) $(LIBS) $(MODLIBS) $(SYSLIBS) $(LDLAST)
 556
 557 platform: $(BUILDPYTHON) pybuilddir.txt
 558     $(RUNSHARED) $(PYTHON_FOR_BUILD) -c 'import sys ; from sysconfig import get_platform ; print(get_platform()+"-"+sys.version[0:     3])' >platform
 589 # Build static library
 ...
 598     $(AR) $(ARFLAGS) $@ $(MODOBJS)
 599     $(RANLIB) $@

 944 $(LIBRARY_OBJS) $(MODOBJS) Modules/python.o: $(PYTHON_HEADERS)

或者如果通过 distutils 创建,路径将类似于:/usr/local/Cellar/python3/3.4.3_2/Frameworks/Python.framework/Versions/3.4/lib/python3.4/_sysconfigdata.py

并假设这被内置到动态库中:

Ξ ~ → strings /usr/local/Cellar/python3/3.4.3_2/Frameworks/Python.framework/Versions/3.4/lib/libpython3.4.dylib | grep itertools
itertools
itertools._tee_dataobject
itertools._tee
itertools._grouper
itertools.groupby
itertools.repeat
itertools.product
...

这意味着在构建时,itertools.c 模块包含在 libpython 动态库中,这意味着它现在是标准库的一部分。

在Python3中,itertools扩展被编译成主要的Python二进制文件:

>>> import sys
>>> 'itertools' in sys.builtin_module_names
True

参见sys.builtin_module_names documentation

A tuple of strings giving the names of all modules that are compiled into this Python interpreter.

该模块已添加到 Python 二进制文件中,因为它在 Python 标准库中非常 广泛使用。

要包含的模块列表取自 Modules/Setup.dist file in the Python distribution; itertools was added together with _collections, as it is a transient dependency of that module. See issue #9545