Skip to content

try/except block in ismethoddescriptor() in inspect.py, so that pydoc works with pygame in Python 3.2 #58575

Closed

Description

@ncdave4life
BPO 14367
Nosy @pitrou, @merwok, @bitdancer, @voidspace, @1st1
Superseder
  • bpo-1785: "inspect" gets broken by some descriptors
  • Files
  • inspect.diff: inspect.py patch so that pydoc works with pygame
  • 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 = <Date 2020-11-07.00:55:20.174>
    created_at = <Date 2012-03-19.04:42:22.936>
    labels = ['type-bug', 'library']
    title = 'try/except block in ismethoddescriptor() in inspect.py, so that pydoc works with pygame in Python 3.2'
    updated_at = <Date 2020-11-07.00:55:20.174>
    user = 'https://bugs.python.org/ncdave4life'

    bugs.python.org fields:

    activity = <Date 2020-11-07.00:55:20.174>
    actor = 'iritkatriel'
    assignee = 'none'
    closed = True
    closed_date = <Date 2020-11-07.00:55:20.174>
    closer = 'iritkatriel'
    components = ['Library (Lib)']
    creation = <Date 2012-03-19.04:42:22.936>
    creator = 'ncdave4life'
    dependencies = []
    files = ['24935']
    hgrepos = []
    issue_num = 14367
    keywords = ['patch']
    message_count = 11.0
    messages = ['156314', '156381', '156553', '156577', '156581', '156601', '156602', '156645', '212154', '236494', '344843']
    nosy_count = 7.0
    nosy_names = ['pitrou', 'illume', 'eric.araujo', 'r.david.murray', 'michael.foord', 'ncdave4life', 'yselivanov']
    pr_nums = []
    priority = 'normal'
    resolution = 'duplicate'
    stage = 'resolved'
    status = 'closed'
    superseder = '1785'
    type = 'behavior'
    url = 'https://bugs.python.org/issue14367'
    versions = ['Python 3.2']

    Activity

    1. ncdave4life commented on Mar 19, 2012

      ncdave4lifemannequin
      MannequinAuthor

      I noticed that pydoc doesn't work for pygame under python 3.2.1 or 3.2.2 for Win32; it just reports:

      NotImplementedError: scrap module not available (ImportError: No module
      named scrap)

      I made a small patch to inspect.py to solve the problem (I just added a
      try/expect around the failing statement in ismethoddescriptor). Here's the diff:
      http://www.burtonsys.com/python32/inspect.diff

      With that patch, pydoc works with pygame, and reports just a few pygame issues:
      *scrap* = <pygame.MissingModule object>
      *sndarray* = <pygame.MissingModule object>
      *surfarray* = <pygame.MissingModule object>

    2. added
      stdlibStandard Library Python modules in the Lib/ directory
      type-bugAn unexpected behavior, bug, or error
      on Mar 19, 2012
    3. ncdave4life commented on Mar 20, 2012

      ncdave4lifemannequin
      MannequinAuthor

      "expect?" Did I type that?? Should be "try/except," of course.

    4. bitdancer commented on Mar 22, 2012

      @bitdancer
      Member

      Thanks for the report and patch. However, in general we prefer not to mask exceptions (doing that can hide bugs in programs). It would probably be reasonable to fix this in pydoc, however.

      Alternatively there might be a specific exception or set of exceptions that should indeed result in a False value. What exception is being raised? Can you provide a test case extracted from what pygame is doing that triggers the problem?

    5. ncdave4life commented on Mar 22, 2012

      ncdave4lifemannequin
      MannequinAuthor

      Well, the exception is NotImplementedError. It's raised explicitly in pygame\init.py

      I uncommented my commented-out print statement in inspect.py, and added a traceback print, and ran pydoc like this:

      c:\python32\python.exe c:\python32\Lib\pydoc.py -p 7464

      The I viewed http://localhost:7464/pygame.html in a web browser, and the pydoc server printed:

      Server ready at http://localhost:7464/
      Server commands: [b]rowser, [q]uit
      server> c:\python32\Lib\inspect.py:105: RuntimeWarning: use scrap: No module named scrap
      (ImportError: No module named scrap)
        result = (hasattr(object, "__get__")
      dbg: ismethoddescriptor(<pygame.MissingModule object at 0x03763F90>) failed, exception=NotImplementedError('scrap module not available\n(ImportError: No module named scrap)',)
      Traceback (most recent call last):
        File "c:\python32\Lib\inspect.py", line 105, in ismethoddescriptor
          result = (hasattr(object, "__get__")
        File "c:\python32\lib\site-packages\pygame\__init__.py", line 74, in __getattr__
          raise NotImplementedError(MissingPygameModule)
      NotImplementedError: scrap module not available
      (ImportError: No module named scrap)
      ------------------------------------------------------------
      
      c:\python32\Lib\inspect.py:105: RuntimeWarning: use sndarray: no module named numpy or Numeric found
      (ImportError: no module named numpy or Numeric found)
        result = (hasattr(object, "__get__")
      dbg: ismethoddescriptor(<pygame.MissingModule object at 0x0376EFF0>) failed, exception=NotImplementedError('sndarray module not available\n(ImportError: no module named numpy or Numeric found)',)
      Traceback (most recent call last):
      ...(etc.)

      Here's the modified ismethoddescriptor() in inspect.py:

      def ismethoddescriptor(object):
          """Return true if the object is a method descriptor.
      But not if ismethod() or isclass() or isfunction() are true.
      
      This is new in Python 2.2, and, for example, is true of int.__add__.
      An object passing this test has a __get__ attribute but not a __set__
      attribute, but beyond that the set of attributes varies.  __name__ is
      usually sensible, and __doc__ often is.
      
      Methods implemented via descriptors that also pass one of the other
      tests return false from the ismethoddescriptor() test, simply because
      the other tests promise more -- you can, e.g., count on having the
      __func__ attribute (etc) when an object passes ismethod()."""
      import traceback
      try:
          result = (hasattr(object, "__get__")
                   and not hasattr(object, "__set__") # else it's a data descriptor
                   and not ismethod(object)           # mutual exclusion
                   and not isfunction(object)
                   and not isclass(object))
      except Exception as whichone:
          print('dbg: ismethoddescriptor('+repr(object)+') failed, exception='+repr(whichone))
          traceback.print_exc(file=sys.stdout)
          print("-"*60 + '\n')
          result = False
      return result
      
    6. bitdancer commented on Mar 22, 2012

      @bitdancer
      Member

      Michael, I thought you might be interested in this one since it looks like it might involve inspect executing code unexpectedly, and I know you worked on ways of not doing that...

    7. voidspace commented on Mar 22, 2012

      @voidspace
      Contributor

      The code is *already* triggering execution through the use of hasattr. It could be switched to use getattr_static instead, but the try...except in the patch looks reasonable whether or not that change is made.

    8. pitrou commented on Mar 22, 2012

      @pitrou
      Member

      Well, the exception shouldn't be silenced. As David said, I think it would be better to silence the exception in pydoc.
      Besides, it would be nice to come with a standalone test case, because it's not clear exactly why pygame triggers this.

    9. voidspace commented on Mar 23, 2012

      @voidspace
      Contributor

      So PyGame __init__.py has various not-a-module objects with __getattr__ that raises a NotImplementedError on every attribute access. Changing inspect to use getattr_static, so it doesn't trigger code execution on attribute access, would fix the problem.

      Making pydoc resilient against exceptions is probably a *better* solution for the specific problem - although changing ismethoddescriptor to not trigger code execution would be good *as well*.

    10. 1st1 commented on Feb 25, 2014

      @1st1
      Member

      Is this still an issue?

    11. BreamoreBoy commented on Feb 24, 2015

      BreamoreBoymannequin
      Mannequin

      The code in ismethoddescriptor was completely rewritten as a result of bpo-1785 rendering the attached patch obsolete.

    12. illume commented on Jun 6, 2019

      illumemannequin
      Mannequin

      This can be closed.

      $ python3 -m pydoc -p 7464

      Then view in browser: http://localhost:7464/pygame.html

      No error.

    13. transferred this issue fromon Apr 10, 2022
    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

      stdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

      Projects

      No projects

        Milestone

        No milestone

        Relationships

        None yet

        Development

        No branches or pull requests

        Issue actions