From 1633b98e22aa64d61082902f756ff05e90d5bcdc Mon Sep 17 00:00:00 2001 From: Kazik Pogoda Date: Wed, 11 Nov 2026 23:45:36 +0000 Subject: [PATCH 1/3] Constrain imported distributions without top_level.txt also on Python 3.10 (#79) packages_distributions of Python 3.10 tells the modules of a distribution only by top_level.txt, which e.g. hatchling and flit do not write, so the top-level packages and single-file modules are told by RECORD. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01FJmHQ3jHRbVdpwUiNUkApG --- src/main/kotlin/PythonDependencies.kt | 26 ++++++++- src/test/kotlin/PythonDependenciesTest.kt | 66 +++++++++++++++++++++-- 2 files changed, 87 insertions(+), 5 deletions(-) diff --git a/src/main/kotlin/PythonDependencies.kt b/src/main/kotlin/PythonDependencies.kt index a342d87..1a9b1ef 100644 --- a/src/main/kotlin/PythonDependencies.kt +++ b/src/main/kotlin/PythonDependencies.kt @@ -413,7 +413,7 @@ internal object PythonDependencies { # while the ones of DIST_INFOS above, of the standard library, cannot request = json.loads(sys.stdin.read()) - import base64, csv, hashlib, importlib, importlib.metadata as metadata + import base64, csv, hashlib, importlib, importlib.machinery, importlib.metadata as metadata from pathlib import Path from urllib.parse import parse_qsl, urlsplit, urlunsplit @@ -629,6 +629,28 @@ internal object PythonDependencies { return file_digests(path, expected) == expected return not requested and is_unmodified_since_uv_install(path, distribution) + def inferred_top_level(distribution): + # the top-level modules of a distribution without top_level.txt, told by its recorded files, + # as packages_distributions of Python 3.10 tells them only by top_level.txt, + # which e.g. hatchling and flit do not write: + # the package directories and the single-file modules, like demo/ or demo.py, + # but not e.g. ../../bin/ of pip install --target, the dist-info, the data, __pycache__ or a .pth file + suffixes = sorted(importlib.machinery.all_suffixes(), key=len, reverse=True) + names = set() + for file in distribution.files or []: + parts = file.parts + if len(parts) > 1: + name = parts[0] + else: + name = next((parts[0][:-len(suffix)] for suffix in suffixes if parts[0].endswith(suffix)), "") + if name.isidentifier() and name != "__pycache__": + names.add(name) + return names + + def is_imported_without_top_level(distribution): + # what packages_distributions of Python 3.11+ already tells by the recorded files + return distribution.read_text("top_level.txt") is None and not imported.isdisjoint(inferred_top_level(distribution)) + # the URL requirements whose marker applies, collected by is_missing url_dependencies = set() missing = [dependency for dependency in dependencies if is_missing(dependency)] @@ -642,7 +664,7 @@ internal object PythonDependencies { for distribution in metadata.distributions(): name = distribution.metadata["Name"] in_dependencies = os.path.realpath(distribution.locate_file("")) == dependency_location - if name and name not in constraints and (not in_dependencies or name in in_use): + if name and name not in constraints and (not in_dependencies or name in in_use or is_imported_without_top_level(distribution)): if in_dependencies and len(dependency_dist_infos.get(normalized_name(name), [])) > 1: # not the version of a dist-info left by a replaced version distribution = imported_distribution(name, distribution) diff --git a/src/test/kotlin/PythonDependenciesTest.kt b/src/test/kotlin/PythonDependenciesTest.kt index 0fb6178..59d1133 100644 --- a/src/test/kotlin/PythonDependenciesTest.kt +++ b/src/test/kotlin/PythonDependenciesTest.kt @@ -1244,6 +1244,64 @@ class PythonDependenciesTest { } } + @Test + fun `should constrain imported package of distribution without top_level_txt`() { + // given + // like a wheel built by hatchling or flit, which Python 3.10 tells the modules of only by top_level.txt + withDistInfos { directory -> + val modules = mapOf( + "replaced_probe/__init__.py" to "X = 1\n", + "../../bin/replaced-probe" to "" + ) + directory.resolve("replaced_probe/__init__.py").createParentDirectories().writeText("X = 1\n") + writeDistInfo(directory, "1.0", modules, topLevel = false) + + // when + val constraints = importedConstraints(directory) + + // then + assert(constraints == listOf("replaced-probe==1.0")) + } + } + + @Test + fun `should constrain imported single-file module of distribution without top_level_txt`() { + // given + withDistInfos { directory -> + val modules = mapOf("replaced_probe.py" to "X = 1\n") + modules.forEach { (path, content) -> directory.resolve(path).writeText(content) } + writeDistInfo(directory, "1.0", modules, topLevel = false) + + // when + val constraints = importedConstraints(directory) + + // then + assert(constraints == listOf("replaced-probe==1.0")) + } + } + + @Test + fun `should not constrain distribution without top_level_txt of modules not imported`() { + // given + // its .pth file, scripts and data, and files outside the directory are no modules + withDistInfos { directory -> + val files = mapOf( + "other_probe/__init__.py" to "X = 1\n", + "replaced_probe.pth" to "", + "replaced_probe-1.0.data/scripts/replaced_probe" to "", + "../../bin/replaced_probe" to "" + ) + directory.resolve("other_probe/__init__.py").createParentDirectories().writeText("X = 1\n") + writeDistInfo(directory, "1.0", files, topLevel = false) + + // when + val constraints = importedConstraints(directory) + + // then + assert(constraints.isEmpty()) + } + } + @Test fun `should remove dist-info of other version by name when identities are unknown`() { withDistInfos { directory -> @@ -1484,14 +1542,16 @@ class PythonDependenciesTest { directory: Path, version: String, files: Map = emptyMap(), - name: String = "replaced-probe" + name: String = "replaced-probe", + topLevel: Boolean = true ) { val distInfo = "${name.replace('-', '_')}-$version.dist-info" val metadata = mapOf( - "$distInfo/METADATA" to "Metadata-Version: 2.1\nName: $name\nVersion: $version\n", + "$distInfo/METADATA" to "Metadata-Version: 2.1\nName: $name\nVersion: $version\n" + ) + if (topLevel) mapOf( // as importlib.metadata.packages_distributions of Python 3.10 tells the modules only by it "$distInfo/top_level.txt" to "${name.replace('-', '_')}\n" - ) + ) else emptyMap() metadata.forEach { (path, content) -> directory.resolve(path).createParentDirectories().writeText(content) } directory.resolve("$distInfo/RECORD").writeText(record(files + metadata, distInfo)) } From e31ab043ebdcdb4040d3350f682b19c4f6e343d2 Mon Sep 17 00:00:00 2001 From: Kazik Pogoda Date: Fri, 13 Nov 2026 09:19:40 +0000 Subject: [PATCH 2/3] Tell top-level modules by an empty top_level.txt and a malformed RECORD too (#79) The imported distributions are told by their top-level modules the same way on every Python, not by packages_distributions, which tells them only by top_level.txt on Python 3.10, only of the .py files on 3.11, and fails on a malformed RECORD: by top_level.txt, otherwise, also when it is empty, by the recorded files, and none of a malformed RECORD. imported_files tells the top-level module of a recorded path by the same rule, with the module suffixes sorted once, the longest first. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01FJmHQ3jHRbVdpwUiNUkApG --- src/main/kotlin/PythonDependencies.kt | 59 +++++++++------- src/test/kotlin/PythonDependenciesTest.kt | 83 ++++++++++++++++++++--- 2 files changed, 106 insertions(+), 36 deletions(-) diff --git a/src/main/kotlin/PythonDependencies.kt b/src/main/kotlin/PythonDependencies.kt index 1a9b1ef..501b6d5 100644 --- a/src/main/kotlin/PythonDependencies.kt +++ b/src/main/kotlin/PythonDependencies.kt @@ -485,6 +485,33 @@ internal object PythonDependencies { return None return distribution + # the suffixes of the module files, the longest first, like .cpython-310-x86_64-linux-gnu.so before .so + module_suffixes = sorted(importlib.machinery.all_suffixes(), key=len, reverse=True) + + def top_level_name(path): + # the top-level module of a path recorded in RECORD, + # like demo of demo/__init__.py, demo.libs/ of auditwheel, demo.py or demo.cpython-310-x86_64-linux-gnu.so, + # or None of e.g. ../../bin/ of pip install --target, the dist-info, the data, __pycache__ or a .pth file + first, separator, _ = path.partition("/") + if separator: + name = first.partition(".")[0] + else: + name = next((first[:-len(suffix)] for suffix in module_suffixes if first.endswith(suffix)), "") + return name if name.isidentifier() and name != "__pycache__" else None + + def top_level(distribution): + # the top-level modules of the distribution, told by top_level.txt, otherwise by the recorded files, + # as e.g. hatchling and flit do not write it: + # packages_distributions of importlib.metadata tells them only by top_level.txt on Python 3.10, + # only of the .py files on 3.11, and fails on a malformed RECORD + names = set((distribution.read_text("top_level.txt") or "").split()) + if names: + return names + try: + return {top_level_name(str(file)) for file in distribution.files or []} - {None} + except (OSError, ValueError, csv.Error): + return set() + def imported_files(distribution): # the number of the recorded files of the imported top-level modules of the distribution, # e.g. demo/__init__.py or demo.py of the imported demo, if they are all on disk with their recorded hashes; @@ -495,7 +522,7 @@ internal object PythonDependencies { (row[0].split("/"), row[1].partition("=")) for row in csv.reader((distribution.read_text("RECORD") or "").splitlines()) if len(row) > 1 and row[1] and not row[0].endswith(".pyc") - and row[0].split("/")[0].partition(".")[0] in imported + and top_level_name(row[0]) in imported ] for parts, (algorithm, _, value) in files: if algorithm not in hashlib.algorithms_guaranteed or algorithm.startswith("shake_"): @@ -629,28 +656,6 @@ internal object PythonDependencies { return file_digests(path, expected) == expected return not requested and is_unmodified_since_uv_install(path, distribution) - def inferred_top_level(distribution): - # the top-level modules of a distribution without top_level.txt, told by its recorded files, - # as packages_distributions of Python 3.10 tells them only by top_level.txt, - # which e.g. hatchling and flit do not write: - # the package directories and the single-file modules, like demo/ or demo.py, - # but not e.g. ../../bin/ of pip install --target, the dist-info, the data, __pycache__ or a .pth file - suffixes = sorted(importlib.machinery.all_suffixes(), key=len, reverse=True) - names = set() - for file in distribution.files or []: - parts = file.parts - if len(parts) > 1: - name = parts[0] - else: - name = next((parts[0][:-len(suffix)] for suffix in suffixes if parts[0].endswith(suffix)), "") - if name.isidentifier() and name != "__pycache__": - names.add(name) - return names - - def is_imported_without_top_level(distribution): - # what packages_distributions of Python 3.11+ already tells by the recorded files - return distribution.read_text("top_level.txt") is None and not imported.isdisjoint(inferred_top_level(distribution)) - # the URL requirements whose marker applies, collected by is_missing url_dependencies = set() missing = [dependency for dependency in dependencies if is_missing(dependency)] @@ -658,13 +663,15 @@ internal object PythonDependencies { # pip resolves the missing ones without the dependency directory, so it would install a distribution # required by them from the index over the one installed from a URL, also in the same version missing = [dependency for dependency in dependencies if dependency in missing or dependency in url_dependencies] - distributions_of = metadata.packages_distributions() - in_use = {name for module in imported for name in distributions_of.get(module, [])} + in_use = { + distribution.metadata["Name"] for distribution in metadata.distributions() + if not imported.isdisjoint(top_level(distribution)) + } constraints = {} for distribution in metadata.distributions(): name = distribution.metadata["Name"] in_dependencies = os.path.realpath(distribution.locate_file("")) == dependency_location - if name and name not in constraints and (not in_dependencies or name in in_use or is_imported_without_top_level(distribution)): + if name and name not in constraints and (not in_dependencies or name in in_use): if in_dependencies and len(dependency_dist_infos.get(normalized_name(name), [])) > 1: # not the version of a dist-info left by a replaced version distribution = imported_distribution(name, distribution) diff --git a/src/test/kotlin/PythonDependenciesTest.kt b/src/test/kotlin/PythonDependenciesTest.kt index 59d1133..bb15362 100644 --- a/src/test/kotlin/PythonDependenciesTest.kt +++ b/src/test/kotlin/PythonDependenciesTest.kt @@ -1254,7 +1254,7 @@ class PythonDependenciesTest { "../../bin/replaced-probe" to "" ) directory.resolve("replaced_probe/__init__.py").createParentDirectories().writeText("X = 1\n") - writeDistInfo(directory, "1.0", modules, topLevel = false) + writeDistInfo(directory, "1.0", modules, topLevel = null) // when val constraints = importedConstraints(directory) @@ -1270,7 +1270,7 @@ class PythonDependenciesTest { withDistInfos { directory -> val modules = mapOf("replaced_probe.py" to "X = 1\n") modules.forEach { (path, content) -> directory.resolve(path).writeText(content) } - writeDistInfo(directory, "1.0", modules, topLevel = false) + writeDistInfo(directory, "1.0", modules, topLevel = null) // when val constraints = importedConstraints(directory) @@ -1292,7 +1292,60 @@ class PythonDependenciesTest { "../../bin/replaced_probe" to "" ) directory.resolve("other_probe/__init__.py").createParentDirectories().writeText("X = 1\n") - writeDistInfo(directory, "1.0", files, topLevel = false) + writeDistInfo(directory, "1.0", files, topLevel = null) + + // when + val constraints = importedConstraints(directory) + + // then + assert(constraints.isEmpty()) + } + } + + @Test + fun `should constrain imported extension module of distribution without top_level_txt`() { + // given + // like replaced_probe.cpython-310-x86_64-linux-gnu.so, which ends also with the shorter suffix .so + withDistInfos { directory -> + val modules = mapOf("replaced_probe${extensionSuffix()}" to "") + modules.forEach { (path, content) -> directory.resolve(path).writeText(content) } + writeDistInfo(directory, "1.0", modules, topLevel = null) + + // when + val constraints = importedConstraints(directory) + + // then + assert(constraints == listOf("replaced-probe==1.0")) + } + } + + @Test + fun `should constrain imported package of distribution with empty top_level_txt`() { + // given + // like Python 3.11+, which tells the modules of such a distribution by its recorded files + withDistInfos { directory -> + val modules = mapOf("replaced_probe/__init__.py" to "X = 1\n") + directory.resolve("replaced_probe/__init__.py").createParentDirectories().writeText("X = 1\n") + writeDistInfo(directory, "1.0", modules, topLevel = "") + + // when + val constraints = importedConstraints(directory) + + // then + assert(constraints == listOf("replaced-probe==1.0")) + } + } + + @Test + fun `should not constrain distribution without top_level_txt of malformed RECORD`() { + // given + // its file size is not a number + withDistInfos { directory -> + directory.resolve("replaced_probe/__init__.py").createParentDirectories().writeText("X = 1\n") + writeDistInfo( + directory, "1.0", topLevel = null, + record = "replaced_probe/__init__.py,sha256=x,notanint\nreplaced_probe-1.0.dist-info/RECORD,,\n" + ) // when val constraints = importedConstraints(directory) @@ -1536,24 +1589,34 @@ class PythonDependenciesTest { /** * Writes the dist-info directory of the [name] [version] into the [directory], - * with a `RECORD` of the [files] with the hashes of the given content, whatever the [directory] holds. + * with a `RECORD` of the [files] with the hashes of the given content, whatever the [directory] holds, + * unless the [record] is given, and with the [topLevel] content of `top_level.txt`, unless it is `null`. */ private fun writeDistInfo( directory: Path, version: String, files: Map = emptyMap(), name: String = "replaced-probe", - topLevel: Boolean = true + // as importlib.metadata.packages_distributions of Python 3.10 tells the modules only by it + topLevel: String? = "${name.replace('-', '_')}\n", + record: String? = null ) { val distInfo = "${name.replace('-', '_')}-$version.dist-info" val metadata = mapOf( "$distInfo/METADATA" to "Metadata-Version: 2.1\nName: $name\nVersion: $version\n" - ) + if (topLevel) mapOf( - // as importlib.metadata.packages_distributions of Python 3.10 tells the modules only by it - "$distInfo/top_level.txt" to "${name.replace('-', '_')}\n" - ) else emptyMap() + ) + if (topLevel != null) mapOf("$distInfo/top_level.txt" to topLevel) else emptyMap() metadata.forEach { (path, content) -> directory.resolve(path).createParentDirectories().writeText(content) } - directory.resolve("$distInfo/RECORD").writeText(record(files + metadata, distInfo)) + directory.resolve("$distInfo/RECORD").writeText(record ?: record(files + metadata, distInfo)) + } + + /** The suffix of the extension modules of the Python of the tests, like `.cpython-310-x86_64-linux-gnu.so`. */ + private fun extensionSuffix(): String { + val process = ProcessBuilder( + PythonEnvironment.executable, "-c", "import importlib.machinery; print(importlib.machinery.EXTENSION_SUFFIXES[0])" + ).redirectError(ProcessBuilder.Redirect.INHERIT).start() + val suffix = process.inputStream.bufferedReader().readText().trim() + check(process.waitFor() == 0 && suffix.isNotEmpty()) { "No extension suffix: $suffix" } + return suffix } private fun Path.distInfoNames(): List = From ce72d10196161bdb202b639affd31e93fdca3c17 Mon Sep 17 00:00:00 2001 From: Kazik Pogoda Date: Fri, 13 Nov 2026 09:30:05 +0000 Subject: [PATCH 3/3] Tell top-level modules by the RECORD rows as they are, only in the dependency directory (#79) Distribution.files fails with TypeError on a RECORD with a blank line or an extra column, and with ValueError on a size which is not a number, so the top-level modules of a distribution without top_level.txt are told by the csv rows of RECORD, like imported_files does, and a top_level.txt not in UTF-8 counts as missing. A directory with a dot in its name, like demo.libs/, is no top-level module of it, while imported_files still counts its files as of demo. They are told only of the distributions in the dependency directory, so that one of the base environment cannot fail the plan. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01FJmHQ3jHRbVdpwUiNUkApG --- src/main/kotlin/PythonDependencies.kt | 35 ++++++---- src/test/kotlin/PythonDependenciesTest.kt | 85 +++++++++++++++++++++-- 2 files changed, 103 insertions(+), 17 deletions(-) diff --git a/src/main/kotlin/PythonDependencies.kt b/src/main/kotlin/PythonDependencies.kt index 501b6d5..6b2e737 100644 --- a/src/main/kotlin/PythonDependencies.kt +++ b/src/main/kotlin/PythonDependencies.kt @@ -488,13 +488,14 @@ internal object PythonDependencies { # the suffixes of the module files, the longest first, like .cpython-310-x86_64-linux-gnu.so before .so module_suffixes = sorted(importlib.machinery.all_suffixes(), key=len, reverse=True) - def top_level_name(path): + def top_level_name(path, vendored=False): # the top-level module of a path recorded in RECORD, - # like demo of demo/__init__.py, demo.libs/ of auditwheel, demo.py or demo.cpython-310-x86_64-linux-gnu.so, + # like demo of demo/__init__.py, demo.py or demo.cpython-310-x86_64-linux-gnu.so, + # also of demo.libs/ of auditwheel, if vendored, as the demo module loads the shared libraries in it, # or None of e.g. ../../bin/ of pip install --target, the dist-info, the data, __pycache__ or a .pth file first, separator, _ = path.partition("/") if separator: - name = first.partition(".")[0] + name = first.partition(".")[0] if vendored else first else: name = next((first[:-len(suffix)] for suffix in module_suffixes if first.endswith(suffix)), "") return name if name.isidentifier() and name != "__pycache__" else None @@ -503,15 +504,29 @@ internal object PythonDependencies { # the top-level modules of the distribution, told by top_level.txt, otherwise by the recorded files, # as e.g. hatchling and flit do not write it: # packages_distributions of importlib.metadata tells them only by top_level.txt on Python 3.10, - # only of the .py files on 3.11, and fails on a malformed RECORD - names = set((distribution.read_text("top_level.txt") or "").split()) + # only of the .py files on 3.11, and Distribution.files fails on a malformed RECORD, + # e.g. of a row with a blank line, an extra column or a size which is not a number, + # so RECORD is read as it is, like in imported_files + try: + names = set((distribution.read_text("top_level.txt") or "").split()) + except (OSError, ValueError): + names = set() if names: return names try: - return {top_level_name(str(file)) for file in distribution.files or []} - {None} + rows = csv.reader((distribution.read_text("RECORD") or "").splitlines()) + return {top_level_name(row[0]) for row in rows if row} - {None} except (OSError, ValueError, csv.Error): return set() + def is_imported(name, distribution): + # whether the distribution in the dependency directory, or another dist-info of its name there, + # tells an imported top-level module + candidates = [distribution] + [ + metadata.PathDistribution(Path(path)) for path in dependency_dist_infos.get(normalized_name(name), []) + ] + return any(not imported.isdisjoint(top_level(candidate)) for candidate in candidates) + def imported_files(distribution): # the number of the recorded files of the imported top-level modules of the distribution, # e.g. demo/__init__.py or demo.py of the imported demo, if they are all on disk with their recorded hashes; @@ -522,7 +537,7 @@ internal object PythonDependencies { (row[0].split("/"), row[1].partition("=")) for row in csv.reader((distribution.read_text("RECORD") or "").splitlines()) if len(row) > 1 and row[1] and not row[0].endswith(".pyc") - and top_level_name(row[0]) in imported + and top_level_name(row[0], vendored=True) in imported ] for parts, (algorithm, _, value) in files: if algorithm not in hashlib.algorithms_guaranteed or algorithm.startswith("shake_"): @@ -663,15 +678,11 @@ internal object PythonDependencies { # pip resolves the missing ones without the dependency directory, so it would install a distribution # required by them from the index over the one installed from a URL, also in the same version missing = [dependency for dependency in dependencies if dependency in missing or dependency in url_dependencies] - in_use = { - distribution.metadata["Name"] for distribution in metadata.distributions() - if not imported.isdisjoint(top_level(distribution)) - } constraints = {} for distribution in metadata.distributions(): name = distribution.metadata["Name"] in_dependencies = os.path.realpath(distribution.locate_file("")) == dependency_location - if name and name not in constraints and (not in_dependencies or name in in_use): + if name and name not in constraints and (not in_dependencies or is_imported(name, distribution)): if in_dependencies and len(dependency_dist_infos.get(normalized_name(name), [])) > 1: # not the version of a dist-info left by a replaced version distribution = imported_distribution(name, distribution) diff --git a/src/test/kotlin/PythonDependenciesTest.kt b/src/test/kotlin/PythonDependenciesTest.kt index bb15362..969cab0 100644 --- a/src/test/kotlin/PythonDependenciesTest.kt +++ b/src/test/kotlin/PythonDependenciesTest.kt @@ -57,6 +57,7 @@ import kotlin.io.path.outputStream import kotlin.io.path.readBytes import kotlin.io.path.readText import kotlin.io.path.setLastModifiedTime +import kotlin.io.path.writeBytes import kotlin.io.path.writeText import kotlin.test.Test import kotlin.test.assertFailsWith @@ -1337,9 +1338,9 @@ class PythonDependenciesTest { } @Test - fun `should not constrain distribution without top_level_txt of malformed RECORD`() { + fun `should constrain imported package of distribution without top_level_txt of RECORD with non-integer size`() { // given - // its file size is not a number + // Distribution.files fails on it with ValueError withDistInfos { directory -> directory.resolve("replaced_probe/__init__.py").createParentDirectories().writeText("X = 1\n") writeDistInfo( @@ -1350,6 +1351,79 @@ class PythonDependenciesTest { // when val constraints = importedConstraints(directory) + // then + assert(constraints == listOf("replaced-probe==1.0")) + } + } + + @Test + fun `should constrain imported package of distribution without top_level_txt of RECORD with extra column`() { + // given + // Distribution.files fails on it with TypeError + withDistInfos { directory -> + directory.resolve("replaced_probe/__init__.py").createParentDirectories().writeText("X = 1\n") + writeDistInfo( + directory, "1.0", topLevel = null, + record = "replaced_probe/__init__.py,sha256=x,2,extra\nreplaced_probe-1.0.dist-info/RECORD,,\n" + ) + + // when + val constraints = importedConstraints(directory) + + // then + assert(constraints == listOf("replaced-probe==1.0")) + } + } + + @Test + fun `should constrain imported package of distribution without top_level_txt of RECORD with blank line`() { + // given + // Distribution.files fails on it with TypeError + withDistInfos { directory -> + directory.resolve("replaced_probe/__init__.py").createParentDirectories().writeText("X = 1\n") + writeDistInfo( + directory, "1.0", topLevel = null, + record = "replaced_probe/__init__.py,,\n\nreplaced_probe-1.0.dist-info/RECORD,,\n" + ) + + // when + val constraints = importedConstraints(directory) + + // then + assert(constraints == listOf("replaced-probe==1.0")) + } + } + + @Test + fun `should constrain imported package of distribution with top_level_txt not in UTF-8`() { + // given + // told by its recorded files then + withDistInfos { directory -> + val modules = mapOf("replaced_probe/__init__.py" to "X = 1\n") + directory.resolve("replaced_probe/__init__.py").createParentDirectories().writeText("X = 1\n") + writeDistInfo(directory, "1.0", modules, topLevel = null) + directory.resolve("replaced_probe-1.0.dist-info/top_level.txt").writeBytes(byteArrayOf(0xff.toByte(), 0xfe.toByte())) + + // when + val constraints = importedConstraints(directory) + + // then + assert(constraints == listOf("replaced-probe==1.0")) + } + } + + @Test + fun `should not constrain distribution without top_level_txt of dotted directory named like imported module`() { + // given + // like replaced_probe.libs/ of auditwheel, which is no module + withDistInfos { directory -> + val files = mapOf("replaced_probe.libs/libprobe.so" to "", "replaced_probe.b/__init__.py" to "") + files.forEach { (path, content) -> directory.resolve(path).createParentDirectories().writeText(content) } + writeDistInfo(directory, "1.0", files, topLevel = null) + + // when + val constraints = importedConstraints(directory) + // then assert(constraints.isEmpty()) } @@ -1614,9 +1688,10 @@ class PythonDependenciesTest { val process = ProcessBuilder( PythonEnvironment.executable, "-c", "import importlib.machinery; print(importlib.machinery.EXTENSION_SUFFIXES[0])" ).redirectError(ProcessBuilder.Redirect.INHERIT).start() - val suffix = process.inputStream.bufferedReader().readText().trim() - check(process.waitFor() == 0 && suffix.isNotEmpty()) { "No extension suffix: $suffix" } - return suffix + process.outputStream.close() + val suffix = process.inputStream.bufferedReader().readLine()?.trim()?.ifEmpty { null } + check(process.waitFor(5, TimeUnit.MINUTES) && process.exitValue() == 0) { "The Python did not tell the extension suffix" } + return checkNotNull(suffix) { "No extension suffix of the Python" } } private fun Path.distInfoNames(): List =