Commits

Brian Kearns  committed 2a1f153

test for string_escape decode errors

  • Participants
  • Parent commits 994b5a2

Comments (0)

Files changed (1)

File pypy/module/_codecs/test/test_codecs.py

     def test_unicodedecodeerror(self):
         assert str(UnicodeDecodeError(
             "ascii", "g\xfcrk", 1, 2, "ouch")) == "'ascii' codec can't decode byte 0xfc in position 1: ouch"
-        
+
         assert str(UnicodeDecodeError(
             "ascii", "g\xfcrk", 1, 3, "ouch")) == "'ascii' codec can't decode bytes in position 1-2: ouch"
-        
 
     def test_unicodetranslateerror(self):
         import sys
         assert str(UnicodeTranslateError(
             u"g\xfcrk", 1, 2, "ouch"))== "can't translate character u'\\xfc' in position 1: ouch"
-        
+
         assert str(UnicodeTranslateError(
             u"g\u0100rk", 1, 2, "ouch"))== "can't translate character u'\\u0100' in position 1: ouch"
-        
+
         assert str(UnicodeTranslateError(
             u"g\uffffrk", 1, 2, "ouch"))== "can't translate character u'\\uffff' in position 1: ouch"
-        
+
         if sys.maxunicode > 0xffff and len(unichr(0x10000)) == 1:
             assert str(UnicodeTranslateError(
                 u"g\U00010000rk", 1, 2, "ouch"))== "can't translate character u'\\U00010000' in position 1: ouch"
-            
+
         assert str(UnicodeTranslateError(
             u"g\xfcrk", 1, 3, "ouch"))=="can't translate characters in position 1-2: ouch"
 
         import sys
         assert str(UnicodeEncodeError(
             "ascii", u"g\xfcrk", 1, 2, "ouch"))=="'ascii' codec can't encode character u'\\xfc' in position 1: ouch"
-            
+
         assert str(UnicodeEncodeError(
             "ascii", u"g\xfcrk", 1, 4, "ouch"))== "'ascii' codec can't encode characters in position 1-3: ouch"
-            
+
         assert str(UnicodeEncodeError(
             "ascii", u"\xfcx", 0, 1, "ouch"))=="'ascii' codec can't encode character u'\\xfc' in position 0: ouch"
 
         assert str(UnicodeEncodeError(
             "ascii", u"\u0100x", 0, 1, "ouch"))=="'ascii' codec can't encode character u'\\u0100' in position 0: ouch"
-       
+
         assert str(UnicodeEncodeError(
             "ascii", u"\uffffx", 0, 1, "ouch"))=="'ascii' codec can't encode character u'\\uffff' in position 0: ouch"
         if sys.maxunicode > 0xffff and len(unichr(0x10000)) == 1:
             assert str(UnicodeEncodeError(
                 "ascii", u"\U00010000x", 0, 1, "ouch")) =="'ascii' codec can't encode character u'\\U00010000' in position 0: ouch"
-    
+
     def test_indexerror(self):
         test =   "\\"     # trailing backslash
         raises (ValueError, test.decode,'string-escape')
                 u"\x00\xff\u07ff\u0800",
                 u"\x00\xff\u07ff\u0800\uffff",
             ]
-            
+
         buffer = ''
         result = u""
         for (c, partialresult) in zip(u"\x00\xff\u07ff\u0800\uffff".encode(encoding), check_partial):
             assert result == partialresult
 
     def test_bug1098990_a(self):
-
         import codecs, StringIO
         self.encoding = 'utf-8'
         s1 = u"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy\r\n"
         s2 = u"offending line: ladfj askldfj klasdj fskla dfzaskdj fasklfj laskd fjasklfzzzzaa%whereisthis!!!\r\n"
         s3 = u"next line.\r\n"
-       
+
         s = (s1+s2+s3).encode(self.encoding)
         stream = StringIO.StringIO(s)
         reader = codecs.getreader(self.encoding)(stream)
         assert reader.readline() == s3
         assert reader.readline() == s4
         assert reader.readline() == s5
-        assert reader.readline() == u""    
-    
+        assert reader.readline() == u""
+
     def test_seek_utf16le(self):
         # all codecs should be able to encode these
         import codecs, StringIO
             line = reader.readline()
             assert s[:len(line)] == line
 
-
     def test_unicode_internal_encode(self):
         import sys
         class U(unicode):
         assert '\\0f'.decode('string_escape') == chr(0) + 'f'
         assert '\\08'.decode('string_escape') == chr(0) + '8'
 
+    def test_escape_decode_errors(self):
+        raises(ValueError, br"\x".decode, 'string_escape')
+        raises(ValueError, br"[\x]".decode, 'string_escape')
+        raises(ValueError, br"\x0".decode, 'string_escape')
+        raises(ValueError, br"[\x0]".decode, 'string_escape')
+
     def test_escape_encode(self):
         assert '"'.encode('string_escape') == '"'
         assert "'".encode('string_escape') == "\\'"