Skip to content

Commit de9cd7e

Browse files
committed
Make __file__ an absolute path in setuptools.build_meta (#3795)
PR MESSAGE: This is a difference between pip's legacy wheel build (or direct invocations of setup.py) and the PEP 517 build. While setup.py scripts that rely on this are fragile, it was quite painful to debug! Since Python 3.4, __file__ is usually an absolute path, so this change might result in fewer surprises.
2 parents 2ab03c7 + 4523ba3 commit de9cd7e

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-1
lines changed

changelog.d/3795.change.rst

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Ensured that ``__file__`` is an absolute path when executing ``setup.py`` as
2+
part of ``setuptools.build_meta``.

setuptools/build_meta.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ def _get_build_requires(self, config_settings, requirements):
326326
def run_setup(self, setup_script='setup.py'):
327327
# Note that we can reuse our build directory between calls
328328
# Correctness comes first, then optimization later
329-
__file__ = setup_script
329+
__file__ = os.path.abspath(setup_script)
330330
__name__ = '__main__'
331331

332332
with _open_setup_script(__file__) as f:

setuptools/tests/test_build_meta.py

+18
Original file line numberDiff line numberDiff line change
@@ -820,6 +820,24 @@ def test_sys_argv_passthrough(self, tmpdir_cwd):
820820
with pytest.raises(AssertionError):
821821
build_backend.build_sdist("temp")
822822

823+
_setup_py_file_abspath = {
824+
'setup.py': DALS("""
825+
import os
826+
assert os.path.isabs(__file__)
827+
__import__('setuptools').setup(
828+
name='foo',
829+
version='0.0.0',
830+
py_modules=['hello'],
831+
setup_requires=['six'],
832+
)
833+
""")
834+
}
835+
836+
def test_setup_py_file_abspath(self, tmpdir_cwd):
837+
path.build(self._setup_py_file_abspath)
838+
build_backend = self.get_build_backend()
839+
build_backend.build_sdist("temp")
840+
823841
@pytest.mark.parametrize('build_hook', ('build_sdist', 'build_wheel'))
824842
def test_build_with_empty_setuppy(self, build_backend, build_hook):
825843
files = {'setup.py': ''}

0 commit comments

Comments
 (0)