commit 7704bf71c774b4efcdbe39484aca44775e4de184
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Fri Aug 3 15:07:19 2012 +0200
[py3] Removed uses of sys.maxint under Python 3.
Fixed #18706 too: improved exceptions raised by
django.utils.http.int_to_base36.
diff --git a/django/utils/http.py b/django/utils/http.py
index f3a3dce..272e73f 100644
a
|
b
|
def base36_to_int(s):
|
167 | 167 | if len(s) > 13: |
168 | 168 | raise ValueError("Base36 input too large") |
169 | 169 | value = int(s, 36) |
170 | | # ... then do a final check that the value will fit into an int. |
171 | | if value > sys.maxint: |
| 170 | # ... then do a final check that the value will fit into an int to avoid |
| 171 | # returning a long (#15067). The long type was removed in Python 3. |
| 172 | if not six.PY3 and value > sys.maxint: |
172 | 173 | raise ValueError("Base36 input too large") |
173 | 174 | return value |
174 | 175 | |
… |
… |
def int_to_base36(i):
|
178 | 179 | """ |
179 | 180 | digits = "0123456789abcdefghijklmnopqrstuvwxyz" |
180 | 181 | factor = 0 |
181 | | if not 0 <= i <= sys.maxint: |
182 | | raise ValueError("Base36 conversion input too large or incorrect type.") |
| 182 | if i < 0: |
| 183 | raise ValueError("Negative base36 conversion input.") |
| 184 | if not six.PY3: |
| 185 | if not isinstance(i, six.integer_types): |
| 186 | raise TypeError("Non-integer base36 conversion input.") |
| 187 | if i > sys.maxint: |
| 188 | raise ValueError("Base36 conversion input too large.") |
183 | 189 | # Find starting factor |
184 | 190 | while True: |
185 | 191 | factor += 1 |
diff --git a/docs/ref/utils.txt b/docs/ref/utils.txt
index c2f2025..5157c39 100644
a
|
b
|
escaping HTML.
|
504 | 504 | |
505 | 505 | .. function:: base36_to_int(s) |
506 | 506 | |
507 | | Converts a base 36 string to an integer. |
| 507 | Converts a base 36 string to an integer. On Python 2 the output is |
| 508 | guaranteed to be an :class:`int` and not a :class:`long`. |
508 | 509 | |
509 | 510 | .. function:: int_to_base36(i) |
510 | 511 | |
511 | | Converts a positive integer less than sys.maxint to a base 36 string. |
| 512 | Converts a positive integer to a base 36 string. On Python 2 ``i`` must be |
| 513 | smaller than :attr:`sys.maxint`. |
512 | 514 | |
513 | 515 | ``django.utils.safestring`` |
514 | 516 | =========================== |
diff --git a/tests/regressiontests/utils/http.py b/tests/regressiontests/utils/http.py
index 67dcd7a..f22e054 100644
a
|
b
|
|
1 | 1 | import sys |
2 | 2 | |
3 | | from django.utils import http |
4 | | from django.utils import unittest |
5 | | from django.utils.datastructures import MultiValueDict |
6 | 3 | from django.http import HttpResponse, utils |
7 | 4 | from django.test import RequestFactory |
| 5 | from django.utils.datastructures import MultiValueDict |
| 6 | from django.utils import http |
| 7 | from django.utils import six |
| 8 | from django.utils import unittest |
8 | 9 | |
9 | 10 | class TestUtilsHttp(unittest.TestCase): |
10 | 11 | |
… |
… |
class TestUtilsHttp(unittest.TestCase):
|
110 | 111 | |
111 | 112 | def test_base36(self): |
112 | 113 | # reciprocity works |
113 | | for n in [0, 1, 1000, 1000000, sys.maxint]: |
| 114 | for n in [0, 1, 1000, 1000000]: |
114 | 115 | self.assertEqual(n, http.base36_to_int(http.int_to_base36(n))) |
| 116 | if not six.PY3: |
| 117 | self.assertEqual(sys.maxint, http.base36_to_int(http.int_to_base36(sys.maxint))) |
115 | 118 | |
116 | 119 | # bad input |
117 | | for n in [-1, sys.maxint+1, '1', 'foo', {1:2}, (1,2,3)]: |
118 | | self.assertRaises(ValueError, http.int_to_base36, n) |
119 | | |
| 120 | self.assertRaises(ValueError, http.int_to_base36, -1) |
| 121 | if not six.PY3: |
| 122 | self.assertRaises(ValueError, http.int_to_base36, sys.maxint + 1) |
| 123 | for n in ['1', 'foo', {1: 2}, (1, 2, 3), 3.141]: |
| 124 | self.assertRaises(TypeError, http.int_to_base36, n) |
| 125 | |
120 | 126 | for n in ['#', ' ']: |
121 | 127 | self.assertRaises(ValueError, http.base36_to_int, n) |
122 | | |
123 | | for n in [123, {1:2}, (1,2,3)]: |
| 128 | for n in [123, {1: 2}, (1, 2, 3), 3.141]: |
124 | 129 | self.assertRaises(TypeError, http.base36_to_int, n) |
125 | 130 | |
126 | | # non-integer input |
127 | | self.assertRaises(TypeError, http.int_to_base36, 3.141) |
128 | | |
129 | 131 | # more explicit output testing |
130 | 132 | for n, b36 in [(0, '0'), (1, '1'), (42, '16'), (818469960, 'django')]: |
131 | 133 | self.assertEqual(http.int_to_base36(n), b36) |