[New-bugs-announce] [issue25884] inspect.getmro() fails when base class lacks __bases__ attribute.

Brandon Zerbe report at bugs.python.org
Wed Dec 16 10:31:56 EST 2015


New submission from Brandon Zerbe:

I am using a possibly non-standard python package called Forthon, and when I inspect an object that is dependent on the Forthon class, I get the following error:

  File "/Users/zerbeb/homemade_programs/config2class/src/method_parsing.py", line 18, in get_all_init_args
    inherited_classes = inspect.getmro(class_obj)  
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/inspect.py", line 346, in getmro
    if hasattr(cls, "__bases__"):
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/inspect.py", line 337, in _searchbases
    for base in cls.__bases__:
AttributeError: 'Forthon' object has no attribute '__bases__'

This was easy enough to fix, simply add "if not hasattr(cls,'__bases__'): return" to the _searchbases function:

def _searchbases(cls, accum):
    # Simulate the "classic class" search order.
    if cls in accum:
        return
    if not hasattr(cls, "__bases__"): #Additional code.
        return
    accum.append(cls)
    for base in cls.__bases__:
        _searchbases(base, accum)

Maybe you have a better solution, but I think this edge case can be trivially solved however you decide to edit the code.

Thanks!

----------
messages: 256525
nosy: billyziege
priority: normal
severity: normal
status: open
title: inspect.getmro() fails when base class lacks __bases__ attribute.
versions: Python 2.7

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue25884>
_______________________________________


More information about the New-bugs-announce mailing list