Re: Why doesn't os.remove work on directories?

2015-12-24 Thread Laurent Delacroix
On 23/12/15 05:29, Random832 wrote:
> 
> This is surprising to anyone accustomed to the POSIX C remove
> function, which can remove either files or directories.  Is there
> any known rationale for this decision?
> 

Hello, in POSIX C the remove() function is actually rmdir() when called
on a directory: http://bit.ly/1NE1rfZ

I'm not sure about the rationale behind raising OSError if the argument
is a directory though. Maybe the "There should be one-- and preferably
only one --obvious way to do it." philosophy explains it. :-)

Lau
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Why doesn't os.remove work on directories?

2015-12-23 Thread eryk sun
On Tue, Dec 22, 2015 at 10:29 PM, Random832  wrote:
>
> This is surprising to anyone accustomed to the POSIX C remove
> function, which can remove either files or directories.  Is there
> any known rationale for this decision?

Guido added os.remove as a synonym for os.unlink in version 1.4 (1996)
[1]. This is also mentioned in the NEWS for 1.4b1 [2].

Note that C99 only specifies the behavior for files, as opposed to the
extended unlink/rmdir that POSIX mandates [3].

7.19.4.1 The remove function
Synopsis
1
#include 
int remove(const char *filename);
Description
2
The remove function causes the file whose name is the string
pointed to by filename to be no longer accessible by that
name. A subsequent attempt to open that file using that name
will fail, unless it is created anew. If the file is open,
the behavior of the remove function is implementation-
defined.
Returns
3
The remove function returns zero if the operation succeeds,
nonzero if it fails.

For Windows, the CRTs remove() function doesn't implement the extended
POSIX behavior. It only calls DeleteFile, not RemoveDirectory. (In
contrast the native NtDeleteFile [4] works for both files and
directories, and relative to an open handle like unlinkat.)

[1]: https://hg.python.org/cpython/rev/9fa2228bb096
[2]: https://hg.python.org/cpython/file/v1.4/Misc/NEWS#l565
[3]: http://pubs.opengroup.org/onlinepubs/9699919799/functions/remove.html
[4]: https://msdn.microsoft.com/en-us/library/ff566435
-- 
https://mail.python.org/mailman/listinfo/python-list


Why doesn't os.remove work on directories?

2015-12-22 Thread Random832

This is surprising to anyone accustomed to the POSIX C remove
function, which can remove either files or directories.  Is there
any known rationale for this decision?

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Why doesn't os.remove work on directories?

2015-12-22 Thread Ben Finney
Random832  writes:

> This is surprising to anyone accustomed to the POSIX C remove
> function, which can remove either files or directories.  Is there
> any known rationale for this decision?

No, I don't know a rationale for implementing it this way.

I expect the explanation will be “mere historical accident”. My
evidence-free reconstruction of the events leading to the current state
of play:

1.  ‘os.unlink’ implemented, using C ‘unlink(3)’. Because ‘unlink(2)’
on a directory will cause an error, Python raises OSError for this.

2.  ‘os.remove’ implemented; “This is identical to the unlink() function
documented below.”.

3.  Backward compatibility concerns (existing code might depend on
‘os.remove’ raising OSError for a directory argument) justify
keeping the existing behaviour.

What you're looking for amounts to “why was ‘os.remove’ implemented as a
synonym of ‘unlink(3)’ instead of ‘remove(3)’?”.

I don't know why that behaviour was chosen, and I consider it a wart.

-- 
 \“… no testimony can be admitted which is contrary to reason; |
  `\   reason is founded on the evidence of our senses.” —Percy Bysshe |
_o__)Shelley, _The Necessity of Atheism_, 1811 |
Ben Finney

-- 
https://mail.python.org/mailman/listinfo/python-list