PyObject * PyFunction_New ( PyObject * code , PyObject * globals )
返回值:新的引用。

返回与代码对象 code 关联的新函数对象。 globals 必须是一个字典,该函数可以访问全局变量。

函数的文档字符串和名称是从代码对象中提取的。 __module__ 是从 globals 中提取的。 参数 defaults, annotations 和 closure 被设为 NULL __qualname__ 被设为与代码对象的 co_qualname 字段相同的值。

PyObject * PyFunction_NewWithQualName ( PyObject * code , PyObject * globals , PyObject * qualname )
返回值:新的引用。

类似 PyFunction_New() ,但还允许设置函数对象的 __qualname__ 属性。 qualname 应当是一个 unicode 对象或为 NULL ;如为 NULL ,则 __qualname__ 属性会被设为与代码对象的 co_qualname 字段相同的值。

在 3.3 版本加入.

PyObject * PyFunction_GetModule ( PyObject * op )
返回值:借入的引用。

函数对象 op __module__ 属性返回一个 borrowed reference 。 该值可以为 NULL

这通常为一个包含模块名称的 字符串 ,但可以通过 Python 代码设为任何其他对象。

int PyFunction_SetDefaults ( PyObject * op , PyObject * defaults )

为函数对象 op 设置参数默认值。 defaults 必须为 Py_None 或一个元组。

失败时引发 SystemError 异常并返回 -1

void PyFunction_SetVectorcall ( PyFunctionObject * func , vectorcallfunc vectorcall )

设置给定函数对象 func 的 vectorcall 字段。

警告:使用此 API 的扩展必须保留未修改的(默认) vectorcall 函数的行为!

在 3.12 版本加入.

int PyFunction_SetClosure ( PyObject * op , PyObject * closure )

设置关联到函数对象 op 的闭包。 closure 必须为 Py_None 或 cell 对象的元组。

失败时引发 SystemError 异常并返回 -1

int PyFunction_SetAnnotations ( PyObject * op , PyObject * annotations )

设置函数对象 op 的标注。 annotations 必须为一个字典或 Py_None

失败时引发 SystemError 异常并返回 -1

int PyFunction_AddWatcher ( PyFunction_WatchCallback callback )

注册 callback 作为当前解释器的函数监视器。 返回一个可被传给 PyFunction_ClearWatcher() 的 ID。 如果出现错误(比如没有足够的可用监视器 ID),则返回 -1 并设置一个异常。

在 3.12 版本加入.

int PyFunction_ClearWatcher ( int watcher_id )

清空当前解释器在之前从Clear watcher identified by previously returned from PyFunction_AddWatcher() 返回的由 watcher_id 所标识的监视器。 成功时返回 0 ,或者出错时(比如当给定的 watcher_id 未被注册)返回 -1 并设置一个异常。

在 3.12 版本加入.

type PyFunction_WatchEvent

由以下可能的函数监视器事件组成的枚举: - PyFunction_EVENT_CREATE - PyFunction_EVENT_DESTROY - PyFunction_EVENT_MODIFY_CODE - PyFunction_EVENT_MODIFY_DEFAULTS - PyFunction_EVENT_MODIFY_KWDEFAULTS

在 3.12 版本加入.

typedef int ( * PyFunction_WatchCallback ) ( PyFunction_WatchEvent event , PyFunctionObject * func , PyObject * new_value )

函数监视器回调函数的类型。

如果 event PyFunction_EVENT_CREATE PyFunction_EVENT_DESTROY new_value 将为 NULL 。 在其他情况下, new_value 将为被修改的属性持有一个指向要保存在 func 中的新值的 borrowed reference

该回调可以检查但不能修改 func ; 这样做可能具有不可预知的影响,包括无限递归。

如果 event PyFunction_EVENT_CREATE ,则该回调会在 func 完成初始化之后被发起调用。 在其他情况下,该回调会在对 func 进行修改之前被发起调用,这样就可以检查 func 之前的状态。 如有可能函数对象的创建允许被运行时优化掉。 在此情况下将不发出任何事件。 虽然根据不同的优化决定这会产生可被观察到的运行时行为变化,但是它不会改变被运行的 Python 代码的语义。

如果 event PyFunction_EVENT_DESTROY ,则在回调中接受一个即将销毁的函数的引用将使其重生,并阻止其在此时被释放。 当重生的对象以后再被销毁时,任何在当时已激活的监视器回调将再次被调用。

如果该回调设置了一个异常,则它必须返回 -1 ;此异常将作为不可引发的异常使用 PyErr_WriteUnraisable() 打印出来。 在其他情况下它应当返回 0

在进入回调时可能已经设置了尚未处理的异常。 在此情况下,回调应当返回 0 并仍然设置同样的异常。 这意味着该回调可能不会调用任何其他可设置异常的 API 除非它先保存并清空异常状态,并在返回之前恢复它。

在 3.12 版本加入.

This page is licensed under the Python Software Foundation License Version 2. Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License. See History and License for more information.
The Python Software Foundation is a non-profit corporation. Please donate. 最后更新于 Feb 26, 2024 (19:18 UTC). Found a bug ?