Skip to content

Commit

Permalink
Merge pull request #122 from kurtmckee/fix-poetry-3545
Browse files Browse the repository at this point in the history
Always close the wheel tempfile after writing to it
  • Loading branch information
sdispater committed Jan 29, 2021
2 parents 8570e2e + 124a879 commit 4db39a6
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 12 deletions.
25 changes: 13 additions & 12 deletions poetry/core/masonry/builders/wheel.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,18 +78,19 @@ def build(self):
new_mode = normalize_file_permissions(st_mode)
os.chmod(temp_path, new_mode)

with zipfile.ZipFile(
os.fdopen(fd, "w+b"), mode="w", compression=zipfile.ZIP_DEFLATED
) as zip_file:
if not self._poetry.package.build_should_generate_setup():
self._build(zip_file)
self._copy_module(zip_file)
else:
self._copy_module(zip_file)
self._build(zip_file)

self._write_metadata(zip_file)
self._write_record(zip_file)
with os.fdopen(fd, "w+b") as fd_file:
with zipfile.ZipFile(
fd_file, mode="w", compression=zipfile.ZIP_DEFLATED
) as zip_file:
if not self._poetry.package.build_should_generate_setup():
self._build(zip_file)
self._copy_module(zip_file)
else:
self._copy_module(zip_file)
self._build(zip_file)

self._write_metadata(zip_file)
self._write_record(zip_file)

wheel_path = dist_dir / self.wheel_filename
if wheel_path.exists():
Expand Down
22 changes: 22 additions & 0 deletions tests/masonry/builders/test_wheel.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# -*- coding: utf-8 -*-
import os
import shutil
import zipfile

Expand Down Expand Up @@ -284,3 +285,24 @@ def test_default_src_with_excluded_data(mocker):
assert "my_package/data/data1.txt" in names
assert "my_package/data/sub_data/data2.txt" not in names
assert "my_package/data/sub_data/data3.txt" in names


def test_wheel_file_is_closed(monkeypatch):
"""Confirm that wheel zip files are explicitly closed."""

# Using a list is a hack for Python 2.7 compatibility.
fd_file = [None]

real_fdopen = os.fdopen

def capturing_fdopen(*args, **kwargs):
fd_file[0] = real_fdopen(*args, **kwargs)
return fd_file[0]

monkeypatch.setattr(os, "fdopen", capturing_fdopen)

module_path = fixtures_dir / "module1"
WheelBuilder.make(Factory().create_poetry(module_path))

assert fd_file[0] is not None
assert fd_file[0].closed

0 comments on commit 4db39a6

Please sign in to comment.