查找用C实现的方法的源代码? [英] Finding the source code of methods implemented in C?

查看:113
本文介绍了查找用C实现的方法的源代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请注意,我问的这个问题仅供参考

Please note I am asking this question for informational purposes only

我知道标题听起来像查找内置Python函数的源代码?。但是,让我解释一下。

I know the title sound like a duplicate of Finding the source code for built-in Python functions?. But let me explain.

例如,我想找到 most_common 方法的 collections.Counter 类。由于 Counter 类是在python中实现的,因此我可以使用 inspect 模块获取其源代码。

Say for example, I want to find the source code of most_common method of collections.Counter class. Since the Counter class is implemented in python I could use the inspect module get it's source code.

>>> import inspect
>>> import collections
>>> print(inspect.getsource(collections.Counter.most_common))

这将打印

    def most_common(self, n=None):
        '''List the n most common elements and their counts from the most
        common to the least.  If n is None, then list all element counts.

        >>> Counter('abcdeabcdabcaba').most_common(3)
        [('a', 5), ('b', 4), ('c', 3)]

        '''
        # Emulate Bag.sortedByCount from Smalltalk
        if n is None:
            return sorted(self.items(), key=_itemgetter(1), reverse=True)
        return _heapq.nlargest(n, self.items(), key=_itemgetter(1))

该方法或类在C inspect.getsource 中实现,将引发 TypeError

So if the method or class is implemented in C inspect.getsource will raise TypeError.

>>> my_list = []
>>> print(inspect.getsource(my_list.append))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\abdul.niyas\AppData\Local\Programs\Python\Python36-32\lib\inspect.py", line 968, in getsource
    lines, lnum = getsourcelines(object)
  File "C:\Users\abdul.niyas\AppData\Local\Programs\Python\Python36-32\lib\inspect.py", line 955, in getsourcelines
    lines, lnum = findsource(object)
  File "C:\Users\abdul.niyas\AppData\Local\Programs\Python\Python36-32\lib\inspect.py", line 768, in findsource
    file = getsourcefile(object)
  File "C:\Users\abdul.niyas\AppData\Local\Programs\Python\Python36-32\lib\inspect.py", line 684, in getsourcefile
    filename = getfile(object)
  File "C:\Users\abdul.niyas\AppData\Local\Programs\Python\Python36-32\lib\inspect.py", line 666, in getfile
    'function, traceback, frame, or code object'.format(object))
TypeError: <built-in method append of list object at 0x00D3A378> is not a module, class, method, function, traceback, frame, or code object.

所以我的问题是,有什么办法(或使用第三方程序包)可以还能找到用C实现的类或方法的源代码吗?

So my question is, Is there is any way(or Using third party package?) that we can find the source code of class or method implemented in C as well?

即,类似这样的东西

>> print(some_how_or_some_custom_package([].append))


int
PyList_Append(PyObject *op, PyObject *newitem)
{
    if (PyList_Check(op) && (newitem != NULL))
        return app1((PyListObject *)op, newitem);
    PyErr_BadInternalCall();
    return -1;
}


推荐答案

不,没有。没有可从Python访问的元数据,它可以让您查找原始源文件。这样的元数据必须由Python开发人员显式创建,而对其实现没有明显的好处。

No, there is not. There is no metadata accessible from Python that will let you find the original source file. Such metadata would have to be created explicitly by the Python developers, without a clear benefit as to what that would achieve.

首先,绝大多数Python安装不包括C源代码。接下来,虽然可以想象到Python语言的用户能够阅读Python源代码,但是Python的用户群非常广泛,并且很多人不了解C或对C代码的工作方式不感兴趣,最后,甚至是开发人员知道C不能指望必须阅读 Python C API文档

First and foremost, the vast majority of Python installations do not include the C source code. Next, while you could conceivably expect users of the Python language to be able to read Python source code, Python's userbase is very broad and a large number do not know C or are interested in how the C code works, and finally, even developers that know C can't be expected to have to read the Python C API documentation, something that quickly becomes a requirement if you want to understand the Python codebase.

C文件不会直接映射到特定的输出文件,这与Python字节码缓存文件和脚本不同,这很快成为了一项要求。除非使用符号表创建调试版本,否则编译器不会将源文件名保留在其输出的生成的目标文件( .o )中,链接器记录也不会什么 .o 文件进入了生成的结果。所有C文件也最终都不会贡献给相同的可执行文件或动态共享对象文件。一些成为Python二进制文件的一部分,另一些成为可加载的扩展,并且混合是可配置的,并且取决于编译时可用的外部库。

C files do not directly map to a specific output file, unlike Python bytecode cache files and scripts. Unless you create a debug build with a symbol table, the compiler doesn't retain the source filename in the generated object file (.o) it outputs, nor will the linker record what .o files went into the result it produces. Nor do all C files end up contributing to the same executable or dynamic shared object file; some become part of the Python binary, others become loadable extensions, and the mix is configurable and dependent on what external libraries are available at the time of compilation.

在makefile, setup.py 和C预压缩器宏之间,以及输入文件和哪些行的组合实际用于创建每个输出文件的源代码也各不相同。最后但并非最不重要的一点,因为在运行时不再参考C源文件,所以不能期望它们在相同的原始位置仍然可用,因此,即使存储了一些元数据,您仍然无法将其映射回

And between makefiles, setup.py and C pre-propressor macros, the combination of input files and what lines of source code are actually used to create each of the output files also varies. Last but not least, because the C source files are no longer consulted at runtime, they can't be expected to still be available in the same original location, so even if there was some metadata stored you still couldn't map that back to the original.

所以,记住一些关于Python C-API工作原理的基本规则只是容易

So, it's just easier to just remember a few base rules about how the Python C-API works, then map that back to the C code with a few informed code searches.

或者,下载Python源代码并创建调试版本,并使用良好的IDE来帮助您映射符号和这样回到源文件。不同的编译器,平台和IDE具有不同的支持符号表进行调试的方法。

Alternatively, download the Python source code and create a debug build, and use a good IDE to help you map symbols and such back to source files. Different compilers, platforms and IDEs have different methods of supporting symbol tables for debugging.

这篇关于查找用C实现的方法的源代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆