diff --git a/pyfakefs/fake_filesystem.py b/pyfakefs/fake_filesystem.py index 4eebbbd8..ddd1f986 100644 --- a/pyfakefs/fake_filesystem.py +++ b/pyfakefs/fake_filesystem.py @@ -2259,8 +2259,11 @@ def create_file(self, file_path, st_mode=S_IFREG | PERM_DEF_FILE, Args: file_path: The path to the file to create. st_mode: The stat constant representing the file type. - contents: The contents of the file. - st_size: The file size; only valid if contents not given. + contents: the contents of the file. If not given and st_size is + None, an empty file is assumed. + st_size: file size; only valid if contents not given. If given, + the file is considered to be in "large file mode" and trying + to read from or write to the file will result in an exception. create_missing_dirs: If `True`, auto create missing directories. apply_umask: `True` if the current umask must be applied on `st_mode`. @@ -2413,8 +2416,11 @@ def create_file_internally(self, file_path, Args: file_path: path to the file to create. st_mode: the stat.S_IF constant representing the file type. - contents: the contents of the file. - st_size: file size; only valid if contents not given. + contents: the contents of the file. If not given and st_size is + None, an empty file is assumed. + st_size: file size; only valid if contents not given. If given, + the file is considered to be in "large file mode" and trying + to read from or write to the file will result in an exception. create_missing_dirs: if True, auto create missing directories. apply_umask: whether or not the current umask must be applied on st_mode. @@ -2456,6 +2462,8 @@ def create_file_internally(self, file_path, file_object.st_ino = self._last_ino self.add_object(parent_directory, file_object, error_fct) + if st_size is None and contents is None: + contents = '' if (not read_from_real_fs and (contents is not None or st_size is not None)): try: diff --git a/pyfakefs/tests/fake_filesystem_test.py b/pyfakefs/tests/fake_filesystem_test.py index 92cd8826..3ee5448b 100644 --- a/pyfakefs/tests/fake_filesystem_test.py +++ b/pyfakefs/tests/fake_filesystem_test.py @@ -543,6 +543,13 @@ def test_create_file(self): self.assertTrue(stat.S_IFREG & new_file.st_mode) self.assertEqual(new_file, retval) + def test_empty_file_created_for_none_contents(self): + fake_open = fake_filesystem.FakeFileOpen(self.filesystem) + path = 'foo/bar/baz' + self.filesystem.create_file(path, contents=None) + with fake_open(path) as f: + self.assertEqual('', f.read()) + def test_create_file_with_incorrect_mode_type(self): self.assertRaises(TypeError, self.filesystem.create_file, 'foo', 'bar')