cpython: 4d4aefa52f49 (original) (raw)
Mercurial > cpython
changeset 104392:4d4aefa52f49 3.6
Issue #28162: Fixes Ctrl+Z handling in console readall() [#28162]
Steve Dower steve.dower@microsoft.com | |
---|---|
date | Sat, 08 Oct 2016 12:37:33 -0700 |
parents | 701e52103528 |
children | 947fa496ca6f 6803a6478056 |
files | Lib/test/test_winconsoleio.py Modules/_io/winconsoleio.c |
diffstat | 2 files changed, 52 insertions(+), 37 deletions(-)[+] [-] Lib/test/test_winconsoleio.py 40 Modules/_io/winconsoleio.c 49 |
line wrap: on
line diff
--- a/Lib/test/test_winconsoleio.py +++ b/Lib/test/test_winconsoleio.py @@ -107,16 +107,15 @@ class WindowsConsoleIOTests(unittest.Tes source = 'ϼўТλФЙ\r\n'.encode('utf-16-le') expected = 'ϼўТλФЙ\r\n'.encode('utf-8') for read_count in range(1, 16):
stdin = open('CONIN$', 'rb', buffering=0)[](#l1.7)
write_input(stdin, source)[](#l1.8)
with open('CONIN$', 'rb', buffering=0) as stdin:[](#l1.9)
write_input(stdin, source)[](#l1.10)
actual = b''[](#l1.12)
while not actual.endswith(b'\n'):[](#l1.13)
b = stdin.read(read_count)[](#l1.14)
actual += b[](#l1.15)
actual = b''[](#l1.16)
while not actual.endswith(b'\n'):[](#l1.17)
b = stdin.read(read_count)[](#l1.18)
actual += b[](#l1.19)
self.assertEqual(actual, expected, 'stdin.read({})'.format(read_count))[](#l1.21)
stdin.close()[](#l1.22)
self.assertEqual(actual, expected, 'stdin.read({})'.format(read_count))[](#l1.23)
def test_partial_surrogate_reads(self): # Test that reading less than 1 full character works when stdin @@ -125,17 +124,24 @@ class WindowsConsoleIOTests(unittest.Tes source = '\U00101FFF\U00101001\r\n'.encode('utf-16-le') expected = '\U00101FFF\U00101001\r\n'.encode('utf-8') for read_count in range(1, 16):
stdin = open('CONIN$', 'rb', buffering=0)[](#l1.31)
write_input(stdin, source)[](#l1.32)
with open('CONIN$', 'rb', buffering=0) as stdin:[](#l1.33)
write_input(stdin, source)[](#l1.34)
actual = b''[](#l1.36)
while not actual.endswith(b'\n'):[](#l1.37)
b = stdin.read(read_count)[](#l1.38)
actual += b[](#l1.39)
actual = b''[](#l1.41)
while not actual.endswith(b'\n'):[](#l1.42)
b = stdin.read(read_count)[](#l1.43)
actual += b[](#l1.44)
self.assertEqual(actual, expected, 'stdin.read({})'.format(read_count))[](#l1.45)
self.assertEqual(actual, expected, 'stdin.read({})'.format(read_count))[](#l1.47)
stdin.close()[](#l1.48)
- def test_ctrl_z(self):
with open('CONIN$', 'rb', buffering=0) as stdin:[](#l1.51)
source = '\xC4\x1A\r\n'.encode('utf-16-le')[](#l1.52)
expected = '\xC4'.encode('utf-8')[](#l1.53)
write_input(stdin, source)[](#l1.54)
a, b = stdin.read(1), stdin.readall()[](#l1.55)
self.assertEqual(expected[0:1], a)[](#l1.56)
self.assertEqual(expected[1:], b)[](#l1.57)
if name == "main": unittest.main()
--- a/Modules/_io/winconsoleio.c +++ b/Modules/_io/winconsoleio.c @@ -816,44 +816,53 @@ static PyObject * PyMem_Free(subbuf);
/* when the read starts with ^Z or is empty we break */[](#l2.7)
if (n == 0 || buf[len] == '\x1a')[](#l2.8)
/* when the read is empty we break */[](#l2.9)
if (n == 0)[](#l2.10) break;[](#l2.11)
- if (len == 0 && _buflen(self) == 0) { /* when the result starts with ^Z we return an empty buffer */ PyMem_Free(buf); return PyBytes_FromStringAndSize(NULL, 0); }
- Py_BEGIN_ALLOW_THREADS
- bytes_size = WideCharToMultiByte(CP_UTF8, 0, buf, len,
NULL, 0, NULL, NULL);[](#l2.25)
- Py_END_ALLOW_THREADS
- if (len) {
Py_BEGIN_ALLOW_THREADS[](#l2.28)
bytes_size = WideCharToMultiByte(CP_UTF8, 0, buf, len,[](#l2.29)
NULL, 0, NULL, NULL);[](#l2.30)
Py_END_ALLOW_THREADS[](#l2.31)
- if (!bytes_size) {
DWORD err = GetLastError();[](#l2.34)
PyMem_Free(buf);[](#l2.35)
return PyErr_SetFromWindowsErr(err);[](#l2.36)
if (!bytes_size) {[](#l2.37)
DWORD err = GetLastError();[](#l2.38)
PyMem_Free(buf);[](#l2.39)
return PyErr_SetFromWindowsErr(err);[](#l2.40)
}[](#l2.41)
- } else {
} bytes_size += _buflen(self); bytes = PyBytes_FromStringAndSize(NULL, bytes_size); rn = _copyfrombuf(self, PyBytes_AS_STRING(bytes), bytes_size);bytes_size = 0;[](#l2.43)
- Py_BEGIN_ALLOW_THREADS
- bytes_size = WideCharToMultiByte(CP_UTF8, 0, buf, len,
&PyBytes_AS_STRING(bytes)[rn], bytes_size - rn, NULL, NULL);[](#l2.52)
- Py_END_ALLOW_THREADS
- if (len) {
Py_BEGIN_ALLOW_THREADS[](#l2.55)
bytes_size = WideCharToMultiByte(CP_UTF8, 0, buf, len,[](#l2.56)
&PyBytes_AS_STRING(bytes)[rn], bytes_size - rn, NULL, NULL);[](#l2.57)
Py_END_ALLOW_THREADS[](#l2.58)
- if (!bytes_size) {
DWORD err = GetLastError();[](#l2.61)
PyMem_Free(buf);[](#l2.62)
Py_CLEAR(bytes);[](#l2.63)
return PyErr_SetFromWindowsErr(err);[](#l2.64)
if (!bytes_size) {[](#l2.65)
DWORD err = GetLastError();[](#l2.66)
PyMem_Free(buf);[](#l2.67)
Py_CLEAR(bytes);[](#l2.68)
return PyErr_SetFromWindowsErr(err);[](#l2.69)
}[](#l2.70)