From bb21cb7c3a1568b8e6ebfc92f6704b0b810754d8 Mon Sep 17 00:00:00 2001 From: Denis Moyogo Jacquerye Date: Mon, 11 Jul 2016 08:00:23 +0100 Subject: [PATCH] userNameToFileName expects userName to be unicode strings, let it raise if not --- Lib/ufoLib/__init__.py | 10 +++------- Lib/ufoLib/filenames.py | 5 +++-- Lib/ufoLib/glifLib.py | 6 +++--- 3 files changed, 9 insertions(+), 12 deletions(-) diff --git a/Lib/ufoLib/__init__.py b/Lib/ufoLib/__init__.py index f2dfc9d73..3e3723b9e 100755 --- a/Lib/ufoLib/__init__.py +++ b/Lib/ufoLib/__init__.py @@ -3,6 +3,7 @@ import shutil from io import StringIO, BytesIO, open import codecs from copy import deepcopy +from fontTools.misc.py23 import basestring, unicode from ufoLib.glifLib import GlyphSet from ufoLib.validators import * from ufoLib.filenames import userNameToFileName @@ -38,11 +39,6 @@ fontinfo.plist values between the possible format versions. convertFontInfoValueForAttributeFromVersion3ToVersion2 """ -try: - basestring -except NameError: - basestring = str - __all__ = [ "makeUFOPath" "UFOLibError", @@ -1066,9 +1062,9 @@ class UFOWriter(object): # not caching this could be slightly expensive, # but caching it will be cumbersome existing = [d.lower() for d in list(self.layerContents.values())] - if not isinstance(layerName, basestring): + if not isinstance(layerName, unicode): try: - layerName = str(layerName) + layerName = unicode(layerName) except UnicodeDecodeError: raise UFOLibError("The specified layer name is not a Unicode string.") directory = userNameToFileName(layerName, existing=existing, prefix="glyphs.") diff --git a/Lib/ufoLib/filenames.py b/Lib/ufoLib/filenames.py index 4a82e2c36..9eb17d340 100644 --- a/Lib/ufoLib/filenames.py +++ b/Lib/ufoLib/filenames.py @@ -2,7 +2,7 @@ User name to file name conversion. This was taken form the UFO 3 spec. """ -from fontTools.misc.py23 import unicode +from fontTools.misc.py23 import basestring, unicode illegalCharacters = "\" * + / : < > ? [ \ ] | \0".split(" ") @@ -68,7 +68,8 @@ def userNameToFileName(userName, existing=[], prefix="", suffix=""): u'alt._con' """ # the incoming name must be a unicode string - assert isinstance(userName, unicode), "The value for userName must be a unicode string." + if not isinstance(userName, unicode): + raise ValueError("The value for userName must be a unicode string.") # establish the prefix and suffix lengths prefixLength = len(prefix) suffixLength = len(suffix) diff --git a/Lib/ufoLib/glifLib.py b/Lib/ufoLib/glifLib.py index 4efa8314c..fb350d839 100755 --- a/Lib/ufoLib/glifLib.py +++ b/Lib/ufoLib/glifLib.py @@ -15,7 +15,7 @@ from __future__ import unicode_literals import os from io import BytesIO, open from warnings import warn -from fontTools.misc.py23 import tobytes +from fontTools.misc.py23 import tobytes, unicode from ufoLib.plistlib import PlistWriter, readPlist, writePlist from ufoLib.plistFromETree import readPlistFromTree from ufoLib.pointPen import AbstractPointPen, PointToSegmentPen @@ -457,9 +457,9 @@ def glyphNameToFileName(glyphName, glyphSet): existing = [name.lower() for name in list(glyphSet.contents.values())] else: existing = [] - if not isinstance(glyphName, basestring): + if not isinstance(glyphName, unicode): try: - new = str(glyphName) + new = unicode(glyphName) glyphName = new except UnicodeDecodeError: pass