From 2eda1785f654125342f6b517e707b24bff92d18c Mon Sep 17 00:00:00 2001 From: Cosimo Lupo Date: Mon, 23 Nov 2015 12:02:12 +0000 Subject: [PATCH] [py23] fix ImportError when trying to import `unichr`, `basestring` or `unicode` when already defined When one does `from fontTools.misc.py23 import *`, everything seems to work fine. However, linters will complain when one uses the asterisk to import all names from a module, since they can't detect when names are left undefined -- asterisks are greedy and will eat all names. If one avoids the asterik and attempts to import explicitly, like in `from fontTools.misc.py23 import basestring`, the problem then is that, if `py23` does not re-define the name -- e.g. under python2 `basestring` or `unicode` are built-ins -- then the import statement raises `ImportError`. The same happens for the `unichr` function on a "wide" Python 2 build (in which `sys.maxunicode == 0x10FFFF`). Now, to work around this, we need to re-assign those built-ins to their very same names. This may look silly, but at least it works. --- Lib/fontTools/misc/py23.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Lib/fontTools/misc/py23.py b/Lib/fontTools/misc/py23.py index a447496da..2a5c4a7b9 100644 --- a/Lib/fontTools/misc/py23.py +++ b/Lib/fontTools/misc/py23.py @@ -4,17 +4,17 @@ from __future__ import print_function, division, absolute_import import sys try: - basestring + basestring = basestring except NameError: basestring = str try: - unicode + unicode = unicode except NameError: unicode = str try: - unichr + unichr = unichr if sys.maxunicode < 0x10FFFF: # workarounds for Python 2 "narrow" builds with UCS2-only support.