Skip to content

Commit

Permalink
Merge pull request #1015 from googlefonts/fix-gslayer-repr
Browse files Browse the repository at this point in the history
GSLayer: fix name property when parent GSGlyph is in turn orphan
  • Loading branch information
anthrotype authored Jul 31, 2024
2 parents 280c208 + 98bcee8 commit aac5cfd
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
7 changes: 6 additions & 1 deletion Lib/glyphsLib/classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -3742,7 +3742,12 @@ def _is_master_layer(self):

@property
def name(self):
if self.associatedMasterId and self._is_master_layer and self.parent:
if (
self.associatedMasterId
and self._is_master_layer
and self.parent
and self.parent.parent
):
master = self.parent.parent.masterForId(self.associatedMasterId)
if master:
return master.name
Expand Down
21 changes: 21 additions & 0 deletions tests/classes_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1091,6 +1091,27 @@ def test_repr(self):
layer = self.layer
self.assertIsNotNone(layer.__repr__())

def test_repr_orphan_glyph(self):
# https://github.com/googlefonts/glyphsLib/issues/1014
layer = GSLayer()
self.assertIsNone(layer.parent) # orphan layer

expected = '<GSLayer "" (orphan)>'
self.assertEqual(repr(layer), expected)

layer.layerId = layer.associatedMasterId = "layer-0"
self.assertTrue(layer._is_master_layer)
self.assertEqual(repr(layer), expected)

parent = GSGlyph()
parent.layers.append(layer)
self.assertEqual(layer.parent, parent) # no longer orphan layer
self.assertIsNone(parent.parent) # but still orphan glyph

# this should not crash with
# AttributeError: 'NoneType' object has no attribute 'masterForId'
self.assertEqual(repr(layer), expected)

def test_parent(self):
self.assertIs(self.layer.parent, self.glyph)
self.assertIs(self.layer._background.parent, self.glyph)
Expand Down

0 comments on commit aac5cfd

Please sign in to comment.