diff options
author | Steve Dower <steve.dower@microsoft.com> | 2017-02-04 23:14:18 (GMT) |
---|---|---|
committer | Steve Dower <steve.dower@microsoft.com> | 2017-02-04 23:14:18 (GMT) |
commit | 43fec9b419512d67e94b26f7da24c8392e835517 (patch) | |
tree | 4e5b2625a30abe9ce6ee4f761d4067aa274b3a26 /Lib | |
parent | 38dbaced03e7406bf4b27489f5c7efc9188abc67 (diff) | |
parent | 722e3e2705e1f7dbbbc2ad58e2957f9fb759ad76 (diff) | |
download | cpython-43fec9b419512d67e94b26f7da24c8392e835517.zip cpython-43fec9b419512d67e94b26f7da24c8392e835517.tar.gz cpython-43fec9b419512d67e94b26f7da24c8392e835517.tar.bz2 |
Merge issue #28164 and issue #29409
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/test/test_fileio.py | 20 | ||||
-rw-r--r-- | Lib/test/test_winconsoleio.py | 28 |
2 files changed, 46 insertions, 2 deletions
diff --git a/Lib/test/test_fileio.py b/Lib/test/test_fileio.py index 12f2f11..3da210a 100644 --- a/Lib/test/test_fileio.py +++ b/Lib/test/test_fileio.py @@ -9,7 +9,8 @@ from array import array from weakref import proxy from functools import wraps -from test.support import TESTFN, check_warnings, run_unittest, make_bad_fd, cpython_only +from test.support import (TESTFN, TESTFN_UNICODE, check_warnings, run_unittest, + make_bad_fd, cpython_only) from collections import UserList import _io # C implementation of io @@ -432,6 +433,23 @@ class OtherFileTests: finally: os.unlink(TESTFN) + @unittest.skipIf(sys.getfilesystemencoding() != 'utf-8', + "test only works for utf-8 filesystems") + def testUtf8BytesOpen(self): + # Opening a UTF-8 bytes filename + try: + fn = TESTFN_UNICODE.encode("utf-8") + except UnicodeEncodeError: + self.skipTest('could not encode %r to utf-8' % TESTFN_UNICODE) + f = self.FileIO(fn, "w") + try: + f.write(b"abc") + f.close() + with open(TESTFN_UNICODE, "rb") as f: + self.assertEqual(f.read(), b"abc") + finally: + os.unlink(TESTFN_UNICODE) + def testConstructorHandlesNULChars(self): fn_with_NUL = 'foo\0bar' self.assertRaises(ValueError, self.FileIO, fn_with_NUL, 'w') diff --git a/Lib/test/test_winconsoleio.py b/Lib/test/test_winconsoleio.py index b1a2f7a..06467e9 100644 --- a/Lib/test/test_winconsoleio.py +++ b/Lib/test/test_winconsoleio.py @@ -1,9 +1,11 @@ '''Tests for WindowsConsoleIO ''' +import os import io -import unittest import sys +import unittest +import tempfile if sys.platform != 'win32': raise unittest.SkipTest("test only relevant on win32") @@ -19,6 +21,16 @@ class WindowsConsoleIOTests(unittest.TestCase): self.assertFalse(issubclass(ConIO, io.TextIOBase)) def test_open_fd(self): + self.assertRaisesRegex(ValueError, + "negative file descriptor", ConIO, -1) + + fd, _ = tempfile.mkstemp() + try: + self.assertRaisesRegex(ValueError, + "Cannot open non-console file", ConIO, fd) + finally: + os.close(fd) + try: f = ConIO(0) except ValueError: @@ -56,6 +68,20 @@ class WindowsConsoleIOTests(unittest.TestCase): f.close() def test_open_name(self): + self.assertRaises(ValueError, ConIO, sys.executable) + + f = open('C:/con', 'rb', buffering=0) + self.assertIsInstance(f, ConIO) + f.close() + + f = open(r'\\.\conin$', 'rb', buffering=0) + self.assertIsInstance(f, ConIO) + f.close() + + f = open('//?/conout$', 'wb', buffering=0) + self.assertIsInstance(f, ConIO) + f.close() + f = ConIO("CON") self.assertTrue(f.readable()) self.assertFalse(f.writable()) |