Make list(ui.config) raise a TypeError

As per PEP234 (https://www.python.org/dev/peps/pep-0234/) if an object
has __getitem__() but not __iter__() then the former will be used to
build an iterable, invoking it with 0, then 1, ... until a KeyError is
raised. Lazy configuration makes it never happen, and list(config) runs
indefinitely, hogging all memory.

Implement __iter__(), which raises a TypeError, to solve that problem.
This commit is contained in:
Bruno Cauet 2015-01-19 10:35:32 +01:00
parent f346853710
commit 70ef96d565

View file

@ -210,6 +210,11 @@ class ConfigView(object):
def __repr__(self):
return '<ConfigView: %s>' % self.name
def __iter__(self):
"""Prevent list(config) from using __getitem__ and never halting"""
raise TypeError(u"{!r} object is not "
u"iterable".format(self.__class__.__name__))
def __getitem__(self, key):
"""Get a subview of this view."""
return Subview(self, key)