feaLib: add IncludedFeaNotFound error

subclass of FeatureLibError, only raised if IOError.errno == ENOENT (i.e. FileNotFoundError)

https://github.com/googlei18n/fontmake/issues/157#issuecomment-367380471

Will be useful in ufo2ft to detect when an included feature file doesn't exist and print a nicer error message
explaining that includes must be relative to the UFO itself, and not relative to the embedded features.fea file.

https://github.com/unified-font-object/ufo-spec/issues/55
This commit is contained in:
Cosimo Lupo 2018-02-21 16:41:11 +00:00
parent 767b9311eb
commit 7d7212b01f
No known key found for this signature in database
GPG Key ID: 59D54DB0C9976482
2 changed files with 10 additions and 2 deletions

View File

@ -14,3 +14,7 @@ class FeatureLibError(Exception):
return "%s:%d:%d: %s" % (path, line, column, message)
else:
return message
class IncludedFeaNotFound(FeatureLibError):
pass

View File

@ -1,7 +1,7 @@
from __future__ import print_function, division, absolute_import
from __future__ import unicode_literals
from fontTools.misc.py23 import *
from fontTools.feaLib.error import FeatureLibError
from fontTools.feaLib.error import FeatureLibError, IncludedFeaNotFound
import re
import os
@ -231,7 +231,11 @@ class IncludingLexer(object):
try:
fileobj = open(filename, "r", encoding="utf-8")
except IOError as err:
raise FeatureLibError(str(err), location)
# FileNotFoundError does not exist on Python < 3.3
import errno
if err.errno == errno.ENOENT:
raise IncludedFeaNotFound(str(err), location)
raise # pragma: no cover
data = fileobj.read()
filename = fileobj.name if hasattr(fileobj, "name") else "<features>"
if closing: