[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.
This commit is contained in:
Cosimo Lupo 2015-11-23 12:02:12 +00:00
parent 505c1a5ea7
commit 2eda1785f6

View File

@ -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.