From 8c1ffeb614406ecaad651609f25f7bffee6237aa Mon Sep 17 00:00:00 2001 From: Nick Coghlan Date: Sat, 17 Oct 2009 15:09:41 +0000 Subject: [PATCH] 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) --- Lib/test/test_support.py | 3 ++ Lib/test/test_xmlrpc.py | 78 +++++++++++++++++----------------------- 2 files changed, 36 insertions(+), 45 deletions(-) diff --git a/Lib/test/test_support.py b/Lib/test/test_support.py index 45b1b0187f8..2f3a3784b3a 100644 --- a/Lib/test/test_support.py +++ b/Lib/test/test_support.py @@ -659,6 +659,9 @@ def captured_output(stream_name): 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. diff --git a/Lib/test/test_xmlrpc.py b/Lib/test/test_xmlrpc.py index 8f882c0bc8c..13db50f4d66 100644 --- a/Lib/test/test_xmlrpc.py +++ b/Lib/test/test_xmlrpc.py @@ -160,17 +160,17 @@ class XMLRPCTestCase(unittest.TestCase): """ # 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 @@ class CGIHandlerTestCase(unittest.TestCase): 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 = """ - - test_method - - - foo - - - bar - - - -""" - open("xmldata.txt", "w").write(data) - tmp1 = sys.stdin - tmp2 = sys.stdout + + test_method + + + foo + + + bar + + + + """ - sys.stdin = open("xmldata.txt", "r") - sys.stdout = open(test_support.TESTFN, "w") - - with test_support.EnvironmentVarGuard() as env: + 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 @@ class CGIHandlerTestCase(unittest.TestCase): len(content)) - os.remove("xmldata.txt") - os.remove(test_support.TESTFN) - class FakeSocket: def __init__(self):