[issue35216] misleading error message from shutil.copy()

2021-03-17 Thread Eryk Sun


Change by Eryk Sun :


--
resolution:  -> duplicate
stage:  -> resolved
status: open -> closed
superseder:  -> shutil.copy raises IsADirectoryError when the directory does 
not actually exist

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue35216] misleading error message from shutil.copy()

2018-11-13 Thread Eryk Sun


Eryk Sun  added the comment:

The EINVAL error in Windows also needs improvement, but I don't what can be 
done after the fact. If there's a trailing slash when opening or creating a 
regular file, the NtCreateFile system call returns STATUS_OBJECT_INVALID_NAME. 
The Windows API maps this to ERROR_INVALID_NAME (123), which the CRT in turn 
maps to EINVAL (22). This error is too generic to handle. Even if the name ends 
in a slash, the error could be due to some other invalid character in the path 
(e.g. a common mistake is a '\t' or '\n' in a string literal). 

The problem could be addressed beforehand in shutil.copy by manually raising an 
exception if isdir() is false and the name has a trailing slash.

--
nosy: +eryksun

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue35216] misleading error message from shutil.copy()

2018-11-12 Thread Julien Palard


Julien Palard  added the comment:

Using `cp` on Debian Buster I'm having a better error message:

$ touch foo
$ cp foo bar/
cp: failed to access 'bar/': Not a directory

>From copy.c (from Debian coreutils):

/* Improve quality of diagnostic when a nonexistent dst_name
   ends in a slash and open fails with errno == EISDIR.  */
if (dest_desc < 0 && dest_errno == EISDIR
&& *dst_name && dst_name[strlen (dst_name) - 1] == '/')
  dest_errno = ENOTDIR;

--
nosy: +mdk

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue35216] misleading error message from shutil.copy()

2018-11-12 Thread Stéphane Wirtel

Stéphane Wirtel  added the comment:

@vstinner & @serhiy

What do you think about this issue?

For the same POSIX syscall (open) we get 2 different values for the error, 2 on 
macOS and EISDIR with Linux.

Is a bug in Python or with the compliance of the operating system and the POSIX 
norm?

Thank you

--
nosy: +serhiy.storchaka, vstinner

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue35216] misleading error message from shutil.copy()

2018-11-12 Thread Stéphane Wirtel

Stéphane Wirtel  added the comment:

This error is specific to the C-API and not to Python,

here is an example.

#include 
#include 
#include 
#include 
#include 

int main(int argc, char **argv, char **environ) {
int fd;
fd = open("/tmp/toto/", O_CREAT);
printf("file descriptor: %d\n", fd);
printf(strerror(errno));
close(fd);
return 0;
}



./a.out
file descriptor: -1
Is a directory

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue35216] misleading error message from shutil.copy()

2018-11-12 Thread Stéphane Wirtel

Stéphane Wirtel  added the comment:

You have the issue with the built-in 'open' function.

--
nosy: +matrixise

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue35216] misleading error message from shutil.copy()

2018-11-12 Thread Karthikeyan Singaravelan


Karthikeyan Singaravelan  added the comment:

Agreed this is confusing. Is this a Linux specific error? Trying this on Mac 
gives me a different error code and exception.

# Mac

$ ./python.exe
Python 3.8.0a0 (heads/master:cd449806fa, Nov 12 2018, 09:51:24)
[Clang 7.0.2 (clang-700.1.81)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import shutil
>>> shutil.copy('/tmp/a.py', 'Lib12/')
Traceback (most recent call last):
  File "", line 1, in 
  File "/Users/karthikeyansingaravelan/stuff/python/cpython/Lib/shutil.py", 
line 385, in copy
copyfile(src, dst, follow_symlinks=follow_symlinks)
  File "/Users/karthikeyansingaravelan/stuff/python/cpython/Lib/shutil.py", 
line 240, in copyfile
with open(src, 'rb') as fsrc, open(dst, 'wb') as fdst:
FileNotFoundError: [Errno 2] No such file or directory: 'Lib12/'

# Ubuntu 

./python
Python 3.8.0a0 (heads/master:dce345c51a, Nov 12 2018, 13:01:05)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import shutil
>>> shutil.copy('/tmp/a.py', 'Lib12/')
Traceback (most recent call last):
  File "", line 1, in 
  File "/home/karthi/cpython/Lib/shutil.py", line 386, in copy
copyfile(src, dst, follow_symlinks=follow_symlinks)
  File "/home/karthi/cpython/Lib/shutil.py", line 241, in copyfile
with open(src, 'rb') as fsrc, open(dst, 'wb') as fdst:
IsADirectoryError: [Errno 21] Is a directory: 'Lib12/'

--
nosy: +xtreak

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue35216] misleading error message from shutil.copy()

2018-11-12 Thread Cédric Van Rompay

New submission from Cédric Van Rompay :

When calling `shutil.copy('file.txt', 'not_here/')`,
if directory `not_here/` does not exist,
the raised error is:

IsADirectoryError: [Errno 21] Is a directory: 'not_here/'

If the intent of the user was to copy a file in a directory
but the user forgot to create the destination directory,
this can be very misleading,
as the error tends to indicate that the directory exists.

It happened to me and I was thinking
"yes it's a directory, then what?
that's exactly what I want,copy to this directory!"
when the problem was that I forgot to create the destination directory.

I would suggest to catch the `IsADirectoryError`
in shutil.copyfile() (at `open(dst, 'wb')`)
and raise instead an error saying something like
"destination is a directory AND this directory does not exists".

--
components: Library (Lib)
messages: 329728
nosy: cedricvanrompay
priority: normal
severity: normal
status: open
title: misleading error message from shutil.copy()
type: behavior
versions: Python 3.6

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com