mirror of
https://github.com/python/cpython.git
synced 2025-09-26 18:29:57 +00:00
In cases where dealing with base64, do the conversion but then get the ASCII
string representation for use in the XML. Also strip out some unneeded encoding/decoding steps.
This commit is contained in:
parent
96d7e8369c
commit
2dbde5ea44
1 changed files with 6 additions and 7 deletions
|
@ -165,7 +165,7 @@ def escape(s):
|
||||||
def _stringify(string):
|
def _stringify(string):
|
||||||
# convert to 7-bit ascii if possible
|
# convert to 7-bit ascii if possible
|
||||||
try:
|
try:
|
||||||
return string.encode("ascii")
|
return string.decode("ascii")
|
||||||
except UnicodeError:
|
except UnicodeError:
|
||||||
return string
|
return string
|
||||||
|
|
||||||
|
@ -384,11 +384,13 @@ class Binary:
|
||||||
return self.data != other
|
return self.data != other
|
||||||
|
|
||||||
def decode(self, data):
|
def decode(self, data):
|
||||||
self.data = base64.decodestring(data)
|
self.data = str8(base64.decodestring(data))
|
||||||
|
|
||||||
def encode(self, out):
|
def encode(self, out):
|
||||||
out.write("<value><base64>\n")
|
out.write("<value><base64>\n")
|
||||||
base64.encode(io.StringIO(self.data), out)
|
encoded = base64.encodestring(self.data)
|
||||||
|
out.write(encoded.decode('ascii'))
|
||||||
|
out.write('\n')
|
||||||
out.write("</base64></value>\n")
|
out.write("</base64></value>\n")
|
||||||
|
|
||||||
def _binary(data):
|
def _binary(data):
|
||||||
|
@ -615,7 +617,6 @@ class Marshaller:
|
||||||
dispatch[str8] = dump_string
|
dispatch[str8] = dump_string
|
||||||
|
|
||||||
def dump_unicode(self, value, write, escape=escape):
|
def dump_unicode(self, value, write, escape=escape):
|
||||||
value = value.encode(self.encoding)
|
|
||||||
write("<value><string>")
|
write("<value><string>")
|
||||||
write(escape(value))
|
write(escape(value))
|
||||||
write("</string></value>\n")
|
write("</string></value>\n")
|
||||||
|
@ -644,9 +645,7 @@ class Marshaller:
|
||||||
write("<value><struct>\n")
|
write("<value><struct>\n")
|
||||||
for k, v in value.items():
|
for k, v in value.items():
|
||||||
write("<member>\n")
|
write("<member>\n")
|
||||||
if isinstance(k, basestring):
|
if not isinstance(k, basestring):
|
||||||
k = k.encode(self.encoding)
|
|
||||||
else:
|
|
||||||
raise TypeError, "dictionary key must be string"
|
raise TypeError, "dictionary key must be string"
|
||||||
write("<name>%s</name>\n" % escape(k))
|
write("<name>%s</name>\n" % escape(k))
|
||||||
dump(v, write)
|
dump(v, write)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue