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

Use better temporary files mechanism #7904

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 4 additions & 2 deletions src/pip/_internal/operations/install/wheel.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from base64 import urlsafe_b64encode
from itertools import starmap
from zipfile import ZipFile
from io import TextIOWrapper

from pip._vendor import pkg_resources
from pip._vendor.distlib.scripts import ScriptMaker
Expand All @@ -32,6 +33,7 @@
from pip._internal.utils.typing import MYPY_CHECK_RUNNING
from pip._internal.utils.unpacking import unpack_file
from pip._internal.utils.wheel import parse_wheel
from pip._internal.utils.filesystem import adjacent_tmp_file

if MYPY_CHECK_RUNNING:
from email.message import Message
Expand Down Expand Up @@ -574,13 +576,13 @@ def is_entrypoint_wrapper(name):
record = os.path.join(dest_info_dir, 'RECORD')
temp_record = os.path.join(dest_info_dir, 'RECORD.pip')
with open_for_csv(record, 'r') as record_in:
with open_for_csv(temp_record, 'w+') as record_out:
with adjacent_tmp_file(temp_record, mode='w+') as record_out:
reader = csv.reader(record_in)
outrows = get_csv_rows_for_installed(
reader, installed=installed, changed=changed,
generated=generated, lib_dir=lib_dir,
)
writer = csv.writer(record_out)
writer = csv.writer(TextIOWrapper(record_out))
# Sort to simplify testing.
for row in sorted_outrows(outrows):
writer.writerow(row)
Expand Down
7 changes: 4 additions & 3 deletions src/pip/_internal/utils/filesystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,13 @@ def is_socket(path):


@contextmanager
def adjacent_tmp_file(path):
# type: (str) -> Iterator[NamedTemporaryFileResult]
"""Given a path to a file, open a temp file next to it securely and ensure
def adjacent_tmp_file(path, mode='w+b'):
# type: (str,str) -> Iterator[NamedTemporaryFileResult]
"""Given a path and mode to a file, open a temp file next to it securely and ensure
it is written to disk after the context reaches its end.
"""
with NamedTemporaryFile(
mode=mode,
delete=False,
dir=os.path.dirname(path),
prefix=os.path.basename(path),
Expand Down