tags:

views:

15

answers:

1

While debuging why cmd2 wont load in in Jython, I found out that it breaks because Jython returns False to gettattr([alist],'reversed') while Python returns True.

I would assume that the correct result is True as a list is reversible..

Anyone knows what is going on?

my next option is to browse around Jython source.. and I am not looking forward to it ;)

BTW, I am using jython 2.5.1 on top of java "1.6.0_18" on ubuntu

Thx in advance for any hints

A: 

It appears as if the __reversed__ attribute of lists in Jython is not implemented. I don't get the results you describe, if I call

getattr([], '__reversed__')

in Python 2.5.2, I get

>>> [].__reversed__
<built-in method __reversed__ of list object at 0x7f72581d7050>

And in Jython 2.5.1, I get

>>> [].__reversed__
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'list' object has no attribute '__reversed__'

If it's returning True or False respectively, then you're probably not using a standard Python list.

In any case, it appears as if Jython doesn't support the __reversed__ attribute of lists to indicate that it is reversible. According to the documentation, this feature appears to have been added to the CPython implementation in Python 2.6, which might explain why it doesn't appear in Jython 2.5.1. I do find that __reversed__ appears in Python 2.5.2, so it may have been back-ported to CPython, but just not officially supported.

For now, you can suggest that cmd2 be patched with something like the following:

def can_be_reversed(o):
    "return True if an object can be reversed by reverse()"
    return hasattr(o, '__reversed__') or \
        hasattr(o, '__len__') and hasattr(o, '__getitem__')
Jason R. Coombs