From 08b539a80efd9c7ebf9954372b6dda1ef9e72977 Mon Sep 17 00:00:00 2001 From: Adrian Sampson Date: Fri, 16 Dec 2011 16:45:50 -0800 Subject: [PATCH] fix field inference w/ null first item (closes #14 on GitHub) When a partial match is found, its first item (task.items[0]) may be None, and _infer_album_fields would crash in this case. This solution walks through the items list and finds the first non-None item. --- beets/importer.py | 19 +++++++++++++------ docs/changelog.rst | 1 + test/test_importer.py | 7 +++++++ 3 files changed, 21 insertions(+), 6 deletions(-) diff --git a/beets/importer.py b/beets/importer.py index a902ea38a..0d076f5ba 100644 --- a/beets/importer.py +++ b/beets/importer.py @@ -170,18 +170,25 @@ def _infer_album_fields(task): elif task.choice_flag == action.APPLY: # Applying autotagged metadata. Just get AA from the first # item. - if not task.items[0].albumartist: - changes['albumartist'] = task.items[0].artist - if not task.items[0].mb_albumartistid: - changes['mb_albumartistid'] = task.items[0].mb_artistid + for item in task.items: + if item is not None: + first_item = item + break + else: + assert False, "all items are None" + if not first_item.albumartist: + changes['albumartist'] = first_item.artist + if not first_item.mb_albumartistid: + changes['mb_albumartistid'] = first_item.mb_artistid else: assert False # Apply new metadata. for item in task.items: - for k, v in changes.iteritems(): - setattr(item, k, v) + if item is not None: + for k, v in changes.iteritems(): + setattr(item, k, v) def _open_state(): """Reads the state file, returning a dictionary.""" diff --git a/docs/changelog.rst b/docs/changelog.rst index a0ad72170..0c710ef98 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -13,6 +13,7 @@ This release focuses on making beets' path formatting vastly more powerful. functions via a plugin, see :ref:`writing-plugins`. * Fix an incompatibility in BPD with libmpc (the library that powers mpc and ncmpc). +* Fix a crash when importing a partial match whose first track was missing. 1.0b11 (December 12, 2011) -------------------------- diff --git a/test/test_importer.py b/test/test_importer.py index be69622df..f87189b56 100644 --- a/test/test_importer.py +++ b/test/test_importer.py @@ -541,6 +541,13 @@ class InferAlbumDataTest(unittest.TestCase): self._infer() self.assertFalse(self.items[0].comp) + def test_first_item_null_apply(self): + self.items[0] = None + self.task.set_choice(({}, self.items)) # APPLY + self._infer() + self.assertFalse(self.items[1].comp) + self.assertEqual(self.items[1].albumartist, self.items[2].artist) + class DuplicateCheckTest(unittest.TestCase): def setUp(self): self.lib = library.Library(':memory:')