[issue34816] ctypes + hasattr

2018-09-27 Thread Lars Friedrich


Lars Friedrich  added the comment:

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?

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue34816] ctypes + hasattr

2018-09-27 Thread Eryk Sun


Eryk Sun  added the comment:

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:

class A:
def __init__(self, name):
raise OSError

class B:
def __getattr__(self, name):
try:
A(name)
except OSError:
raise AttributeError

Demo:

>>> b = B()
>>> b.test
Traceback (most recent call last):
  File "", line 4, in __getattr__
  File "", line 3, in __init__
OSError

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "", line 1, in 
  File "", line 6, in __getattr__
AttributeError

>>> hasattr(b, 'test')
False

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.

--
nosy: +eryksun
stage:  -> test needed
versions: +Python 3.8

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue34816] ctypes + hasattr

2018-09-27 Thread Lars Friedrich


New submission from Lars Friedrich :

The following creates an OSError:

import ctypes
hasattr(ctypes.windll, 'test')

The expected behavior would be to return "False"

--
components: ctypes
messages: 326528
nosy: lfriedri
priority: normal
severity: normal
status: open
title: ctypes + hasattr
type: behavior
versions: Python 3.7

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com