durusmail: quixote-users: Patch to protect against future changes to integer division semantics
Patch to protect against future changes to integer division semantics
2004-04-18
Patch to protect against future changes to integer division semantics
2004-04-18
2004-04-18
Patch to protect against future changes to integer division semantics
2004-04-18
Patch to protect against future changes to integer division semantics
rmunn@pobox.com
2004-04-18
I noticed that http_response.py uses integer division in the set_status
method to get the base status (e.g., 400) of an unknown status code
(e.g., 493):

    reason = status_reasons[(status / 100) * 100]

Once integer division semantics change in Python 3.0, this is going to
start failing. Here's patch to guard against this change while still
remaining compatible with versions of Python all the way back to 2.0.

I hope this helps.


--- http_response.py.old        2004-04-18 07:17:13.260325634 -0500
+++ http_response.py    2004-04-18 07:19:25.591863968 -0500
@@ -26,6 +26,12 @@
 import time
 from rfc822 import formatdate
 from types import StringType, IntType
+try:
+    from __future__ import division
+except SyntaxError:
+    _new_division = 0
+else:
+    _new_division = 1

 status_reasons = {
     100: 'Continue',
@@ -158,7 +164,10 @@
             else:
                 # Eg. for generic 4xx failures, use the reason
                 # associated with status 400.
-                reason = status_reasons[(status / 100) * 100]
+                if _new_division:
+                    reason = status_reasons[(status // 100) * 100]
+                else:
+                    reason = status_reasons[(status / 100) * 100]
         else:
             reason = str(reason)


--
Robin Munn
rmunn@pobox.com


reply