Back to index

python3.2  3.2.2
Public Member Functions
test.test_codecs.BasicUnicodeTest Class Reference
Inheritance diagram for test.test_codecs.BasicUnicodeTest:
Inheritance graph
[legend]
Collaboration diagram for test.test_codecs.BasicUnicodeTest:
Collaboration graph
[legend]

List of all members.

Public Member Functions

def test_basics
def test_seek
def test_bad_decode_args
def test_bad_encode_args
def test_encoding_map_type_initialized
def test_decoder_state
def check_state_handling_decode
def check_state_handling_encode

Detailed Description

Definition at line 1387 of file test_codecs.py.


Member Function Documentation

def test.test_codecs.MixInCheckStateHandling.check_state_handling_decode (   self,
  encoding,
  u,
  s 
) [inherited]

Definition at line 28 of file test_codecs.py.

00028 
00029     def check_state_handling_decode(self, encoding, u, s):
00030         for i in range(len(s)+1):
00031             d = codecs.getincrementaldecoder(encoding)()
00032             part1 = d.decode(s[:i])
00033             state = d.getstate()
00034             self.assertIsInstance(state[1], int)
00035             # Check that the condition stated in the documentation for
00036             # IncrementalDecoder.getstate() holds
00037             if not state[1]:
00038                 # reset decoder to the default state without anything buffered
00039                 d.setstate((state[0][:0], 0))
00040                 # Feeding the previous input may not produce any output
00041                 self.assertTrue(not d.decode(state[0]))
00042                 # The decoder must return to the same state
00043                 self.assertEqual(state, d.getstate())
00044             # Create a new decoder and set it to the state
00045             # we extracted from the old one
00046             d = codecs.getincrementaldecoder(encoding)()
00047             d.setstate(state)
00048             part2 = d.decode(s[i:], True)
00049             self.assertEqual(u, part1+part2)

Here is the call graph for this function:

Here is the caller graph for this function:

def test.test_codecs.MixInCheckStateHandling.check_state_handling_encode (   self,
  encoding,
  u,
  s 
) [inherited]

Definition at line 50 of file test_codecs.py.

00050 
00051     def check_state_handling_encode(self, encoding, u, s):
00052         for i in range(len(u)+1):
00053             d = codecs.getincrementalencoder(encoding)()
00054             part1 = d.encode(u[:i])
00055             state = d.getstate()
00056             d = codecs.getincrementalencoder(encoding)()
00057             d.setstate(state)
00058             part2 = d.encode(u[i:], True)
00059             self.assertEqual(s, part1+part2)

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 1494 of file test_codecs.py.

01494 
01495     def test_bad_decode_args(self):
01496         for encoding in all_unicode_encodings:
01497             decoder = codecs.getdecoder(encoding)
01498             self.assertRaises(TypeError, decoder)
01499             if encoding not in ("idna", "punycode"):
01500                 self.assertRaises(TypeError, decoder, 42)

Here is the call graph for this function:

Definition at line 1501 of file test_codecs.py.

01501 
01502     def test_bad_encode_args(self):
01503         for encoding in all_unicode_encodings:
01504             encoder = codecs.getencoder(encoding)
01505             self.assertRaises(TypeError, encoder)

Here is the call graph for this function:

Definition at line 1388 of file test_codecs.py.

01388 
01389     def test_basics(self):
01390         s = "abc123" # all codecs should be able to encode these
01391         for encoding in all_unicode_encodings:
01392             name = codecs.lookup(encoding).name
01393             if encoding.endswith("_codec"):
01394                 name += "_codec"
01395             elif encoding == "latin_1":
01396                 name = "latin_1"
01397             self.assertEqual(encoding.replace("_", "-"), name.replace("_", "-"))
01398             (b, size) = codecs.getencoder(encoding)(s)
01399             self.assertEqual(size, len(s), "%r != %r (encoding=%r)" % (size, len(s), encoding))
01400             (chars, size) = codecs.getdecoder(encoding)(b)
01401             self.assertEqual(chars, s, "%r != %r (encoding=%r)" % (chars, s, encoding))
01402 
01403             if encoding not in broken_unicode_with_streams:
01404                 # check stream reader/writer
01405                 q = Queue(b"")
01406                 writer = codecs.getwriter(encoding)(q)
01407                 encodedresult = b""
01408                 for c in s:
01409                     writer.write(c)
01410                     chunk = q.read()
01411                     self.assertTrue(type(chunk) is bytes, type(chunk))
01412                     encodedresult += chunk
01413                 q = Queue(b"")
01414                 reader = codecs.getreader(encoding)(q)
01415                 decodedresult = ""
01416                 for c in encodedresult:
01417                     q.write(bytes([c]))
01418                     decodedresult += reader.read()
01419                 self.assertEqual(decodedresult, s, "%r != %r (encoding=%r)" % (decodedresult, s, encoding))
01420 
01421             if encoding not in broken_incremental_coders:
01422                 # check incremental decoder/encoder (fetched via the Python
01423                 # and C API) and iterencode()/iterdecode()
01424                 try:
01425                     encoder = codecs.getincrementalencoder(encoding)()
01426                     cencoder = _testcapi.codec_incrementalencoder(encoding)
01427                 except LookupError: # no IncrementalEncoder
01428                     pass
01429                 else:
01430                     # check incremental decoder/encoder
01431                     encodedresult = b""
01432                     for c in s:
01433                         encodedresult += encoder.encode(c)
01434                     encodedresult += encoder.encode("", True)
01435                     decoder = codecs.getincrementaldecoder(encoding)()
01436                     decodedresult = ""
01437                     for c in encodedresult:
01438                         decodedresult += decoder.decode(bytes([c]))
01439                     decodedresult += decoder.decode(b"", True)
01440                     self.assertEqual(decodedresult, s, "%r != %r (encoding=%r)" % (decodedresult, s, encoding))
01441 
01442                     # check C API
01443                     encodedresult = b""
01444                     for c in s:
01445                         encodedresult += cencoder.encode(c)
01446                     encodedresult += cencoder.encode("", True)
01447                     cdecoder = _testcapi.codec_incrementaldecoder(encoding)
01448                     decodedresult = ""
01449                     for c in encodedresult:
01450                         decodedresult += cdecoder.decode(bytes([c]))
01451                     decodedresult += cdecoder.decode(b"", True)
01452                     self.assertEqual(decodedresult, s, "%r != %r (encoding=%r)" % (decodedresult, s, encoding))
01453 
01454                     # check iterencode()/iterdecode()
01455                     result = "".join(codecs.iterdecode(codecs.iterencode(s, encoding), encoding))
01456                     self.assertEqual(result, s, "%r != %r (encoding=%r)" % (result, s, encoding))
01457 
01458                     # check iterencode()/iterdecode() with empty string
01459                     result = "".join(codecs.iterdecode(codecs.iterencode("", encoding), encoding))
01460                     self.assertEqual(result, "")
01461 
01462                 if encoding not in ("idna", "mbcs"):
01463                     # check incremental decoder/encoder with errors argument
01464                     try:
01465                         encoder = codecs.getincrementalencoder(encoding)("ignore")
01466                         cencoder = _testcapi.codec_incrementalencoder(encoding, "ignore")
01467                     except LookupError: # no IncrementalEncoder
01468                         pass
01469                     else:
01470                         encodedresult = b"".join(encoder.encode(c) for c in s)
01471                         decoder = codecs.getincrementaldecoder(encoding)("ignore")
01472                         decodedresult = "".join(decoder.decode(bytes([c])) for c in encodedresult)
01473                         self.assertEqual(decodedresult, s, "%r != %r (encoding=%r)" % (decodedresult, s, encoding))
01474 
01475                         encodedresult = b"".join(cencoder.encode(c) for c in s)
01476                         cdecoder = _testcapi.codec_incrementaldecoder(encoding, "ignore")
01477                         decodedresult = "".join(cdecoder.decode(bytes([c])) for c in encodedresult)
01478                         self.assertEqual(decodedresult, s, "%r != %r (encoding=%r)" % (decodedresult, s, encoding))

Here is the call graph for this function:

Definition at line 1512 of file test_codecs.py.

01512 
01513     def test_decoder_state(self):
01514         # Check that getstate() and setstate() handle the state properly
01515         u = "abc123"
01516         for encoding in all_unicode_encodings:
01517             if encoding not in broken_incremental_coders:
01518                 self.check_state_handling_decode(encoding, u, u.encode(encoding))
01519                 self.check_state_handling_encode(encoding, u, u.encode(encoding))

Here is the call graph for this function:

Definition at line 1506 of file test_codecs.py.

01506 
01507     def test_encoding_map_type_initialized(self):
01508         from encodings import cp1140
01509         # This used to crash, we are only verifying there's no crash.
01510         table_type = type(cp1140.encoding_table)
01511         self.assertEqual(table_type, table_type)

Here is the call graph for this function:

Definition at line 1479 of file test_codecs.py.

01479 
01480     def test_seek(self):
01481         # all codecs should be able to encode these
01482         s = "%s\n%s\n" % (100*"abc123", 100*"def456")
01483         for encoding in all_unicode_encodings:
01484             if encoding == "idna": # FIXME: See SF bug #1163178
01485                 continue
01486             if encoding in broken_unicode_with_streams:
01487                 continue
01488             reader = codecs.getreader(encoding)(io.BytesIO(s.encode(encoding)))
01489             for t in range(5):
01490                 # Test that calling seek resets the internal codec state and buffers
01491                 reader.seek(0, 0)
01492                 data = reader.read()
01493                 self.assertEqual(s, data)

Here is the call graph for this function:


The documentation for this class was generated from the following file: