Exemplo n.º 1
0
    def test_basics(self):
        s = u"abc123"  # all codecs should be able to encode these
        for encoding in all_unicode_encodings:
            name = codecs.lookup(encoding).name
            if encoding.endswith("_codec"):
                name += "_codec"
            elif encoding == "latin_1":
                name = "latin_1"
            self.assertEqual(encoding.replace("_", "-"),
                             name.replace("_", "-"))
            (bytes, size) = codecs.getencoder(encoding)(s)
            if encoding != "unicode_internal":
                self.assertEqual(
                    size, len(s),
                    "%r != %r (encoding=%r)" % (size, len(s), encoding))
            (chars, size) = codecs.getdecoder(encoding)(bytes)
            self.assertEqual(chars, s,
                             "%r != %r (encoding=%r)" % (chars, s, encoding))

            if encoding not in broken_unicode_with_streams:
                # check stream reader/writer
                q = Queue()
                writer = codecs.getwriter(encoding)(q)
                encodedresult = ""
                for c in s:
                    writer.write(c)
                    encodedresult += q.read()
                q = Queue()
                reader = codecs.getreader(encoding)(q)
                decodedresult = u""
                for c in encodedresult:
                    q.write(c)
                    decodedresult += reader.read()
                self.assertEqual(
                    decodedresult, s,
                    "%r != %r (encoding=%r)" % (decodedresult, s, encoding))

                # check incremental decoder/encoder (fetched via the Python
                # and C API) and iterencode()/iterdecode()
                try:
                    encoder = codecs.getincrementalencoder(encoding)()
                    cencoder = _testcapi.codec_incrementalencoder(encoding)
                except LookupError:  # no IncrementalEncoder
                    pass
                else:
                    # check incremental decoder/encoder
                    encodedresult = ""
                    for c in s:
                        encodedresult += encoder.encode(c)
                    encodedresult += encoder.encode(u"", True)
                    decoder = codecs.getincrementaldecoder(encoding)()
                    decodedresult = u""
                    for c in encodedresult:
                        decodedresult += decoder.decode(c)
                    decodedresult += decoder.decode("", True)
                    self.assertEqual(
                        decodedresult, s, "%r != %r (encoding=%r)" %
                        (decodedresult, s, encoding))

                    # check C API
                    encodedresult = ""
                    for c in s:
                        encodedresult += cencoder.encode(c)
                    encodedresult += cencoder.encode(u"", True)
                    cdecoder = _testcapi.codec_incrementaldecoder(encoding)
                    decodedresult = u""
                    for c in encodedresult:
                        decodedresult += cdecoder.decode(c)
                    decodedresult += cdecoder.decode("", True)
                    self.assertEqual(
                        decodedresult, s, "%r != %r (encoding=%r)" %
                        (decodedresult, s, encoding))

                    # check iterencode()/iterdecode()
                    result = u"".join(
                        codecs.iterdecode(codecs.iterencode(s, encoding),
                                          encoding))
                    self.assertEqual(
                        result, s,
                        "%r != %r (encoding=%r)" % (result, s, encoding))

                    # check iterencode()/iterdecode() with empty string
                    result = u"".join(
                        codecs.iterdecode(codecs.iterencode(u"", encoding),
                                          encoding))
                    self.assertEqual(result, u"")
Exemplo n.º 2
0
    def test_basics(self):
        s = u"abc123" # all codecs should be able to encode these
        for encoding in all_unicode_encodings:
            name = codecs.lookup(encoding).name
            if encoding.endswith("_codec"):
                name += "_codec"
            elif encoding == "latin_1":
                name = "latin_1"
            self.assertEqual(encoding.replace("_", "-"), name.replace("_", "-"))
            (bytes, size) = codecs.getencoder(encoding)(s)
            if encoding != "unicode_internal":
                self.assertEqual(size, len(s), "%r != %r (encoding=%r)" % (size, len(s), encoding))
            (chars, size) = codecs.getdecoder(encoding)(bytes)
            self.assertEqual(chars, s, "%r != %r (encoding=%r)" % (chars, s, encoding))

            if encoding not in broken_unicode_with_streams:
                # check stream reader/writer
                q = Queue()
                writer = codecs.getwriter(encoding)(q)
                encodedresult = ""
                for c in s:
                    writer.write(c)
                    encodedresult += q.read()
                q = Queue()
                reader = codecs.getreader(encoding)(q)
                decodedresult = u""
                for c in encodedresult:
                    q.write(c)
                    decodedresult += reader.read()
                self.assertEqual(decodedresult, s, "%r != %r (encoding=%r)" % (decodedresult, s, encoding))

                # check incremental decoder/encoder (fetched via the Python
                # and C API) and iterencode()/iterdecode()
                try:
                    encoder = codecs.getincrementalencoder(encoding)()
                    cencoder = _testcapi.codec_incrementalencoder(encoding)
                except LookupError: # no IncrementalEncoder
                    pass
                else:
                    # check incremental decoder/encoder
                    encodedresult = ""
                    for c in s:
                        encodedresult += encoder.encode(c)
                    encodedresult += encoder.encode(u"", True)
                    decoder = codecs.getincrementaldecoder(encoding)()
                    decodedresult = u""
                    for c in encodedresult:
                        decodedresult += decoder.decode(c)
                    decodedresult += decoder.decode("", True)
                    self.assertEqual(decodedresult, s, "%r != %r (encoding=%r)" % (decodedresult, s, encoding))

                    # check C API
                    encodedresult = ""
                    for c in s:
                        encodedresult += cencoder.encode(c)
                    encodedresult += cencoder.encode(u"", True)
                    cdecoder = _testcapi.codec_incrementaldecoder(encoding)
                    decodedresult = u""
                    for c in encodedresult:
                        decodedresult += cdecoder.decode(c)
                    decodedresult += cdecoder.decode("", True)
                    self.assertEqual(decodedresult, s, "%r != %r (encoding=%r)" % (decodedresult, s, encoding))

                    # check iterencode()/iterdecode()
                    result = u"".join(codecs.iterdecode(codecs.iterencode(s, encoding), encoding))
                    self.assertEqual(result, s, "%r != %r (encoding=%r)" % (result, s, encoding))

                    # check iterencode()/iterdecode() with empty string
                    result = u"".join(codecs.iterdecode(codecs.iterencode(u"", encoding), encoding))
                    self.assertEqual(result, u"")