问题描述
itertools C模块是否包含在主程序中3.x中的Python二进制文件?
Is the itertools C module included somehow in the main Python binary in 3.x?
假定已构建并包含C模块,它看起来像是:
Assuming that the C module is built and included, which it appears to be:
>>> 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的.
I can't find an itertools.so
for Python 3.x on my system, but there's one for 2.7.
我注意到其他一些C模块作为磁盘上的共享对象(locate '.so' | grep -E '^/usr/local/' | grep '.so'
例如mmap.so
)存在,那么itertools
有什么用呢?如果没有共享库,怎么用?
I noted that some other C modules exist as shared objects (locate '.so' | grep -E '^/usr/local/' | grep '.so'
e.g. mmap.so
) on disk, so what's the deal with itertools
? How can I use it if there's not a shared library?
推荐答案
makefile中的提示靠近inspect.py
的Python包装器:
There are hints in the makefile that's near the Python wrapper of inspect.py
:
/usr/local/Cellar/python3/3.4.3_2/Frameworks/Python.framework/Versions/3.4/lib/python3.4/config-3.4m/Makefile
我们可以看到itertools.c源代码的构建规则:
We can see the build rules for the itertools.c source:
1668 Modules/itertoolsmodule.o: $(srcdir)/Modules/itertoolsmodule.c; $(CC) $(PY_CORE_CFLAGS) -c $(srcdir)/Modules/itertoolsmodule.c -o Modules/itertoolsmodule.o
然后对其进行一点跟踪以查看其是否已捆绑在其中:
And then trace it a little to see that it's being bundled in:
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
Or if made via distutils
, the path will be something like: /usr/local/Cellar/python3/3.4.3_2/Frameworks/Python.framework/Versions/3.4/lib/python3.4/_sysconfigdata.py
并假定将其内置到动态库中
And assuming that this gets built into a dynamic library:
Ξ ~ → 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
动态库中,这意味着它现在已成为标准库的一部分.
Which means that at build time, the itertools.c module gets included in the libpython
dynamic library, meaning that it's now part of the standard library.
这篇关于在Python 3.x中,为什么磁盘上没有itertools共享对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!