[Python-checkins] [2.7] bpo-8765: Deprecate writing unicode to binary streams in Py3k mode. (GH-11127)
Serhiy Storchaka
webhook-mailer at python.org
Tue Jan 15 07:34:52 EST 2019
https://github.com/python/cpython/commit/1462234baf7398a6b00c0f51905e26caa17d3c60
commit: 1462234baf7398a6b00c0f51905e26caa17d3c60
branch: 2.7
author: Serhiy Storchaka <storchaka at gmail.com>
committer: GitHub <noreply at github.com>
date: 2019-01-15T14:34:48+02:00
summary:
[2.7] bpo-8765: Deprecate writing unicode to binary streams in Py3k mode. (GH-11127)
files:
A Misc/NEWS.d/next/Library/2018-12-12-09-59-41.bpo-8765.IFupT2.rst
M Lib/test/test_fileio.py
M Modules/_io/bufferedio.c
M Modules/_io/fileio.c
diff --git a/Lib/test/test_fileio.py b/Lib/test/test_fileio.py
index 2825a87024da..57c9f14f47ea 100644
--- a/Lib/test/test_fileio.py
+++ b/Lib/test/test_fileio.py
@@ -12,7 +12,7 @@
from UserList import UserList
from test.test_support import TESTFN, check_warnings, run_unittest, make_bad_fd
-from test.test_support import py3k_bytes as bytes, cpython_only
+from test.test_support import py3k_bytes as bytes, cpython_only, check_py3k_warnings
from test.script_helper import run_python
from _io import FileIO as _FileIO
@@ -101,6 +101,10 @@ def test_none_args(self):
self.assertEqual(self.f.readline(None), b"hi\n")
self.assertEqual(self.f.readlines(None), [b"bye\n", b"abc"])
+ def testWriteUnicode(self):
+ with check_py3k_warnings():
+ self.f.write(u'')
+
def testRepr(self):
self.assertEqual(repr(self.f), "<_io.FileIO name=%r mode='%s'>"
% (self.f.name, self.f.mode))
@@ -210,7 +214,7 @@ def testErrnoOnClose(self, f):
@ClosedFDRaises
def testErrnoOnClosedWrite(self, f):
- f.write('a')
+ f.write(b'a')
@ClosedFDRaises
def testErrnoOnClosedSeek(self, f):
diff --git a/Misc/NEWS.d/next/Library/2018-12-12-09-59-41.bpo-8765.IFupT2.rst b/Misc/NEWS.d/next/Library/2018-12-12-09-59-41.bpo-8765.IFupT2.rst
new file mode 100644
index 000000000000..24a8efc2a94f
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2018-12-12-09-59-41.bpo-8765.IFupT2.rst
@@ -0,0 +1,2 @@
+The write() method of buffered and unbuffered binary streams in the io
+module emits now a DeprecationWarning in Py3k mode for unicode argument.
diff --git a/Modules/_io/bufferedio.c b/Modules/_io/bufferedio.c
index 5bef7463e7cb..b8c98a4d0d04 100644
--- a/Modules/_io/bufferedio.c
+++ b/Modules/_io/bufferedio.c
@@ -1812,6 +1812,13 @@ bufferedwriter_write(buffered *self, PyObject *args)
if (!PyArg_ParseTuple(args, "s*:write", &buf)) {
return NULL;
}
+ if (PyUnicode_Check(PyTuple_GET_ITEM(args, 0)) &&
+ PyErr_WarnPy3k("write() argument must be string or buffer, "
+ "not 'unicode'", 1) < 0)
+ {
+ PyBuffer_Release(&buf);
+ return NULL;
+ }
if (IS_CLOSED(self)) {
PyErr_SetString(PyExc_ValueError, "write to closed file");
diff --git a/Modules/_io/fileio.c b/Modules/_io/fileio.c
index 2b40ada195a1..ed5b918e76a7 100644
--- a/Modules/_io/fileio.c
+++ b/Modules/_io/fileio.c
@@ -716,8 +716,16 @@ fileio_write(fileio *self, PyObject *args)
if (!self->writable)
return err_mode("writing");
- if (!PyArg_ParseTuple(args, "s*", &pbuf))
+ if (!PyArg_ParseTuple(args, "s*:write", &pbuf)) {
return NULL;
+ }
+ if (PyUnicode_Check(PyTuple_GET_ITEM(args, 0)) &&
+ PyErr_WarnPy3k("write() argument must be string or buffer, "
+ "not 'unicode'", 1) < 0)
+ {
+ PyBuffer_Release(&pbuf);
+ return NULL;
+ }
if (_PyVerify_fd(self->fd)) {
Py_BEGIN_ALLOW_THREADS
More information about the Python-checkins
mailing list