Skip to content

Commit bd4964d

Browse files
committed
Add more robust logic to find the last Bar in a ScorePart
This avoids an AttributeError if the last object in a ScorePart's barlist is not a Bar. For example, \addlyrics blocks are represented by simple Python lists rather than Bar objects. Fixes #69.
1 parent ea3c0c4 commit bd4964d

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

ly/musicxml/xml_objs.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,8 @@ def iterate_partgroup(self, group):
9696

9797
def iterate_part(self, part):
9898
"""The part is iterated."""
99-
if part.barlist:
100-
last_bar = part.barlist[-1]
99+
last_bar = part.last_bar()
100+
if last_bar:
101101
last_bar_objs = last_bar.obj_list
102102
part.set_first_bar(self.divisions)
103103
self.musxml.create_part(part.name, part.abbr, part.midi)
@@ -458,6 +458,12 @@ def extract_global_to_section(self, name):
458458
section.barlist.append(section_bar)
459459
return section
460460

461+
def last_bar(self):
462+
"""Returns the last Bar object in the score, or None if the score is empty."""
463+
for obj in reversed(self.barlist):
464+
if isinstance(obj, Bar):
465+
return obj
466+
461467

462468
class Bar():
463469
""" Representing the bar/measure.

0 commit comments

Comments
 (0)