commit:     e85a8aa744e735d3bd04f7afe2b39e4ca15cb195
Author:     Brian Harring <ferringb <AT> gmail <DOT> com>
AuthorDate: Wed Dec 17 20:19:34 2025 +0000
Commit:     Brian Harring <ferringb <AT> gmail <DOT> com>
CommitDate: Wed Dec 17 20:23:18 2025 +0000
URL:        
https://gitweb.gentoo.org/proj/pkgcore/snakeoil.git/commit/?id=e85a8aa7

deprecate contexts.syspath.  This shouldn't be used.

For the runtime scenario, `import_module_from_path` is correct.

For tests, `python_namespace.protect_imports` is correct.  Which in
hindsight should've been put in tests to tell people to not use it
unless it's for tests...

Signed-off-by: Brian Harring <ferringb <AT> gmail.com>

 src/snakeoil/contexts.py | 13 +++++++------
 tests/test_contexts.py   |  1 +
 2 files changed, 8 insertions(+), 6 deletions(-)

diff --git a/src/snakeoil/contexts.py b/src/snakeoil/contexts.py
index b6d2231..2d00dba 100644
--- a/src/snakeoil/contexts.py
+++ b/src/snakeoil/contexts.py
@@ -16,6 +16,7 @@ from importlib import import_module
 from multiprocessing.connection import Pipe
 
 from snakeoil._internals import deprecated
+from snakeoil.python_namespaces import protect_imports
 
 from .cli.exceptions import UserException
 from .process import namespaces
@@ -368,6 +369,9 @@ def chdir(path: str) -> contextlib.chdir:
     return _contextlib_chdir(path)
 
 
+@deprecated(
+    "This is not threadsafe.  For runtime use 
`snakeoil.python_namespaces.import_module_from_path`.  *Strictly* for tests, 
use `protect_imports`."
+)
 @contextmanager
 def syspath(path: str, condition: bool = True, position: int = 0):
     """Context manager that mangles ``sys.path`` and then reverts on exit.
@@ -378,13 +382,10 @@ def syspath(path: str, condition: bool = True, position: 
int = 0):
     :param position: Optional integer that is the place where the path is 
inserted
         in ``sys.path``, defaults to prepending.
     """
-    syspath = sys.path[:]
-    if condition:
-        sys.path.insert(position, path)
-    try:
+    with protect_imports() as (paths, _):
+        if condition:
+            paths.insert(position, path)
         yield
-    finally:
-        sys.path = syspath
 
 
 @contextmanager

diff --git a/tests/test_contexts.py b/tests/test_contexts.py
index aa01edd..a25124e 100644
--- a/tests/test_contexts.py
+++ b/tests/test_contexts.py
@@ -22,6 +22,7 @@ def test_chdir(tmpdir):
     assert orig_cwd == os.getcwd()
 
 
[email protected]_deprecations()
 def test_syspath(tmpdir):
     orig_syspath = tuple(sys.path)
 

Reply via email to