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: C-API Intro should be more explicit about error return codes
Type: enhancement Stage:
Components: Documentation Versions: Python 2.7
process
Status: closed Resolution: works for me
Dependencies: Superseder:
Assigned To: docs@python Nosy List: docs@python, georg.brandl, nikratio, terry.reedy
Priority: normal Keywords:

Created on 2010-10-09 23:37 by nikratio, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Messages (6)
msg118294 - (view) Author: Nikolaus Rath (nikratio) * Date: 2010-10-09 23:37
http://docs.python.org/c-api/string.html says about the return value of AsStringAndSize:

"If length is NULL, the resulting buffer may not contain NUL characters; if it does, the function returns -1 and a TypeError is raised."

"If string is not a string object at all, PyString_AsStringAndSize() returns -1 and raises TypeError."

This makes me wonder what the return code is if a) the function succeeds and b) it encounteres some other problem (i.e. out of memory when it tries to encode a unicode string into its default encoding).

I guess that the return value is 0 on success and -1 on all errors, but it would be nice if the documentation would be clear about this.
msg118821 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2010-10-15 19:01
I am not much familiar with the C api but I presume that all functions return -1 on error and that this is documented somewhere in the beginning. I also presume that functions that return values thru passed in pointers and that are documented as returning an integer type return 0 on success. And ditto on some general note somewhere. I do not think that such should be repeated for each function. So without further justification, I would be inclined to close this.
msg118824 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2010-10-15 19:21
Same here.  There is only ever one return value on error unless documented otherwise, since the type of error is already contained in the exception that is set on return.
msg118851 - (view) Author: Nikolaus Rath (nikratio) * Date: 2010-10-16 02:00
Georg, this is an important piece of information, but I think it is not yet clearly stated in the documentation. Here is what  http://docs.python.org/c-api/intro.html says about return codes:

"In general, when a function encounters an error, it sets an exception, discards any object references that it owns, and returns an error indicator — usually NULL or -1. A few functions return a Boolean true/false result, with false indicating an error. Very few functions return no explicit error indicator or have an ambiguous return value, and require explicit testing for errors with PyErr_Occurred()."

If this is meant to say that a function returns *only* -1 or NULL in case of an error, *unless documented otherwise* I suggest that the wording be changed to reflect that. Maybe like this:

"In general, when a function encounters an error, it sets an exception, discards any object references that it owns, and returns either NULL or -1 (depending on what is compatible with the function's type). If a functions documentation does not say otherwise, the function can be assumed to follow this convention. Nevertheless, a few functions return a Boolean true/false result, with false indicating an error, and very few functions return no explicit error indicator or have an ambiguous return value, and require explicit testing for errors with PyErr_Occurred()."



Wouldn't that be a reasonable change?
msg118856 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2010-10-16 04:40
Assuming it is true, the rewrite strikes me as an improvement.
msg118916 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2010-10-17 06:34
Committed a change in that spirit in r85606.  Thanks!
History
Date User Action Args
2022-04-11 14:57:07adminsetgithub: 54267
2010-10-17 06:34:46georg.brandlsetmessages: + msg118916
2010-10-16 04:40:31terry.reedysetmessages: + msg118856
2010-10-16 02:00:14nikratiosetmessages: + msg118851
title: Unclear PyString_AsStringAndSize return value -> C-API Intro should be more explicit about error return codes
2010-10-15 19:21:53georg.brandlsetstatus: open -> closed

nosy: + georg.brandl
messages: + msg118824

resolution: works for me
2010-10-15 19:01:26terry.reedysetnosy: + terry.reedy
messages: + msg118821
2010-10-09 23:37:56nikratiocreate