[Python-checkins] r75462 - in python/trunk/Lib/test: test_support.py test_xmlrpc.py

nick.coghlan python-checkins at python.org
Sat Oct 17 17:09:41 CEST 2009


Author: nick.coghlan
Date: Sat Oct 17 17:09:41 2009
New Revision: 75462

Log:
Don't invoke reload(sys) and use StringIO objects instead of real files to capture stdin and stdout when needed (ensures all sys attributes remain unmodified after test_xmlrpc runs)

Modified:
   python/trunk/Lib/test/test_support.py
   python/trunk/Lib/test/test_xmlrpc.py

Modified: python/trunk/Lib/test/test_support.py
==============================================================================
--- python/trunk/Lib/test/test_support.py	(original)
+++ python/trunk/Lib/test/test_support.py	Sat Oct 17 17:09:41 2009
@@ -659,6 +659,9 @@
 def captured_stdout():
     return captured_output("stdout")
 
+def captured_stdin():
+    return captured_output("stdin")
+
 def gc_collect():
     """Force as many objects as possible to be collected.
 

Modified: python/trunk/Lib/test/test_xmlrpc.py
==============================================================================
--- python/trunk/Lib/test/test_xmlrpc.py	(original)
+++ python/trunk/Lib/test/test_xmlrpc.py	Sat Oct 17 17:09:41 2009
@@ -160,17 +160,17 @@
                   """
 
         # sys.setdefaultencoding() normally doesn't exist after site.py is
-        # loaded.  reload(sys) is the way to get it back.
+        # loaded.  Import a temporary fresh copy to get access to it
+        # but then restore the original copy to avoid messing with
+        # other potentially modified sys module attributes
         old_encoding = sys.getdefaultencoding()
-        setdefaultencoding_existed = hasattr(sys, "setdefaultencoding")
-        reload(sys) # ugh!
-        sys.setdefaultencoding("iso-8859-1")
-        try:
-            (s, d), m = xmlrpclib.loads(utf8)
-        finally:
-            sys.setdefaultencoding(old_encoding)
-            if not setdefaultencoding_existed:
-                del sys.setdefaultencoding
+        with test_support.CleanImport('sys'):
+            import sys as temp_sys
+            temp_sys.setdefaultencoding("iso-8859-1")
+            try:
+                (s, d), m = xmlrpclib.loads(utf8)
+            finally:
+                temp_sys.setdefaultencoding(old_encoding)
 
         items = d.items()
         if have_unicode:
@@ -831,54 +831,45 @@
             env['REQUEST_METHOD'] = 'GET'
             # if the method is GET and no request_text is given, it runs handle_get
             # get sysout output
-            tmp = sys.stdout
-            sys.stdout = open(test_support.TESTFN, "w")
-            self.cgi.handle_request()
-            sys.stdout.close()
-            sys.stdout = tmp
+            with test_support.captured_stdout() as data_out:
+                self.cgi.handle_request()
 
             # parse Status header
-            handle = open(test_support.TESTFN, "r").read()
+            data_out.seek(0)
+            handle = data_out.read()
             status = handle.split()[1]
             message = ' '.join(handle.split()[2:4])
 
             self.assertEqual(status, '400')
             self.assertEqual(message, 'Bad Request')
 
-            os.remove(test_support.TESTFN)
 
     def test_cgi_xmlrpc_response(self):
         data = """<?xml version='1.0'?>
-<methodCall>
-    <methodName>test_method</methodName>
-    <params>
-        <param>
-            <value><string>foo</string></value>
-        </param>
-        <param>
-            <value><string>bar</string></value>
-        </param>
-     </params>
-</methodCall>
-"""
-        open("xmldata.txt", "w").write(data)
-        tmp1 = sys.stdin
-        tmp2 = sys.stdout
-
-        sys.stdin = open("xmldata.txt", "r")
-        sys.stdout = open(test_support.TESTFN, "w")
-
-        with test_support.EnvironmentVarGuard() as env:
+        <methodCall>
+            <methodName>test_method</methodName>
+            <params>
+                <param>
+                    <value><string>foo</string></value>
+                </param>
+                <param>
+                    <value><string>bar</string></value>
+                </param>
+            </params>
+        </methodCall>
+        """
+
+        with test_support.EnvironmentVarGuard() as env, \
+             test_support.captured_stdout() as data_out, \
+             test_support.captured_stdin() as data_in:
+            data_in.write(data)
+            data_in.seek(0)
             env['CONTENT_LENGTH'] = str(len(data))
             self.cgi.handle_request()
-
-        sys.stdin.close()
-        sys.stdout.close()
-        sys.stdin = tmp1
-        sys.stdout = tmp2
+        data_out.seek(0)
 
         # will respond exception, if so, our goal is achieved ;)
-        handle = open(test_support.TESTFN, "r").read()
+        handle = data_out.read()
 
         # start with 44th char so as not to get http header, we just need only xml
         self.assertRaises(xmlrpclib.Fault, xmlrpclib.loads, handle[44:])
@@ -895,9 +886,6 @@
             len(content))
 
 
-        os.remove("xmldata.txt")
-        os.remove(test_support.TESTFN)
-
 class FakeSocket:
 
     def __init__(self):


More information about the Python-checkins mailing list