Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: eryk sun Newsgroups: comp.lang.python Subject: Re: Remove directory tree without following symlinks Date: Sun, 24 Apr 2016 14:42:29 -0500 Lines: 57 Message-ID: References: <571a3ba2$0$1597$c3e8da3$5496439d@news.astraweb.com> <1461337766.365000.586700849.0DDBDB0B@webmail.messagingengine.com> <571a5be6$0$1590$c3e8da3$5496439d@news.astraweb.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de gVe/iiRVhe0QMPFUr+ptlQbHzmj4kNWNB3xrjxyUL2Bw== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'api.': 0.04; 'string.': 0.04; 'needed,': 0.05; 'paths': 0.05; 'url:pipermail': 0.05; "'a'": 0.07; 'dynamically': 0.07; 'incompatible': 0.07; 'prefix': 0.07; 'shutil': 0.07; 'wrapper': 0.07; 'api': 0.09; 'buffer,': 0.09; 'decodes': 0.09; 'filename,': 0.09; 'os.path': 0.09; 'typeerror:': 0.09; 'example:': 0.10; 'python': 0.10; 'syntax': 0.13; '2016': 0.16; '24,': 0.16; '[1].': 0.16; 'already,': 0.16; 'api,': 0.16; 'appends': 0.16; 'function).': 0.16; 'prefix,': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'subject:Remove': 0.16; 'wrote:': 0.16; 'byte': 0.18; '(in': 0.18; '>>>': 0.20; 'windows': 0.20; '"",': 0.22; 'am,': 0.23; '(or': 0.23; 'help.': 0.23; 'seems': 0.23; 'forgot': 0.23; '(most': 0.24; 'implemented': 0.24; 'header:In-Reply-To:1': 0.24; 'discussion': 0.24; 'message-id:@mail.gmail.com': 0.27; 'allocated': 0.27; 'function': 0.28; 'outlook': 0.28; 'ansi': 0.29; 'sentence': 0.29; 'str': 0.29; 'handled': 0.29; 'branch': 0.30; 'skip:[ 10': 0.31; 'implement': 0.32; '[1]': 0.32; 'generally': 0.32; 'possibly': 0.32; 'returned': 0.32; 'useful': 0.33; 'url:python': 0.33; 'common': 0.33; 'usually': 0.33; 'doubt': 0.33; 'though.': 0.33; 'traceback': 0.33; "skip:' 20": 0.34; 'file': 0.34; 'gets': 0.35; 'received:google.com': 0.35; 'files,': 0.35; 'label': 0.35; 'path': 0.35; 'protocol': 0.35; 'but': 0.36; 'needed': 0.36; 'there': 0.36; 'url:org': 0.36; 'received:209.85': 0.36; 'volume': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'really': 0.37; 'received:209.85.213': 0.37; "won't": 0.38; 'received:209': 0.38; 'skip:o 20': 0.38; 'url:mail': 0.40; 'to:addr:python.org': 0.40; 'believe': 0.66; 'forward': 0.66; 'subject': 0.70; '2.7.': 0.84; '[error': 0.84; 'subject:tree': 0.84; 'hand,': 0.97 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to; bh=SVr0PmuBBEaxdTqxz9KcHCz7YzqAg5bE8ocsDQ7v3Pg=; b=KtOGNIqQ6Q9+lAbgmynA2AMaKUBN7Fe5xtHFIAMoSGvDKzZignT/7EDb6Qj+TboqJc I2SLOtv6Dmhls8HfRd3h+a8xxY8jjSoXB6zgAu4dHPEmmtiCQmGmc6Z/+rkRoIwcLEMa KCnw45xUDlqLLXR2nX/EkU78jlsGdHQbfl867yv+kGJUuzahGdeXEr4EmcKrAHO46RWL c1jNqYkT6HkyVLOwlWeaFaiJDRNFW1+tRjaHw5BC7z1j/TvYTw3E+rj8PxAjOKBkIc40 g6Td4/06fpfpyyVX7kDRLPqlXGgh/h76JxGKIkWgQuAgpRh8Lpeu33B/2ou+bfe5tkw7 zTuQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to; bh=SVr0PmuBBEaxdTqxz9KcHCz7YzqAg5bE8ocsDQ7v3Pg=; b=HfD1v79ZLVTr2d63OUpg1b/IRypHPfSsAEj669NwcHEHymS1XmYbsWN9OXqYSv7RYn rVhokAKMGh/r6jd6qoOnMrRgzj5TwyOipDKg3kGsoY01w8W8+E6O2K2KSFk2bzHaGZr7 9cKS7Rm+c0ujt0g+Whcc07T9DizGOg9kAqs5By54L2EUTwL4p/3cnEpi8e61GKAav1HI Il0yedhjYC+v5FYgmhHDUTqqeuLtElT1IAcYVwe5yueQKxvVxBBGjRF69xDgZaqLMRhO GChUhP5Y6u3Py70mNOt/eeiy3l7Jtx54rxs8UzN2rPJGhmW1AR4QN+MTwzVZOoxNGfYi C7bw== X-Gm-Message-State: AOPr4FVDNm3N0q3Ou8nlSiRviC12Da57Ap6jMVRM0bE3Sh+bBJFaTVR32c2CtTK6Eqioe6e2wzrhFia6Lo81Jw== X-Received: by 10.50.13.74 with SMTP id f10mr8609821igc.55.1461526988986; Sun, 24 Apr 2016 12:43:08 -0700 (PDT) In-Reply-To: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Mailman-Original-Message-ID: X-Mailman-Original-References: <571a3ba2$0$1597$c3e8da3$5496439d@news.astraweb.com> <1461337766.365000.586700849.0DDBDB0B@webmail.messagingengine.com> <571a5be6$0$1590$c3e8da3$5496439d@news.astraweb.com> Xref: csiph.com comp.lang.python:107570 On Sun, Apr 24, 2016 at 5:42 AM, Albert-Jan Roskam wrote: > Aww, I kinda forgot about that already, but I came across this last > year [1]. Apparently, shutil.rmtree(very_long_path) failed under Win 7, > even with the "silly prefix". I believe very_long_path was a > Python2-str. > [1] > https://mail.python.org/pipermail/python-list/2015-June/693156.html Python 2's str branch of the os functions gets implemented on Windows using the [A]NSI API, such as FindFirstFileA and FindNextFileA to implement listdir(). Generally the ANSI API is a light wrapper around the [W]ide-character API. It simply decodes byte strings to UTF-16 and calls the wide-character function (or a common internal function). IIRC, in Windows 7, byte strings are decoded using a per-thread buffer with size MAX_PATH (260), so prefixing the path with "\\?\" won't help. You have to use the wide-character API. Windows 10, on the other hand, decodes using a dynamically allocated buffer, so you can usually get away with using a long byte string. But not with Python 2 os.listdir(), which uses a stack-allocated MAX_PATH+5 buffer in the str branch. For example: Python 2 os.mkdir works: >>> path = os.path.normpath('//?/C:/Temp/long/' + 'a' * 255) >>> os.makedirs(path) but os.listdir requires unicode: >>> os.listdir(path) Traceback (most recent call last): File "", line 1, in TypeError: must be (buffer overflow), not str >>> os.listdir(path.decode('mbcs')) [] Also, the str branch of listdir appends "/*.*", with a forward slash, so it's incompatible with the "\\?\" prefix, even for short paths: >>> os.listdir(r'\\?\C:\Temp') Traceback (most recent call last): File "", line 1, in WindowsError: [Error 123] The filename, directory name, or volume label syntax is incorrect: '\\\\?\\C:\\Temp/*.*' > It seems useful if shutil or os.path would automatically prefix paths > with "\\?\". It is rarely really needed, though. (in my case it was > needed to copy a bunch of MS Outlook .msg files, which automatically > get the subject line as the filename, and perhaps the first sentence > of the mail of the mail has no subject). I doubt a change like that would get backported to 2.7. Recently there was a lengthy discussion about adding an __fspath__ protocol to Python 3. Possibly this can be automatically handled in the __fspath__ implementation of pathlib.WindowsPath and the DirEntry type returned by os.scandir.