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

inspect.getmro() fails when base class lacks __bases__ attribute. #70072

Closed
billyziege mannequin opened this issue Dec 16, 2015 · 4 comments
Closed

inspect.getmro() fails when base class lacks __bases__ attribute. #70072

billyziege mannequin opened this issue Dec 16, 2015 · 4 comments

Comments

@billyziege
Copy link
Mannequin

billyziege mannequin commented Dec 16, 2015

BPO 25884
Nosy @bitdancer, @iritkatriel

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-30.22:51:53.051>
created_at = <Date 2015-12-16.15:31:56.936>
labels = []
title = 'inspect.getmro() fails when base class lacks __bases__ attribute.'
updated_at = <Date 2020-11-30.22:51:53.050>
user = 'https://bugs.python.org/billyziege'

bugs.python.org fields:

activity = <Date 2020-11-30.22:51:53.050>
actor = 'iritkatriel'
assignee = 'none'
closed = True
closed_date = <Date 2020-11-30.22:51:53.051>
closer = 'iritkatriel'
components = []
creation = <Date 2015-12-16.15:31:56.936>
creator = 'billyziege'
dependencies = []
files = []
hgrepos = []
issue_num = 25884
keywords = []
message_count = 4.0
messages = ['256525', '256526', '256527', '382205']
nosy_count = 3.0
nosy_names = ['r.david.murray', 'billyziege', 'iritkatriel']
pr_nums = []
priority = 'normal'
resolution = 'out of date'
stage = 'resolved'
status = 'closed'
superseder = None
type = None
url = 'https://bugs.python.org/issue25884'
versions = ['Python 2.7']

@billyziege
Copy link
Mannequin Author

billyziege mannequin commented Dec 16, 2015

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!

@bitdancer
Copy link
Member

Which version of python are you running? I can't match that traceback up to the code in the current 2.7 inspect module.

That said, the same issue probably exists in the current code. A 2.7 class is expected to have either an __mro__ or a __bases__ attribute, and if it has neither inspect probably *should* throw an error, since it can't know what to do with the class. The error could be clearer, though.

But, let's see what others think.

@billyziege
Copy link
Mannequin Author

billyziege mannequin commented Dec 16, 2015

I am using Python 2.7.5.  The segment of code from inspect that I previously extracted came from line 332 although you may also find it by "finding" _searchbases.

This is really an issue with Forthon:

http://hifweb.lbl.gov/Forthon/

Specifically the Forthon class, which I think is older than current standards.  If you guys want this Forthon class to flag an error (I can work around that too), than the proposed fix I sent can be rejected, and this ticket can be closed.  I just wanted to bring this case to you attention just in case.

Thanks again,

Brandon

Quoting "R. David Murray" <report@bugs.python.org>:

R. David Murray added the comment:

Which version of python are you running?  I can't match that
traceback up to the code in the current 2.7 inspect module.

That said, the same issue probably exists in the current code.  A 2.7
class is expected to have either an __mro__ or a __bases__ attribute,
and if it has neither inspect probably *should* throw an error, since
it can't know what to do with the class.  The error could be clearer,
though.

But, let's see what others think.

----------
nosy: +r.david.murray


Python tracker <report@bugs.python.org>
<http://bugs.python.org/issue25884\>


@iritkatriel
Copy link
Member

The _searchbases function was removed here, when getmro was simplified following the removal of old style classes:

b82c8e5

@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants