美文网首页
Python 源码剖析-INT对象(下)

Python 源码剖析-INT对象(下)

作者: 敬贤icode121 | 来源:发表于2019-02-02 17:23 被阅读0次

    INT函数与对象剖析(下)

    引言

    根据前两章的分析,我们主要通过 type(int) == 'type'这个线索找到了int()函数调用的底层原理,本节我们将按照(中)的[3] Undefined 程序分支, 来继续讲解 int() 函数给一个对象的的ob_type的类型赋值。从而使得type(1) == 'int'成立。

    继续回到PyInt_FromLong

    if (free_list == NULL) {
            if ((free_list = fill_free_list()) == NULL)
                return NULL;
        }
        /* Inline PyObject_New */
        v = free_list;
        free_list = (PyIntObject *)Py_TYPE(v);
        (void)PyObject_INIT(v, &PyInt_Type);
        v->ob_ival = ival;
        return (PyObject *) v;
    

    关注(void)PyObject_INIT(v, &PyInt_Type);
    PyObject_Init 这个宏就是将对象v的ob_type设置为 PyInt_Type->ob_type (也就是int),从而完成对对象的类型更新。
    这里的PyInt_Type是Python的一个重要的Type型对象,诸如PyString_Type,PyType_Type都有个相似的定义:

    PyTypeObject PyInt_Type = {
        PyVarObject_HEAD_INIT(&PyType_Type, 0)
        "int",      //ob_tyoe
        sizeof(PyIntObject),
        0,
        (destructor)int_dealloc,                    /* tp_dealloc */
        (printfunc)int_print,                       /* tp_print */
        0,                                          /* tp_getattr */
        0,                                          /* tp_setattr */
        (cmpfunc)int_compare,                       /* tp_compare */
        (reprfunc)int_to_decimal_string,            /* tp_repr */
        &int_as_number,                             /* tp_as_number */
        0,                                          /* tp_as_sequence */
        0,                                          /* tp_as_mapping */
        (hashfunc)int_hash,                         /* tp_hash */
        0,                                          /* tp_call */
        (reprfunc)int_to_decimal_string,            /* tp_str */
        PyObject_GenericGetAttr,                    /* tp_getattro */
        0,                                          /* tp_setattro */
        0,                                          /* tp_as_buffer */
        Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES |
            Py_TPFLAGS_BASETYPE | Py_TPFLAGS_INT_SUBCLASS,          /* tp_flags */
        int_doc,                                    /* tp_iternext */
            ....
        int_methods,                                /* tp_methods */
        0,                                          /* tp_members */
        int_getset,                                 /* tp_init */
        0,                                          /* tp_alloc */
        int_new,        //上一章分析的int_new在这里      /* tp_new */
    };
    

    可以看到这是一个PyTypeObject结构体对象。
    该C结构体定义如下:

    typedef struct _typeobject {
        PyObject_VAR_HEAD  //包含了引用计数,对象的value值信息
        const char *tp_name; /* For printing, in format "<module>.<name>" */
        Py_ssize_t tp_basicsize, tp_itemsize; /* For allocation */
        /* Methods to implement standard operations */
        destructor tp_dealloc;
        printfunc tp_print;
        getattrfunc tp_getattr;
        setattrfunc tp_setattr;
        cmpfunc tp_compare;
        reprfunc tp_repr;
        /* Method suites for standard classes */
        PyNumberMethods *tp_as_number;
        PySequenceMethods *tp_as_sequence;
        PyMappingMethods *tp_as_mapping;
        /* More standard operations (here for binary compatibility) */
        hashfunc tp_hash;
        ternaryfunc tp_call;
        reprfunc tp_str;
        getattrofunc tp_getattro;
        setattrofunc tp_setattro;
        /* Functions to access object as input/output buffer */
        PyBufferProcs *tp_as_buffer;
        /* Flags to define presence of optional/expanded features */
        long tp_flags;
        const char *tp_doc; /* Documentation string */
        /* call function for all accessible objects */
        traverseproc tp_traverse;
        /* delete references to contained objects */
        inquiry tp_clear;
        /* Assigned meaning in release 2.1 */
        /* rich comparisons */
        richcmpfunc tp_richcompare;
        /* weak reference enabler */
        Py_ssize_t tp_weaklistoffset;
        /* Iterators */
        getiterfunc tp_iter;
        iternextfunc tp_iternext;
        struct PyMethodDef *tp_methods;
        struct PyMemberDef *tp_members;
        struct PyGetSetDef *tp_getset;
        struct _typeobject *tp_base;
        PyObject *tp_dict;
        descrgetfunc tp_descr_get;
        descrsetfunc tp_descr_set;
        Py_ssize_t tp_dictoffset;
        initproc tp_init;
        allocfunc tp_alloc;
        newfunc tp_new;
        ...
    } PyTypeObject;
    

    可以直观的了解到,一个整数对象在对应的C struct中包含大量的元信息。
    在这里仅仅只看/* tp_methods 和 int_as_number*/中的包含的方法,其余的请读者自行了解观读源码:

    • 首先看看dir(int),观察int对象的方法
      [图片上传失败...(image-5d36a2-1549099402971)]
      一些不常用的发放,其实就是int(type) 对象的原生方法,e.g. bit_length
      这种通过 int.xx 访问的属性都 封装在 上述的 tp_methods中
      [图片上传失败...(image-d818a2-1549099402971)]

    • int_as_number封装大量的内置函数
      结构体如下:

    static PyNumberMethods int_as_number = {
        (binaryfunc)int_add,        /*nb_add*/
        (binaryfunc)int_sub,        /*nb_subtract*/
        (binaryfunc)int_mul,        /*nb_multiply*/
        (binaryfunc)int_mod,        /*nb_remainder*/
        (binaryfunc)int_divmod,     /*nb_divmod*/
         ....
        (binaryfunc)int_div,        /* nb_floor_divide */
         ...
        (unaryfunc)int_int,         /* nb_index */
    };
    

    举个🌰:

    • int.divmod()
      * 结果是元组,更精简

    divmod(2,4) == (0,2)
    divmod(1,5) == (0,1)
    divmod(1,0) => ZeroDivisionError: integer division or modulo by zero
    divmod(1,-5) == (-1,-4)

    static PyObject *
    int_divmod(PyIntObject *x, PyIntObject *y)
    {
       long xi, yi;
       long d, m;
       CONVERT_TO_LONG(x, xi);
       CONVERT_TO_LONG(y, yi);
       switch (i_divmod(xi, yi, &d, &m)) /*i_divmod 是enum体,其结果分为成功,除0失败,溢出三种*/{
       case DIVMOD_OK:
           return Py_BuildValue("(ll)", d, m);
       case DIVMOD_OVERFLOW:
           return PyLong_Type.tp_as_number->nb_divmod((PyObject *)x,(PyObject *)y);
       default:
           return NULL;
       }
    }
    

    内置的 Py_BuildValue("(ll)"...)
    支持的符号l为:
    Convert a Python integer to a C long int.
    更多详情请参考:Py_BuildValue

    总结

    本章不涉及到Python代码的编译生成字节码的过程,仅仅讨论从终端输入 int('11') 到输出 11 的过程, 对于Python虚拟机捕获 int 调用CALL_FUNCTION 的过程到 int 对象的内部结构,以及Python中的一切皆为对象有了很好的启示。下一章将继续介绍源码系列,敬请期待。

    鸣谢作者,感谢阅读

    ©敬贤。 分享知识,便是分享快乐。
    2018-06-14 00:51:25 星期四

    相关文章

      网友评论

          本文标题:Python 源码剖析-INT对象(下)

          本文链接:https://www.haomeiwen.com/subject/aeossqtx.html