Skip to content

Commit 9c86e0e

Browse files
committed
Inject '' into sys.path in PEP 517 implementation
This maintains backward compatibility to non-isolated build semantics, where it is common to import the module in setup.py.
1 parent 9b777b7 commit 9c86e0e

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-0
lines changed

changelog.d/1642.change.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
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`.

setuptools/build_meta.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,15 @@ def _run_setup(setup_script='setup.py'):
8282
f = getattr(tokenize, 'open', open)(__file__)
8383
code = f.read().replace('\\r\\n', '\\n')
8484
f.close()
85+
86+
# Execute setup.py. The current directory is added into sys.path to emulate
87+
# the behavior when setup.py is run globally (i.e. no PEP 517 isolation)
88+
# to maintain backward compatibility. (pypa/setuptools#1642)
89+
sys_path = sys.path
90+
if '' not in sys.path:
91+
sys.path.insert(0, '')
8592
exec(compile(code, __file__, 'exec'), locals())
93+
sys.path = sys_path
8694

8795

8896
def _fix_config(config_settings):

setuptools/tests/test_build_meta.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,3 +236,22 @@ def test_build_sdist_builds_targz_even_if_zip_indicated(tmpdir_cwd):
236236

237237
build_files(files)
238238
build_sdist("temp")
239+
240+
241+
def test_build_sdist_with_pwd_in_sys_path(tmpdir_cwd):
242+
files = {
243+
'setup.py': DALS("""
244+
__import__('setuptools').setup(
245+
name='foo',
246+
version=__import__('hello').__version__,
247+
py_modules=['hello']
248+
)"""),
249+
'hello.py': '__version__ = "0.0.0"',
250+
'setup.cfg': DALS("""
251+
[sdist]
252+
formats=zip
253+
""")
254+
}
255+
256+
build_files(files)
257+
build_sdist("temp")

0 commit comments

Comments
 (0)