Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6b95f1d

Browse files
committed
Bug #1194181: bz2.BZ2File didn't handle mode 'U' correctly.
1 parent 6d6917b commit 6b95f1d

3 files changed

Lines changed: 16 additions & 0 deletions

File tree

Lib/test/test_bz2.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,16 @@ def testOpenNonexistent(self):
235235
# "Test opening a nonexistent file"
236236
self.assertRaises(IOError, BZ2File, "/non/existent")
237237

238+
def testModeU(self):
239+
# Bug #1194181: bz2.BZ2File opened for write with mode "U"
240+
self.createTempFile()
241+
bz2f = BZ2File(self.filename, "U")
242+
bz2f.close()
243+
f = file(self.filename)
244+
f.seek(0, 2)
245+
self.assertEqual(f.tell(), len(self.DATA))
246+
f.close()
247+
238248
class BZ2CompressorTest(BaseTest):
239249
def testCompress(self):
240250
# "Test BZ2Compressor.compress()/flush()"

Misc/NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ Core and builtins
9191
Extension Modules
9292
-----------------
9393

94+
- Bug #1194181: bz2.BZ2File didn't handle mode 'U' correctly.
95+
9496
- Patch #1212117: os.stat().st_flags is now accessible as a attribute
9597
if available on the platform.
9698

Modules/bz2module.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1308,6 +1308,10 @@ BZ2File_init(BZ2FileObject *self, PyObject *args, PyObject *kwargs)
13081308
break;
13091309
}
13101310

1311+
if (mode_char == 0) {
1312+
mode_char = 'r';
1313+
}
1314+
13111315
mode = (mode_char == 'r') ? "rb" : "wb";
13121316

13131317
self->file = PyObject_CallFunction((PyObject*)&PyFile_Type, "(Osi)",

0 commit comments

Comments
 (0)