public inbox for cygwin@cygwin.com
 help / color / mirror / Atom feed
From: Michael Wild <themiwi@gmail.com>
To: The Cygwin Mailing List <cygwin@cygwin.com>
Subject: Re: Incorrect Python errors when using os.remove to delete a directory
Date: Fri, 27 Nov 2015 15:20:00 -0000	[thread overview]
Message-ID: <CAALQ5r=CO+TwEMbtTfrgBb8BCZSWPe6XxZ6n8Att06e9dV-uRg@mail.gmail.com> (raw)
In-Reply-To: <20151127145105.GG14466@dinwoodie.org>

On Fri, Nov 27, 2015 at 3:51 PM, Adam Dinwoodie <adam@dinwoodie.org> wrote:
> If I use os.remove in Python to remove a directory, I expect it to fail
> with an OSError on Python2 or a IsADirectoryError on Python3.  On
> Python2, I get OSError, but with the wrong error code, whereas on
> Python3 I get completely the wrong exception.
>
> Simple testcases:
>
>     $ rm -rf testdir && mkdir testdir && python -c 'import os; os.remove("testdir")'
>     Traceback (most recent call last):
>       File "<string>", line 1, in <module>
>     OSError: [Errno 1] Operation not permitted: 'testdir'
>
>     $ rm -rf testdir && mkdir testdir && python3 -c 'import os; os.remove("testdir")'
>     Traceback (most recent call last):
>       File "<string>", line 1, in <module>
>     PermissionError: [Errno 1] Operation not permitted: 'testdir'
>
> On my handy CentOS box, I see the following instead:
>
>     $ rm -rf testdir && mkdir testdir && python -c 'import os; os.remove("testdir")'
>     Traceback (most recent call last):
>       File "<string>", line 1, in <module>
>     OSError: [Errno 21] Is a directory: 'testdir'
>
>     $ rm -rf testdir && mkdir testdir && python3 -c 'import os; os.remove("testdir")'
>     Traceback (most recent call last):
>       File "<string>", line 1, in <module>
>     IsADirectoryError: [Errno 21] Is a directory: 'testdir'
>
> Now I've realised what's going wrong I can work around it, but it'd be
> nice if the correct error were raised in the first place.

This is a bug in Python itself, and not Cygwin specific. At least I
get the same behavior with my Anaconda installation.

Not really a fix for the bug itself, but usually I consider it good
practice to first call os.isdir().

Cheers

--
Problem reports:       http://cygwin.com/problems.html
FAQ:                   http://cygwin.com/faq/
Documentation:         http://cygwin.com/docs.html
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple

  reply	other threads:[~2015-11-27 15:10 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-11-27 15:07 Adam Dinwoodie
2015-11-27 15:20 ` Michael Wild [this message]
2015-12-02 15:31   ` Adam Dinwoodie

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAALQ5r=CO+TwEMbtTfrgBb8BCZSWPe6XxZ6n8Att06e9dV-uRg@mail.gmail.com' \
    --to=themiwi@gmail.com \
    --cc=cygwin@cygwin.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).