[issue42796] tempfile doesn't seem to play nicely with os.chdir on Windows

Gabriele Tornetta report at bugs.python.org
Fri Jan 1 08:34:49 EST 2021


Gabriele Tornetta <phoenix1987 at gmail.com> added the comment:

That makes sense, but I wonder what the "right" behaviour should be in this case. Surely the infinite recursion should be fixed at the very minimum. Perhaps the code on Windows could be enhanced to catch the case whereby one is trying to delete the cwd and do something like chdir('..') and then delete the temp folder. However, I suspect that something like this still wouldn't be enough. For example, this works fine

~~~ python
def test_chdir():
    with tempfile.TemporaryDirectory() as tempdir:
        old = os.getcwd()
        os.chdir(tempdir)
        os.chdir(old)
~~~

whereas this doesn't (same stacktrace as the original case)

~~~ python
def test_chdir():
    with tempfile.TemporaryDirectory() as tempdir:
        old = os.getcwd()
        os.chdir(tempdir)
        with open(os.path.join(tempdir, "delme")) as fout:
            fout.write("Hello")
        os.chdir(old)
~~~

----------

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue42796>
_______________________________________


More information about the Python-bugs-list mailing list