Python codecs.unicode_escape_decode() Examples

The following are 30 code examples of codecs.unicode_escape_decode(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module codecs , or try the search function .
Example #1
Source File: test_codecs.py    From ironpython3 with Apache License 2.0 6 votes vote down vote up
def test_unicode_escape_decode_errors_replace(self):
        test_data = [
            (b"abc\\xyz", "abc�yz"),
            (b"abc\\x0xyz", "abc�xyz"),
            (b"abc\\u20klm\xffxyz", "abc�klm\xffxyz"),
            (b"abc\\U0001F44xyz", "abc�xyz"),
            (b"abc\\U00110011xyz", "abc�xyz"),
            (b"abc\\N{EURO}xyz", "abc�xyz"),
            (b"abc\\Nxyz", "abc�xyz"),
            (b"abc\\N", "abc�"),
            (b"abc\\N{xyz", "abc�"),
            (b"abc\\N{", "abc�"),
            (b"abc\\N{}xyz", "abc�}xyz"),
            (b"abc\\N{}", "abc�}"),
            (b"abc\\", "abc�"),
        ]

        for sample in test_data:
            self.assertEqual(codecs.unicode_escape_decode(sample[0], 'replace')[0], sample[1]) 
Example #2
Source File: test_codecs.py    From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 6 votes vote down vote up
def test_unicode_escape(self):
        # Escape-decoding a unicode string is supported and gives the same
        # result as decoding the equivalent ASCII bytes string.
        self.assertEqual(codecs.unicode_escape_decode(r"\u1234"), ("\u1234", 6))
        self.assertEqual(codecs.unicode_escape_decode(br"\u1234"), ("\u1234", 6))
        self.assertEqual(codecs.raw_unicode_escape_decode(r"\u1234"), ("\u1234", 6))
        self.assertEqual(codecs.raw_unicode_escape_decode(br"\u1234"), ("\u1234", 6))

        self.assertRaises(UnicodeDecodeError, codecs.unicode_escape_decode, br"\U00110000")
        self.assertEqual(codecs.unicode_escape_decode(r"\U00110000", "replace"), ("\ufffd", 10))
        self.assertEqual(codecs.unicode_escape_decode(r"\U00110000", "backslashreplace"),
                         (r"\x5c\x55\x30\x30\x31\x31\x30\x30\x30\x30", 10))

        self.assertRaises(UnicodeDecodeError, codecs.raw_unicode_escape_decode, br"\U00110000")
        self.assertEqual(codecs.raw_unicode_escape_decode(r"\U00110000", "replace"), ("\ufffd", 10))
        self.assertEqual(codecs.raw_unicode_escape_decode(r"\U00110000", "backslashreplace"),
                         (r"\x5c\x55\x30\x30\x31\x31\x30\x30\x30\x30", 10)) 
Example #3
Source File: test_codecs.py    From Fluid-Designer with GNU General Public License v3.0 6 votes vote down vote up
def test_unicode_escape(self):
        # Escape-decoding an unicode string is supported ang gives the same
        # result as decoding the equivalent ASCII bytes string.
        self.assertEqual(codecs.unicode_escape_decode(r"\u1234"), ("\u1234", 6))
        self.assertEqual(codecs.unicode_escape_decode(br"\u1234"), ("\u1234", 6))
        self.assertEqual(codecs.raw_unicode_escape_decode(r"\u1234"), ("\u1234", 6))
        self.assertEqual(codecs.raw_unicode_escape_decode(br"\u1234"), ("\u1234", 6))

        self.assertRaises(UnicodeDecodeError, codecs.unicode_escape_decode, br"\U00110000")
        self.assertEqual(codecs.unicode_escape_decode(r"\U00110000", "replace"), ("\ufffd", 10))
        self.assertEqual(codecs.unicode_escape_decode(r"\U00110000", "backslashreplace"),
                         (r"\x5c\x55\x30\x30\x31\x31\x30\x30\x30\x30", 10))

        self.assertRaises(UnicodeDecodeError, codecs.raw_unicode_escape_decode, br"\U00110000")
        self.assertEqual(codecs.raw_unicode_escape_decode(r"\U00110000", "replace"), ("\ufffd", 10))
        self.assertEqual(codecs.raw_unicode_escape_decode(r"\U00110000", "backslashreplace"),
                         (r"\x5c\x55\x30\x30\x31\x31\x30\x30\x30\x30", 10)) 
Example #4
Source File: test_codecs.py    From ironpython3 with Apache License 2.0 6 votes vote down vote up
def test_unicode_escape_decode_errors_ignore(self):
        test_data = [
            (b"abc\\xyz", "abcyz"),
            (b"abc\\x0xyz", "abcxyz"),
            (b"abc\\u20klm\xffxyz", "abcklm\xffxyz"),
            (b"abc\\U0001F44xyz", "abcxyz"),
            (b"abc\\U00110011xyz", "abcxyz"),
            (b"abc\\N{EURO}xyz", "abcxyz"),
            (b"abc\\Nxyz", "abcxyz"),
            (b"abc\\N", "abc"),
            (b"abc\\N{xyz", "abc"),
            (b"abc\\N{", "abc"),
            (b"abc\\N{}xyz", "abc}xyz"),
            (b"abc\\N{}", "abc}"),
            (b"abc\\", "abc"),
        ]

        for sample in test_data:
            self.assertEqual(codecs.unicode_escape_decode(sample[0], 'ignore')[0], sample[1]) 
Example #5
Source File: unicode_escape.py    From PhonePi_SampleServer with MIT License 5 votes vote down vote up
def decode(self, input, final=False):
        return codecs.unicode_escape_decode(input, self.errors)[0] 
Example #6
Source File: addons_xml_generator.py    From Mafarricos-kodi-demo-repo with GNU General Public License v2.0 5 votes vote down vote up
def u(x):
        return codecs.unicode_escape_decode(x)[0] 
Example #7
Source File: addons_xml_generator.py    From xbmc-addons-chinese with GNU General Public License v2.0 5 votes vote down vote up
def u(x):
        return codecs.unicode_escape_decode(x)[0] 
Example #8
Source File: helpers.py    From mamonsu with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def u(x):
        return codecs.unicode_escape_decode(x)[0] 
Example #9
Source File: test_codecs.py    From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 5 votes vote down vote up
def test_empty(self):
        self.assertEqual(codecs.unicode_escape_encode(""), (b"", 0))
        self.assertEqual(codecs.unicode_escape_decode(b""), ("", 0)) 
Example #10
Source File: unicode_escape.py    From Splunking-Crime with GNU Affero General Public License v3.0 5 votes vote down vote up
def decode(self, input, final=False):
        return codecs.unicode_escape_decode(input, self.errors)[0] 
Example #11
Source File: util.py    From dodotable with MIT License 5 votes vote down vote up
def to_str(x):
        if isinstance(x, text_type):
            return x
        if isinstance(x, numbers.Number):
            x = str(x)
        elif x is None:
            x = ''
        return codecs.unicode_escape_decode(x)[0] 
Example #12
Source File: py3compat.py    From P4VFX with MIT License 5 votes vote down vote up
def u(obj):
        """Make unicode object"""
        return codecs.unicode_escape_decode(obj)[0] 
Example #13
Source File: unicode_escape.py    From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 5 votes vote down vote up
def decode(self, input, final=False):
        return codecs.unicode_escape_decode(input, self.errors)[0] 
Example #14
Source File: test_codecs.py    From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 5 votes vote down vote up
def test_escape_decode(self):
        decode = codecs.unicode_escape_decode
        check = coding_checker(self, decode)
        check(b"[\\\n]", "[]")
        check(br'[\"]', '["]')
        check(br"[\']", "[']")
        check(br"[\\]", r"[\]")
        check(br"[\a]", "[\x07]")
        check(br"[\b]", "[\x08]")
        check(br"[\t]", "[\x09]")
        check(br"[\n]", "[\x0a]")
        check(br"[\v]", "[\x0b]")
        check(br"[\f]", "[\x0c]")
        check(br"[\r]", "[\x0d]")
        check(br"[\7]", "[\x07]")
        check(br"[\8]", r"[\8]")
        check(br"[\78]", "[\x078]")
        check(br"[\41]", "[!]")
        check(br"[\418]", "[!8]")
        check(br"[\101]", "[A]")
        check(br"[\1010]", "[A0]")
        check(br"[\x41]", "[A]")
        check(br"[\x410]", "[A0]")
        check(br"\u20ac", "\u20ac")
        check(br"\U0001d120", "\U0001d120")
        for b in range(256):
            if b not in b'\n"\'\\abtnvfr01234567xuUN':
                check(b'\\' + bytes([b]), '\\' + chr(b)) 
Example #15
Source File: UIconst.py    From artisan with GNU General Public License v3.0 5 votes vote down vote up
def u(x):
        return codecs.unicode_escape_decode(x)[0] 
Example #16
Source File: unicode_escape.py    From medicare-demo with Apache License 2.0 5 votes vote down vote up
def decode(self, input, final=False):
        return codecs.unicode_escape_decode(input, self.errors)[0] 
Example #17
Source File: Update_repo.py    From DepotQuebec with GNU General Public License v3.0 5 votes vote down vote up
def u(x):
        return codecs.unicode_escape_decode(x)[0] 
Example #18
Source File: addons_xml_generator.py    From repository.adulthideout with GNU General Public License v2.0 5 votes vote down vote up
def u(x):
        return codecs.unicode_escape_decode(x)[0] 
Example #19
Source File: unicode_escape.py    From python with Apache License 2.0 5 votes vote down vote up
def decode(self, input, final=False):
        return codecs.unicode_escape_decode(input, self.errors)[0] 
Example #20
Source File: unicode_escape.py    From ImageFusion with MIT License 5 votes vote down vote up
def decode(self, input, final=False):
        return codecs.unicode_escape_decode(input, self.errors)[0] 
Example #21
Source File: py3compat.py    From P4VFX with MIT License 5 votes vote down vote up
def u(obj):
        """Make unicode object"""
        return codecs.unicode_escape_decode(obj)[0] 
Example #22
Source File: __init__.py    From cadquery-freecad-module with GNU Lesser General Public License v3.0 5 votes vote down vote up
def u(x):
        return codecs.unicode_escape_decode(x)[0] 
Example #23
Source File: test_codecs.py    From ironpython3 with Apache License 2.0 5 votes vote down vote up
def test_unicode_escape_decode_errors(self):
        def check(data, msg, start, end, ex_data):
            with self.assertRaises(UnicodeDecodeError) as cm:
                codecs.unicode_escape_decode(data)

            self.assertEqual(cm.exception.encoding, 'unicodeescape')
            self.assertEqual(cm.exception.reason, msg)
            self.assertEqual(cm.exception.start, start)
            self.assertEqual(cm.exception.end, end)
            self.assertEqual(cm.exception.object, ex_data)

        test_data = [
            ("abc\\xyz", "truncated \\xXX escape", 3, 5, b"abc\\xyz"), # str to bytes
            ("abc\\x0xyz", "truncated \\xXX escape", 3, 6, b"abc\\x0xyz"),
            ("abc\\u20klm\xffxyz\u20ac", "truncated \\uXXXX escape", 3, 7, b"abc\\u20klm\xc3\xbfxyz\xe2\x82\xac"), # Unicode to UTF-8
            ("abc\\U0001F44xyz", "truncated \\UXXXXXXXX escape", 3, 12, b"abc\\U0001F44xyz"),
            ("abc\\U00110011xyz", "illegal Unicode character", 3, 13, b"abc\\U00110011xyz"),
            ("abc\\N{EURO}xyz", "unknown Unicode character name", 3, 11, b"abc\\N{EURO}xyz"),
            ("abc\\Nxyz", "malformed \\N character escape", 3, 5, b"abc\\Nxyz"),
            ("abc\\N", "malformed \\N character escape", 3, 5, b"abc\\N"),
            ("abc\\N{xyz", "malformed \\N character escape", 3, 9, b"abc\\N{xyz"),
            ("abc\\N{", "malformed \\N character escape", 3, 6, b"abc\\N{"),
            ("abc\\N{}xyz", "malformed \\N character escape", 3, 6, b"abc\\N{}xyz"),
            ("abc\\N{}", "malformed \\N character escape", 3, 6, b"abc\\N{}"),
            ("abc\\", "\\ at end of string", 3, 4, b"abc\\"),
        ]

        for params in test_data:
            check(*params)

        self.assertRaises(TypeError, codecs.unicode_escape_decode, None)
        self.assertRaises(TypeError, codecs.unicode_escape_decode, None, None)
        self.assertEqual(codecs.unicode_escape_decode(b"", None), ("", 0))
        self.assertRaises(UnicodeDecodeError, codecs.unicode_escape_decode, b"\\u", None) 
Example #24
Source File: test_codecs.py    From ironpython3 with Apache License 2.0 5 votes vote down vote up
def test_escape_decode(self):
        decode = codecs.unicode_escape_decode
        check = coding_checker(self, decode)
        check(b"[\\\n]", "[]")
        check(br'[\"]', '["]')
        check(br"[\']", "[']")
        check(br"[\\]", r"[\]")
        check(br"[\a]", "[\x07]")
        check(br"[\b]", "[\x08]")
        check(br"[\t]", "[\x09]")
        check(br"[\n]", "[\x0a]")
        check(br"[\v]", "[\x0b]")
        check(br"[\f]", "[\x0c]")
        check(br"[\r]", "[\x0d]")
        check(br"[\7]", "[\x07]")
        check(br"[\8]", r"[\8]")
        check(br"[\78]", "[\x078]")
        check(br"[\41]", "[!]")
        check(br"[\418]", "[!8]")
        check(br"[\101]", "[A]")
        check(br"[\1010]", "[A0]")
        check(br"[\x41]", "[A]")
        check(br"[\x410]", "[A0]")
        check(br"\u20ac", "\u20ac")
        check(br"\U0001d120", "\U0001d120")
        for b in range(256):
            if b not in b'\n"\'\\abtnvfr01234567xuUN':
                check(b'\\' + bytes([b]), '\\' + chr(b)) 
Example #25
Source File: test_codecs.py    From ironpython3 with Apache License 2.0 5 votes vote down vote up
def test_raw_decode(self):
        decode = codecs.unicode_escape_decode
        for b in range(256):
            if b != b'\\'[0]:
                self.assertEqual(decode(bytes([b]) + b'0'), (chr(b) + '0', 2)) 
Example #26
Source File: test_codecs.py    From ironpython3 with Apache License 2.0 5 votes vote down vote up
def test_empty(self):
        self.assertEqual(codecs.unicode_escape_encode(""), (b"", 0))
        self.assertEqual(codecs.unicode_escape_decode(b""), ("", 0)) 
Example #27
Source File: test_codecs.py    From ironpython3 with Apache License 2.0 5 votes vote down vote up
def test_unicode_escape(self):
        # Escape-decoding an unicode string is supported ang gives the same
        # result as decoding the equivalent ASCII bytes string.
        self.assertEqual(codecs.unicode_escape_decode(r"\u1234"), ("\u1234", 6))
        self.assertEqual(codecs.unicode_escape_decode(br"\u1234"), ("\u1234", 6))
        self.assertEqual(codecs.raw_unicode_escape_decode(r"\u1234"), ("\u1234", 6))
        self.assertEqual(codecs.raw_unicode_escape_decode(br"\u1234"), ("\u1234", 6))

        self.assertRaises(UnicodeDecodeError, codecs.unicode_escape_decode, br"\U00110000")
        self.assertEqual(codecs.unicode_escape_decode(r"\U00110000", "replace"), ("\ufffd", 10))

        self.assertRaises(UnicodeDecodeError, codecs.raw_unicode_escape_decode, br"\U00110000")
        self.assertEqual(codecs.raw_unicode_escape_decode(r"\U00110000", "replace"), ("\ufffd", 10)) 
Example #28
Source File: unicode_escape.py    From ironpython3 with Apache License 2.0 5 votes vote down vote up
def decode(self, input, final=False):
        return codecs.unicode_escape_decode(input, self.errors)[0] 
Example #29
Source File: py3compat.py    From winpython with MIT License 5 votes vote down vote up
def u(obj):
        """Make unicode object"""
        return codecs.unicode_escape_decode(obj)[0] 
Example #30
Source File: py3compat.py    From winpython with MIT License 5 votes vote down vote up
def u(obj):
        """Make unicode object"""
        return codecs.unicode_escape_decode(obj)[0]