Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/IPython/utils/syspathcontext.py: 43%

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

23 statements  

1from __future__ import annotations 

2 

3import sys 

4from types import TracebackType 

5from typing import Literal, Self 

6 

7 

8 

9class prepended_to_syspath: 

10 """A context for prepending a directory to sys.path for a second.""" 

11 

12 dir: str 

13 added: bool 

14 

15 def __init__(self, dir: str) -> None: 

16 self.dir = dir 

17 self.added = False 

18 

19 def __enter__(self) -> Self: 

20 if self.dir not in sys.path: 

21 sys.path.insert(0, self.dir) 

22 self.added = True 

23 else: 

24 self.added = False 

25 return self 

26 

27 def __exit__( 

28 self, 

29 exc_type: type[BaseException] | None, 

30 exc_val: BaseException | None, 

31 exc_tb: TracebackType | None, 

32 ) -> Literal[False]: 

33 if self.added: 

34 try: 

35 sys.path.remove(self.dir) 

36 except ValueError: 

37 pass 

38 # Returning False causes any exceptions to be re-raised. 

39 return False