python38moduledocs是什么_模块对象 — Python 3.8.1 文档

Multi-phase initialization¶

An alternate way to specify extensions is to request "multi-phase initialization".

Extension modules created this way behave more like Python modules: the

initialization is split between the creation phase, when the module object

is created, and the execution phase, when it is populated.

The distinction is similar to the __new__() and __init__() methods

of classes.

Unlike modules created using single-phase initialization, these modules are not

singletons: if the sys.modules entry is removed and the module is re-imported,

a new module object is created, and the old module is subject to normal garbage

collection -- as with Python modules.

By default, multiple modules created from the same definition should be

independent: changes to one should not affect the others.

This means that all state should be specific to the module object (using e.g.

using PyModule_GetState()), or its contents (such as the module's

__dict__ or individual classes created with PyType_FromSpec()).

All modules created using multi-phase initialization are expected to support

sub-interpreters. Making sure multiple modules

are independent is typically enough to achieve this.

To request multi-phase initialization, the initialization function

(PyInit_modulename) returns a PyModuleDef instance with non-empty

m_slots. Before it is returned, the PyModuleDef

instance must be initialized with the following function:

PyObject* PyModuleDef_Init(PyModuleDef*def)¶

Return value: Borrowed reference.

Ensures a module definition is a properly initialized Python object that

correctly reports its type and reference count.

Returns def cast to PyObject*, or NULL if an error occurred.

3.5 新版功能.

The m_slots member of the module definition must point to an array of

PyModuleDef_Slot structures:

PyModuleDef_Slot¶

int slot¶

A slot ID, chosen from the available values explained below.

void* value¶

Value of the slot, whose meaning depends on the slot ID.

3.5 新版功能.

The m_slots array must be terminated by a slot with id 0.

The available slot types are:

Py_mod_create¶

Specifies a function that is called to create the module object itself.

The value pointer of this slot must point to a function of the signature:

The function receives a ModuleSpec

instance, as defined in PEP 451, and the module definition.

It should return a new module object, or set an error

and return NULL.

This function should be kept minimal. In particular, it should not

call arbitrary Python code, as trying to import the same module again may

result in an infinite loop.

Multiple Py_mod_create slots may not be specified in one module

definition.

If Py_mod_create is not specified, the import machinery will create

a normal module object using PyModule_New(). The name is taken from

spec, not the definition, to allow extension modules to dynamically adjust

to their place in the module hierarchy and be imported under different

names through symlinks, all while sharing a single module definition.

There is no requirement for the returned object to be an instance of

PyModule_Type. Any type can be used, as long as it supports

setting and getting import-related attributes.

However, only PyModule_Type instances may be returned if the

PyModuleDef has non-NULL m_traverse, m_clear,

m_free; non-zero m_size; or slots other than Py_mod_create.

Py_mod_exec¶

Specifies a function that is called to execute the module.

This is equivalent to executing the code of a Python module: typically,

this function adds classes and constants to the module.

The signature of the function is:

int exec_module(PyObject*module)¶

If multiple Py_mod_exec slots are specified, they are processed in the

order they appear in the m_slots array.

See PEP 489 for more details on multi-phase initialization.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值