Skip to content

Commit

Permalink
chore: Close IO objects (#2005)
Browse files Browse the repository at this point in the history
* chore: close file handle

* chore: close package metadata IO objects

* chore: close file handle
  • Loading branch information
sanmai-NL authored Jun 12, 2023
1 parent 4b88a68 commit ebfb98c
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 4 deletions.
6 changes: 5 additions & 1 deletion src/pdm/cli/commands/publish/package.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@


def parse_metadata(fp: IO[bytes]) -> email.message.Message:
return email.message_from_file(io.TextIOWrapper(fp, encoding="utf-8", errors="surrogateescape"))
"""
Note that this function will close fp. See https://github.com/python/cpython/issues/65562.
"""
with io.TextIOWrapper(fp, encoding="utf-8", errors="surrogateescape") as file:
return email.message_from_file(file)


@dataclass
Expand Down
3 changes: 2 additions & 1 deletion src/pdm/models/working_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ def find_distributions(cls, context: im.DistributionFinder.Context = default_con
meta_finder = im.MetadataPathFinder()
for link in found_links:
name = link.stem
link_pointer = Path(link.open("rb").readline().decode().strip())
with link.open("rb") as file_link:
link_pointer = Path(file_link.readline().decode().strip())
dist = next(
iter(
meta_finder.find_distributions(im.DistributionFinder.Context(name=name, path=[str(link_pointer)]))
Expand Down
5 changes: 3 additions & 2 deletions tests/models/test_setup_parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,10 @@ def test_parse_setup_cfg(content, result, tmp_path):
Setup("foo", "0.1.0", ["click", "requests"], {"tui": ["rich"]}, ">=3.6"),
),
(
"""from setuptools import setup
"""from pathlib import Path
from setuptools import setup
version = open('__version__.py').read().strip()
version = Path('__version__.py').read_text().strip()
setup(name="foo", version=version)
""",
Expand Down

0 comments on commit ebfb98c

Please sign in to comment.