# HG changeset patch # Parent 8e4903dc83d99c67a0e24bafde10e84cc58d0ec7 # User anatoly techtonik do not allow CGIHTTPServer to meddle with parent os.environ diff -r 8e4903dc83d9 Lib/CGIHTTPServer.py --- a/Lib/CGIHTTPServer.py Tue Jul 20 19:23:00 2010 +0300 +++ b/Lib/CGIHTTPServer.py Wed Jul 21 10:14:05 2010 +0300 @@ -154,7 +154,7 @@ # Reference: http://hoohoo.ncsa.uiuc.edu/cgi/env.html # XXX Much of the following could be prepared ahead of time! - env = {} + env = os.environ.copy() env['SERVER_SOFTWARE'] = self.version_string() env['SERVER_NAME'] = self.server.server_name env['GATEWAY_INTERFACE'] = 'CGI/1.1' @@ -216,7 +216,6 @@ for k in ('QUERY_STRING', 'REMOTE_HOST', 'CONTENT_LENGTH', 'HTTP_USER_AGENT', 'HTTP_COOKIE', 'HTTP_REFERER'): env.setdefault(k, "") - os.environ.update(env) self.send_response(200, "Script output follows") @@ -248,7 +247,7 @@ pass os.dup2(self.rfile.fileno(), 0) os.dup2(self.wfile.fileno(), 1) - os.execve(scriptfile, args, os.environ) + os.execve(scriptfile, args, env) except: self.server.handle_error(self.request, self.client_address) os._exit(127) @@ -274,7 +273,8 @@ p = subprocess.Popen(cmdline, stdin = subprocess.PIPE, stdout = subprocess.PIPE, - stderr = subprocess.PIPE + stderr = subprocess.PIPE, + env = env ) if self.command.lower() == "post" and nbytes > 0: data = self.rfile.read(nbytes) diff -r 8e4903dc83d9 Lib/test/test_httpservers.py --- a/Lib/test/test_httpservers.py Tue Jul 20 19:23:00 2010 +0300 +++ b/Lib/test/test_httpservers.py Wed Jul 21 10:14:05 2010 +0300 @@ -288,6 +288,8 @@ class CGIHTTPServerTestCase(BaseTestCase): class request_handler(NoLogRequestHandler, CGIHTTPRequestHandler): + """this class is used in BaseTestCase.setUp to launch \ + CGIHTTPRequestHandler in a separate thread""" pass def setUp(self): @@ -398,6 +400,14 @@ self.assertEqual(('Hello World\n', 'text/html', 200), (res.read(), res.getheader('Content-type'), res.status)) + def test_os_environ_is_not_altered(self): + # http://bugs.python.org/issue9272 + signature = "Ma Test CGI Server v1.0" + os.environ['SERVER_SOFTWARE'] = signature + res = self.request('cgi-bin/file1.py') + # test works, because os.environ is shared between threads + self.assertEqual(os.environ['SERVER_SOFTWARE'], signature) + def test_main(verbose=None): try: