[Checkins] SVN: zope.publisher/trunk/ Fixed LP #322486: setStatus() now allows any int()-able status value.

Shane Hathaway shane at hathawaymix.org
Fri Jan 30 13:45:20 EST 2009


Log message for revision 95601:
  Fixed LP #322486: setStatus() now allows any int()-able status value.
  
  Also converted a test containing non-ASCII characters to hex encoding.
  

Changed:
  U   zope.publisher/trunk/CHANGES.txt
  U   zope.publisher/trunk/src/zope/publisher/http.py
  U   zope.publisher/trunk/src/zope/publisher/interfaces/http.py
  U   zope.publisher/trunk/src/zope/publisher/tests/test_http.py

-=-
Modified: zope.publisher/trunk/CHANGES.txt
===================================================================
--- zope.publisher/trunk/CHANGES.txt	2009-01-30 18:31:20 UTC (rev 95600)
+++ zope.publisher/trunk/CHANGES.txt	2009-01-30 18:45:20 UTC (rev 95601)
@@ -4,7 +4,7 @@
 3.5.5 (unreleased)
 ------------------
 
-* ...
+* LP #322486: setStatus() now allows any int()-able status value.
 
 
 3.5.4 (2008-09-22)

Modified: zope.publisher/trunk/src/zope/publisher/http.py
===================================================================
--- zope.publisher/trunk/src/zope/publisher/http.py	2009-01-30 18:31:20 UTC (rev 95600)
+++ zope.publisher/trunk/src/zope/publisher/http.py	2009-01-30 18:45:20 UTC (rev 95601)
@@ -647,12 +647,16 @@
         self.authUser = '-'
 
     def setStatus(self, status, reason=None):
-        'See IHTTPResponse'
+        """See IHTTPResponse"""
         if status is None:
             status = 200
-        else:
-            if type(status) in StringTypes:
+        try:
+            status = int(status)
+        except ValueError:
+            if isinstance(status, basestring):
                 status = status.lower()
+            # Use a standard status code, falling back to 500 for
+            # nonstandard values (such as "valueerror")
             status = status_codes.get(status, 500)
         self._status = status
 
@@ -662,7 +666,6 @@
         self._reason = reason
         self._status_set = True
 
-
     def getStatus(self):
         'See IHTTPResponse'
         return self._status

Modified: zope.publisher/trunk/src/zope/publisher/interfaces/http.py
===================================================================
--- zope.publisher/trunk/src/zope/publisher/interfaces/http.py	2009-01-30 18:31:20 UTC (rev 95600)
+++ zope.publisher/trunk/src/zope/publisher/interfaces/http.py	2009-01-30 18:45:20 UTC (rev 95601)
@@ -333,12 +333,17 @@
     def setStatus(status, reason=None):
         """Sets the HTTP status code of the response
 
-        The argument may either be an integer or a string from { OK,
-        Created, Accepted, NoContent, MovedPermanently,
-        MovedTemporarily, NotModified, BadRequest, Unauthorized,
-        Forbidden, NotFound, InternalError, NotImplemented,
-        BadGateway, ServiceUnavailable } that will be converted to the
-        correct integer value.
+        The status parameter must be either an integer, a value
+        that can be converted to an integer using the int() function,
+        or one of the standard status messages listed in the status_codes
+        dict of the zope.publisher.http module (including "OK", "NotFound",
+        and so on).  If the parameter is some other value, the status will
+        be set to 500.
+
+        The reason parameter is a short message to be sent with the status
+        code to the client.  If reason is not provided, a standard
+        reason will be supplied, falling back to "Unknown" for unregistered
+        status codes.
         """
 
     def getStatusString():

Modified: zope.publisher/trunk/src/zope/publisher/tests/test_http.py
===================================================================
--- zope.publisher/trunk/src/zope/publisher/tests/test_http.py	2009-01-30 18:31:20 UTC (rev 95600)
+++ zope.publisher/trunk/src/zope/publisher/tests/test_http.py	2009-01-30 18:45:20 UTC (rev 95601)
@@ -263,6 +263,12 @@
         request.response.redirect('http://foobar.com/explicit', 304)
         self.assertEquals(request.response.getStatus(), 304)
 
+    def testUnregisteredStatus(self):
+        # verify we can set the status to an unregistered int value
+        request = self._createRequest({}, '')
+        request.response.setStatus(289)
+        self.assertEquals(request.response.getStatus(), 289)
+
     def testRequestEnvironment(self):
         req = self._createRequest()
         publish(req, handle_errors=0) # Force expansion of URL variables
@@ -551,9 +557,9 @@
         req = self._createRequest(
             {'PATH_INFO': '/\xc3\xa4\xc3\xb6/\xc3\xbc\xc3\x9f/foo/bar.html'})
         self.assertEqual(req._traversal_stack,
-                         [u'bar.html', u'foo', u'üß', u'äö'])
+                         [u'bar.html', u'foo', u'\xfc\xdf', u'\xe4\xf6'])
         # the request should have converted PATH_INFO to unicode
-        self.assertEqual(req['PATH_INFO'], u'/äö/üß/foo/bar.html')
+        self.assertEqual(req['PATH_INFO'], u'/\xe4\xf6/\xfc\xdf/foo/bar.html')
 
     def testResponseWriteFaile(self):
         self.assertRaises(TypeError,



More information about the Checkins mailing list