Skip to content

Commit

Permalink
Make __file__ an absolute path in setuptools.build_meta (#3795)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
abravalheri committed Feb 1, 2023
2 parents 2ab03c7 + 4523ba3 commit de9cd7e
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
2 changes: 2 additions & 0 deletions changelog.d/3795.change.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Ensured that ``__file__`` is an absolute path when executing ``setup.py`` as
part of ``setuptools.build_meta``.
2 changes: 1 addition & 1 deletion setuptools/build_meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ def _get_build_requires(self, config_settings, requirements):
def run_setup(self, setup_script='setup.py'):
# Note that we can reuse our build directory between calls
# Correctness comes first, then optimization later
__file__ = setup_script
__file__ = os.path.abspath(setup_script)
__name__ = '__main__'

with _open_setup_script(__file__) as f:
Expand Down
18 changes: 18 additions & 0 deletions setuptools/tests/test_build_meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -820,6 +820,24 @@ def test_sys_argv_passthrough(self, tmpdir_cwd):
with pytest.raises(AssertionError):
build_backend.build_sdist("temp")

_setup_py_file_abspath = {
'setup.py': DALS("""
import os
assert os.path.isabs(__file__)
__import__('setuptools').setup(
name='foo',
version='0.0.0',
py_modules=['hello'],
setup_requires=['six'],
)
""")
}

def test_setup_py_file_abspath(self, tmpdir_cwd):
path.build(self._setup_py_file_abspath)
build_backend = self.get_build_backend()
build_backend.build_sdist("temp")

@pytest.mark.parametrize('build_hook', ('build_sdist', 'build_wheel'))
def test_build_with_empty_setuppy(self, build_backend, build_hook):
files = {'setup.py': ''}
Expand Down

0 comments on commit de9cd7e

Please sign in to comment.