Fix allow_unicode (ticket:3).

This commit is contained in:
Kirill Simonov 2006-04-12 22:26:41 +00:00
parent cc316a4699
commit fcf5d614a9
3 changed files with 14 additions and 9 deletions

View File

@ -67,7 +67,7 @@ def emit(events, writer=None, Emitter=Emitter):
def dump_all(natives, writer=None, Emitter=Emitter,
Serializer=Serializer, Representer=Representer,
encoding=None, line_break=None, canonical=None,
encoding='utf-8', line_break=None, canonical=None,
indent=None, width=None, allow_unicode=None):
if writer is None:
try:
@ -91,7 +91,7 @@ def dump_all(natives, writer=None, Emitter=Emitter,
def safe_dump_all(natives, writer=None, Emitter=Emitter,
Serializer=Serializer, Representer=SafeRepresenter,
encoding=None, line_break=None, canonical=None,
encoding='utf-8', line_break=None, canonical=None,
indent=None, width=None, allow_unicode=None):
return dump_all(natives, writer, Emitter, Serializer, Representer,
encoding, line_break, canonical, indent, width, allow_unicode)

View File

@ -119,14 +119,19 @@ class SafeRepresenter(Detector, BaseRepresenter):
u'null')
def represent_str(self, native):
encoding = None
try:
native.encode('ascii')
ascii = True
except (UnicodeDecodeError, UnicodeEncodeError):
ascii = False
if ascii:
unicode(native, 'ascii')
encoding = 'ascii'
except UnicodeDecodeError:
try:
unicode(native, 'utf-8')
encoding = 'utf-8'
except UnicodeDecodeError:
pass
if encoding:
return self.represent_scalar(u'tag:yaml.org,2002:str',
unicode(native, 'ascii'))
unicode(native, encoding))
else:
return self.represent_scalar(u'tag:yaml.org,2002:binary',
unicode(native.encode('base64')), style='|')

View File

@ -19,7 +19,7 @@ class Serializer:
self.anchors = {}
self.last_anchor_id = 0
self.closed = None
self.open(encoding, line_break, canonical, indent, width)
self.open(encoding, line_break, canonical, indent, width, allow_unicode)
def open(self, encoding=None, line_break=None, canonical=None,
indent=None, width=None, allow_unicode=None):