Python C API:PyEval_CallFunction?

发布于 2024-10-16 15:31:37 字数 948 浏览 4 评论 0原文

我在 Python C API 中发现了一个名为 PyEval_CallFunction 的函数,它似乎很有用。它允许您通过以下内容来调用 Python 可调用函数:

PyEval_CallFunction(obj, "OOO", a, b, c);

但是,我找不到有关此函数的任何官方文档。谷歌搜索会显示各种非官方 教程讨论了这个函数,但是:

  1. 函数不是 官方文档中记录了 Python 文档,所以我不知道它是否是 即使是应该做的事情 成为公共 API 的一部分。

  2. 网络搜索发现 使用政策不一致。一些 教程表明 格式字符串需要括号 围绕类型列表,例如 “(OiiO)”,而其他时候我 看到它不带括号使用。 当我实际尝试该功能时 一个真正的程序,似乎需要 括号内,否则 段错误。

我想使用这个功能,因为它很方便。有谁知道这件事,或者知道为什么没有记录?它是公共 API 的一部分吗?

I've discovered a function in the Python C API named PyEval_CallFunction which seems to be useful. It allows you to invoke a Python callable by saying something like:

PyEval_CallFunction(obj, "OOO", a, b, c);

However, I can't find any official documentation on this function. A google search brings up various unofficial tutorials which discuss this function, but:

  1. The function isn't
    documented
    in the official
    Python docs, so I don't know if it's
    even something that is supposed to
    be part of the public API.

  2. Searching the web turns up
    inconsistent usage policies. Some
    tutorials indicate the
    format string needs parenthesis
    around the type list, like
    "(OiiO)", whereas other times I
    see it used without the parenthesis.
    When I actually try the function in
    a real program, it seems to require
    the parenthesis, otherwise it
    segfaults.

I'd like to use this function because it's convenient. Does anyone know anything about this, or know why it isn't documented? Is it part of the public API?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

丑丑阿 2024-10-23 15:31:37

我也找不到很多参考资料,但您链接到的教程提到了这一点:

字符串格式如下
参数与 Py_BuildValue 相同
(XXX所以我真的应该描述
到现在为止!)。诸如这样的调用

PyEval_CallFunction(obj, "iii", a, b, c);

相当于

PyEval_CallObject(obj, Py_BuildValue("iii", a, b, c));

我认为 PyEval_CallFunction 不是公共 API,因为它的价值似乎相当有限。这两者之间没有太大区别。但话又说回来,我并没有真正参与 python 扩展,所以这只是我对此的看法。

PyEval_CallObject 本身只是一个围绕 PyEval_CallObjectWithKeywords 的宏。

#define PyEval_CallObject(func,arg) \
        PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL)

关于“什么是公共API?”的问题以下是 Martin 诉 Löwis 的最新消息:

只是为了强调和支持乔治
解释:API定义
通过文档,而是
主要是通过头文件。
所有声明为 PyAPI_FUNC 的函数
并且不以 _Py 开头的都是公开的
API。曾经有很多无文档的API(直到1.4,根本没有API文档,只有扩展模块教程);如今,越来越多的 API 被记录下来。

http://mail.python.org/pipermail/python-dev /2011-2月/107973.html

I couldn't find many references to it either, but the tutorial you linked to mentions this:

The string format and the following
arguments are as for Py_BuildValue
(XXX so i really should have described
that by now!). A call such as

PyEval_CallFunction(obj, "iii", a, b, c);

is equivalent to

PyEval_CallObject(obj, Py_BuildValue("iii", a, b, c));

I suppose PyEval_CallFunction is not public API, as its value seems rather limited. There is not much of a difference between these two. But then again, I'm not really involved in python extensions, so this is just my view on this.

PyEval_CallObject itself is just a macro around PyEval_CallObjectWithKeywords.

#define PyEval_CallObject(func,arg) \
        PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL)

On the matter of "What is public API?" here is a recent message from Martin v. Löwis:

Just to stress and support Georg's
explanation: the API is not defined
through the documentation, but instead
primarily through the header files.
All functions declared as PyAPI_FUNC
and not starting with _Py are public
API. There used to be a lot of undocumented API (up to 1.4, there was no API documentation at all, only the extension module tutorial); these days, more and more API gets documented.

http://mail.python.org/pipermail/python-dev/2011-February/107973.html

叹倦 2024-10-23 15:31:37

未记录的原因是您应该使用 PyObject_CallFunction< /a> 相反。

PyEval_* 函数系列是解释器评估循环的原始内部调用。相应记录的 PyObject_* 调用包括所有附加的解释器状态完整性检查、参数验证和堆栈保护。

The reason it isn't documented is because you should be using PyObject_CallFunction instead.

The PyEval_* function family are the raw internal calls for the interpreter evaluation loop. The corresponding documented PyObject_* calls include all the additional interpreter state integrity checks, argument validation and stack protection.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文