Skip to content

Commit

Permalink
Inject '' into sys.path in PEP 517 implementation
Browse files Browse the repository at this point in the history
This maintains backward compatibility to non-isolated build semantics,
where it is common to import the module in setup.py.
  • Loading branch information
uranusjr committed Jan 25, 2019
1 parent 9b777b7 commit 9c86e0e
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
1 change: 1 addition & 0 deletions changelog.d/1642.change.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The current working directory is injected into `sys.path` when executing `setup.py`, maintaining compatibility to non-isolated projects where the module is imported in `setup.py`.
8 changes: 8 additions & 0 deletions setuptools/build_meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,15 @@ def _run_setup(setup_script='setup.py'):
f = getattr(tokenize, 'open', open)(__file__)
code = f.read().replace('\\r\\n', '\\n')
f.close()

# Execute setup.py. The current directory is added into sys.path to emulate
# the behavior when setup.py is run globally (i.e. no PEP 517 isolation)
# to maintain backward compatibility. (pypa/setuptools#1642)
sys_path = sys.path
if '' not in sys.path:
sys.path.insert(0, '')
exec(compile(code, __file__, 'exec'), locals())
sys.path = sys_path


def _fix_config(config_settings):
Expand Down
19 changes: 19 additions & 0 deletions setuptools/tests/test_build_meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,3 +236,22 @@ def test_build_sdist_builds_targz_even_if_zip_indicated(tmpdir_cwd):

build_files(files)
build_sdist("temp")


def test_build_sdist_with_pwd_in_sys_path(tmpdir_cwd):
files = {
'setup.py': DALS("""
__import__('setuptools').setup(
name='foo',
version=__import__('hello').__version__,
py_modules=['hello']
)"""),
'hello.py': '__version__ = "0.0.0"',
'setup.cfg': DALS("""
[sdist]
formats=zip
""")
}

build_files(files)
build_sdist("temp")

0 comments on commit 9c86e0e

Please sign in to comment.