From bf0771063f286a72d651857ea3461135c14d16c7 Mon Sep 17 00:00:00 2001 From: Georg Brandl Date: Fri, 3 Jun 2005 19:45:53 +0000 Subject: Backport bug #1194181: bz2.BZ2File didn't handle mode 'U' correctly. --- Lib/test/test_bz2.py | 10 ++++++++++ Misc/NEWS | 2 ++ Modules/bz2module.c | 4 ++++ 3 files changed, 16 insertions(+) diff --git a/Lib/test/test_bz2.py b/Lib/test/test_bz2.py index 7d844d8..fc8213f 100644 --- a/Lib/test/test_bz2.py +++ b/Lib/test/test_bz2.py @@ -235,6 +235,16 @@ class BZ2FileTest(BaseTest): # "Test opening a nonexistent file" self.assertRaises(IOError, BZ2File, "/non/existent") + def testModeU(self): + # Bug #1194181: bz2.BZ2File opened for write with mode "U" + self.createTempFile() + bz2f = BZ2File(self.filename, "U") + bz2f.close() + f = file(self.filename) + f.seek(0, 2) + self.assertEqual(f.tell(), len(self.DATA)) + f.close() + class BZ2CompressorTest(BaseTest): def testCompress(self): # "Test BZ2Compressor.compress()/flush()" diff --git a/Misc/NEWS b/Misc/NEWS index 9b17b46..7eba41f 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -27,6 +27,8 @@ Core and builtins Extension Modules ----------------- +- Bug #1194181: bz2.BZ2File didn't handle mode 'U' correctly. + - Bug #1166660: The readline module could segfault if hook functions were set in a different thread than that which called readline. diff --git a/Modules/bz2module.c b/Modules/bz2module.c index 8a93cd9..b9874eb 100644 --- a/Modules/bz2module.c +++ b/Modules/bz2module.c @@ -1308,6 +1308,10 @@ BZ2File_init(BZ2FileObject *self, PyObject *args, PyObject *kwargs) break; } + if (mode_char == 0) { + mode_char = 'r'; + } + mode = (mode_char == 'r') ? "rb" : "wb"; self->file = PyObject_CallFunction((PyObject*)&PyFile_Type, "(Osi)", -- cgit v0.12