Skip to content

Commit b85c136

Browse files
authored
bpo-30108: Restore sys.path in test_site (#1197)
Add setUpModule() and tearDownModule() functions to test_site to save/restore sys.path at the module level to prevent warning if the user site directory is created, since site.addsitedir() modifies sys.path.
1 parent b4dc6af commit b85c136

1 file changed

Lines changed: 21 additions & 8 deletions

File tree

Lib/test/test_site.py

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,27 @@
2727

2828
import site
2929

30-
if site.ENABLE_USER_SITE and not os.path.isdir(site.USER_SITE):
31-
# need to add user site directory for tests
32-
try:
33-
os.makedirs(site.USER_SITE)
34-
site.addsitedir(site.USER_SITE)
35-
except PermissionError as exc:
36-
raise unittest.SkipTest('unable to create user site directory (%r): %s'
37-
% (site.USER_SITE, exc))
30+
31+
OLD_SYS_PATH = None
32+
33+
34+
def setUpModule():
35+
global OLD_SYS_PATH
36+
OLD_SYS_PATH = sys.path[:]
37+
38+
if site.ENABLE_USER_SITE and not os.path.isdir(site.USER_SITE):
39+
# need to add user site directory for tests
40+
try:
41+
os.makedirs(site.USER_SITE)
42+
# modify sys.path: will be restored by tearDownModule()
43+
site.addsitedir(site.USER_SITE)
44+
except PermissionError as exc:
45+
raise unittest.SkipTest('unable to create user site directory (%r): %s'
46+
% (site.USER_SITE, exc))
47+
48+
49+
def tearDownModule():
50+
sys.path[:] = OLD_SYS_PATH
3851

3952

4053
class HelperFunctionsTests(unittest.TestCase):

0 commit comments

Comments
 (0)