Skip to content

PyUnicode_FSConverter() has confusing reference semantics #90241

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Yhg1s opened this issue Dec 15, 2021 · 2 comments
Closed

PyUnicode_FSConverter() has confusing reference semantics #90241

Yhg1s opened this issue Dec 15, 2021 · 2 comments
Labels
3.8 (EOL) end of life 3.9 only security fixes 3.10 only security fixes 3.11 only security fixes topic-C-API

Comments

@Yhg1s
Copy link
Member

Yhg1s commented Dec 15, 2021

BPO 46083
Nosy @Yhg1s

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = None
created_at = <Date 2021-12-15.14:28:25.028>
labels = ['expert-C-API', '3.8', '3.9', '3.10', '3.11']
title = 'PyUnicode_FSConverter() has confusing reference semantics'
updated_at = <Date 2021-12-15.14:28:25.028>
user = 'https://github.com/Yhg1s'

bugs.python.org fields:

activity = <Date 2021-12-15.14:28:25.028>
actor = 'twouters'
assignee = 'none'
closed = False
closed_date = None
closer = None
components = ['C API']
creation = <Date 2021-12-15.14:28:25.028>
creator = 'twouters'
dependencies = []
files = []
hgrepos = []
issue_num = 46083
keywords = []
message_count = 1.0
messages = ['408604']
nosy_count = 1.0
nosy_names = ['twouters']
pr_nums = []
priority = 'normal'
resolution = None
stage = None
status = 'open'
superseder = None
type = None
url = 'https://bugs.python.org/issue46083'
versions = ['Python 3.8', 'Python 3.9', 'Python 3.10', 'Python 3.11']

Linked PRs

@Yhg1s
Copy link
Member Author

Yhg1s commented Dec 15, 2021

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.

@Yhg1s Yhg1s added 3.8 (EOL) end of life 3.9 only security fixes 3.10 only security fixes 3.11 only security fixes topic-C-API labels Dec 15, 2021
@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
serhiy-storchaka added a commit to serhiy-storchaka/cpython that referenced this issue Jan 3, 2025
@encukou
Copy link
Member

encukou commented Jan 3, 2025

Another gotcha, if you're used to “regular” C API functions, are the return values. Should we also document these? And perhaps explicitly point out that it's different than most C API functions, in case readers skim past the numbers? e.g.:

   On failure, return 0 with an exception set.
   On success, return ``Py_CLEANUP_SUPPORTED`` is *obj* was set, or 1 if *obj* is ``NULL``.
   (Note that the values are different from most C API functions.)

encukou pushed a commit that referenced this issue Jan 6, 2025
…de_FSDecoder (GH-128451)

Co-authored-by: Stan Ulbrych <[email protected]>
Co-authored-by: Erlend E. Aasland <[email protected]>
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Jan 6, 2025
…yUnicode_FSDecoder (pythonGH-128451)

(cherry picked from commit 657d7b7)

Co-authored-by: Serhiy Storchaka <[email protected]>
Co-authored-by: Stan Ulbrych <[email protected]>
Co-authored-by: Erlend E. Aasland <[email protected]>
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Jan 6, 2025
…yUnicode_FSDecoder (pythonGH-128451)

(cherry picked from commit 657d7b7)

Co-authored-by: Serhiy Storchaka <[email protected]>
Co-authored-by: Stan Ulbrych <[email protected]>
Co-authored-by: Erlend E. Aasland <[email protected]>
serhiy-storchaka added a commit that referenced this issue Jan 6, 2025
…PyUnicode_FSDecoder (GH-128451) (GH-128542)

(cherry picked from commit 657d7b7)

Co-authored-by: Serhiy Storchaka <[email protected]>
Co-authored-by: Stan Ulbrych <[email protected]>
Co-authored-by: Erlend E. Aasland <[email protected]>
serhiy-storchaka added a commit that referenced this issue Jan 6, 2025
…PyUnicode_FSDecoder (GH-128451) (GH-128543)

(cherry picked from commit 657d7b7)

Co-authored-by: Serhiy Storchaka <[email protected]>
Co-authored-by: Stan Ulbrych <[email protected]>
Co-authored-by: Erlend E. Aasland <[email protected]>
srinivasreddy pushed a commit to srinivasreddy/cpython that referenced this issue Jan 8, 2025
…yUnicode_FSDecoder (pythonGH-128451)

Co-authored-by: Stan Ulbrych <[email protected]>
Co-authored-by: Erlend E. Aasland <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.8 (EOL) end of life 3.9 only security fixes 3.10 only security fixes 3.11 only security fixes topic-C-API
Projects
None yet
Development

No branches or pull requests

3 participants