changeset: 97066:1a5bbb31f740 parent: 97064:8f65be73eb3a user: Robert Collins date: Sun Jul 26 06:43:13 2015 +1200 files: Lib/_pyio.py Lib/test/test_file.py Misc/NEWS Modules/_io/_iomodule.c description: - Issue #2091: error correctly on open() with mode 'U' and '+' open() accepted a 'U' mode string containing '+', but 'U' can only be used with 'r'. Patch from Jeff Balogh and John O'Connor. diff -r 8f65be73eb3a -r 1a5bbb31f740 Lib/_pyio.py --- a/Lib/_pyio.py Sat Jul 25 14:55:32 2015 +0300 +++ b/Lib/_pyio.py Sun Jul 26 06:43:13 2015 +1200 @@ -181,8 +181,8 @@ text = "t" in modes binary = "b" in modes if "U" in modes: - if creating or writing or appending: - raise ValueError("can't use U and writing mode at once") + if creating or writing or appending or updating: + raise ValueError("mode U cannot be combined with 'x', 'w', 'a', or '+'") import warnings warnings.warn("'U' mode is deprecated", DeprecationWarning, 2) diff -r 8f65be73eb3a -r 1a5bbb31f740 Lib/test/test_file.py --- a/Lib/test/test_file.py Sat Jul 25 14:55:32 2015 +0300 +++ b/Lib/test/test_file.py Sun Jul 26 06:43:13 2015 +1200 @@ -139,7 +139,7 @@ def testModeStrings(self): # check invalid mode strings - for mode in ("", "aU", "wU+"): + for mode in ("", "aU", "wU+", "U+", "+U", "rU+"): try: f = self.open(TESTFN, mode) except ValueError: diff -r 8f65be73eb3a -r 1a5bbb31f740 Misc/NEWS --- a/Misc/NEWS Sat Jul 25 14:55:32 2015 +0300 +++ b/Misc/NEWS Sun Jul 26 06:43:13 2015 +1200 @@ -15,6 +15,9 @@ - Issue #13938: 2to3 converts StringTypes to a tuple. Patch from Mark Hammond. +- Issue #2091: open() accepted a 'U' mode string containing '+', but 'U' can + only be used with 'r'. Patch from Jeff Balogh and John O'Connor. + - Issue #8585: improved tests for zipimporter2. Patch from Mark Lawrence. - Issue #18622: unittest.mock.mock_open().reset_mock would recurse infinitely. diff -r 8f65be73eb3a -r 1a5bbb31f740 Modules/_io/_iomodule.c --- a/Modules/_io/_iomodule.c Sat Jul 25 14:55:32 2015 +0300 +++ b/Modules/_io/_iomodule.c Sun Jul 26 06:43:13 2015 +1200 @@ -248,8 +248,8 @@ _Py_IDENTIFIER(close); if (!PyUnicode_Check(file) && - !PyBytes_Check(file) && - !PyNumber_Check(file)) { + !PyBytes_Check(file) && + !PyNumber_Check(file)) { PyErr_Format(PyExc_TypeError, "invalid file: %R", file); return NULL; } @@ -307,9 +307,9 @@ /* Parameters validation */ if (universal) { - if (writing || appending) { + if (creating || writing || appending || updating) { PyErr_SetString(PyExc_ValueError, - "can't use U and writing mode at once"); + "mode U cannot be combined with x', 'w', 'a', or '+'"); return NULL; } if (PyErr_WarnEx(PyExc_DeprecationWarning, @@ -437,10 +437,10 @@ /* wraps into a TextIOWrapper */ wrapper = PyObject_CallFunction((PyObject *)&PyTextIOWrapper_Type, - "Osssi", - buffer, - encoding, errors, newline, - line_buffering); + "Osssi", + buffer, + encoding, errors, newline, + line_buffering); if (wrapper == NULL) goto error; result = wrapper;