Ticket #18706: 18706.patch

File 18706.patch, 4.2 KB (added by Aymeric Augustin, 12 years ago)
  • django/utils/http.py

    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):  
    167167    if len(s) > 13:
    168168        raise ValueError("Base36 input too large")
    169169    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:
    172173        raise ValueError("Base36 input too large")
    173174    return value
    174175
    def int_to_base36(i):  
    178179    """
    179180    digits = "0123456789abcdefghijklmnopqrstuvwxyz"
    180181    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.")
    183189    # Find starting factor
    184190    while True:
    185191        factor += 1
  • docs/ref/utils.txt

    diff --git a/docs/ref/utils.txt b/docs/ref/utils.txt
    index c2f2025..5157c39 100644
    a b escaping HTML.  
    504504
    505505.. function:: base36_to_int(s)
    506506
    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`.
    508509
    509510.. function:: int_to_base36(i)
    510511
    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`.
    512514
    513515``django.utils.safestring``
    514516===========================
  • tests/regressiontests/utils/http.py

    diff --git a/tests/regressiontests/utils/http.py b/tests/regressiontests/utils/http.py
    index 67dcd7a..f22e054 100644
    a b  
    11import sys
    22
    3 from django.utils import http
    4 from django.utils import unittest
    5 from django.utils.datastructures import MultiValueDict
    63from django.http import HttpResponse, utils
    74from django.test import RequestFactory
     5from django.utils.datastructures import MultiValueDict
     6from django.utils import http
     7from django.utils import six
     8from django.utils import unittest
    89
    910class TestUtilsHttp(unittest.TestCase):
    1011
    class TestUtilsHttp(unittest.TestCase):  
    110111
    111112    def test_base36(self):
    112113        # reciprocity works
    113         for n in [0, 1, 1000, 1000000, sys.maxint]:
     114        for n in [0, 1, 1000, 1000000]:
    114115            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)))
    115118
    116119        # 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
    120126        for n in ['#', ' ']:
    121127            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]:
    124129            self.assertRaises(TypeError, http.base36_to_int, n)
    125130
    126         # non-integer input
    127         self.assertRaises(TypeError, http.int_to_base36, 3.141)
    128        
    129131        # more explicit output testing
    130132        for n, b36 in [(0, '0'), (1, '1'), (42, '16'), (818469960, 'django')]:
    131133            self.assertEqual(http.int_to_base36(n), b36)
Back to Top