[doc] ctypes: use_errno=True does not work #52008
Copy link
Copy link
Closed
Labels
3.11only security fixesonly security fixesdocsDocumentation in the Doc dirDocumentation in the Doc direasytopic-ctypes
Description
| BPO | 7760 |
|---|---|
| Nosy | @amauryfa, @abalkin, @meadori, @eryksun, @iritkatriel |
| Files |
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 = 'https://github.com/theller'
closed_at = None
created_at = <Date 2010-01-23.00:18:56.292>
labels = ['easy', 'ctypes', 'type-feature', '3.11', 'docs']
title = '[doc] ctypes: use_errno=True does not work'
updated_at = <Date 2021-05-18.20:36:39.807>
user = 'https://bugs.python.org/nikratio'bugs.python.org fields:
activity = <Date 2021-05-18.20:36:39.807>
actor = 'eryksun'
assignee = 'theller'
closed = False
closed_date = None
closer = None
components = ['Documentation', 'ctypes']
creation = <Date 2010-01-23.00:18:56.292>
creator = 'nikratio'
dependencies = []
files = ['15976']
hgrepos = []
issue_num = 7760
keywords = ['easy']
message_count = 8.0
messages = ['98171', '98188', '98195', '189784', '189807', '189886', '393894', '393906']
nosy_count = 8.0
nosy_names = ['amaury.forgeotdarc', 'belopolsky', 'marcin.bachry', 'nikratio', 'meador.inge', 'eryksun', 'Matt.Jones', 'iritkatriel']
pr_nums = []
priority = 'normal'
resolution = None
stage = 'needs patch'
status = 'open'
superseder = None
type = 'enhancement'
url = 'https://bugs.python.org/issue7760'
versions = ['Python 3.11']Linked PRs
- gh-52008: document that absolute paths can break use_errno=True in ct… #146504
- gh-52008: Document that
ctypes.CDLLcan only observeerrnofrom its own C runtime #159239 - [3.15] gh-52008: Document that
ctypes.CDLLcan only observeerrnofrom its own C runtime (GH-159239) #159242 - [3.14] gh-52008: Document that
ctypes.CDLLcan only observeerrnofrom its own C runtime (GH-159239) #159243
Metadata
Metadata
Assignees
Labels
3.11only security fixesonly security fixesdocsDocumentation in the Doc dirDocumentation in the Doc direasytopic-ctypes
Projects
- StatusShow more project fieldsDone
- StatusShow more project fieldsTodo
Activity
nikratio commentedon Jan 23, 2010
On my system (Ubuntu Karmic, Python 2.6.4 (r264:75706, Dec 7 2009, 18:45:15), Kernel 2.6.31-17-generic, libc6 2.10.1-0ubuntu16) the attached test script produces the following output:
So the function call failed, but the errno provided by ctypes is zero.
(There are two variants of construction the getxattr() foreign function in the script and both produce the same result).
marcinbachry commentedon Jan 23, 2010
In the libc case you shouldn't give absolute path in CDLL: CDLL('libc.so.6') is better. You use "/lib/libc.so.6" path, but Python (and ctypes.so) actually uses something like "/lib/tls/i686/cmov/libc.so.6" - these are two separate libraries with separate "errno" variables, mapped to different memory regions. Your call to getxattr modifies errno in the former library, but ctypes get_errno() fetches value from the latter library.
If I fix CDLL() call, the test works ok for me.
nikratio commentedon Jan 23, 2010
I can confirm that without the path it works for me too.
But I have to admit that I don't really understand your explanation. Should I generally not use full paths with CDLL? Or just in the case of libc?
In either case, I think the ctypes dokumentation could be more explicit about this.
BreamoreBoy commentedon May 21, 2013
Would someone like to propose a documentation patch that clarifies this situation.
MattJones commentedon May 22, 2013
Is this really a documentation issue? Is it not generally understood that using absolute paths to libraries is a bad idea due to the amount of PATH/symlink spaghetti that the average file system contains?
nikratio commentedon May 24, 2013
Matt, I believe in that case it's still a documentation issue, because then the documentation probably should say that using absolute paths to libraries is a bad idea in general.
[-]use_errno=True does not work[/-][+]ctypes: use_errno=True does not work[/+]iritkatriel commentedon May 18, 2021
The recommendation not to use absolute paths in CDLL is still not mentioned in the docs:
https://docs.python.org/3/library/ctypes.html#ctypes.CDLL
Adding the (active) ctypes experts to nosy.
[-]ctypes: use_errno=True does not work[/-][+][doc] ctypes: use_errno=True does not work[/+]4 remaining items
pythongh-52008: document that absolute paths can break use_errno=True…
pythongh-52008: add news entry
pythongh-52008: fix RST cross-reference and unclosed parenthesis in note
gh-52008: Document that `ctypes.CDLL` can only observe `errno` from i…
ZeroIntensity commentedon Oct 12, 2026
The issue described here is obsolete since Ubuntu doesn't have multiple libc versions anymore, but I did add a note to the docs saying that the library must use the same
errnoas ctypes for it to work. Backports are on auto-merge.[3.14] gh-52008: Document that `ctypes.CDLL` can only observe `errno`…
[3.15] gh-52008: Document that `ctypes.CDLL` can only observe `errno`…