googlefonts/glyphsLib

Iterating over layers of an 'orphan' GSGlyph raises KeyError

anthrotype opened this issue · 0 comments

In [1]: from glyphsLib.classes import *
In [2]: g = GSGlyph()
In [3]: l = GSLayer()
In [4]: l.layerId = l.associatedMasterId = "layer-0"
In [5]: g.layers.append(l)
In [6]: list(g.layers)
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
Cell In[6], line 1
----> 1 list(g.layers)

File ~/oss/glyphsLib/Lib/glyphsLib/classes.py:544, in LayersIterator.__next__(self)
    542     if self.curInd >= len(self._owner._layers):
    543         raise StopIteration
--> 544     item = self._owner._layers[self.curInd]
    545 self.curInd += 1
    546 return item

KeyError: 0

this is because GSGlyph._layers is an OrderedDict keyed by layerIds (str) and self.curInd is an integer (current index of the iterator), I think this is dead code which is never used in real world since most glyphs do have a GSFont parent, and originally the _layers were a list which could be indexed by integer key.

I found this while writing some unit tests. I'll send a PR