模块对象

There are only a few functions special to module objects.

PyTypeObject PyModule_Type

此实例的 PyTypeObject 表示 Python 模块类型。暴露这给 Python 程序作为 types.ModuleType .

int PyModule_Check ( PyObject  *p )

返回 True 若 p 是模块对象,或模块对象的子类型。

int PyModule_CheckExact ( PyObject  *p )

返回 True 若 p 是模块对象,但不是子类型的 PyModule_Type .

PyObject * PyModule_NewObject ( PyObject  *name )

返回新的模块对象采用 __name__ 属性设置为 name 。模块的 __name__ , __doc__ , __package__ ,和 __loader__ 属性被填充(除了 __name__ 被设为 None );调用者负责提供 __file__ 属性。

3.3 版新增。

3.4 版改变: __package__ and __loader__ 被设为 None .

PyObject * PyModule_New ( const char  *name )
返回值:新引用。

类似于 PyImport_NewObject() , but the name is an UTF-8 encoded string instead of a Unicode object.

PyObject * PyModule_GetDict ( PyObject  *module )
返回值:借位引用。

Return the dictionary object that implements module ‘s namespace; this object is the same as the __dict__ attribute of the module object. This function never fails. It is recommended extensions use other PyModule_*() and PyObject_*() functions rather than directly manipulate a module’s __dict__ .

PyObject * PyModule_GetNameObject ( PyObject  *module )

返回 module ‘s __name__ value. If the module does not provide one, or if it is not a string, SystemError is raised and NULL 被返回。

3.3 版新增。

char* PyModule_GetName ( PyObject  *module )

类似于 PyModule_GetNameObject() but return the name encoded to 'utf-8' .

PyObject * PyModule_GetFilenameObject ( PyObject  *module )

Return the name of the file from which module was loaded using module ‘s __file__ attribute. If this is not defined, or if it is not a unicode string, raise SystemError 并返回 NULL ; otherwise return a reference to a Unicode object.

3.2 版新增。

char* PyModule_GetFilename ( PyObject  *module )

类似于 PyModule_GetFilenameObject() 但返回 UTF-8 编码的文件名。

从 3.2 版起弃用: PyModule_GetFilename() 引发 UnicodeEncodeError 当文件名不可编码时,使用 PyModule_GetFilenameObject() 代替。

void* PyModule_GetState ( PyObject  *module )

Return the “state” of the module, that is, a pointer to the block of memory allocated at module creation time, or NULL 。见 PyModuleDef.m_size .

PyModuleDef * PyModule_GetDef ( PyObject  *module )

返回指针指向 PyModuleDef struct from which the module was created, or NULL if the module wasn’t created with PyModule_Create() .

PyObject * PyState_FindModule ( PyModuleDef  *def )

Returns the module object that was created from def for the current interpreter. This method requires that the module object has been attached to the interpreter state with PyState_AddModule() beforehand. In case the corresponding module object is not found or has not been attached to the interpreter state yet, it returns NULL.

int PyState_AddModule ( PyObject  *module , PyModuleDef  *def )

Attaches the module object passed to the function to the interpreter state. This allows the module object to be accessible via PyState_FindModule() .

3.3 版新增。

int PyState_RemoveModule ( PyModuleDef  *def )

移除创建的模块对象自 def 从解释器状态。

3.3 版新增。

初始化 C 模块

These functions are usually used in the module initialization function.

PyObject * PyModule_Create ( PyModuleDef  *module )

创建新的模块对象,给定定义在 module 。这的行为像 PyModule_Create2() with module_api_version 设为 PYTHON_API_VERSION .

PyObject * PyModule_Create2 ( PyModuleDef  *module , int  module_api_version )

创建新的模块对象,给定定义在 module ,假定 API 版本 module_api_version 。若该版本不匹配正运行解释器版本, RuntimeWarning 被发射。

注意

此函数的大多数用途应该是使用 PyModule_Create() 代替;才使用这若确定需要它。

PyModuleDef

This struct holds all information that is needed to create a module object. There is usually only one static variable of that type for each module, which is statically initialized and then passed to PyModule_Create() in the module initialization function.

PyModuleDef_Base m_base

始终将此成员初始化为 PyModuleDef_HEAD_INIT .

char* m_name

新模块的名称。

char* m_doc

Docstring for the module; usually a docstring variable created with PyDoc_STRVAR() 被使用。

Py_ssize_t m_size

Some modules allow re-initialization (calling their PyInit_* function more than once). These modules should keep their state in a per-module memory area that can be retrieved with PyModule_GetState() .

This memory should be used, rather than static globals, to hold per-module state, since it is then safe for use in multiple sub-interpreters. It is freed when the module object is deallocated, after the m_free function has been called, if present.

设置 m_size to -1 means that the module can not be re-initialized because it has global state. Setting it to a non-negative value means that the module can be re-initialized and specifies the additional amount of memory it requires for its state.

PEP 3121 了解更多细节。

PyMethodDef * m_methods

A pointer to a table of module-level functions, described by PyMethodDef values. Can be NULL if no functions are present.

inquiry m_reload

Currently unused, should be NULL .

traverseproc m_traverse

A traversal function to call during GC traversal of the module object, or NULL if not needed.

inquiry m_clear

A clear function to call during GC clearing of the module object, or NULL if not needed.

freefunc m_free

A function to call during deallocation of the module object, or NULL if not needed.

int PyModule_AddObject ( PyObject  *module , const char  *name , PyObject  *value )

将对象添加到 module as name 。这是可以从模块初始化函数中使用的方便函数。这窃取引用对于 value 。返回 -1 当出错时, 0 当成功时。

int PyModule_AddIntConstant ( PyObject  *module , const char  *name , long  value )

将整数常量添加到 module as name 。这是可以从模块初始化函数中使用的方便函数。返回 -1 当出错时, 0 当成功时。

int PyModule_AddStringConstant ( PyObject  *module , const char  *name , const char  *value )

将字符串常量添加到 module as name 。这是可以从模块初始化函数中使用的方便函数。字符串 value must be null-terminated. Return -1 当出错时, 0 当成功时。

int PyModule_AddIntMacro ( PyObject  *module , macro )

将整数常量添加到 module 。名称和值取自 macro 。例如 PyModule_AddIntMacro(module, AF_INET) 添加 int 常量 AF_INET 采用值的 AF_INET to module 。返回 -1 当出错时, 0 当成功时。

int PyModule_AddStringMacro ( PyObject  *module , macro )

将字符串常量添加到 module .

内容表

上一话题

文件对象

下一话题

迭代器对象

本页