MCPcopy Index your code
hub / github.com/ipython/ipython / prepended_to_syspath

Class prepended_to_syspath

IPython/utils/syspathcontext.py:10–40  ·  view source on GitHub ↗

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

Source from the content-addressed store, hash-verified

8
9
10class prepended_to_syspath:
11 """A context for prepending a directory to sys.path for a second."""
12
13 dir: str
14 added: bool
15
16 def __init__(self, dir: str) -> None:
17 self.dir = dir
18 self.added = False
19
20 def __enter__(self) -> Self:
21 if self.dir not in sys.path:
22 sys.path.insert(0, self.dir)
23 self.added = True
24 else:
25 self.added = False
26 return self
27
28 def __exit__(
29 self,
30 exc_type: type[BaseException] | None,
31 exc_val: BaseException | None,
32 exc_tb: TracebackType | None,
33 ) -> Literal[False]:
34 if self.added:
35 try:
36 sys.path.remove(self.dir)
37 except ValueError:
38 pass
39 # Returning False causes any exceptions to be re-raised.
40 return False

Callers 9

safe_execfileMethod · 0.90
safe_execfile_ipyMethod · 0.90
test_extension_loadingFunction · 0.90
test_extension_builtinsFunction · 0.90
test_deepreloadFunction · 0.90
test_lazy_magicsFunction · 0.90
test_changing_py_fileMethod · 0.90
test_nonascii_pathMethod · 0.90
test_iso8859_5Method · 0.90

Calls

no outgoing calls

Tested by 7

test_extension_loadingFunction · 0.72
test_extension_builtinsFunction · 0.72
test_deepreloadFunction · 0.72
test_lazy_magicsFunction · 0.72
test_changing_py_fileMethod · 0.72
test_nonascii_pathMethod · 0.72
test_iso8859_5Method · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…