Skip to content
Snippets Groups Projects
Commit 0582e130 authored by Piotr Maślanka's avatar Piotr Maślanka
Browse files

fix a bug in read_in_file

parent a5d079b8
No related branches found
No related tags found
No related merge requests found
...@@ -3,3 +3,4 @@ ...@@ -3,3 +3,4 @@
* added `encoding` to `read_lines` * added `encoding` to `read_lines`
* added `none_if_false` * added `none_if_false`
* fixed import mishap in `satella.coding.transforms` with `hashables_to_int` * fixed import mishap in `satella.coding.transforms` with `hashables_to_int`
* fixed `read_in_file` if file does not exist and default is not set
__version__ = '2.14.33a3' __version__ = '2.14.33a4'
...@@ -168,6 +168,7 @@ def read_in_file(path: str, encoding: tp.Optional[str] = None, ...@@ -168,6 +168,7 @@ def read_in_file(path: str, encoding: tp.Optional[str] = None,
:param default: value to return when the file does not exist. Default (None) will raise a :param default: value to return when the file does not exist. Default (None) will raise a
FileNotFoundError FileNotFoundError
:return: file content, either decoded as a str, or not as bytes :return: file content, either decoded as a str, or not as bytes
:raises FileNotFoundError: file did not exist and default was not set
""" """
if os.path.isdir(path): if os.path.isdir(path):
if default is not _NOTSET: if default is not _NOTSET:
...@@ -178,9 +179,9 @@ def read_in_file(path: str, encoding: tp.Optional[str] = None, ...@@ -178,9 +179,9 @@ def read_in_file(path: str, encoding: tp.Optional[str] = None,
if encoding is None: if encoding is None:
file = open(path, 'rb') file = open(path, 'rb')
else: else:
file = codecs.open(path, 'rb', encoding) file = codecs.open(path, 'r', encoding)
except FileNotFoundError: except FileNotFoundError:
if default: if default is not _NOTSET:
return default return default
raise raise
......
...@@ -16,6 +16,9 @@ def putfile(path: str) -> None: ...@@ -16,6 +16,9 @@ def putfile(path: str) -> None:
class TestFiles(unittest.TestCase): class TestFiles(unittest.TestCase):
def test_read_nonexistent_file(self):
self.assertRaises(FileNotFoundError, lambda: read_in_file('moot'))
def test_read_lines(self): def test_read_lines(self):
lines = read_lines('LICENSE') lines = read_lines('LICENSE')
self.assertTrue(all(lines)) self.assertTrue(all(lines))
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment