python tp_ready函数分析
int
PyType_Ready(PyTypeObject *type)
{
PyObject *dict, *bases;
PyTypeObject *base;
Py_ssize_t i, n; if (type->tp_flags & Py_TPFLAGS_READY) {
assert(type->tp_dict != NULL);
return ;
}
assert((type->tp_flags & Py_TPFLAGS_READYING) == ); type->tp_flags |= Py_TPFLAGS_READYING; #ifdef Py_TRACE_REFS
/* PyType_Ready is the closest thing we have to a choke point
* for type objects, so is the best place I can think of to try
* to get type objects into the doubly-linked list of all objects.
* Still, not all type objects go thru PyType_Ready.
*/
_Py_AddToAllObjects((PyObject *)type, );
#endif /* Initialize tp_base (defaults to BaseObject unless that's us) */
base = type->tp_base;
if (base == NULL && type != &PyBaseObject_Type) {
base = type->tp_base = &PyBaseObject_Type;
Py_INCREF(base);
} /* Now the only way base can still be NULL is if type is
* &PyBaseObject_Type.
*/ /* Initialize the base class */
if (base && base->tp_dict == NULL) {
if (PyType_Ready(base) < )
goto error;
} /* Initialize ob_type if NULL. This means extensions that want to be
compilable separately on Windows can call PyType_Ready() instead of
initializing the ob_type field of their type objects. */
/* The test for base != NULL is really unnecessary, since base is only
NULL when type is &PyBaseObject_Type, and we know its ob_type is
not NULL (it's initialized to &PyType_Type). But coverity doesn't
know that. */
if (Py_TYPE(type) == NULL && base != NULL)
Py_TYPE(type) = Py_TYPE(base); /* Initialize tp_bases */
bases = type->tp_bases;
if (bases == NULL) {
if (base == NULL)
bases = PyTuple_New();
else
bases = PyTuple_Pack(, base);
if (bases == NULL)
goto error;
type->tp_bases = bases;
} /* Initialize tp_dict */
dict = type->tp_dict;
if (dict == NULL) {
dict = PyDict_New();
if (dict == NULL)
goto error;
type->tp_dict = dict;
} /* Add type-specific descriptors to tp_dict */
if (add_operators(type) < )
goto error;
if (type->tp_methods != NULL) {
if (add_methods(type, type->tp_methods) < )
goto error;
}
if (type->tp_members != NULL) {
if (add_members(type, type->tp_members) < )
goto error;
}
if (type->tp_getset != NULL) {
if (add_getset(type, type->tp_getset) < )
goto error;
} /* Calculate method resolution order */
if (mro_internal(type) < ) {
goto error;
} /* Inherit special flags from dominant base */
if (type->tp_base != NULL)
inherit_special(type, type->tp_base); /* Initialize tp_dict properly */
bases = type->tp_mro;
assert(bases != NULL);
assert(PyTuple_Check(bases));
n = PyTuple_GET_SIZE(bases);
for (i = ; i < n; i++) {
PyObject *b = PyTuple_GET_ITEM(bases, i);
if (PyType_Check(b))
inherit_slots(type, (PyTypeObject *)b);
} /* Sanity check for tp_free. */
if (PyType_IS_GC(type) && (type->tp_flags & Py_TPFLAGS_BASETYPE) &&
(type->tp_free == NULL || type->tp_free == PyObject_Del)) {
/* This base class needs to call tp_free, but doesn't have
* one, or its tp_free is for non-gc'ed objects.
*/
PyErr_Format(PyExc_TypeError, "type '%.100s' participates in "
"gc and is a base type but has inappropriate "
"tp_free slot",
type->tp_name);
goto error;
} /* if the type dictionary doesn't contain a __doc__, set it from
the tp_doc slot.
*/
if (PyDict_GetItemString(type->tp_dict, "__doc__") == NULL) {
if (type->tp_doc != NULL) {
PyObject *doc = PyString_FromString(type->tp_doc);
if (doc == NULL)
goto error;
PyDict_SetItemString(type->tp_dict, "__doc__", doc);
Py_DECREF(doc);
} else {
PyDict_SetItemString(type->tp_dict,
"__doc__", Py_None);
}
} /* Some more special stuff */
base = type->tp_base;
if (base != NULL) {
if (type->tp_as_number == NULL)
type->tp_as_number = base->tp_as_number;
if (type->tp_as_sequence == NULL)
type->tp_as_sequence = base->tp_as_sequence;
if (type->tp_as_mapping == NULL)
type->tp_as_mapping = base->tp_as_mapping;
if (type->tp_as_buffer == NULL)
type->tp_as_buffer = base->tp_as_buffer;
} /* Link into each base class's list of subclasses */
bases = type->tp_bases;
n = PyTuple_GET_SIZE(bases);
for (i = ; i < n; i++) {
PyObject *b = PyTuple_GET_ITEM(bases, i);
if (PyType_Check(b) &&
add_subclass((PyTypeObject *)b, type) < )
goto error;
} /* All done -- set the ready flag */
assert(type->tp_dict != NULL);
type->tp_flags =
(type->tp_flags & ~Py_TPFLAGS_READYING) | Py_TPFLAGS_READY;
return ; error:
type->tp_flags &= ~Py_TPFLAGS_READYING;
return -;
}
python tp_ready函数分析的更多相关文章
- Python回调函数用法实例详解
本文实例讲述了Python回调函数用法.分享给大家供大家参考.具体分析如下: 一.百度百科上对回调函数的解释: 回调函数就是一个通过函数指针调用的函数.如果你把函数的指针(地址)作为参数传递给另一个函 ...
- Python之函数与变量
本节内容 函数介绍及其作用 函数的定义与调用 函数的参数说明 全局变量与局部变量 值传递和引用传递 一.函数的介绍及其作用 编程语言中的函数与数学中的函数是有区别的:数学中的函数有参数(输入),就会有 ...
- 【C++实现python字符串函数库】一:分割函数:split、rsplit
[C++实现python字符串函数库]split()与rsplit()方法 前言 本系列文章将介绍python提供的字符串函数,并尝试使用C++来实现这些函数.这些C++函数在这里做单独的分析,最后我 ...
- 第三章:Python基础の函数和文件操作实战
本課主題 Set 集合和操作实战 函数介紹和操作实战 参数的深入介绍和操作实战 format 函数操作实战 lambda 表达式介绍 文件操作函数介紹和操作实战 本周作业 Set 集合和操作实战 Se ...
- Python文章相关性分析---金庸武侠小说分析
百度到<金庸小说全集 14部>全(TXT)作者:金庸 下载下来,然后读取内容with open('names.txt') as f: data = [line.strip() for li ...
- Python 3 函数自由变量的大坑
Python中函数是一个对象, 和整数,字符串等对象有很多相似之处,例如可以作为其他函数的参数或返回对象, Python中的函数还可以携带自由变量, 两者无疑极大增进了Python的表达力. 但是Py ...
- 学以致用三十二-----python中函数的括号使用
一直以来对python中函数括号的使用,有点分不清楚,到底什么时候用括号,什么时候不用括号,造成了很大看困惑. 今天来总结下. class aaa(): y = 'you' def __init__( ...
- 用python探索和分析网络数据
Edited by Markdown Refered from: John Ladd, Jessica Otis, Christopher N. Warren, and Scott Weingart, ...
- python第六天 函数 python标准库实例大全
今天学习第一模块的最后一课课程--函数: python的第一个函数: 1 def func1(): 2 print('第一个函数') 3 return 0 4 func1() 1 同时返回多种类型时, ...
随机推荐
- Charles抓包基本用法
Charles抓包 浏览器发送和接受的所有请求都可以抓到 1.可以定位问题(如果看不出来是服务端问题还是前端问题) 2.可以设置弱网模式 清空请求按钮如图: 抓包: 1 打开charles,在浏览器中 ...
- JDK8源码之Arrays
Arrays是一个工具类,包含了各种数组的操作方法,包括排序.搜索.转化,在JDK1.8中新增了一些方法,对原有一些方法的并发性做了增强,极大地提高了原有方法的性能. 1.rangeCheck(int ...
- php \r\n
代码a: 复制代码 代码如下: <?php echo'hello</br>'; echo'world!'; ?> output: helllo world! 代码b: 复制代码 ...
- 团队-爬虫豆瓣top250项目-模块开发过程
项目托管平台地址:https://github.com/gengwenhao/GetTop250.git 开发模块功能: "get_info()单个页面的爬取"功能,开发时间:15 ...
- ASP.NET MVC 阻止通过Url直接访问服务器上的静态文件
某些情况下我们需要在服务器上保存一些静态文件,比如用户上传到服务器的文件,如果刚好这些文件的保存目录是应用程序目录下的一个子目录的话,别人就可以通过Url直接访问这个文件. 例如:在应用程序目录下的U ...
- 阿里的maven镜像仓库,eclipse中使用maven下载jar包的时候提升速度
<mirrors> <mirror> <id>alimaven</id> <name>aliyun maven</name> & ...
- 剑指Offer 57. 二叉树的下一个结点 (二叉树)
题目描述 给定一个二叉树和其中的一个结点,请找出中序遍历顺序的下一个结点并且返回.注意,树中的结点不仅包含左右子结点,同时包含指向父结点的指针. 题目地址 https://www.nowcoder.c ...
- spring(一、原理、IOC、AOP、依赖注入)
1.spring原理 内部最核心的就是IOC了,动态注入,让一个对象的创建不用new了,可以自动的生产,这其实就是利用java里的反射,反射其实就是在运行时动态的去创建.调用对象,Spring就是在运 ...
- char * p = "abc"与const char *p = "abc"
char * p = "abc"与const char *p = "abc"的区别是什么呢? 第一个语句会产生问题: warning: deprecated c ...
- day05 集合
今日进度(数据类型) 集合 内存相关 深浅拷贝 1.集合表示 1.无序 2.不重复 3.hash查找 #问题:v={}表示? set: v1=set()#空集合 v1={1,2,3,4,5} dict ...