Convert path to string right before passing it to the filesystem layer

This commit is contained in:
Lukas Geiger 2020-07-09 00:01:23 +02:00
parent 7fe25a5872
commit 5ef85fc6c8
2 changed files with 9 additions and 11 deletions

View File

@ -49,7 +49,7 @@ class FileIO(object):
""" """
def __init__(self, name, mode): def __init__(self, name, mode):
self.__name = compat.path_to_str(name) self.__name = name
self.__mode = mode self.__mode = mode
self._read_buf = None self._read_buf = None
self._writable_file = None self._writable_file = None
@ -77,7 +77,7 @@ class FileIO(object):
raise errors.PermissionDeniedError(None, None, raise errors.PermissionDeniedError(None, None,
"File isn't open for reading") "File isn't open for reading")
self._read_buf = _pywrap_file_io.BufferedInputStream( self._read_buf = _pywrap_file_io.BufferedInputStream(
self.__name, 1024 * 512) compat.path_to_str(self.__name), 1024 * 512)
def _prewrite_check(self): def _prewrite_check(self):
if not self._writable_file: if not self._writable_file:
@ -85,7 +85,7 @@ class FileIO(object):
raise errors.PermissionDeniedError(None, None, raise errors.PermissionDeniedError(None, None,
"File isn't open for writing") "File isn't open for writing")
self._writable_file = _pywrap_file_io.WritableFile( self._writable_file = _pywrap_file_io.WritableFile(
compat.as_bytes(self.__name), compat.as_bytes(self.__mode)) compat.path_to_bytes(self.__name), compat.as_bytes(self.__mode))
def _prepare_value(self, val): def _prepare_value(self, val):
if self._binary_mode: if self._binary_mode:

View File

@ -50,10 +50,6 @@ class FileIoTest(test.TestCase, parameterized.TestCase):
with self.assertRaises(errors.NotFoundError): with self.assertRaises(errors.NotFoundError):
_ = f.read() _ = f.read()
def testPathLike(self):
f = file_io.FileIO(pathlib.Path("temp_file"), mode="w")
self.assertEqual(f.name, "temp_file")
@run_all_path_types @run_all_path_types
def testFileDoesntExist(self, join): def testFileDoesntExist(self, join):
file_path = join(self._base_dir, "temp_file") file_path = join(self._base_dir, "temp_file")
@ -88,14 +84,16 @@ class FileIoTest(test.TestCase, parameterized.TestCase):
file_contents = file_io.read_file_to_string(file_path) file_contents = file_io.read_file_to_string(file_path)
self.assertEqual("new", file_contents) self.assertEqual("new", file_contents)
def testReadBinaryMode(self): @run_all_path_types
file_path = os.path.join(self._base_dir, "temp_file") def testReadBinaryMode(self, join):
file_path = join(self._base_dir, "temp_file")
file_io.write_string_to_file(file_path, "testing") file_io.write_string_to_file(file_path, "testing")
with file_io.FileIO(file_path, mode="rb") as f: with file_io.FileIO(file_path, mode="rb") as f:
self.assertEqual(b"testing", f.read()) self.assertEqual(b"testing", f.read())
def testWriteBinaryMode(self): @run_all_path_types
file_path = os.path.join(self._base_dir, "temp_file") def testWriteBinaryMode(self, join):
file_path = join(self._base_dir, "temp_file")
file_io.FileIO(file_path, "wb").write("testing") file_io.FileIO(file_path, "wb").write("testing")
with file_io.FileIO(file_path, mode="r") as f: with file_io.FileIO(file_path, mode="r") as f:
self.assertEqual("testing", f.read()) self.assertEqual("testing", f.read())