New submission from Thomas Wouters <tho...@python.org>:

The PyUnicode_FSConverter function has confusing reference semantics, and 
confusing documentation.

https://docs.python.org/3/c-api/unicode.html#c.PyUnicode_FSConverter says the 
output argument "must be a PyBytesObject* which must be released when it is no 
longer used." That seems to suggest one must pass a PyBytesObject to it, and 
indeed one of the error paths assumes an object was passed 
(https://github.com/python/cpython/blob/main/Objects/unicodeobject.c#L4116-- 
'addr' is called 'result' in the docs). Not passing a valid object would result 
in trying to DECREF NULL, or garbage. However, the function doesn't actually 
use the object, and later in the function overwrites the value *without* 
DECREFing it, so passing a valid object would in fact cause a leak.

I understand the function signature is the way it is so it can be used with 
PyArg_ParseTuple's O& format, but there are reasons to call it directly (e.g. 
with METH_O functions), and it would be nice if the semantics were more clear.

----------
components: C API
messages: 408604
nosy: twouters
priority: normal
severity: normal
status: open
title: PyUnicode_FSConverter() has confusing reference semantics
versions: Python 3.10, Python 3.11, Python 3.8, Python 3.9

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue46083>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to