Repository: spyder-ide/spyder

Issue:
Spyder 4.0.1: Error when saving file to network drive (but saving worked)
## Problem Description

This is a weird one: after updating to Spyder 4.0.1 in Anaconda (for Linux) I get an error every time I save a file to a (samba) network drive: "Unable to save file filename.py. [Errno 1] Operation not permitted: filename.py"

The error message is not correct, however, because the file *is* saved.

I do not get this error when I save a file locally.

Also, I do not get this error when I downgrade spyder using "conda install spyder=3".

Finally, I tried "spyder --reset" but this did not help.

* Spyder version:  4.0.1
* Python version: 3.7.6
* Qt version: 5.9.7
* PyQt version: 5.9.2
* Operating System name/version: Ubuntu 18.04.3 LTS

### Dependencies

atomicwrites >=1.2.0         :  1.3.0 (OK)
chardet >=2.0.0              :  3.0.4 (OK)
cloudpickle >=0.5.0          :  1.2.2 (OK)
diff_match_patch >=20181111  :  20181111 (OK)
intervaltree                 :  None (OK)
IPython >=4.0                :  7.11.1 (OK)
jedi =0.14.1                 :  0.14.1 (OK)
keyring                      :  None (OK)
nbconvert >=4.0              :  5.6.1 (OK)
numpydoc >=0.6.0             :  0.9.2 (OK)
pexpect >=4.4.0              :  4.7.0 (OK)
pickleshare >=0.4            :  0.7.5 (OK)
psutil >=0.3                 :  5.6.7 (OK)
pygments >=2.0               :  2.5.2 (OK)
pylint >=0.25                :  2.4.4 (OK)
pyls >=0.31.2;<0.32.0        :  0.31.2 (OK)
xdg >=0.26                   :  0.26 (OK)
zmq >=17                     :  18.1.0 (OK)
qdarkstyle >=2.7             :  2.7 (OK)
qtawesome >=0.5.7            :  0.6.0 (OK)
qtconsole >=4.6.0            :  4.6.0 (OK)
qtpy >=1.5.0                 :  1.9.0 (OK)
rtree >=0.8.3                :  0.8.3 (OK)
sphinx >=0.6.6               :  2.3.1 (OK)
spyder_kernels >=1.8.1;<2.0.0:  1.8.1 (OK)
watchdog                     :  None (OK)
cython >=0.21                :  0.29.14 (OK)
matplotlib >=2.0.0           :  3.1.1 (OK)
numpy >=1.7                  :  1.17.4 (OK)
pandas >=0.13.1              :  0.25.3 (OK)
scipy >=0.17.0               :  1.3.2 (OK)
sympy >=0.7.3                :  1.5.1 (OK)


Discussion:
This reminds me of issue #10208.
@andfoy, please take a look at this one.

Resolving patch:
diff --git a/spyder/utils/encoding.py b/spyder/utils/encoding.py
--- a/spyder/utils/encoding.py
+++ b/spyder/utils/encoding.py
@@ -260,10 +260,15 @@ def write(text, filename, encoding='utf-8', mode='wb'):
             if error.errno != errno.EINVAL:
                 with open(filename, mode) as textfile:
                     textfile.write(text)
-        os.chmod(filename, original_mode)
-        file_stat = os.stat(filename)
-        # Preserve creation timestamps
-        os.utime(filename, (creation, file_stat.st_mtime))
+        try:
+            os.chmod(filename, original_mode)
+            file_stat = os.stat(filename)
+            # Preserve creation timestamps
+            os.utime(filename, (creation, file_stat.st_mtime))
+        except OSError:
+            # Prevent error when chmod/utime is not allowed
+            # See spyder-ide/spyder#11308
+            pass
     return encoding
 
 


Test patch: