This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: For Bug [ 490168 ] shutil.copy(path, pat
Type: Stage:
Components: Library (Lib) Versions: Python 2.3
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: rhettinger, tzot
Priority: normal Keywords: patch

Created on 2002-09-04 16:21 by tzot, last changed 2022-04-10 16:05 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
shutil.py.diff tzot, 2002-09-04 16:21 diff -c
Messages (4)
msg41103 - (view) Author: Χρήστος Γεωργίου (Christos Georgiou) (tzot) * Date: 2002-09-04 16:21
As the bug says, shutil.copy(src, dst) deletes the
contents of src if it is the same as dst.  Example:
$ pwd
/home/tzot
$ echo hello >file01
$ ln file01 file02
$ echo hello >file03
$ ln -s file03 file04
$ echo hello >file05
$ python
Python 2.3a0 (#26, Sep  4 2002, 17:39:22) [C] on irix646
Type "help", "copyright", "credits" or "license" for
more information.
>>> from shutil import copyfile
>>> copyfile('file01', 'file02')
>>> copyfile('file03', 'file04')
>>> copyfile('file05', '../tzot/file05')

After this sequence, all files have zero length.
The patch corrects this.
msg41104 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2002-09-08 03:54
Logged In: YES 
user_id=80475

What is this line for?

  if _src.st_ino>0<_dst.st_ino \
msg41105 - (view) Author: Χρήστος Γεωργίου (Christos Georgiou) (tzot) * Date: 2002-09-08 14:20
Logged In: YES 
user_id=539787

This is a check that is true only on POSIX systems AFAIK.
On windows systems, st_ino is reported as 0 for all files, so 
the files would falsely be presumed to be the same file and 
therefore not copied.

PS At least on NTFS, files do have an inode number (same 
concept)... there is even a CreateHardLink call in kernel32.dll, 
which can be used to create hard links; MS implemented this 
since NT 3.51 I think, in order to attain POSIX conformability; 
however, either python ignores that, or MS do not provide 
information in their stat and / or link simulated system calls.  
I will invest some time in getting acquainted to Windows 
programming so that I research into this and offer a patch to 
posixmodule.c.
If interested, see http://msdn.microsoft.com/library/en-
us/fileio/base/createhardlink.asp
msg41106 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2002-09-08 20:46
Logged In: YES 
user_id=80475

Added a simplified version of the patch to Py2.3.
See shutil.py 1.23

Am leaving the Posix piece out for several reasons:
-- I'm on Windows and can't verify the code
-- As patched, the code didn't work at all (it assumed that 
the destination existed and the call to stat fails if it doesn't)
-- Handling symbolic links is a somewhat special case.

Thanks for the patch.  It's great that you helped address 
one of the older pending bugs.

For the future, please send a cvs diff -c taken from the 
current version of the file.  Please test all cases (when src 
and dst are the same, when the same but spelled 
differently, and when they are different altogether).  Also, 
please remove the print statements so the patch can be 
applied directly.  Also, please post the patches directly to 
the original bug instead of opening a separate patch -- this 
simplifies tracking the discussion.
History
Date User Action Args
2022-04-10 16:05:39adminsetgithub: 37134
2002-09-04 16:21:20tzotcreate