Commits

anatoly techtonik committed 9f4f5bb

add test, remove deepcopy

  • Participants
  • Parent commits 00ef523

Comments (0)

Files changed (1)

File 9272.CGIHTTPServer-poisons-os.environ.patch

 # HG changeset patch
-# Parent 131b45a57b05c71965b164752baf8a4a810a56cd
+# Parent 8e4903dc83d99c67a0e24bafde10e84cc58d0ec7
 # User anatoly techtonik <techtonik@gmail.com>
 do not allow CGIHTTPServer to meddle with parent os.environ
 
-diff -r 131b45a57b05 Lib/CGIHTTPServer.py
---- a/Lib/CGIHTTPServer.py	Thu Jul 15 20:48:37 2010 +0300
-+++ b/Lib/CGIHTTPServer.py	Fri Jul 16 11:51:47 2010 +0300
-@@ -29,6 +29,7 @@
- import BaseHTTPServer
- import SimpleHTTPServer
- import select
-+import copy
- 
- 
- class CGIHTTPRequestHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
-@@ -154,7 +155,7 @@
+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 = copy.deepcopy(os.environ)
++        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 +217,6 @@
+@@ -216,7 +216,6 @@
          for k in ('QUERY_STRING', 'REMOTE_HOST', 'CONTENT_LENGTH',
                    'HTTP_USER_AGENT', 'HTTP_COOKIE', 'HTTP_REFERER'):
              env.setdefault(k, "")
  
          self.send_response(200, "Script output follows")
  
-@@ -248,7 +248,7 @@
+@@ -248,7 +247,7 @@
                      pass
                  os.dup2(self.rfile.fileno(), 0)
                  os.dup2(self.wfile.fileno(), 1)
              except:
                  self.server.handle_error(self.request, self.client_address)
                  os._exit(127)
-@@ -274,7 +274,8 @@
+@@ -274,7 +273,8 @@
              p = subprocess.Popen(cmdline,
                                   stdin = subprocess.PIPE,
                                   stdout = subprocess.PIPE,
                                  )
              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: