Skip to content

Commit

Permalink
ContextManagers: Add preserve_sys_path
Browse files Browse the repository at this point in the history
  • Loading branch information
AbdealiLoKo authored and sils committed May 26, 2015
1 parent df3c170 commit 9666394
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
10 changes: 10 additions & 0 deletions coalib/misc/ContextManagers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import os
from io import StringIO
import builtins
import copy



Expand Down Expand Up @@ -82,3 +83,12 @@ def generate_input(self, x=''):
yield input_generator
finally:
builtins.__dict__["input"] = _input


@contextmanager
def preserve_sys_path():
_path = copy.copy(sys.path)
try:
yield
finally:
sys.path = _path
11 changes: 10 additions & 1 deletion coalib/tests/misc/ContextManagersTest.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import copy
import unittest
import sys

sys.path.insert(0, ".")
from coalib.misc.ContextManagers import (suppress_stdout,
retrieve_stdout,
simulate_console_inputs)
simulate_console_inputs,
preserve_sys_path)


class ContextManagersTest(unittest.TestCase):
Expand Down Expand Up @@ -41,6 +43,13 @@ def test_simulate_console_inputs(self):
self.assertEqual(input(), 3)
self.assertEqual(generator.last_input, 3)

def test_preserve_sys_path(self):
old_sys_path = copy.copy(sys.path)
with preserve_sys_path():
sys.path = 5

self.assertEqual(old_sys_path, sys.path)


if __name__ == '__main__':
unittest.main(verbosity=2)

1 comment on commit 9666394

@sils
Copy link
Member

@sils sils commented on 9666394 May 26, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ack from me, would like to get another one since I co authored this commit.

Please sign in to comment.