Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

prefer stdlib TemporaryDirectory #7680

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/poetry/inspection/info.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import tempfile

from pathlib import Path
from tempfile import TemporaryDirectory
from typing import TYPE_CHECKING
from typing import Any
from typing import Mapping
Expand All @@ -21,7 +22,6 @@
from poetry.core.packages.package import Package
from poetry.core.pyproject.toml import PyProjectTOML
from poetry.core.utils.helpers import parse_requires
from poetry.core.utils.helpers import temporary_directory
from poetry.core.version.markers import InvalidMarker
from poetry.core.version.requirements import InvalidRequirement

Expand Down Expand Up @@ -316,7 +316,7 @@ def _from_sdist_file(cls, path: Path) -> PackageInfo:
elif not zip:
suffix = ".tar.gz"

with temporary_directory() as tmp_str:
with TemporaryDirectory(ignore_cleanup_errors=True) as tmp_str:
tmp = Path(tmp_str)
extractall(source=path, dest=tmp, zip=zip)

Expand Down
4 changes: 2 additions & 2 deletions src/poetry/installation/chef.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
import tempfile

from pathlib import Path
from tempfile import TemporaryDirectory
from typing import TYPE_CHECKING

from build import BuildBackendException
from poetry.core.utils.helpers import temporary_directory

from poetry.utils._compat import decode
from poetry.utils.helpers import extractall
Expand Down Expand Up @@ -87,7 +87,7 @@
suffix = archive.suffix
zip = suffix == ".zip"

with temporary_directory() as tmp_dir:
with TemporaryDirectory(ignore_cleanup_errors=True) as tmp_dir:

Check failure on line 90 in src/poetry/installation/chef.py

View check run for this annotation

Cirrus CI / Tests / FreeBSD (Python 3.9) / pytest

src/poetry/installation/chef.py#L90

tests.installation.test_executor.test_other_error
Raw output
config = <tests.conftest.Config object at 0x3efba6ac0820>
pool = <poetry.repositories.repository_pool.RepositoryPool object at 0x3efba2c31a30>
io = <cleo.io.buffered_io.BufferedIO object at 0x3efba7a5bf40>
env = MockEnv("/tmp/pytest-of-root/pytest-0/popen-gw1/test_other_error0/.venv")
fixture_dir = <function fixture_dir.<locals>._fixture_dir at 0x3efba41809d0>

    def test_other_error(
        config: Config,
        pool: RepositoryPool,
        io: BufferedIO,
        env: MockEnv,
        fixture_dir: FixtureDirGetter,
    ) -> None:
        io.set_verbosity(Verbosity.NORMAL)
    
        executor = Executor(env, pool, config, io)
    
        package_name = "simple-project"
        package_version = "1.2.3"
        directory_package = Package(
            package_name,
            package_version,
            source_type="directory",
            source_url=fixture_dir("non-existing").resolve().as_posix(),
        )
    
        return_code = executor.execute([Install(directory_package)])
    
        assert return_code == 1
    
        package_url = directory_package.source_url
        expected_start = f"""\
    Package operations: 1 install, 0 updates, 0 removals
    
      - Installing {package_name} ({package_version} {package_url})
    
      FileNotFoundError
    """
        expected_end = "Cannot install simple-project."
    
        output = io.fetch_output().strip()
>       assert output.startswith(expected_start)
E       assert False
E        +  where False = <built-in method startswith of str object at 0x3efba2914900>('Package operations: 1 install, 0 updates, 0 removals\n\n  - Installing simple-project (1.2.3 /tmp/cirrus-ci-build/tests/fixtures/non-existing)\n\n  FileNotFoundError\n')
E        +    where <built-in method startswith of str object at 0x3efba2914900> = 'Package operations: 1 install, 0 updates, 0 removals\n\n  - Installing simple-project (1.2.3 /tmp/cirrus-ci-build/tests/fixtures/non-existing)\n\n  TypeError\n\n  __init__() got an unexpected keyword argument \'ignore_cleanup_errors\'\n\n  at /tmp/cirrus-ci-build/src/poetry/installation/chef.py:90 in _prepare_sdist\n       86│ \n       87│         suffix = archive.suffix\n       88│         zip = suffix == ".zip"\n       89│ \n    →  90│         with TemporaryDirectory(ignore_cleanup_errors=True) as tmp_dir:\n       91│             archive_dir = Path(tmp_dir)\n       92│             extractall(source=archive, dest=archive_dir, zip=zip)\n       93│ \n       94│             elements = list(archive_dir.glob("*"))\n\nCannot install simple-project.'.startswith

/tmp/cirrus-ci-build/tests/installation/test_executor.py:1469: AssertionError

Check failure on line 90 in src/poetry/installation/chef.py

View check run for this annotation

Cirrus CI / Tests / FreeBSD (Python 3.8) / pytest

src/poetry/installation/chef.py#L90

tests.installation.test_executor.test_other_error
Raw output
config = <tests.conftest.Config object at 0xfb9eb114520>
pool = <poetry.repositories.repository_pool.RepositoryPool object at 0xfb9ef8c6f10>
io = <cleo.io.buffered_io.BufferedIO object at 0xfb9ef8c6d60>
env = MockEnv("/tmp/pytest-of-root/pytest-0/popen-gw1/test_other_error0/.venv")
fixture_dir = <function fixture_dir.<locals>._fixture_dir at 0xfb9e6ce60d0>

    def test_other_error(
        config: Config,
        pool: RepositoryPool,
        io: BufferedIO,
        env: MockEnv,
        fixture_dir: FixtureDirGetter,
    ) -> None:
        io.set_verbosity(Verbosity.NORMAL)
    
        executor = Executor(env, pool, config, io)
    
        package_name = "simple-project"
        package_version = "1.2.3"
        directory_package = Package(
            package_name,
            package_version,
            source_type="directory",
            source_url=fixture_dir("non-existing").resolve().as_posix(),
        )
    
        return_code = executor.execute([Install(directory_package)])
    
        assert return_code == 1
    
        package_url = directory_package.source_url
        expected_start = f"""\
    Package operations: 1 install, 0 updates, 0 removals
    
      - Installing {package_name} ({package_version} {package_url})
    
      FileNotFoundError
    """
        expected_end = "Cannot install simple-project."
    
        output = io.fetch_output().strip()
>       assert output.startswith(expected_start)
E       assert False
E        +  where False = <built-in method startswith of str object at 0xfb9eb449700>('Package operations: 1 install, 0 updates, 0 removals\n\n  - Installing simple-project (1.2.3 /tmp/cirrus-ci-build/tests/fixtures/non-existing)\n\n  FileNotFoundError\n')
E        +    where <built-in method startswith of str object at 0xfb9eb449700> = 'Package operations: 1 install, 0 updates, 0 removals\n\n  - Installing simple-project (1.2.3 /tmp/cirrus-ci-build/tests/fixtures/non-existing)\n\n  TypeError\n\n  __init__() got an unexpected keyword argument \'ignore_cleanup_errors\'\n\n  at /tmp/cirrus-ci-build/src/poetry/installation/chef.py:90 in _prepare_sdist\n       86│ \n       87│         suffix = archive.suffix\n       88│         zip = suffix == ".zip"\n       89│ \n    →  90│         with TemporaryDirectory(ignore_cleanup_errors=True) as tmp_dir:\n       91│             archive_dir = Path(tmp_dir)\n       92│             extractall(source=archive, dest=archive_dir, zip=zip)\n       93│ \n       94│             elements = list(archive_dir.glob("*"))\n\nCannot install simple-project.'.startswith

/tmp/cirrus-ci-build/tests/installation/test_executor.py:1469: AssertionError
archive_dir = Path(tmp_dir)
extractall(source=archive, dest=archive_dir, zip=zip)

Expand Down
5 changes: 3 additions & 2 deletions src/poetry/repositories/http_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from contextlib import contextmanager
from contextlib import suppress
from pathlib import Path
from tempfile import TemporaryDirectory
from typing import TYPE_CHECKING
from typing import Any
from typing import Iterator
Expand All @@ -16,7 +17,7 @@
from packaging.metadata import parse_email
from poetry.core.constraints.version import parse_constraint
from poetry.core.packages.dependency import Dependency
from poetry.core.utils.helpers import temporary_directory
from poetry.core.packages.utils.link import Link
from poetry.core.version.markers import parse_marker

from poetry.config.config import Config
Expand Down Expand Up @@ -103,7 +104,7 @@ def _cached_or_downloaded_file(
self, link: Link, *, raise_accepts_ranges: bool = False
) -> Iterator[Path]:
self._log(f"Downloading: {link.url}", level="debug")
with temporary_directory() as temp_dir:
with TemporaryDirectory(ignore_cleanup_errors=True) as temp_dir:
filepath = Path(temp_dir) / link.filename
self._download(
link.url, filepath, raise_accepts_ranges=raise_accepts_ranges
Expand Down
5 changes: 2 additions & 3 deletions src/poetry/utils/env/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@

from contextlib import contextmanager
from pathlib import Path
from tempfile import TemporaryDirectory
from typing import TYPE_CHECKING

from poetry.core.utils.helpers import temporary_directory

from poetry.utils.env.base_env import Env
from poetry.utils.env.env_manager import EnvManager
from poetry.utils.env.exceptions import EnvCommandError
Expand Down Expand Up @@ -44,7 +43,7 @@ def ephemeral_environment(
executable: Path | None = None,
flags: dict[str, str | bool] | None = None,
) -> Iterator[VirtualEnv]:
with temporary_directory() as tmp_dir:
with TemporaryDirectory(ignore_cleanup_errors=True) as tmp_dir:
# TODO: cache PEP 517 build environment corresponding to each project venv
venv_dir = Path(tmp_dir) / ".venv"
EnvManager.build_venv(
Expand Down
10 changes: 6 additions & 4 deletions tests/packages/test_locker.py
Original file line number Diff line number Diff line change
Expand Up @@ -1120,11 +1120,13 @@

See https://github.com/python-poetry/poetry/issues/5849
"""
with tempfile.TemporaryDirectory() as d1:
with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as d1:

Check failure on line 1123 in tests/packages/test_locker.py

View workflow job for this annotation

GitHub Actions / Ubuntu (Python 3.9) / pytest

test_lock_file_resolves_file_url_symlinks TypeError: __init__() got an unexpected keyword argument 'ignore_cleanup_errors'

Check failure on line 1123 in tests/packages/test_locker.py

View workflow job for this annotation

GitHub Actions / macOS (Python 3.9) / pytest

test_lock_file_resolves_file_url_symlinks TypeError: __init__() got an unexpected keyword argument 'ignore_cleanup_errors'

Check failure on line 1123 in tests/packages/test_locker.py

View check run for this annotation

Cirrus CI / Tests / FreeBSD (Python 3.9) / pytest

tests/packages/test_locker.py#L1123

tests.packages.test_locker.test_lock_file_resolves_file_url_symlinks
Raw output
root = Package('root', '1.2.3')

    def test_lock_file_resolves_file_url_symlinks(root: ProjectPackage) -> None:
        """
        Create directories and file structure as follows:
    
        d1/
        d1/testsymlink -> d1/d2/d3
        d1/d2/d3/lock_file
        d1/d4/source_file
    
        Using the testsymlink as the Locker.lock file path should correctly resolve to
        the real physical path of the source_file when calculating the relative path
        from the lock_file, i.e. "../../d4/source_file" instead of the unresolved path
        from the symlink itself which would have been "../d4/source_file"
    
        See https://github.com/python-poetry/poetry/issues/5849
        """
>       with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as d1:
E       TypeError: __init__() got an unexpected keyword argument 'ignore_cleanup_errors'

/tmp/cirrus-ci-build/tests/packages/test_locker.py:1123: TypeError

Check failure on line 1123 in tests/packages/test_locker.py

View check run for this annotation

Cirrus CI / Tests / FreeBSD (Python 3.8) / pytest

tests/packages/test_locker.py#L1123

tests.packages.test_locker.test_lock_file_resolves_file_url_symlinks
Raw output
root = Package('root', '1.2.3')

    def test_lock_file_resolves_file_url_symlinks(root: ProjectPackage) -> None:
        """
        Create directories and file structure as follows:
    
        d1/
        d1/testsymlink -> d1/d2/d3
        d1/d2/d3/lock_file
        d1/d4/source_file
    
        Using the testsymlink as the Locker.lock file path should correctly resolve to
        the real physical path of the source_file when calculating the relative path
        from the lock_file, i.e. "../../d4/source_file" instead of the unresolved path
        from the symlink itself which would have been "../d4/source_file"
    
        See https://github.com/python-poetry/poetry/issues/5849
        """
>       with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as d1:
E       TypeError: __init__() got an unexpected keyword argument 'ignore_cleanup_errors'

/tmp/cirrus-ci-build/tests/packages/test_locker.py:1123: TypeError
symlink_path = Path(d1).joinpath("testsymlink")
with tempfile.TemporaryDirectory(dir=d1) as d2, tempfile.TemporaryDirectory(
dir=d1
) as d4, tempfile.TemporaryDirectory(dir=d2) as d3, tempfile.NamedTemporaryFile(
with tempfile.TemporaryDirectory(
dir=d1, ignore_cleanup_errors=True
) as d2, tempfile.TemporaryDirectory(dir=d1) as d4, tempfile.TemporaryDirectory(
dir=d2, ignore_cleanup_errors=True
) as d3, tempfile.NamedTemporaryFile(
dir=d4
) as source_file, tempfile.NamedTemporaryFile(dir=d3) as lock_file:
lock_file.close()
Expand Down
Loading