This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: PyErr_SetObject documentation lacks mention of reference counting
Type: Stage: resolved
Components: Documentation Versions:
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: docs@python Nosy List: docs@python, eblake, ronaldoussoren, serhiy.storchaka
Priority: normal Keywords:

Created on 2021-11-30 15:37 by eblake, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (3)
msg407381 - (view) Author: Eric Blake (eblake) Date: 2021-11-30 15:37
While PyErr_SetString is obvious that you do not have to worry about the reference count of 'exception', the documentation for PyErr_SetObject is silent on whether it steals or adds a reference to 'value'.  This is particularly confusing, since other functions on the page (like PyErr_Restore) are explicit about stealing references.  Looking at non-canonical documentation, I found https://docstore.mik.ua/orelly/other/python/0596001886_pythonian-chp-24-sect-1.html which claims PyErr_SetObject steals a reference to 'value'.  But that is wrong; looking at the source code for _PyErr_SetString, it becomes obvious that once you use SetObject, the reference count will be increased, and the caller can release its own reference because nothing was stolen.

I noticed this lack of documentation when trying to debug a memory leak in nbdsh: https://listman.redhat.com/archives/libguestfs/2021-November/msg00280.html
msg407383 - (view) Author: Ronald Oussoren (ronaldoussoren) * (Python committer) Date: 2021-11-30 15:59
The default behaviour of functions is to not steal references, in general only exceptions to this are documented. Likewise with return values: by default the result is a strong reference, and borrowed references are documented as such.
msg407437 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2021-12-01 10:16
I concur with Ronald. If we explicitly document that PyErr_SetObject() does not steal reference we would need to document it for every parameter of every function. It would make the documentation worse because it would be more difficult to distinguish ouliners which steal references from normal functions which does not steal references.

Use official Python documentation.
History
Date User Action Args
2022-04-11 14:59:53adminsetgithub: 90097
2021-12-01 10:16:19serhiy.storchakasetstatus: open -> closed

nosy: + serhiy.storchaka
messages: + msg407437

resolution: not a bug
stage: resolved
2021-11-30 15:59:42ronaldoussorensetnosy: + ronaldoussoren
messages: + msg407383
2021-11-30 15:37:16eblakecreate