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

Skip to content

Commit c94a1dc

Browse files
committed
- 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.
1 parent 8826672 commit c94a1dc

4 files changed

Lines changed: 14 additions & 11 deletions

File tree

Lib/_pyio.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,8 +181,8 @@ def open(file, mode="r", buffering=-1, encoding=None, errors=None,
181181
text = "t" in modes
182182
binary = "b" in modes
183183
if "U" in modes:
184-
if creating or writing or appending:
185-
raise ValueError("can't use U and writing mode at once")
184+
if creating or writing or appending or updating:
185+
raise ValueError("mode U cannot be combined with 'x', 'w', 'a', or '+'")
186186
import warnings
187187
warnings.warn("'U' mode is deprecated",
188188
DeprecationWarning, 2)

Lib/test/test_file.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ class OtherFileTests:
139139

140140
def testModeStrings(self):
141141
# check invalid mode strings
142-
for mode in ("", "aU", "wU+"):
142+
for mode in ("", "aU", "wU+", "U+", "+U", "rU+"):
143143
try:
144144
f = self.open(TESTFN, mode)
145145
except ValueError:

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ Library
1515

1616
- Issue #13938: 2to3 converts StringTypes to a tuple. Patch from Mark Hammond.
1717

18+
- Issue #2091: open() accepted a 'U' mode string containing '+', but 'U' can
19+
only be used with 'r'. Patch from Jeff Balogh and John O'Connor.
20+
1821
- Issue #8585: improved tests for zipimporter2. Patch from Mark Lawrence.
1922

2023
- Issue #18622: unittest.mock.mock_open().reset_mock would recurse infinitely.

Modules/_io/_iomodule.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -248,8 +248,8 @@ _io_open_impl(PyModuleDef *module, PyObject *file, const char *mode,
248248
_Py_IDENTIFIER(close);
249249

250250
if (!PyUnicode_Check(file) &&
251-
!PyBytes_Check(file) &&
252-
!PyNumber_Check(file)) {
251+
!PyBytes_Check(file) &&
252+
!PyNumber_Check(file)) {
253253
PyErr_Format(PyExc_TypeError, "invalid file: %R", file);
254254
return NULL;
255255
}
@@ -307,9 +307,9 @@ _io_open_impl(PyModuleDef *module, PyObject *file, const char *mode,
307307

308308
/* Parameters validation */
309309
if (universal) {
310-
if (writing || appending) {
310+
if (creating || writing || appending || updating) {
311311
PyErr_SetString(PyExc_ValueError,
312-
"can't use U and writing mode at once");
312+
"mode U cannot be combined with x', 'w', 'a', or '+'");
313313
return NULL;
314314
}
315315
if (PyErr_WarnEx(PyExc_DeprecationWarning,
@@ -437,10 +437,10 @@ _io_open_impl(PyModuleDef *module, PyObject *file, const char *mode,
437437

438438
/* wraps into a TextIOWrapper */
439439
wrapper = PyObject_CallFunction((PyObject *)&PyTextIOWrapper_Type,
440-
"Osssi",
441-
buffer,
442-
encoding, errors, newline,
443-
line_buffering);
440+
"Osssi",
441+
buffer,
442+
encoding, errors, newline,
443+
line_buffering);
444444
if (wrapper == NULL)
445445
goto error;
446446
result = wrapper;

0 commit comments

Comments
 (0)