Python 2.6.2 __length_hint__ behavior

Luke Macken lmacken at redhat.com
Fri Sep 11 21:35:01 UTC 2009


So when rebuilding python-genshi in rawhide with python 2.6.2 we hit a problem
running it's test suite.  Come to find out, this is due to some behavioral
changes in 2.6.2 with regard to iterators and __length_hint__.

    http://koji.fedoraproject.org/koji/getfile?taskID=1671970&name=build.log

This is the class and doctest that was causing problems::

    class Undefined(object):
        """Represents a reference to an undefined variable.

        >>> foo = Undefined('foo')
        >>> list(foo)
        []
        """
        def __iter__(self):
            return iter([])

        def _die(self, *args, **kwargs):
            __traceback_hide__ = True
            raise UndefinedError(self._name, self._owner)
        __call__ = __getattr__ = __getitem__ = _die


When tweaking the class a little and running the tests again:

    python-2.4.3-27.el5

    Failed example: list(foo)
    Expected: []
    Got:
        Undefined.__iter__
        []


    python-2.6.2-2.fc12.i686

    Failed example: list(foo)
    Expected: []
    Got:
        Undefined.__iter__
        __getattr__(('__length_hint__',))
        []

So, it looks like list(foo) calls foo.__iter__ and then calls
foo.__getattr__('__length_hint__').  Since Genshi's Undefined class overrides
__getattr__, this triggers some unexpected behavior.

This issue was filed upstream, where I attached a patch, which I built in
rawhide.

    http://genshi.edgewall.org/ticket/324

I believe this behavior was introduced in http://bugs.python.org/issue1242657

We will want to keep a lookout for similar problems in other places when
rebuilding our python modules again python 2.6.2


luke




More information about the fedora-devel-list mailing list