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

Close IO objects #2005

Merged
merged 3 commits into from
Jun 12, 2023
Merged
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
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