Skip to content

Commit

Permalink
[2.7] bpo-8765: Deprecate writing unicode to binary streams in Py3k m…
Browse files Browse the repository at this point in the history
…ode. (GH-11127)
  • Loading branch information
serhiy-storchaka committed Jan 15, 2019
1 parent 77b80c9 commit 1462234
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 3 deletions.
8 changes: 6 additions & 2 deletions Lib/test/test_fileio.py
Expand Up @@ -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
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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):
Expand Down
@@ -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.
7 changes: 7 additions & 0 deletions Modules/_io/bufferedio.c
Expand Up @@ -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");
Expand Down
10 changes: 9 additions & 1 deletion Modules/_io/fileio.c
Expand Up @@ -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
Expand Down

0 comments on commit 1462234

Please sign in to comment.