test/fake_fs: fix tests for py3 compatibility
-from cStringIO import StringIO
+from io import BytesIO
from contextlib import closing
+try:
+ FileNotFoundError, NotADirectoryError
+except NameError:
+ FileNotFoundError = NotADirectoryError = OSError
+
+
def make_fake_fstools(fake_filesystem):
"""
Build fake versions of os.listdir(), os.isfile(), etc. for use in
@@ -41,13 +47,13 @@ def make_fake_fstools(fake_filesystem):
while '/' in remainder:
next_dir, remainder = remainder.split('/', 1)
if next_dir not in subdict:
- raise OSError(
+ raise FileNotFoundError(
'[Errno 2] No such file or directory: %s' % next_dir)
subdict = subdict.get(next_dir)
if not isinstance(subdict, dict):
- raise OSError('[Errno 20] Not a directory: %s' % next_dir)
+ raise NotADirectoryError('[Errno 20] Not a directory: %s' % next_dir)
if subdict and not remainder:
- return subdict.keys()
+ return list(subdict)
return []
def fake_isfile(path, fsdict=False):
@@ -58,7 +64,7 @@ def make_fake_fstools(fake_filesystem):
subdict = fsdict
for component in components:
if component not in subdict:
- raise OSError(
+ raise FileNotFoundError(
'[Errno 2] No such file or directory: %s' % component)
subdict = subdict.get(component)
return subdict is None or isinstance(subdict, str)
@@ -80,7 +86,7 @@ def make_fake_fstools(fake_filesystem):
if isinstance(subdict, dict):
raise IOError('[Errno 21] Is a directory: %s' % path)
elif subdict is None:
- return closing(StringIO(''))
- return closing(StringIO(subdict))
+ return closing(BytesIO(b''))
+ return closing(BytesIO(subdict.encode()))
return fake_exists, fake_listdir, fake_isfile, fake_isdir, fake_open
