This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: os.chdir in unittest's test case causes RecursionError on Windows
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.9
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: eryksun, serhiy.storchaka, tos-kamiya
Priority: normal Keywords:

Created on 2021-12-05 14:25 by tos-kamiya, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
test_chdir_fails.py tos-kamiya, 2021-12-05 14:25
FF2dfQ_acAc497b.png tos-kamiya, 2021-12-05 23:03
test_chdir_wo_fail.py tos-kamiya, 2021-12-05 23:18
Messages (6)
msg407711 - (view) Author: Toshihiro Kamiya (tos-kamiya) Date: 2021-12-05 14:25
When I was writing unit tests, I noticed that os.chdir in the unittest test case failed with RecursionError.

It seems to work fine on Ubuntu 20.04, but fails on Windows 10 (might fail on macOS).

The attached file is a minimal code to reproduce this.

When I run it, I got an error message as follows

```
PS C:\Users\toshihiro\tmp> python .\test_chdir_fails.py
Traceback (most recent call last):
  File "C:\Users\toshihiro\tmp\test_chdir_fails.py", line 11, in <module>
    unittest.main()
  File "C:\Users\toshihiro\AppData\Local\Programs\Python\Python39\lib\unittest\main.py", line 101, in __init__
    self.runTests()
(snip)
  File "C:\Users\toshihiro\AppData\Local\Programs\Python\Python39\lib\traceback.py", line 493, in __init__
    context = TracebackException(
  [Previous line repeated 487 more times]
RecursionError: maximum recursion depth exceeded
PS C:\Users\toshihiro\tmp>
```

The version of python was:
Python 3.9.5 (tags/v3.9.5:0a7dcbd, May  3 2021, 17:27:52) [MSC v.1928 64 bit (AMD64)] on win32

Regards,
msg407732 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2021-12-05 19:10
What is (snip)?
msg407735 - (view) Author: Eryk Sun (eryksun) * (Python triager) Date: 2021-12-05 20:05
Windows opens the working directory without delete sharing, so trying to delete it fails with a PermissionError for ERROR_SHARING_VIOLATION (winerror 32).

I discussed a modified version of the _rmtree() method in msg377358. The proposed version would try to reset the permissions in the first pass, recur once, and then give up because the permission error cannot be handled.
msg407754 - (view) Author: Toshihiro Kamiya (tos-kamiya) Date: 2021-12-05 23:03
Sorry, the "(snip)" means that it was omitted because it was too long.
I' ll attach a screenshot.
msg407757 - (view) Author: Toshihiro Kamiya (tos-kamiya) Date: 2021-12-05 23:18
Thank you, eryksun, for pointing out the cause of the problem. I have modified the test script and it can now be run on Windows.

The attached file is a revised version of the reproduction script, which reflects the modifications made to the test script to prevent the error.
msg407761 - (view) Author: Toshihiro Kamiya (tos-kamiya) Date: 2021-12-05 23:51
I think it may be difficult to understand the error message that deleting a directory causes "RecursionError or stack overflow", though.

I hope the issue reported as msg377358 will be resolved in some way.
History
Date User Action Args
2022-04-11 14:59:53adminsetgithub: 90145
2021-12-05 23:51:21tos-kamiyasetmessages: + msg407761
2021-12-05 23:18:44tos-kamiyasetstatus: open -> closed
files: + test_chdir_wo_fail.py
messages: + msg407757

resolution: not a bug
stage: resolved
2021-12-05 23:03:56tos-kamiyasetfiles: + FF2dfQ_acAc497b.png

messages: + msg407754
2021-12-05 20:05:14eryksunsetnosy: + eryksun
messages: + msg407735
2021-12-05 19:10:45serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg407732
2021-12-05 17:14:45AlexWaygoodsettype: crash -> behavior
2021-12-05 14:25:09tos-kamiyacreate