Skip to content
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

raise AttributeError if loading fails in ctypes.LibraryLoader.__getattr__ #78997

Open
lfriedri mannequin opened this issue Sep 27, 2018 · 5 comments
Open

raise AttributeError if loading fails in ctypes.LibraryLoader.__getattr__ #78997

lfriedri mannequin opened this issue Sep 27, 2018 · 5 comments
Labels
3.8 3.9 3.10 easy expert-ctypes type-bug An unexpected behavior, bug, or error

Comments

@lfriedri
Copy link
Mannequin

lfriedri mannequin commented Sep 27, 2018

BPO 34816
Nosy @eryksun, @lfriedri, @farfella
PRs
  • gh-78997: AttributeError if loading fails in LibraryLoader.__getattr__ #25177
  • Files
  • 34816.patch
  • 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

    @lfriedri
    Copy link
    Mannequin Author

    lfriedri mannequin commented Sep 27, 2018

    The following creates an OSError:

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

    The expected behavior would be to return "False"

    @lfriedri lfriedri mannequin added 3.7 expert-ctypes type-bug An unexpected behavior, bug, or error labels Sep 27, 2018
    @eryksun
    Copy link
    Contributor

    eryksun commented 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:

        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 "<stdin>", line 4, in __getattr__
          File "<stdin>", line 3, in __init__
        OSError
    
        During handling of the above exception, another exception occurred:
    
        Traceback (most recent call last):
          File "<stdin>", line 1, in <module>
          File "<stdin>", 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.

    @eryksun eryksun added the 3.8 label Sep 27, 2018
    @lfriedri
    Copy link
    Mannequin Author

    lfriedri mannequin commented 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
    Copy link
    Contributor

    eryksun commented Mar 28, 2021

    __getattr__ method of LibraryLoader catches the OSError and
    raises an AttributeError

    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.

    @eryksun eryksun changed the title ctypes + hasattr raise AttributeError if loading fails in ctypes.LibraryLoader.__getattr__ Mar 28, 2021
    @farfella
    Copy link
    Mannequin

    farfella mannequin commented Apr 4, 2021

    First patch fixing only the issue at hand on master. LibraryLoader now catches OSError for FileNotFoundError and raises AttributeError.

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    FFY00 added a commit that referenced this issue Dec 15, 2022
    Co-authored-by: Zachary Ware <zachary.ware@gmail.com>
    Co-authored-by: Filipe Laíns <filipe.lains@gmail.com>
    Co-authored-by: Eric Wieser <wieser.eric@gmail.com>
    carljm added a commit to carljm/cpython that referenced this issue Dec 16, 2022
    * main:
      Improve stats presentation for calls. (pythonGH-100274)
      Better stats for `LOAD_ATTR` and `STORE_ATTR` (pythonGH-100295)
      pythongh-81057: Move the Cached Parser Dummy Name to _PyRuntimeState (python#100277)
      Document that zipfile's pwd parameter is a `bytes` object (python#100209)
      pythongh-99767: mark `PyTypeObject.tp_watched` as internal use only in table (python#100271)
      Fix typo in introduction.rst (python#100266)
      pythongh-78997: AttributeError if loading fails in LibraryLoader.__getattr__
      pythonGH-100234: Set a default value for random.expovariate() (pythonGH-100235)
      Remove uninformative itertools recipe (pythonGH-100253)
      pythonGH-99767: update PyTypeObject docs for type watchers (pythonGH-99928)
      Move stats for the method cache into the `Py_STAT` machinery (pythonGH-100255)
      pythonGH-100222: fix typo _py_set_opocde -> _py_set_opcode (pythonGH-100259)
      pythonGH-100000: Cleanup and polish various watchers code (pythonGH-99998)
      pythongh-90111: Minor Cleanup for Runtime-Global Objects (pythongh-100254)
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.8 3.9 3.10 easy expert-ctypes type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    1 participant