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

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 星期四

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,911评论 5 460
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 82,014评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 142,129评论 0 320
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,283评论 1 264
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,159评论 4 357
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,161评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,565评论 3 382
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,251评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,531评论 1 292
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,619评论 2 310
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,383评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,255评论 3 313
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,624评论 3 299
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,916评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,199评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,553评论 2 342
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,756评论 2 335

推荐阅读更多精彩内容