浏览代码

[bazel/py] Omit google/__init__.py from the Protobuf runtime. (#7908)

Since `google` is a Python namespace package, the google/__init__.py
file should be omitted to avoid collisions. For example, its presence
may cause other Pip packages in the google namespace not to be found.

This change narrows which __init__.py files are included in the
`//:protobuf_runtime` target to include only those under
`google/protobuf`. It also moves their canonical inclusion to the
internal `//:python_srcs` rule, eliminating the dual specification in
`python_protobuf.extra_srcs`.

This is the difference in files, lexicographically ordered:

```
$ bazel cquery 'labels(srcs, kind(py_library, deps(:protobuf_python)))' | sort > /tmp/deps-{before,after}.txt
$ diff /tmp/deps-{before,after}.txt
1,5d0
< //:python/compatibility_tests/v2.5.0/tests/__init__.py (null)
< //:python/compatibility_tests/v2.5.0/tests/google/__init__.py (null)
< //:python/compatibility_tests/v2.5.0/tests/google/protobuf/__init__.py (null)
< //:python/compatibility_tests/v2.5.0/tests/google/protobuf/internal/__init__.py (null)
< //:python/google/__init__.py (null)
51d45
< //:python/protobuf_distutils/protobuf_distutils/__init__.py (null)
```

This seems like a desirable change, since it avoids polluting other
packages, too.

This change is conceptually similar to #7877, but for Bazel.
David L. Jones 5 年之前
父节点
当前提交
02955622cc
共有 1 个文件被更改,包括 1 次插入3 次删除
  1. 1 3
      BUILD

+ 1 - 3
BUILD

@@ -739,10 +739,9 @@ py_library(
     name = "python_srcs",
     name = "python_srcs",
     srcs = glob(
     srcs = glob(
         [
         [
-            "python/google/**/*.py",
+            "python/google/protobuf/**/*.py",
         ],
         ],
         exclude = [
         exclude = [
-            "python/google/protobuf/**/__init__.py",
             "python/google/protobuf/internal/*_test.py",
             "python/google/protobuf/internal/*_test.py",
             "python/google/protobuf/internal/test_util.py",
             "python/google/protobuf/internal/test_util.py",
         ],
         ],
@@ -843,7 +842,6 @@ py_proto_library(
     }),
     }),
     default_runtime = "",
     default_runtime = "",
     protoc = ":protoc",
     protoc = ":protoc",
-    py_extra_srcs = glob(["python/**/__init__.py"]),
     py_libs = [
     py_libs = [
         ":python_srcs",
         ":python_srcs",
         "@six//:six",
         "@six//:six",