From 688a0ca97b9e78f40f8c2a97150406db712a3547 Mon Sep 17 00:00:00 2001
From: Fred Drake <fdrake@acm.org>
Date: Fri, 11 Feb 2005 18:00:16 +0000
Subject: fix decoding in _stringify to not depend on the default encoding
 (closes SF bug #1115989)

---
 Lib/test/test_xmlrpc.py | 42 ++++++++++++++++++++++++++++++++++++++++++
 Lib/xmlrpclib.py        |  2 +-
 2 files changed, 43 insertions(+), 1 deletion(-)

diff --git a/Lib/test/test_xmlrpc.py b/Lib/test/test_xmlrpc.py
index 1f533d2..3a4d808 100644
--- a/Lib/test/test_xmlrpc.py
+++ b/Lib/test/test_xmlrpc.py
@@ -3,6 +3,13 @@ import unittest
 import xmlrpclib
 from test import test_support
 
+try:
+    unicode
+except NameError:
+    have_unicode = False
+else:
+    have_unicode = True
+
 alist = [{'astring': 'foo@bar.baz.spam',
           'afloat': 7283.43,
           'anint': 2**20,
@@ -39,6 +46,41 @@ class XMLRPCTestCase(unittest.TestCase):
                           xmlrpclib.loads(strg)[0][0])
         self.assertRaises(TypeError, xmlrpclib.dumps, (arg1,))
 
+    def test_default_encoding_issues(self):
+        # SF bug #1115989: wrong decoding in '_stringify'
+        utf8 = """<?xml version='1.0' encoding='iso-8859-1'?>
+                  <params>
+                    <param><value>
+                      <string>abc \x95</string>
+                      </value></param>
+                    <param><value>
+                      <struct>
+                        <member>
+                          <name>def \x96</name>
+                          <value><string>ghi \x97</string></value>
+                          </member>
+                        </struct>
+                      </value></param>
+                  </params>
+                  """
+        old_encoding = sys.getdefaultencoding()
+        reload(sys) # ugh!
+        sys.setdefaultencoding("iso-8859-1")
+        try:
+            (s, d), m = xmlrpclib.loads(utf8)
+        finally:
+            sys.setdefaultencoding(old_encoding)
+        items = d.items()
+        if have_unicode:
+            self.assertEquals(s, u"abc \x95")
+            self.assert_(isinstance(s, unicode))
+            self.assertEquals(items, [(u"def \x96", u"ghi \x97")])
+            self.assert_(isinstance(items[0][0], unicode))
+            self.assert_(isinstance(items[0][1], unicode))
+        else:
+            self.assertEquals(s, "abc \xc2\x95")
+            self.assertEquals(items, [("def \xc2\x96", "ghi \xc2\x97")])
+
 def test_main():
     test_support.run_unittest(XMLRPCTestCase)
 
diff --git a/Lib/xmlrpclib.py b/Lib/xmlrpclib.py
index 7b68196..0c7cee2 100644
--- a/Lib/xmlrpclib.py
+++ b/Lib/xmlrpclib.py
@@ -168,7 +168,7 @@ if unicode:
     def _stringify(string):
         # convert to 7-bit ascii if possible
         try:
-            return str(string)
+            return string.encode("ascii")
         except UnicodeError:
             return string
 else:
-- 
cgit v0.12

