Skip to content

Lib/importlib: Ensure stale empty python module directories don't break the build #120492

Description

@kanavin

Bug report

Bug description:

The Yocto project has been seeing mysterious build failures that were traced down to stale empty python module directories left behind by previous module versions:
https://bugzilla.yoctoproject.org/show_bug.cgi?id=14816

We are carrying the following patch to address the issue (it both ignores the empty directories, and looks at directories in deterministic sorted order):

--- a/Lib/importlib/metadata/__init__.py
+++ b/Lib/importlib/metadata/__init__.py
@@ -710,7 +710,14 @@ class Lookup:
self.infos = FreezableDefaultDict(list)
self.eggs = FreezableDefaultDict(list)
- for child in path.children():
+ for child in sorted(path.children()):
+ childpath = pathlib.Path(path.root, child)
+ try:
+ if childpath.is_dir() and not any(childpath.iterdir()):
+ # Empty directories aren't interesting
+ continue
+ except PermissionError:
+ continue
low = child.lower()
if low.endswith((".dist-info", ".egg-info")):
# rpartition is faster than splitext and suitable for this purpose.

We'd like to discuss with upstream if such a fix is appropriate and will be taken as a proper pull request.

CPython versions tested on:

CPython main branch

Operating systems tested on:

Linux

Linked PRs

Metadata

Metadata

Assignees

Labels

stdlibStandard Library Python modules in the Lib/ directorytopic-importlibtype-bugAn unexpected behavior, bug, or error

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions