Make these modules work when Python is compiled without Unicode support.

This commit is contained in:
Guido van Rossum 2001-09-21 19:22:34 +00:00
parent 11310bf867
commit dbb718fa87
3 changed files with 18 additions and 5 deletions

View file

@ -54,6 +54,12 @@ def __init__(self, value):
except ImportError:
PyStringMap = None
try:
UnicodeType
except NameError:
UnicodeType = None
MARK = '('
STOP = '.'
POP = '0'
@ -304,8 +310,8 @@ def save_unicode(self, object):
s = mdumps(l)[1:]
self.write(BINUNICODE + s + encoding)
else:
object = object.replace(u"\\", u"\\u005c")
object = object.replace(u"\n", u"\\u000a")
object = object.replace("\\", "\\u005c")
object = object.replace("\n", "\\u000a")
self.write(UNICODE + object.encode('raw-unicode-escape') + '\n')
memo_len = len(memo)
@ -334,8 +340,8 @@ def save_string(self, object):
self.write(BINSTRING + s + object)
else:
if unicode:
object = object.replace(u"\\", u"\\u005c")
object = object.replace(u"\n", u"\\u000a")
object = object.replace("\\", "\\u005c")
object = object.replace("\n", "\\u000a")
object = object.encode('raw-unicode-escape')
self.write(UNICODE + object + '\n')
else:

View file

@ -394,11 +394,15 @@ def f():
if z != 2: raise TestFailed, 'exec \'z=1+1\''
z = None
del z
import types
if hasattr(types, "UnicodeType"):
exec r"""if 1:
exec u'z=1+1\n'
if z != 2: raise TestFailed, 'exec u\'z=1+1\'\\n'
del z
exec u'z=1+1'
if z != 2: raise TestFailed, 'exec u\'z=1+1\''
"""
f()
g = {}
exec 'z = 1' in g

View file

@ -66,7 +66,10 @@ class BadZipfile(Exception):
_FH_EXTRA_FIELD_LENGTH = 11
# Used to compare file passed to ZipFile
_STRING_TYPES = (type('s'), type(u's'))
import types
_STRING_TYPES = (types.StringType,)
if hasattr(types, "UnicodeType"):
_STRING_TYPES = _STRING_TYPES + (types.UnicodeType,)
def is_zipfile(filename):