GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — raise AttributeError if loading fails in ctypes.LibraryLoader.__getattr__ · Issue #78997 · python/cpython

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

New issue
Copy link
New issue
Copy link
Closed
Closed
raise AttributeError if loading fails in ctypes.LibraryLoader.__getattr__#78997
Copy link
Labels
3.10 (EOL)end of lifeend of life3.8 (EOL)end of lifeend of life3.9 (EOL)end of lifeend of lifeeasytopic-ctypestype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

Description

lfriedri
mannequin
opened on Sep 27, 2018
Issue body actions
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

    • gh-78997: fix bad rebase of moved test file #100424

    Activity

    1. lfriedri commented on Sep 27, 2018

      @lfriedri
      lfriedrimannequin
      on Sep 27, 2018
      MannequinAuthor
      More actions

      The following creates an OSError:

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

      The expected behavior would be to return "False"

    2. lfriedri
      added
      3.7 (EOL)end of lifeend of life
      topic-ctypes
      type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
      on Sep 27, 2018
    3. eryksun commented on Sep 27, 2018

      @eryksun
      eryksun
      on Sep 27, 2018
      Contributor
      More actions

      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.

    4. eryksun
      added
      3.8 (EOL)end of lifeend of life
      on Sep 27, 2018
    5. lfriedri commented on Sep 27, 2018

      @lfriedri
      lfriedrimannequin
      on Sep 27, 2018
      MannequinAuthor
      More actions

      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?

    6. eryksun commented on Mar 28, 2021

      @eryksun
      eryksun
      on Mar 28, 2021
      Contributor
      More actions

      __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.

    7. eryksun
      added
      easy
      3.9 (EOL)end of lifeend of life
      3.10 (EOL)end of lifeend of life
      and removed
      3.7 (EOL)end of lifeend of life
      on Mar 28, 2021
    8. eryksun
      changed the title [-]ctypes + hasattr[/-] [+]raise AttributeError if loading fails in ctypes.LibraryLoader.__getattr__[/+] on Mar 28, 2021
    9. farfella commented on Apr 4, 2021

      @farfella
      farfellamannequin
      on Apr 4, 2021
      Mannequin
      More actions

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

    10. ezio-melotti
      transferred this issue fromon Apr 10, 2022
    11. FFY00
      added a commit that references this issue on Dec 15, 2022

      gh-78997: AttributeError if loading fails in LibraryLoader.__getattr__

      101cfe6
    12. carljm
      added a commit that references this issue on Dec 16, 2022

      Merge branch 'main' into fastmock

      2260a54
    13. FFY00
      added a commit that references this issue on Dec 22, 2022

      gh-78997: fix bad rebase of moved test file (#100424)

      aa878f0
    14. iritkatriel
      added a commit that references this issue on Dec 28, 2022

      Merged main into regmachine branch (#50)

      fdc73cb
    15. hauntsaninja commented on Jan 29, 2023

      @hauntsaninja
      hauntsaninja
      on Jan 29, 2023
      Contributor
      More actions

      Thanks, looks like this was fixed!

    16. hauntsaninja
      closed this as completedon Jan 29, 2023
    17. kevinhendricks commented on Feb 28, 2024

      @kevinhendricks
      kevinhendricks
      on Feb 28, 2024
      More actions

      For the record, why was a backwards incompatible change made in just a point release without any deprecation warnings? We can work around this change from throwing OSError to AttrbteError in cdll but why on earth was this change added in a minor release?

    18. fzimmermann89 commented on Jun 25, 2025

      @fzimmermann89
      fzimmermann89
      on Jun 25, 2025
      More actions

      This change was really communicated badly in the windows section of the changelog:

      [bpo-34816](https://bugs.python.org/issue?@action=redirect&bpo=34816): hasattr(ctypes.windll, 'nonexistant') now returns False instead of raising [OSError](https://docs.python.org/3/library/exceptions.html#OSError). 
      
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    No one assigned

      Labels

      3.10 (EOL)end of lifeend of life3.8 (EOL)end of lifeend of life3.9 (EOL)end of lifeend of lifeeasytopic-ctypestype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

      Projects

      No projects

        Milestone

        No milestone

        Relationships

        None yet

        Development

        No branches or pull requests

        Issue actions

        You are visitor 01402503 — GeezerVison does not store the pages you look up.
        Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.