Skip to content

Issues in py.path.local.make_numbered_dir() #3810

@fabioz

Description

@fabioz

Info:

Python 2.7.12 (default, Sep 20 2016, 14:51:55) [MSC v.1600 64 bit (AMD64)] on win32
>>> import pytest
>>> pytest.__version__
u'3.7.1'

I found a case where I had many files on the disk (which were always traversed) which were relying on py.path.local.make_numbered_dir().

One is that is that py.path.local was being passed a unicode on Python 2 and the API doesn't handle it properly (yet everything seems to work) and the other is that if the test generates one file with more than 256 chars in the path, the temp dir remains there forever.

The problem is with the API:

from py.path import local
local.make_numbered_dir(prefix=u'tmp-', rootdir=root_dir)

The main issue is that all fails were silent (i.e.: the old files removal didn't work and there was no acknowledgement of it because remove errors are completely swallowed -- and there were many files on my use case which were still traversed because it does a chmod first which traverses the tree -- in my case 1-2 seconds of my pytest startup were always here for any run I did).

Below is a test with the issues:

# coding: utf-8


def test_py_local_remove_unicode(tmpdir):
    import os.path
    import py
    import sys

    unicode_chars = 'áéíóú'
    path_with_unicode_chars = os.path.join(str(tmpdir), unicode_chars, unicode_chars)
    os.makedirs(path_with_unicode_chars)

    with open(os.path.join(path_with_unicode_chars, unicode_chars), 'w') as stream:
        stream.write(unicode_chars)

    path_as_unicode = os.path.dirname(path_with_unicode_chars).decode(sys.getfilesystemencoding())
    local_path = py.path.local(path_as_unicode)
    local_path.remove()
    assert not os.path.exists(path_with_unicode_chars)

    os.makedirs(path_with_unicode_chars)

    with open("\\\\?\\" + os.path.join(path_with_unicode_chars, 'a' * 250), 'w') as stream:
        stream.write(unicode_chars)
    local_path.remove()

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions