mirror of
https://gitee.com/openharmony/third_party_pyyaml
synced 2024-11-23 15:30:46 +00:00
Fix allow_unicode (ticket:3).
This commit is contained in:
parent
cc316a4699
commit
fcf5d614a9
@ -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)
|
||||
|
@ -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='|')
|
||||
|
@ -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):
|
||||
|
Loading…
Reference in New Issue
Block a user