Kaydet (Commit) c94a1dc4 authored tarafından Robert Collins's avatar Robert Collins

- 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.
üst 88266720
...@@ -181,8 +181,8 @@ def open(file, mode="r", buffering=-1, encoding=None, errors=None, ...@@ -181,8 +181,8 @@ def open(file, mode="r", buffering=-1, encoding=None, errors=None,
text = "t" in modes text = "t" in modes
binary = "b" in modes binary = "b" in modes
if "U" in modes: if "U" in modes:
if creating or writing or appending: if creating or writing or appending or updating:
raise ValueError("can't use U and writing mode at once") raise ValueError("mode U cannot be combined with 'x', 'w', 'a', or '+'")
import warnings import warnings
warnings.warn("'U' mode is deprecated", warnings.warn("'U' mode is deprecated",
DeprecationWarning, 2) DeprecationWarning, 2)
......
...@@ -139,7 +139,7 @@ class OtherFileTests: ...@@ -139,7 +139,7 @@ class OtherFileTests:
def testModeStrings(self): def testModeStrings(self):
# check invalid mode strings # check invalid mode strings
for mode in ("", "aU", "wU+"): for mode in ("", "aU", "wU+", "U+", "+U", "rU+"):
try: try:
f = self.open(TESTFN, mode) f = self.open(TESTFN, mode)
except ValueError: except ValueError:
......
...@@ -15,6 +15,9 @@ Library ...@@ -15,6 +15,9 @@ Library
- Issue #13938: 2to3 converts StringTypes to a tuple. Patch from Mark Hammond. - 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 #8585: improved tests for zipimporter2. Patch from Mark Lawrence.
- Issue #18622: unittest.mock.mock_open().reset_mock would recurse infinitely. - Issue #18622: unittest.mock.mock_open().reset_mock would recurse infinitely.
......
...@@ -248,8 +248,8 @@ _io_open_impl(PyModuleDef *module, PyObject *file, const char *mode, ...@@ -248,8 +248,8 @@ _io_open_impl(PyModuleDef *module, PyObject *file, const char *mode,
_Py_IDENTIFIER(close); _Py_IDENTIFIER(close);
if (!PyUnicode_Check(file) && if (!PyUnicode_Check(file) &&
!PyBytes_Check(file) && !PyBytes_Check(file) &&
!PyNumber_Check(file)) { !PyNumber_Check(file)) {
PyErr_Format(PyExc_TypeError, "invalid file: %R", file); PyErr_Format(PyExc_TypeError, "invalid file: %R", file);
return NULL; return NULL;
} }
...@@ -307,9 +307,9 @@ _io_open_impl(PyModuleDef *module, PyObject *file, const char *mode, ...@@ -307,9 +307,9 @@ _io_open_impl(PyModuleDef *module, PyObject *file, const char *mode,
/* Parameters validation */ /* Parameters validation */
if (universal) { if (universal) {
if (writing || appending) { if (creating || writing || appending || updating) {
PyErr_SetString(PyExc_ValueError, 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; return NULL;
} }
if (PyErr_WarnEx(PyExc_DeprecationWarning, if (PyErr_WarnEx(PyExc_DeprecationWarning,
...@@ -437,10 +437,10 @@ _io_open_impl(PyModuleDef *module, PyObject *file, const char *mode, ...@@ -437,10 +437,10 @@ _io_open_impl(PyModuleDef *module, PyObject *file, const char *mode,
/* wraps into a TextIOWrapper */ /* wraps into a TextIOWrapper */
wrapper = PyObject_CallFunction((PyObject *)&PyTextIOWrapper_Type, wrapper = PyObject_CallFunction((PyObject *)&PyTextIOWrapper_Type,
"Osssi", "Osssi",
buffer, buffer,
encoding, errors, newline, encoding, errors, newline,
line_buffering); line_buffering);
if (wrapper == NULL) if (wrapper == NULL)
goto error; goto error;
result = wrapper; result = wrapper;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment