classification
Title: shutil.copy2 raises OSError if filesystem doesn't support chmod
Type: Stage: patch review
Components: Documentation, Library (Lib) Versions: Python 3.9, Python 3.8
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: docs@python Nosy List: Vojtěch Pachol, docs@python, eric.smith, matrixise, miss-islington
Priority: normal Keywords: patch

Created on 2016-03-02 09:21 by Vojtěch Pachol, last changed 2019-09-13 13:43 by miss-islington.

Pull Requests
URL Status Linked Edit
PR 13765 merged Windson Yang, 2019-06-03 02:13
PR 16102 merged miss-islington, 2019-09-13 13:36
Messages (6)
msg261100 - (view) Author: Vojtěch Pachol (Vojtěch Pachol) Date: 2016-03-02 09:21
`copy2` checks if `os` nodule has `chmod` and then it uses it without thinking. 

On filesystems that doesn't support `chmod` it ends with

    OSError: [Errno 95] Operation not supported: <filename>

which is not acceptable since documentation says "copy2() never returns failure".
msg261106 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2016-03-02 11:26
I agree that copy2 should not fail because chmod fails. Could you please provide the entire traceback message when it fails (on both 2.7 and 3.4 or 3.5)? And what OS are you running on, and what filesystem?

It looks like the error happens because errno=95 (EOPNOTSUPP, probably) isn't in the list of errors that _copyxattr catches, while (ENOTSUP=134 on Linux) is.

I think the documentation is also incorrect. Of course copy2 can fail: what about out of disk space? I think the documentation is trying to say that copy2 never fails because it can't preserve metadata. And the wording in the docs "copy2() never returns failure" is also odd, it should say "copy2() never raises an exception because it cannot preserve file metadata".
msg261110 - (view) Author: Vojtěch Pachol (Vojtěch Pachol) Date: 2016-03-02 12:33
Actually it is a remote fs mounted via samba. OS is Linux.

Tracebacks with Python versions I have:

$ python2.7
Python 2.7.11 (default, Jan 11 2016, 21:04:40) 
[GCC 5.3.1 20160101] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import shutil
>>> shutil.copy2("src", "dst")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/shutil.py", line 131, in copy2
    copystat(src, dst)
  File "/usr/lib/python2.7/shutil.py", line 100, in copystat
    os.chmod(dst, mode)
OSError: [Errno 95] Operation not supported: 'dst'
>>> 
$ python3.4
Python 3.4.4 (default, Jan  5 2016, 15:35:18) 
[GCC 5.3.1 20160101] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import shutil
>>> shutil.copy2("src", "dst")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.4/shutil.py", line 246, in copy2
    copystat(src, dst, follow_symlinks=follow_symlinks)
  File "/usr/lib/python3.4/shutil.py", line 191, in copystat
    lookup("chmod")(dst, mode, follow_symlinks=follow)
OSError: [Errno 95] Operation not supported: 'dst'
>>> 
$ python3.5
Python 3.5.1+ (default, Jan 13 2016, 15:09:18) 
[GCC 5.3.1 20160101] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import shutil
>>> shutil.copy2("src", "dst")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.5/shutil.py", line 252, in copy2
    copystat(src, dst, follow_symlinks=follow_symlinks)
  File "/usr/lib/python3.5/shutil.py", line 197, in copystat
    lookup("chmod")(dst, mode, follow_symlinks=follow)
OSError: [Errno 95] Operation not supported: 'dst'
>>>
msg352326 - (view) Author: Stéphane Wirtel (matrixise) * (Python committer) Date: 2019-09-13 13:36
New changeset 9585f46b97931d2640c3343dfe03aed15beb9fea by Stéphane Wirtel (Windson yang) in branch 'master':
bpo-26468: Doc: improve the documentation of shutil.copy2 when it can fail. (GH-13765)
https://github.com/python/cpython/commit/9585f46b97931d2640c3343dfe03aed15beb9fea
msg352328 - (view) Author: Stéphane Wirtel (matrixise) * (Python committer) Date: 2019-09-13 13:38
I have merged the PR with the change in the documentation, feel free to open a new PR for the other part of this issue.
msg352332 - (view) Author: miss-islington (miss-islington) Date: 2019-09-13 13:43
New changeset c27bcc3b476bbda7958724376189816c4e9d19c4 by Miss Islington (bot) in branch '3.8':
bpo-26468: Doc: improve the documentation of shutil.copy2 when it can fail. (GH-13765)
https://github.com/python/cpython/commit/c27bcc3b476bbda7958724376189816c4e9d19c4
History
Date User Action Args
2019-09-13 13:43:37miss-islingtonsetnosy: + miss-islington
messages: + msg352332
2019-09-13 13:38:27matrixisesetmessages: + msg352328
versions: + Python 3.8, Python 3.9, - Python 2.7, Python 3.4, Python 3.5
2019-09-13 13:36:25miss-islingtonsetpull_requests: + pull_request15721
2019-09-13 13:36:12matrixisesetnosy: + matrixise
messages: + msg352326
2019-06-28 22:26:15Jeffrey.Kintschersetnosy: - Jeffrey.Kintscher
2019-06-03 02:13:08Windson Yangsetkeywords: + patch
stage: patch review
pull_requests: + pull_request13651
2019-06-01 20:52:11Jeffrey.Kintschersetnosy: + Jeffrey.Kintscher
2016-03-02 12:33:26Vojtěch Pacholsetmessages: + msg261110
2016-03-02 11:26:37eric.smithsetnosy: + docs@python, eric.smith
messages: + msg261106

assignee: docs@python
components: + Documentation
2016-03-02 09:21:59Vojtěch Pacholcreate