From fe7e9a122e1efbddcbcadf703a0f2ec721eb01b3 Mon Sep 17 00:00:00 2001 From: Sander Maijers <3374183+sanmai-NL@users.noreply.github.com> Date: Mon, 12 Jun 2023 10:10:17 +0200 Subject: [PATCH 1/3] chore: close file handle --- src/pdm/models/working_set.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/pdm/models/working_set.py b/src/pdm/models/working_set.py index e8f524dae4..68f9656f77 100644 --- a/src/pdm/models/working_set.py +++ b/src/pdm/models/working_set.py @@ -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)])) From 6a6c847a8822a93b7c880923e3be358758142d2a Mon Sep 17 00:00:00 2001 From: Sander Maijers <3374183+sanmai-NL@users.noreply.github.com> Date: Mon, 12 Jun 2023 10:48:25 +0200 Subject: [PATCH 2/3] chore: close package metadata IO objects --- src/pdm/cli/commands/publish/package.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/pdm/cli/commands/publish/package.py b/src/pdm/cli/commands/publish/package.py index 0eca3e0d5d..6e537ed554 100644 --- a/src/pdm/cli/commands/publish/package.py +++ b/src/pdm/cli/commands/publish/package.py @@ -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 From f546a89cb0db17aa926533830709d4ae49dc165c Mon Sep 17 00:00:00 2001 From: Sander Maijers <3374183+sanmai-NL@users.noreply.github.com> Date: Mon, 12 Jun 2023 10:48:47 +0200 Subject: [PATCH 3/3] chore: close file handle --- tests/models/test_setup_parsing.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tests/models/test_setup_parsing.py b/tests/models/test_setup_parsing.py index 0d8d4693b1..aced77e859 100644 --- a/tests/models/test_setup_parsing.py +++ b/tests/models/test_setup_parsing.py @@ -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) """,