delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/2015/11/27/10:10:37

X-Recipient: archive-cygwin AT delorie DOT com
DomainKey-Signature: a=rsa-sha1; c=nofws; d=sourceware.org; h=list-id
:list-unsubscribe:list-subscribe:list-archive:list-post
:list-help:sender:mime-version:in-reply-to:references:from:date
:message-id:subject:to:content-type; q=dns; s=default; b=lwaauAg
JbxtgufRTcWgggzZAmZOIEeGbikotCCKP6UcZFs/ajf3Ck8pQDA60Nz0GJKbmC6z
F7YKGXtPFbYD02EnwR4BxzrefVNgUXF59sONEFYn64ZXvTfRO0IsnAX03+a9VjUN
dCrZ9KeBME1s84hGTbxsDiNL0xoHYkw/+kwA=
DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=sourceware.org; h=list-id
:list-unsubscribe:list-subscribe:list-archive:list-post
:list-help:sender:mime-version:in-reply-to:references:from:date
:message-id:subject:to:content-type; s=default; bh=X49CAx6fjIy72
W0pXvkp8G2Ub/o=; b=luMSTYeZ7esAKKYhHhT3hL7rK6HjNP+5YZN3FFPB5XDDf
e34k+eUb/JtOmXClNwlZFThlRxYjFhp2j/2dIzaUvAVspv9zr4taiz1fMlzibmtE
rsFAcQOgMKIDKtD7GKhxiCjFzJGcwPfbIx86U/0opH4Y2tFihj+TGWrAEmUkdY=
Mailing-List: contact cygwin-help AT cygwin DOT com; run by ezmlm
List-Id: <cygwin.cygwin.com>
List-Subscribe: <mailto:cygwin-subscribe AT cygwin DOT com>
List-Archive: <http://sourceware.org/ml/cygwin/>
List-Post: <mailto:cygwin AT cygwin DOT com>
List-Help: <mailto:cygwin-help AT cygwin DOT com>, <http://sourceware.org/ml/#faqs>
Sender: cygwin-owner AT cygwin DOT com
Mail-Followup-To: cygwin AT cygwin DOT com
Delivered-To: mailing list cygwin AT cygwin DOT com
Authentication-Results: sourceware.org; auth=none
X-Virus-Found: No
X-Spam-SWARE-Status: No, score=-1.2 required=5.0 tests=AWL,BAYES_00,FREEMAIL_FROM,RCVD_IN_DNSWL_LOW,SPF_PASS autolearn=ham version=3.3.2
X-HELO: mail-lf0-f54.google.com
X-Received: by 10.25.158.134 with SMTP id h128mr3803020lfe.53.1448637011740; Fri, 27 Nov 2015 07:10:11 -0800 (PST)
MIME-Version: 1.0
In-Reply-To: <20151127145105.GG14466@dinwoodie.org>
References: <20151127145105 DOT GG14466 AT dinwoodie DOT org>
From: Michael Wild <themiwi AT gmail DOT com>
Date: Fri, 27 Nov 2015 16:09:52 +0100
Message-ID: <CAALQ5r=CO+TwEMbtTfrgBb8BCZSWPe6XxZ6n8Att06e9dV-uRg@mail.gmail.com>
Subject: Re: Incorrect Python errors when using os.remove to delete a directory
To: The Cygwin Mailing List <cygwin AT cygwin DOT com>
X-IsSubscribed: yes

On Fri, Nov 27, 2015 at 3:51 PM, Adam Dinwoodie <adam AT dinwoodie DOT 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

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019