Issue #23908: os functions, open() and the io.FileIO constructor now reject
unicode paths with embedded null character on Windows instead of silently
truncate them.
diff --git a/Lib/test/test_fileio.py b/Lib/test/test_fileio.py
index 8fdad14..634a764 100644
--- a/Lib/test/test_fileio.py
+++ b/Lib/test/test_fileio.py
@@ -361,6 +361,11 @@
finally:
os.unlink(TESTFN)
+ def testConstructorHandlesNULChars(self):
+ fn_with_NUL = 'foo\0bar'
+ self.assertRaises(TypeError, _FileIO, fn_with_NUL, 'w')
+ self.assertRaises(TypeError, _FileIO, fn_with_NUL.encode('ascii'), 'w')
+
def testInvalidFd(self):
self.assertRaises(ValueError, _FileIO, -10)
self.assertRaises(OSError, _FileIO, make_bad_fd())
diff --git a/Lib/test/test_getargs2.py b/Lib/test/test_getargs2.py
index 2b2fa60..e0efcaa 100644
--- a/Lib/test/test_getargs2.py
+++ b/Lib/test/test_getargs2.py
@@ -779,7 +779,7 @@
def test_u(self):
from _testcapi import getargs_u
self.assertEqual(getargs_u(u'abc\xe9'), u'abc\xe9')
- self.assertEqual(getargs_u(u'nul:\0'), u'nul:')
+ self.assertRaises(TypeError, getargs_u, u'nul:\0')
self.assertRaises(TypeError, getargs_u, 'bytes')
self.assertRaises(TypeError, getargs_u, bytearray('bytearray'))
self.assertRaises(TypeError, getargs_u, memoryview('memoryview'))
diff --git a/Lib/test/test_io.py b/Lib/test/test_io.py
index e26ffba..3d4da46 100644
--- a/Lib/test/test_io.py
+++ b/Lib/test/test_io.py
@@ -349,6 +349,15 @@
self.assertRaises(IOError, fp.write, "blah")
self.assertRaises(IOError, fp.writelines, ["blah\n"])
+ def test_open_handles_NUL_chars(self):
+ fn_with_NUL = 'foo\0bar'
+ self.assertRaises(TypeError, self.open, fn_with_NUL, 'w')
+
+ bytes_fn = fn_with_NUL.encode('ascii')
+ with warnings.catch_warnings():
+ warnings.simplefilter("ignore", DeprecationWarning)
+ self.assertRaises(TypeError, self.open, bytes_fn, 'w')
+
def test_raw_file_io(self):
with self.open(support.TESTFN, "wb", buffering=0) as f:
self.assertEqual(f.readable(), False)
diff --git a/Lib/test/test_posix.py b/Lib/test/test_posix.py
index 9810c77..7478234 100644
--- a/Lib/test/test_posix.py
+++ b/Lib/test/test_posix.py
@@ -577,6 +577,44 @@
set([int(x) for x in groups.split()]),
set(posix.getgroups() + [posix.getegid()]))
+ @test_support.requires_unicode
+ def test_path_with_null_unicode(self):
+ fn = test_support.TESTFN_UNICODE
+ fn_with_NUL = fn + u'\0'
+ self.addCleanup(test_support.unlink, fn)
+ test_support.unlink(fn)
+ fd = None
+ try:
+ with self.assertRaises(TypeError):
+ fd = os.open(fn_with_NUL, os.O_WRONLY | os.O_CREAT) # raises
+ finally:
+ if fd is not None:
+ os.close(fd)
+ self.assertFalse(os.path.exists(fn))
+ self.assertRaises(TypeError, os.mkdir, fn_with_NUL)
+ self.assertFalse(os.path.exists(fn))
+ open(fn, 'wb').close()
+ self.assertRaises(TypeError, os.stat, fn_with_NUL)
+
+ def test_path_with_null_byte(self):
+ fn = test_support.TESTFN
+ fn_with_NUL = fn + '\0'
+ self.addCleanup(test_support.unlink, fn)
+ test_support.unlink(fn)
+ fd = None
+ try:
+ with self.assertRaises(TypeError):
+ fd = os.open(fn_with_NUL, os.O_WRONLY | os.O_CREAT) # raises
+ finally:
+ if fd is not None:
+ os.close(fd)
+ self.assertFalse(os.path.exists(fn))
+ self.assertRaises(TypeError, os.mkdir, fn_with_NUL)
+ self.assertFalse(os.path.exists(fn))
+ open(fn, 'wb').close()
+ self.assertRaises(TypeError, os.stat, fn_with_NUL)
+
+
class PosixGroupsTester(unittest.TestCase):
def setUp(self):