Skip to content

Commit

Permalink
Set sys.path in try/finally block with comment
Browse files Browse the repository at this point in the history
Per Nick Coghlan's suggestion on PR #1652, a try/finally block ensures
that the path is restored even in the event of an error.
  • Loading branch information
pganssle committed Jan 28, 2019
1 parent 87911c3 commit f1bb8db
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions setuptools/build_meta_legacy.py
Expand Up @@ -6,6 +6,7 @@
and will eventually be removed.
"""

import os
import sys

from setuptools.build_meta import _BuildMetaBackend
Expand All @@ -25,14 +26,21 @@ def run_setup(self, setup_script='setup.py'):
# In order to maintain compatibility with scripts assuming that
# the setup.py script is in a directory on the PYTHONPATH, inject
# '' into sys.path. (pypa/setuptools#1642)
sys_path = list(sys.path) # Save the old path
if '' not in sys.path:
sys.path.insert(0, '')

super(_BuildMetaLegacyBackend,
self).run_setup(setup_script=setup_script)

sys.path = sys_path # Restore the old path
sys_path = list(sys.path) # Save the original path

try:
if '' not in sys.path:
sys.path.insert(0, '')

super(_BuildMetaLegacyBackend,
self).run_setup(setup_script=setup_script)
finally:
# While PEP 517 frontends should be calling each hook in a fresh
# subprocess according to the standard (and thus it should not be
# strictly necessary to restore the old sys.path), we'll restore
# the original path so that the path manipulation does not persist
# within the hook after run_setup is called.
sys.path[:] = sys_path


_BACKEND = _BuildMetaLegacyBackend()
Expand Down

0 comments on commit f1bb8db

Please sign in to comment.