summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHye-Shik Chang <hyeshik@gmail.com>2007-06-05 19:02:59 +0000
committerHye-Shik Chang <hyeshik@gmail.com>2007-06-05 19:02:59 +0000
commitda96e479ed930ca56d7007cb218da7b4192c0ae5 (patch)
tree63074d7e225f1b8f4d61b8c5e7bc6d0d83bf1fc8
parent8f95bb6b846ccb4d8c4e29434c8dbff5dbace41f (diff)
downloadcpython-da96e479ed930ca56d7007cb218da7b4192c0ae5.tar.gz
(Backport from r55770)
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.
-rw-r--r--Lib/test/test_multibytecodec.py13
-rw-r--r--Misc/NEWS4
-rw-r--r--Modules/cjkcodecs/multibytecodec.c6
3 files changed, 21 insertions, 2 deletions
diff --git a/Lib/test/test_multibytecodec.py b/Lib/test/test_multibytecodec.py
index 3fcc8ba81a..052f1681dd 100644
--- a/Lib/test/test_multibytecodec.py
+++ b/Lib/test/test_multibytecodec.py
@@ -7,7 +7,17 @@
from test import test_support
from test import test_multibytecodec_support
-import unittest, StringIO, codecs, sys
+from test.test_support import TESTFN
+import unittest, StringIO, codecs, sys, os
+
+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
@@ -99,6 +109,7 @@ class Test_ISO2022(unittest.TestCase):
def test_main():
suite = unittest.TestSuite()
+ suite.addTest(unittest.makeSuite(Test_StreamReader))
suite.addTest(unittest.makeSuite(Test_StreamWriter))
suite.addTest(unittest.makeSuite(Test_ISO2022))
test_support.run_suite(suite)
diff --git a/Misc/NEWS b/Misc/NEWS
index e8ea8cc01a..002ea18d9c 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -26,6 +26,10 @@ Extension Modules
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.
+
- HTML-escape the plain traceback in cgitb's HTML output, to prevent
the traceback inadvertently or maliciously closing the comment and
injecting HTML into the error page.
diff --git a/Modules/cjkcodecs/multibytecodec.c b/Modules/cjkcodecs/multibytecodec.c
index 7d2d15ebfb..b69eefff0a 100644
--- a/Modules/cjkcodecs/multibytecodec.c
+++ b/Modules/cjkcodecs/multibytecodec.c
@@ -705,6 +705,8 @@ mbstreamreader_iread(MultibyteStreamReaderObject *self,
cres = NULL;
for (;;) {
+ int endoffile;
+
if (sizehint < 0)
cres = PyObject_CallMethod(self->stream,
(char *)method, NULL);
@@ -721,6 +723,8 @@ mbstreamreader_iread(MultibyteStreamReaderObject *self,
goto errorexit;
}
+ endoffile = (PyString_GET_SIZE(cres) == 0);
+
if (self->pendingsize > 0) {
PyObject *ctr;
char *ctrdata;
@@ -772,7 +776,7 @@ mbstreamreader_iread(MultibyteStreamReaderObject *self,
goto errorexit;
}
- if (rsize == 0 || sizehint < 0) { /* end of file */
+ if (endoffile || sizehint < 0) { /* end of file */
if (buf.inbuf < buf.inbuf_end &&
multibytecodec_decerror(self->codec, &self->state,
&buf, self->errors, MBERR_TOOFEW))