Python C API:Pylist_append在错误上做什么?
文档中没有记录这一点。它只是说它“返回0以取得成功,而-1返回错误”。这是什么意思?
如果我们要迭代一些数据结构(例如链接列表/数组),并且在迭代时添加元素,然后突然Pylist_append
返回-1
- ? < / strong>的元素是否被添加,参考计数减少了,或者Python只是死了,而退出 /减少参考计数是我最少的担心吗?
如果迄今为止添加的元素 的参考计数减少了,那么我知道没有任何事情必须完成,如在这种情况下,我的下属分配函数处理了其他参考计数的减少(如果元素的参考计数达到0) - 但是,如果Python只是扔了双手,说“您是您自己的” - 我必须确保我在错误时管理参考计数,请立即(或遭受参考泄露)。
那是哪个呢?
This is not documented in the docs. It just says it "returns 0 for success and -1 for error". What does this mean?
If we're iterating over some data structure (say a linked list/array) and we're adding elements as we iterate, and suddenly PyList_Append
returns -1
- what happens? Does the elements that did get added, have their reference count decremented or does Python just die and de-allocating / decrementing reference counts is the least of my worries?
If the elements that thus far was added have their reference count decremented then I understand nothing has to be done, as in this case, my de-allocation function handles decrementing of other reference counts where it needs to (if the element's reference count reaches 0) - but if Python just throws it's hands up and say "you're on your own" - I would have to make sure I manage the reference counting at time of error, immediately (or suffer a reference leak).
So which is it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我相信
Pylist_append
:memoryError
可能会发生两个错误Systemerror
如果将其传递给不是列表的东西。您应该能够通过适当的类型检查避免这种情况,因此,如果正确编写代码,则永远不会发生。在这两种情况下:
DECREF
。实际上,很难从内存 - 错误从中恢复 - 大多数操作系统都会分配内存,因此,当您实际上看到一段时间以前用完的内存错误时。
示例(未经测试)代码大致显示您要做什么:
I believe there are two errors that can occur with
PyList_Append
:MemoryError
(if it had to enlarge the allocated memory for the list, and was unable to allocate enough).SystemError
if you pass it something that isn't a list. You should be able to avoid this through appropriate type-checks so if your code is written correctly it should never happen.In both cases:
DECREF
.Practically memory-errors are hard to recover from - most operating systems over-allocate memory so by the time you actually see a memory error you've usually run out a while ago.
Example (untested) code showing roughly what you'd do: