Skip to content

Commit 13fede5

Browse files
committed
Fix my fix for #1586: iterate over views
1 parent 3c8823d commit 13fede5

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

beets/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def read(self, user=True, defaults=True):
3333
super(IncludeLazyConfig, self).read(user, defaults)
3434

3535
try:
36-
for view in self['include'].all_contents():
36+
for view in self['include']:
3737
filename = view.as_filename()
3838
if os.path.isfile(filename):
3939
self.set_file(filename)

beets/util/confit.py

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -215,10 +215,28 @@ def __repr__(self):
215215
return '<{}: {}>'.format(self.__class__.__name__, self.name)
216216

217217
def __iter__(self):
218-
# Prevent list(config) from using __getitem__ and entering an
219-
# infinite loop.
220-
raise TypeError(u"{!r} object is not "
221-
u"iterable".format(self.__class__.__name__))
218+
"""Iterate over the keys of a dictionary view or the *subviews*
219+
of a list view.
220+
"""
221+
# Try getting the keys, if this is a dictionary view.
222+
try:
223+
keys = self.keys()
224+
for key in keys:
225+
yield key
226+
227+
except ConfigTypeError:
228+
# Otherwise, try iterating over a list.
229+
collection = self.get()
230+
if not isinstance(collection, (list, tuple)):
231+
raise ConfigTypeError(
232+
'{0} must be a dictionary or a list, not {1}'.format(
233+
self.name, type(collection).__name__
234+
)
235+
)
236+
237+
# Yield all the indices in the list.
238+
for index in range(len(collection)):
239+
yield self[index]
222240

223241
def __getitem__(self, key):
224242
"""Get a subview of this view."""

0 commit comments

Comments
 (0)