mirror of
https://gitee.com/openharmony/third_party_pyyaml
synced 2025-02-17 09:18:44 +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,
|
def dump_all(natives, writer=None, Emitter=Emitter,
|
||||||
Serializer=Serializer, Representer=Representer,
|
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):
|
indent=None, width=None, allow_unicode=None):
|
||||||
if writer is None:
|
if writer is None:
|
||||||
try:
|
try:
|
||||||
@ -91,7 +91,7 @@ def dump_all(natives, writer=None, Emitter=Emitter,
|
|||||||
|
|
||||||
def safe_dump_all(natives, writer=None, Emitter=Emitter,
|
def safe_dump_all(natives, writer=None, Emitter=Emitter,
|
||||||
Serializer=Serializer, Representer=SafeRepresenter,
|
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):
|
indent=None, width=None, allow_unicode=None):
|
||||||
return dump_all(natives, writer, Emitter, Serializer, Representer,
|
return dump_all(natives, writer, Emitter, Serializer, Representer,
|
||||||
encoding, line_break, canonical, indent, width, allow_unicode)
|
encoding, line_break, canonical, indent, width, allow_unicode)
|
||||||
|
@ -119,14 +119,19 @@ class SafeRepresenter(Detector, BaseRepresenter):
|
|||||||
u'null')
|
u'null')
|
||||||
|
|
||||||
def represent_str(self, native):
|
def represent_str(self, native):
|
||||||
|
encoding = None
|
||||||
try:
|
try:
|
||||||
native.encode('ascii')
|
unicode(native, 'ascii')
|
||||||
ascii = True
|
encoding = 'ascii'
|
||||||
except (UnicodeDecodeError, UnicodeEncodeError):
|
except UnicodeDecodeError:
|
||||||
ascii = False
|
try:
|
||||||
if ascii:
|
unicode(native, 'utf-8')
|
||||||
|
encoding = 'utf-8'
|
||||||
|
except UnicodeDecodeError:
|
||||||
|
pass
|
||||||
|
if encoding:
|
||||||
return self.represent_scalar(u'tag:yaml.org,2002:str',
|
return self.represent_scalar(u'tag:yaml.org,2002:str',
|
||||||
unicode(native, 'ascii'))
|
unicode(native, encoding))
|
||||||
else:
|
else:
|
||||||
return self.represent_scalar(u'tag:yaml.org,2002:binary',
|
return self.represent_scalar(u'tag:yaml.org,2002:binary',
|
||||||
unicode(native.encode('base64')), style='|')
|
unicode(native.encode('base64')), style='|')
|
||||||
|
@ -19,7 +19,7 @@ class Serializer:
|
|||||||
self.anchors = {}
|
self.anchors = {}
|
||||||
self.last_anchor_id = 0
|
self.last_anchor_id = 0
|
||||||
self.closed = None
|
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,
|
def open(self, encoding=None, line_break=None, canonical=None,
|
||||||
indent=None, width=None, allow_unicode=None):
|
indent=None, width=None, allow_unicode=None):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user