# Buggy code
def write(text, filename, encoding="utf-8", mode="wb"):
    """
    Write 'text' to file ('filename') assuming 'encoding' in an atomic way
    Return (eventually new) encoding
    """
    text, encoding = encode(text, encoding)
    if "a" in mode:
        with open(filename, mode) as textfile:
            textfile.write(text)
    else:
        # Based in the solution at untitaker/python-atomicwrites#42.
        # Needed to fix file permissions overwritting.
        # See spyder-ide/spyder#9381.
        try:
            original_mode = os.stat(filename).st_mode
        except OSError:  # Change to FileNotFoundError for PY3
            # Creating a new file, emulate what os.open() does
            umask = os.umask(0)
            os.umask(umask)
            original_mode = 0o777 & ~umask
        with atomic_write(filename, overwrite=True, mode=mode) as textfile:
            textfile.write(text)
        os.chmod(filename, original_mode)
    return encoding


# Traceback (OSError)
ERROR:spyder.plugins.editor.widgets.autosaveerror:Error while autosaving /scratch/yms23/netscratch/PhD/Python/Scripts/test_save.py to /home/yms23/.config/spyder-py3-dev/autosave/test_save.py
Traceback (most recent call last):
File "/home/yms23/.conda/envs/spyder4beta/lib/python3.7/site-packages/spyder/plugins/editor/widgets/editor.py", line 1818, in save
self._write_to_file(finfo, finfo.filename)
File "/home/yms23/.conda/envs/spyder4beta/lib/python3.7/site-packages/spyder/plugins/editor/widgets/editor.py", line 1777, in _write_to_file
fileinfo.encoding = encoding.write(txt, filename, fileinfo.encoding)
File "/home/yms23/.conda/envs/spyder4beta/lib/python3.7/site-packages/spyder/utils/encoding.py", line 251, in write
textfile.write(text)
File "/home/yms23/.conda/envs/spyder4beta/lib/python3.7/contextlib.py", line 119, in __exit__
next(self.gen)
File "/home/yms23/.conda/envs/spyder4beta/lib/python3.7/site-packages/atomicwrites/__init__.py", line 159, in _open
self.commit(f)
File "/home/yms23/.conda/envs/spyder4beta/lib/python3.7/site-packages/atomicwrites/__init__.py", line 192, in commit
replace_atomic(f.name, self._path)
File "/home/yms23/.conda/envs/spyder4beta/lib/python3.7/site-packages/atomicwrites/__init__.py", line 93, in replace_atomic
return _replace_atomic(src, dst)
File "/home/yms23/.conda/envs/spyder4beta/lib/python3.7/site-packages/atomicwrites/__init__.py", line 50, in _replace_atomic
_sync_directory(os.path.normpath(os.path.dirname(dst)))
File "/home/yms23/.conda/envs/spyder4beta/lib/python3.7/site-packages/atomicwrites/__init__.py", line 44, in _sync_directory
_proper_fsync(fd)
OSError: [Errno 22] Invalid argument

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/home/yms23/.conda/envs/spyder4beta/lib/python3.7/site-packages/spyder/plugins/editor/utils/autosave.py", line 362, in autosave
self.stack._write_to_file(finfo, autosave_filename)
File "/home/yms23/.conda/envs/spyder4beta/lib/python3.7/site-packages/spyder/plugins/editor/widgets/editor.py", line 1777, in _write_to_file
fileinfo.encoding = encoding.write(txt, filename, fileinfo.encoding)
File "/home/yms23/.conda/envs/spyder4beta/lib/python3.7/site-packages/spyder/utils/encoding.py", line 251, in write
textfile.write(text)
File "/home/yms23/.conda/envs/spyder4beta/lib/python3.7/contextlib.py", line 119, in __exit__
next(self.gen)
File "/home/yms23/.conda/envs/spyder4beta/lib/python3.7/site-packages/atomicwrites/__init__.py", line 159, in _open
self.commit(f)
File "/home/yms23/.conda/envs/spyder4beta/lib/python3.7/site-packages/atomicwrites/__init__.py", line 192, in commit
replace_atomic(f.name, self._path)
File "/home/yms23/.conda/envs/spyder4beta/lib/python3.7/site-packages/atomicwrites/__init__.py", line 93, in replace_atomic
return _replace_atomic(src, dst)
File "/home/yms23/.conda/envs/spyder4beta/lib/python3.7/site-packages/atomicwrites/__init__.py", line 50, in _replace_atomic
_sync_directory(os.path.normpath(os.path.dirname(dst)))
File "/home/yms23/.conda/envs/spyder4beta/lib/python3.7/site-packages/atomicwrites/__init__.py", line 44, in _sync_directory
_proper_fsync(fd)
OSError: [Errno 22] Invalid argument
Traceback (most recent call last):
File "/home/yms23/.conda/envs/spyder4beta/lib/python3.7/site-packages/spyder/plugins/editor/utils/autosave.py", line 116, in do_autosave
stack.autosave.autosave_all()
File "/home/yms23/.conda/envs/spyder4beta/lib/python3.7/site-packages/spyder/plugins/editor/utils/autosave.py", line 374, in autosave_all
self.maybe_autosave(index)
File "/home/yms23/.conda/envs/spyder4beta/lib/python3.7/site-packages/spyder/plugins/editor/utils/autosave.py", line 338, in maybe_autosave
autosave_hash = self.file_hashes[autosave_filename]
KeyError: '/home/yms23/.config/spyder-py3-dev/autosave/test_save.py'

# Fixed code
def write(text, filename, encoding="utf-8", mode="wb"):
    """
    Write 'text' to file ('filename') assuming 'encoding' in an atomic way
    Return (eventually new) encoding
    """
    text, encoding = encode(text, encoding)
    if "a" in mode:
        with open(filename, mode) as textfile:
            textfile.write(text)
    else:
        # Based in the solution at untitaker/python-atomicwrites#42.
        # Needed to fix file permissions overwritting.
        # See spyder-ide/spyder#9381.
        try:
            original_mode = os.stat(filename).st_mode
        except OSError:  # Change to FileNotFoundError for PY3
            # Creating a new file, emulate what os.open() does
            umask = os.umask(0)
            os.umask(umask)
            original_mode = 0o777 & ~umask
        try:
            with atomic_write(filename, overwrite=True, mode=mode) as textfile:
                textfile.write(text)
        except OSError as error:
            # Some filesystems don't support the option to sync directories
            # See untitaker/python-atomicwrites#17
            if error.errno != errno.EINVAL:
                with open(filename, mode) as textfile:
                    textfile.write(text)
        os.chmod(filename, original_mode)
    return encoding