Python源$ C ​​$ C为内置"在"操作者 [英] Python source code for built-in "in" operator

查看:138
本文介绍了Python源$ C ​​$ C为内置"在"操作者的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找到内置的运营商中(C)的实施Python源$ C ​​$ C。我已搜查了内置函数源$ C ​​$ C, bltinmodule.c ,但无法找到该运营商的实施。我在哪里可以找到这个实现?

I am trying to find the implementation of the built-in in operator in the (C) Python source code. I have searched in the built-in functions source code, bltinmodule.c, but cannot find the implementation of this operator. Where can I find this implementation?

我的目标是通过扩大该搜索不同的C语言实现,以提高在Python子字符串搜索,虽然我不知道,如果Python中已经使用的想法,我有。

My goal is to improve the sub-string search in Python by extending different C implementations of this search, although I am not sure if Python already uses the idea I have.

推荐答案

要找到实施的任何的蟒蛇运营商,首先找出字节code Python中生成它,使用< A HREF =htt​​p://docs.python.org/library/dis.html#dis.dis> dis.dis 功能:

To find the implementation of any python operator, first find out what bytecode Python generates for it, using the dis.dis function:

>>> def inop():
...     '0' in []
... 
>>> dis.dis(inop)
  2           0 LOAD_CONST               1 ('0')
              3 LOAD_CONST               2 (())
              6 COMPARE_OP               6 (in)
              9 POP_TOP             
             10 LOAD_CONST               0 (None)
             13 RETURN_VALUE        

运营商就变成了 COMPARE_OP 字节code。现在,您可以在 的Python / ceval.c <评价循环跟踪这/ code>

The in operator becomes a COMPARE_OP byte code. Now you can trace this in the evaluation loop in Python/ceval.c:

TARGET(COMPARE_OP)
    w = POP();
    v = TOP();
    x = cmp_outcome(oparg, v, w);
    Py_DECREF(v);
    Py_DECREF(w);
    SET_TOP(x);
    if (x == NULL) break;
    PREDICT(POP_JUMP_IF_FALSE);
    PREDICT(POP_JUMP_IF_TRUE);
    DISPATCH();

cmp_outcome()在同一文件中定义,而运营商是交换机之一:

cmp_outcome() is defined in the same file, and the in operator is one of the switches:

case PyCmp_IN:
    res = PySequence_Contains(w, v);
    if (res < 0)
         return NULL;
    break;

一个快速的grep向我们展示了其中 PySequence_Contains 的定义,在的对象/ abstract.c

A quick grep shows us where PySequence_Contains is defined, in Objects/abstract.c:

int
PySequence_Contains(PyObject *seq, PyObject *ob)
{
    Py_ssize_t result;
    PySequenceMethods *sqm = seq->ob_type->tp_as_sequence;
    if (sqm != NULL && sqm->sq_contains != NULL)
        return (*sqm->sq_contains)(seq, ob);
    result = _PySequence_IterSearch(seq, ob, PY_ITERSEARCH_CONTAINS);
    return Py_SAFE_DOWNCAST(result, Py_ssize_t, int);
}

PySequence_Contains 从而使用<一个href=\"http://docs.python.org/c-api/typeobj.html#PySequenceMethods.sq_contains\"><$c$c>sq_contains在序列对象结构或以其他方式迭代搜索,Python的C对象插槽。

PySequence_Contains thus uses the sq_contains slot on the Sequence object structure or an iterative search otherwise, for Python C objects.

对于Python 3 UNI code字符串对象,该插槽被实现为<$c$c>PyUni$c$c_Contains在对象/ UNI codeobject.c ,在Python 2也想看看<一个href=\"http://hg.python.org/cpython/file/2370e331241b/Objects/stringobject.c#l1145\"><$c$c>string_contains在对象/ stringobject.c 。基本上只用grep对由不同的Python类型的各种实现 sq_contains 在对象/子目录中。

For python 3 unicode string objects, this slot is implemented as PyUnicode_Contains in Objects/unicodeobject.c, in Python 2 you also want to check out string_contains in Objects/stringobject.c. Basically just grep for sq_contains in the Objects/ subdirectory for the various implementations by the different Python types.

有关通用的Python对象,它们也同样吸引注意,对象/ typeobject.c 这推迟到 __ __包含在自定义类的方法,如果是这样定义的。

For generic python objects, it's interesting to note that Objects/typeobject.c defers this to the __contains__ method on custom classes, if so defined.

这篇关于Python源$ C ​​$ C为内置&QUOT;在&QUOT;操作者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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