Skip to content
Open
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
3 changes: 3 additions & 0 deletions news/2178.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
(pypi) Preserve bytecode package initializers explicitly included as
`__init__.pyc` in wheel data
([#2178](https://github.com/bazel-contrib/rules_python/issues/2178)).
5 changes: 5 additions & 0 deletions python/private/pypi/attrs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ and py_test targets must specify either `legacy_create_init=False` or the global
`--incompatible_default_to_explicit_init_py` to prevent `__init__.py` being automatically generated in every directory.

This option is required to support some packages which cannot handle the conversion to pkg-util style.

:::{versionchanged} VERSION_NEXT_PATCH
Namespace conversion recognizes `__init__.pyc` as the package initializer
when it is explicitly included in wheel data.
:::
""",
),
"environment": attr.string_dict(
Expand Down
7 changes: 5 additions & 2 deletions python/private/pypi/namespace_pkgs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,12 @@ def get_files(*, srcs, ignored_dirnames = [], root = None):
_add_all(root, ignored)

for file in srcs:
dirname, _, filename = file.rpartition("/")
# The wheel BUILD file is at the repository root. Compare both label
# forms and glob results as paths relative to that root.
path = file.removeprefix("//:").removeprefix(":")
dirname, _, filename = path.rpartition("/")

if filename == "__init__.py":
if filename in ("__init__.py", "__init__.pyc"):
ignored[dirname] = None
dirname, _, _ = dirname.rpartition("/")
elif filename.endswith(_ext.py):
Expand Down
15 changes: 15 additions & 0 deletions tests/pypi/namespace_pkgs/namespace_pkgs_tests.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@ _tests.append(test_parent_child_relationship_of_namespace_and_nested_standard_pk
def test_recognized_all_nonstandard_module_types(env):
srcs = [
"ayy/my_module.pyc",
"ayy/regular/__init__.pyc",
"ayy/regular/other.pyc",
"bee/ccc/dee/eee.so",
"eff/jee/aych.pyd",
]
Expand All @@ -141,6 +143,19 @@ def test_recognized_all_nonstandard_module_types(env):

_tests.append(test_recognized_all_nonstandard_module_types)

def test_bytecode_initializer_file_labels(env):
srcs = [
"site-packages/relative/module.py",
":site-packages/relative/__init__.pyc",
"site-packages/absolute/module.py",
"//:site-packages/absolute/__init__.pyc",
"site-packages/namespace/module.py",
]
got = get_files(srcs = srcs, root = "site-packages")
env.expect.that_collection(got).contains_exactly(["site-packages/namespace"])

_tests.append(test_bytecode_initializer_file_labels)

def test_skips_ignored_directories(env):
srcs = [
"root/foo/boo/my_module.py",
Expand Down