Skip to content

Commit d72cebe

Browse files
committed
Set sys.path in try/finally block with comment
Per Nick Coghlan's suggestion on PR pypa#1652, a try/finally block ensures that the path is restored even in the event of an error.
1 parent a310dd6 commit d72cebe

File tree

1 file changed

+16
-8
lines changed

1 file changed

+16
-8
lines changed

setuptools/build_meta_legacy.py

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
and will eventually be removed.
77
"""
88

9+
import os
910
import sys
1011

1112
from setuptools.build_meta import _BuildMetaBackend
@@ -25,14 +26,21 @@ def run_setup(self, setup_script='setup.py'):
2526
# In order to maintain compatibility with scripts assuming that
2627
# the setup.py script is in a directory on the PYTHONPATH, inject
2728
# '' into sys.path. (pypa/setuptools#1642)
28-
sys_path = list(sys.path) # Save the old path
29-
if '' not in sys.path:
30-
sys.path.insert(0, '')
31-
32-
super(_BuildMetaLegacyBackend,
33-
self).run_setup(setup_script=setup_script)
34-
35-
sys.path = sys_path # Restore the old path
29+
sys_path = list(sys.path) # Save the original path
30+
31+
try:
32+
if '' not in sys.path:
33+
sys.path.insert(0, '')
34+
35+
super(_BuildMetaLegacyBackend,
36+
self).run_setup(setup_script=setup_script)
37+
finally:
38+
# While PEP 517 frontends should be calling each hook in a fresh
39+
# subprocess according to the standard (and thus it should not be
40+
# strictly necessary to restore the old sys.path), we'll restore
41+
# the original path so that the path manipulation does not persist
42+
# within the hook after run_setup is called.
43+
sys.path[:] = sys_path
3644

3745

3846
_BACKEND = _BuildMetaLegacyBackend()

0 commit comments

Comments
 (0)