示例#1
0
    def test_init__to_unicode__default(self):
        loader = Loader()
        self.assertRaises(TypeError, loader.to_unicode, u"abc")

        decode_errors = defaults.DECODE_ERRORS
        string_encoding = defaults.STRING_ENCODING

        nonascii = 'abcdé'

        try:
            defaults.DECODE_ERRORS = 'strict'
            defaults.STRING_ENCODING = 'ascii'
            loader = Loader()
            self.assertRaises(UnicodeDecodeError, loader.to_unicode, nonascii)

            defaults.DECODE_ERRORS = 'ignore'
            loader = Loader()
            self.assertString(loader.to_unicode(nonascii), u'abcd')

            defaults.STRING_ENCODING = 'utf-8'
            loader = Loader()
            self.assertString(loader.to_unicode(nonascii), u'abcdé')

        finally:
            defaults.DECODE_ERRORS = decode_errors
            defaults.STRING_ENCODING = string_encoding
示例#2
0
    def test_init__to_unicode__default(self):
        loader = Loader()
        self.assertRaises(TypeError, loader.to_unicode, u"abc")

        decode_errors = defaults.DECODE_ERRORS
        string_encoding = defaults.STRING_ENCODING

        nonascii = "abcdé"

        try:
            defaults.DECODE_ERRORS = "strict"
            defaults.STRING_ENCODING = "ascii"
            loader = Loader()
            self.assertRaises(UnicodeDecodeError, loader.to_unicode, nonascii)

            defaults.DECODE_ERRORS = "ignore"
            loader = Loader()
            self.assertString(loader.to_unicode(nonascii), u"abcd")

            defaults.STRING_ENCODING = "utf-8"
            loader = Loader()
            self.assertString(loader.to_unicode(nonascii), u"abcdé")

        finally:
            defaults.DECODE_ERRORS = decode_errors
            defaults.STRING_ENCODING = string_encoding
示例#3
0
    def test_init__to_unicode__default(self):
        loader = Loader()
        self.assertRaises(TypeError, loader.to_unicode, "abc")

        decode_errors = defaults.DECODE_ERRORS
        string_encoding = defaults.STRING_ENCODING

        nonascii = 'abcdé'.encode('utf-8')

        loader = Loader()
        self.assertRaises(UnicodeDecodeError, loader.to_unicode, nonascii)

        defaults.DECODE_ERRORS = 'ignore'
        loader = Loader()
        self.assertString(loader.to_unicode(nonascii), 'abcd')

        defaults.STRING_ENCODING = 'utf-8'
        loader = Loader()
        self.assertString(loader.to_unicode(nonascii), 'abcdé')
示例#4
0
    def test_unicode__to_unicode__attribute(self):
        """
        Test unicode(): encoding attribute.

        """
        loader = Loader()

        non_ascii = 'abcdé'.encode('utf-8')
        self.assertRaises(UnicodeDecodeError, loader.str, non_ascii)

        def to_unicode(s, encoding=None):
            if encoding is None:
                encoding = 'utf-8'
            return str(s, encoding)

        loader.to_unicode = to_unicode
        self.assertString(loader.str(non_ascii), "abcdé")
示例#5
0
    def test_unicode__to_unicode__attribute(self):
        """
        Test unicode(): encoding attribute.

        """
        loader = Loader()

        non_ascii = 'abcdé'.encode('utf-8')
        self.assertRaises(UnicodeDecodeError, loader.str, non_ascii)

        def to_unicode(s, encoding=None):
            if encoding is None:
                encoding = 'utf-8'
            return str(s, encoding)

        loader.to_unicode = to_unicode
        self.assertString(loader.str(non_ascii), "abcdé")
示例#6
0
    def test_unicode__to_unicode__attribute(self):
        """
        Test unicode(): encoding attribute.

        """
        reader = Loader()

        non_ascii = u'abcdé'.encode('utf-8')

        self.assertRaises(UnicodeDecodeError, reader.unicode, non_ascii)

        def to_unicode(s, encoding=None):
            if encoding is None:
                encoding = 'utf-8'
            return unicode(s, encoding)

        reader.to_unicode = to_unicode
        self.assertString(reader.unicode(non_ascii), u"abcdé")