Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/nbconvert/utils/_contextlib_chdir.py: 50%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

12 statements  

1"""Backport of Python 3.11's contextlib.chdir.""" 

2 

3import os 

4from contextlib import AbstractContextManager 

5 

6 

7class chdir(AbstractContextManager): # type:ignore[type-arg] 

8 """Non thread-safe context manager to change the current working directory.""" 

9 

10 def __init__(self, path): 

11 """Initialize the manager.""" 

12 self.path = path 

13 self._old_cwd = [] 

14 

15 def __enter__(self): 

16 """Enter the context.""" 

17 self._old_cwd.append(os.getcwd()) 

18 os.chdir(self.path) 

19 

20 def __exit__(self, *excinfo): 

21 """Exit the context.""" 

22 os.chdir(self._old_cwd.pop())