]> granicus.if.org Git - python/commitdiff
Bug #1728403: Fix a bug that CJKCodecs StreamReader hangs when it
authorHye-Shik Chang <hyeshik@gmail.com>
Tue, 5 Jun 2007 18:58:51 +0000 (18:58 +0000)
committerHye-Shik Chang <hyeshik@gmail.com>
Tue, 5 Jun 2007 18:58:51 +0000 (18:58 +0000)
reads a file that ends with incomplete sequence and sizehint argument
for .read() is specified.

Lib/test/test_multibytecodec.py
Misc/NEWS
Modules/cjkcodecs/multibytecodec.c

index de273394cd49c4932780b2f7a3b1ac2eb60357d6..b6557f3d98af5a1fb819ae222180c45377be2998 100644 (file)
@@ -136,11 +136,19 @@ class Test_IncrementalDecoder(unittest.TestCase):
         self.assertRaises(UnicodeDecodeError, decoder.decode, '', True)
         self.assertEqual(decoder.decode('B@$'), u'\u4e16')
 
+class Test_StreamReader(unittest.TestCase):
+    def test_bug1728403(self):
+        try:
+            open(TESTFN, 'w').write('\xa1')
+            f = codecs.open(TESTFN, encoding='cp949')
+            self.assertRaises(UnicodeDecodeError, f.read, 2)
+        finally:
+            os.unlink(TESTFN)
 
 class Test_StreamWriter(unittest.TestCase):
     if len(u'\U00012345') == 2: # UCS2
         def test_gb18030(self):
-            s= StringIO.StringIO()
+            s = StringIO.StringIO()
             c = codecs.getwriter('gb18030')(s)
             c.write(u'123')
             self.assertEqual(s.getvalue(), '123')
index cf61b89f200c7e09710717c65805f8d2c6dfe250..a477fb8633da9f4c3f8c371fbc259237e2a3af03 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -222,6 +222,10 @@ Core and builtins
 Library
 -------
 
+- Bug #1728403: Fix a bug that CJKCodecs StreamReader hangs when it
+  reads a file that ends with incomplete sequence and sizehint argument
+  for .read() is specified.
+
 - Bug #1730389: Change time.strptime() to use ``\s+`` instead of ``\s*`` when
   matching spaces in the specified format argument.
 
index 7c6b989355a5eff1e730e11337d53a898af160d7..9fb9570225edb36c02d8faa149fa30803b03e828 100644 (file)
@@ -1214,6 +1214,8 @@ mbstreamreader_iread(MultibyteStreamReaderObject *self,
        cres = NULL;
 
        for (;;) {
+               int endoffile;
+
                if (sizehint < 0)
                        cres = PyObject_CallMethod(self->stream,
                                        (char *)method, NULL);
@@ -1230,6 +1232,8 @@ mbstreamreader_iread(MultibyteStreamReaderObject *self,
                        goto errorexit;
                }
 
+               endoffile = (PyString_GET_SIZE(cres) == 0);
+
                if (self->pendingsize > 0) {
                        PyObject *ctr;
                        char *ctrdata;
@@ -1257,7 +1261,7 @@ mbstreamreader_iread(MultibyteStreamReaderObject *self,
                                (MultibyteStatefulDecoderContext *)self, &buf))
                        goto errorexit;
 
-               if (rsize == 0 || sizehint < 0) { /* end of file */
+               if (endoffile || sizehint < 0) {
                        if (buf.inbuf < buf.inbuf_end &&
                            multibytecodec_decerror(self->codec, &self->state,
                                        &buf, self->errors, MBERR_TOOFEW))