-
-
Notifications
You must be signed in to change notification settings - Fork 32.5k
Closed
Labels
3.10only security fixesonly security fixes3.8 (EOL)end of lifeend of life3.9only security fixesonly security fixeseasytopic-ctypestype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
Description
BPO | 34816 |
---|---|
Nosy | @eryksun, @lfriedri, @farfella |
PRs | |
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 = None
closed_at = None
created_at = <Date 2018-09-27.07:14:17.884>
labels = ['easy', 'type-bug', '3.8', '3.9', '3.10', 'ctypes']
title = 'raise AttributeError if loading fails in ctypes.LibraryLoader.__getattr__'
updated_at = <Date 2021-04-04.02:29:42.566>
user = 'https://github.com/lfriedri'
bugs.python.org fields:
activity = <Date 2021-04-04.02:29:42.566>
actor = 'ateeq'
assignee = 'none'
closed = False
closed_date = None
closer = None
components = ['ctypes']
creation = <Date 2018-09-27.07:14:17.884>
creator = 'lfriedri'
dependencies = []
files = ['49932']
hgrepos = []
issue_num = 34816
keywords = ['patch', 'easy']
message_count = 5.0
messages = ['326528', '326556', '326557', '389629', '390168']
nosy_count = 3.0
nosy_names = ['eryksun', 'lfriedri', 'ateeq']
pr_nums = ['25177']
priority = 'normal'
resolution = None
stage = 'patch review'
status = 'open'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue34816'
versions = ['Python 3.8', 'Python 3.9', 'Python 3.10']
Linked PRs
Metadata
Metadata
Assignees
Labels
3.10only security fixesonly security fixes3.8 (EOL)end of lifeend of life3.9only security fixesonly security fixeseasytopic-ctypestype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
Projects
Milestone
Relationships
Development
Select code repository
Activity
lfriedri commentedon Sep 27, 2018
The following creates an OSError:
The expected behavior would be to return "False"
eryksun commentedon Sep 27, 2018
ctypes.windll is an instance of ctypes.LibraryLoader, which has a __getattr__ method that calls ctypes.WinDLL(name) and caches the result as an instance attribute. I suppose with chained exceptions it's reasonable to handle OSError in __getattr__ by raising AttributeError. For example:
Demo:
FYI, I recommend avoiding the cdll and windll LibraryLoader instances. I wish they were deprecated because globally caching CDLL and WinDLL instances leads to conflicts between projects that use the same shared libraries.
lfriedri commentedon Sep 27, 2018
Thank you for your reply.
I am not sure if I understood correctly:
Do you suggest to modify ctypes.__init__.py so that the __getattr__ method of LibraryLoader catches the OSError and raises an AttributeError instead, as in your example?
eryksun commentedon Mar 28, 2021
Yes. It seems no one was keen to work on this. I think it's relatively easy, so I'll add that flag in case someone is looking for an easy issue.
[-]ctypes + hasattr[/-][+]raise AttributeError if loading fails in ctypes.LibraryLoader.__getattr__[/+]farfella commentedon Apr 4, 2021
First patch fixing only the issue at hand on master. LibraryLoader now catches OSError for FileNotFoundError and raises AttributeError.
16 remaining items