Ticket #6070: ticket_6070_3.diff

File ticket_6070_3.diff, 6.3 KB (added by meissenPlate, 4 years ago)

A different way to generate _allASCIICharacters.

  • twisted/python/compat.py

    diff --git twisted/python/compat.py twisted/python/compat.py
    index 7a56f86..cf4ffdd 100644
    integer. 
    357357"""
    358358
    359359
     360
     361if _PY3:
     362    def chr_(i):
     363        if isinstance(i, int):
     364            if i > 255:
     365                raise ValueError("chr() arg not in range(256)")
     366            return chr(i).encode("latin1")
     367        else:
     368            raise TypeError("an integer is required")
     369else:
     370    # We need to emulate 2.x? THIS. IS. 2.X!
     371    def chr_(i):
     372        return chr(i)
     373
     374chr_.__doc__ = """
     375Duplicate the behavior of 2x chr in 3x.
     376
     377The Python 2.x built-in chr converted integers in [0, 255] to the
     378one-character byte strings containing the character with that ASCII encoded
     379value.
     380
     381The Python 3.x chr instead takes integers in the range [0, 1114111], and
     382converts them to one-character Unicode strings containing the character
     383with that code-point.
     384
     385The chr_ in this function duplicates 2.x behavior on both 2.x and 3.x.
     386
     387See these extracts from the Python manual for details.
     388
     389Python 2.x
     390----------
     391chr:
     392"Return a string of one character whose ASCII code is the integer i. For
     393example, chr(97) returns the string 'a'. This is the inverse of ord(). The
     394argument must be in the range [0..255], inclusive; ValueError will be raised if
     395i is outside that range. See also unichr()."
     396http://docs.python.org/library/functions.html#chr
     397
     398Python 3.x
     399----------
     400chr:
     401"Return the string representing a character whose Unicode codepoint is the
     402integer i. For example, chr(97) returns the string 'a'. This is the inverse of
     403ord(). The valid range for the argument is from 0 through 1,114,111 (0x10FFFF
     404in base 16). ValueError will be raised if i is outside that range."
     405http://docs.python.org/py3k/library/functions.html#chr
     406"""
     407
     408
     409
    360410__all__ = [
    361411    "reraise",
    362412    "execfile",
    __all__ = [ 
    370420    "unicode",
    371421    "iterbytes",
    372422    "intToBytes",
     423    "chr_"
    373424    ]
  • twisted/test/test_compat.py

    diff --git twisted/test/test_compat.py twisted/test/test_compat.py
    index b408153..5ea055f 100644
    from twisted.python.compat import set, frozenset, reduce, execfile, _PY3 
    1616from twisted.python.compat import comparable, cmp, nativeString
    1717from twisted.python.compat import unicode as unicodeCompat
    1818from twisted.python.compat import reraise, NativeStringIO, iterbytes, intToBytes
     19from twisted.python.compat import chr_
    1920from twisted.python.filepath import FilePath
    2021
    2122
    class Python3BytesTests(unittest.SynchronousTestCase): 
    538539        ASCII-encoded string representation of the number.
    539540        """
    540541        self.assertEqual(intToBytes(213), b"213")
     542
     543
     544
     545class chr_Tests(unittest.SynchronousTestCase):
     546    """
     547    Tests for L{compat.chr_}.
     548
     549    L{compat.chr_} should emulate the Python 2.x built-in chr which had
     550    these key characteristics:
     551        -Accepted an integer in [0, 255].
     552        -Returned a bytestring containing the character encoded to that
     553         integer under ASCII.
     554        -Raised a ValueError in response to any integer outside that range.
     555        -Raised a TypeError in response to any other input.
     556    """
     557    # There are only 256 ASCII-representable characters, so let's just test
     558    # them all.
     559    _ASCIIEncodedValues = list(range(256))
     560    if _PY3:
     561        _allASCIICharacters = [chr(i).encode("latin1") for i in range(256)]
     562    else:
     563        _allASCIICharacters = [chr(i) for i in range(256)]
     564    # And here is a sampling of things that chr_ should NOT accept.
     565    _negativeIntegers = [-10, -1]
     566    _integersOver255 = [256, 257, 500, 1000, 10000, 60000, 65534, 65535]
     567    _integersOver65535 = [65536, 65537, 100000]
     568    _bytestringASCIICharacters = [b"a", b"\x15"]
     569    _UnicodeCharacters = [u"b", u"\xeff", u"\xfffe", u"\xffff"]
     570    _UnicodeCharactersOver65535 = [u"\x10000", u"\x10001"]
     571    _nonIntegersNotCharacters = [2.3, dict()]
     572
     573    def test_chr_Accepts(self):
     574        """
     575        L{compat.chr_} accepts integers in [0, 255] and returns the
     576        corresponding character in an ASCII-encoded bytestring.
     577        """
     578        validIntegers = self._ASCIIEncodedValues
     579        for i in validIntegers:
     580            # Deliberately leak any exceptions; failing the test with ERROR.
     581            ret = chr_(i)
     582            self.assertIsInstance(ret, bytes)
     583            self.assertEqual(ret, self._allASCIICharacters[i])
     584
     585    def _assert_chr_Rejects(self, invalidInput, exceptionToRejectWith):
     586        """Convenience function. map assertRaises across invalidInput."""
     587        for x in invalidInput:
     588            self.assertRaises(exceptionToRejectWith, chr_, x)
     589
     590    def test_chr_RejectsNegativeIntegers(self):
     591        """
     592        L{compat.chr_} rejects negative integers with ValueError.
     593        """
     594        self._assert_chr_Rejects(self._negativeIntegers, ValueError)
     595
     596    def test_chr_RejectsIntegersOver255(self):
     597        """
     598        L{compat.chr_} rejects integers over 255 with ValueError.
     599        """
     600        self._assert_chr_Rejects(self._integersOver255, ValueError)
     601
     602    def test_chr_RejectsIntegersOver65535(self):
     603        """
     604        L{compat.chr_} rejects integers over 65535 with ValueError.
     605        """
     606        self._assert_chr_Rejects(self._integersOver65535, ValueError)
     607
     608    def test_chr_RejectsBytestringASCIICharacters(self):
     609        """
     610        L{compat.chr_} rejects ASCII-encoded bytestrings with TypeError.
     611        """
     612        self._assert_chr_Rejects(self._bytestringASCIICharacters, TypeError)
     613
     614    def test_chr_RejectsUnicodeCharacters(self):
     615        """
     616        L{compat.chr_} rejects Unicode strings with TypeError.
     617        """
     618        self._assert_chr_Rejects(self._UnicodeCharacters, TypeError)
     619
     620    def test_chr_RejectsUnicodeCharactersOver65535(self):
     621        """
     622        L{compat.chr_} rejects Unicode strings with codepoints over 65535 with
     623        TypeError.
     624        """
     625        self._assert_chr_Rejects(self._UnicodeCharactersOver65535, TypeError)
     626
     627    def test_chr_RejectsNonIntegersNonCharacters(self):
     628        """
     629        L{compat.chr_} rejects any other odd thing you give it, like a dict,
     630        with TypeError.
     631        """
     632        self._assert_chr_Rejects(self._nonIntegersNotCharacters, TypeError)