Skip to content

Commit

Permalink
fix: close response files correctly
Browse files Browse the repository at this point in the history
Signed-off-by: Frost Ming <[email protected]>
  • Loading branch information
frostming committed Aug 3, 2023
1 parent f0aea16 commit b3ee827
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 18 deletions.
6 changes: 3 additions & 3 deletions src/unearth/evaluator.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,10 +257,10 @@ def evaluate_package(


def _get_hash(link: Link, hash_name: str, session: Session) -> str:
resp = session.get(link.normalized, stream=True)
hasher = hashlib.new(hash_name)
for chunk in resp.iter_content(chunk_size=1024 * 8):
hasher.update(chunk)
with session.get(link.normalized, stream=True) as resp:
for chunk in resp.iter_content(chunk_size=1024 * 8):
hasher.update(chunk)
digest = hasher.hexdigest()
if not link.hashes:
link.hashes = {}
Expand Down
30 changes: 15 additions & 15 deletions src/unearth/preparer.py
Original file line number Diff line number Diff line change
Expand Up @@ -297,21 +297,21 @@ def unpack_link(
# A remote artfiact link, check the download dir first
artifact = download_dir / link.filename
if not _check_downloaded(artifact, hashes):
resp = session.get(link.normalized, stream=True)
try:
resp.raise_for_status()
except HTTPError as e:
raise UnpackError(f"Download failed: {e}") from None
if getattr(resp, "from_cache", False):
logger.info("Using cached %s", link)
else:
size = format_size(resp.headers.get("Content-Length", ""))
logger.info("Downloading %s (%s)", link, size)
with artifact.open("wb") as f:
for chunk in resp.iter_content(chunk_size=READ_CHUNK_SIZE):
if chunk:
validator.update(chunk)
f.write(chunk)
with session.get(link.normalized, stream=True) as resp:
try:
resp.raise_for_status()
except HTTPError as e:
raise UnpackError(f"Download failed: {e}") from None
if getattr(resp, "from_cache", False):
logger.info("Using cached %s", link)
else:
size = format_size(resp.headers.get("Content-Length", ""))
logger.info("Downloading %s (%s)", link, size)
with artifact.open("wb") as f:
for chunk in resp.iter_content(chunk_size=READ_CHUNK_SIZE):
if chunk:
validator.update(chunk)
f.write(chunk)
validator.validate()
if link.is_wheel:
if link.is_file:
Expand Down

0 comments on commit b3ee827

Please sign in to comment.