Skip to content

Commit

Permalink
Prevent infinite recursion with pip wheel with $TMPDIR in $PWD
Browse files Browse the repository at this point in the history
During a build of extension module within `pip wheel` the source directory is
recursively copied in a temporary directory.

See pypa#7555

When the temporary directory is inside the source directory
(for example by setting `TMPDIR=$PWD/tmp`) this caused an infinite recursion
that ended in:

    [Errno 36] File name too long

We prevent that buy never copying the target to the target in _copy_source_tree.

Fixes pypa#7872
  • Loading branch information
hroncok committed Mar 19, 2020
1 parent 104b665 commit 98aa09c
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 5 deletions.
1 change: 1 addition & 0 deletions news/7872.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Prevent an infinite recursion with ``pip wheel`` when ``$TMPDIR`` is within the source directory.
22 changes: 17 additions & 5 deletions src/pip/_internal/operations/prepare.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,13 +156,25 @@ def _copy2_ignoring_special_files(src, dest):

def _copy_source_tree(source, target):
# type: (str, str) -> None
target_abspath = os.path.abspath(target)
target_basename = os.path.basename(target_abspath)
target_dirname = os.path.dirname(target_abspath)

def ignore(d, names):
# type: (str, List[str]) -> List[str]
# Pulling in those directories can potentially be very slow,
# exclude the following directories if they appear in the top
# level dir (and only it).
# See discussion at https:/pypa/pip/pull/6770
return ['.tox', '.nox'] if d == source else []
skipped = [] # type: List[str]
if d == source:
# Pulling in those directories can potentially be very slow,
# exclude the following directories if they appear in the top
# level dir (and only it).
# See discussion at https:/pypa/pip/pull/6770
skipped += ['.tox', '.nox']
if os.path.abspath(d) == target_dirname:
# Prevent an infinite recursion if the target is in source.
# This can happen when TMPDIR is set to ${PWD}/...
# and we copy PWD to TMPDIR.
skipped += [target_basename]
return skipped

kwargs = dict(ignore=ignore, symlinks=True) # type: CopytreeKwargs

Expand Down
Empty file.
4 changes: 4 additions & 0 deletions tests/data/src/extension/setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from setuptools import Extension, setup

module = Extension('extension', sources=['extension.c'])
setup(name='extension', version='0.0.1', ext_modules = [module])
11 changes: 11 additions & 0 deletions tests/functional/test_wheel.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,17 @@ def test_pip_wheel_with_user_set_in_config(script, data, common_wheels):
assert "Successfully built withpyproject" in result.stdout, result.stdout


def test_pip_wheel_ext_module_with_tmpdir_inside(script, data, common_wheels):
tmpdir = data.src / 'extension/tmp'
tmpdir.mkdir()
script.environ['TMPDIR'] = str(tmpdir)
result = script.pip(
'wheel', data.src / 'extension',
'--no-index', '-f', common_wheels
)
assert "Successfully built extension" in result.stdout, result.stdout


@pytest.mark.network
def test_pep517_wheels_are_not_confused_with_other_files(script, tmpdir, data):
"""Check correct wheels are copied. (#6196)
Expand Down

0 comments on commit 98aa09c

Please sign in to comment.