[New-bugs-announce] [issue10066] xmlrpclib does not handle some non-printable characters properly
Peter Gyorko
report at bugs.python.org
Mon Oct 11 18:16:41 CEST 2010
New submission from Peter Gyorko <gyorkop at balabit.hu>:
If I add a string to the response, which contains non-printable characters, the output will not be parsed by the most of the XML parsers (I tried with XML-RPC for PHP).
Here is my quick and dirty fix:
--- a/Lib/xmlrpclib.py
+++ b/Lib/xmlrpclib.py
@@ -165,9 +165,18 @@ def _decode(data, encoding, is8bit=re.compile("[\x80-\xff]").search):
return data
def escape(s, replace=string.replace):
- s = replace(s, "&", "&")
- s = replace(s, "<", "<")
- return replace(s, ">", ">",)
+ res = ''
+ for char in s:
+ char_code = ord(char)
+ if (char_code < 32 and char_code not in (9, 10, 13)) or char_code > 126:
+ res += '\\x%02x' % ord(char)
+ else:
+ res += char
+
+ res = replace(res, "&", "&")
+ res = replace(res, "<", "<")
+ res = replace(res, ">", ">")
+ return res
if unicode:
def _stringify(string):
----------
components: XML
messages: 118376
nosy: gyorkop
priority: normal
severity: normal
status: open
title: xmlrpclib does not handle some non-printable characters properly
type: behavior
versions: Python 2.6
_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue10066>
_______________________________________
More information about the New-bugs-announce
mailing list