使用 Py_BEGIN_ALLOW_THREADS 时如何避免 Python C 扩展中的 gcc 警告
在 Python C 扩展中操作 GIL 的最简单方法是使用提供的宏:
my_awesome_C_function()
{
blah;
Py_BEGIN_ALLOW_THREADS
// do stuff that doesn't need the GIL
if (should_i_call_back) {
Py_BLOCK_THREADS
// do stuff that needs the GIL
Py_UNBLOCK_THREADS
}
Py_END_ALLOW_THREADS
return blah blah;
}
这非常有效,让我可以为大部分代码释放 GIL,但可以为需要它的少量代码重新获取它。
问题是当我用 gcc 编译这个时,我得到:
ext/engine.c:548: warning: '_save' might be used uninitialized in this function
因为 Py_BEGIN_ALLOW_THREADS 是这样定义的:
#define Py_BEGIN_ALLOW_THREADS { \
PyThreadState *_save; \
_save = PyEval_SaveThread();
所以,三个问题:
- 是否有可能抑制 gcc 的警告,
- 有谁知道为什么 gcc 认为
_save
可能可以在未初始化的情况下使用,因为它是在声明后立即分配的, - 为什么不定义宏来在一条语句中声明和初始化变量以避免出现问题?
(最后两个实际上只是出于我自己的好奇心)。
我可以通过不使用宏并自己完成这一切来避免这个问题,但我宁愿不这样做。
The simplest way to manipulate the GIL in Python C extensions is to use the macros provided:
my_awesome_C_function()
{
blah;
Py_BEGIN_ALLOW_THREADS
// do stuff that doesn't need the GIL
if (should_i_call_back) {
Py_BLOCK_THREADS
// do stuff that needs the GIL
Py_UNBLOCK_THREADS
}
Py_END_ALLOW_THREADS
return blah blah;
}
This works great, letting me release the GIL for the bulk of my code, but re-grabbing it for small bits of code that need it.
The problem is when I compile this with gcc, I get:
ext/engine.c:548: warning: '_save' might be used uninitialized in this function
because Py_BEGIN_ALLOW_THREADS is defined like this:
#define Py_BEGIN_ALLOW_THREADS { \
PyThreadState *_save; \
_save = PyEval_SaveThread();
So, three questions:
- Is it possible to suppress gcc's warning,
- Does anyone have any idea why gcc thinks
_save
might be used uninitialized, since it is assigned to immediately after its declaration, and - Why wouldn't the macro have been defined to declare and initialize the variable in one statement to avoid the issue?
(the last two are really just for my own curiosity).
I can avoid the issue by not using the macros and doing it all myself, but I'd rather not.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
发布评论
评论(3)
~没有更多了~
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
-Wall -Wno-uninitialized
如果您只想删除此警告,只需将
_save
初始化为空指针,这样它就不会依赖于函数返回值...一行代码和一条注释对我来说很有意义:-Wall -Wno-uninitialized
If you want to remove just this warning, you could simply initialize
_save
to a null pointer so that it doesn't rely on a function return value... that one line of code and a comment makes sense to me: