Added a test that the Python module is importable, and fixed several issues that were required to make the test pass:

1. For some reason the version script was not working, it was failing to export the main symbol for the Python extension.  I fixed this by using the `visibility` attribute instead to export the `PyInit__message` function.
2. We were not properly stripping the `python/dist/` prefix for the C module, which was making the module exported under the name `dist.google._upb` instead of `google._upb`.
3. The `py_library()` rule was failing to actually propagate the module file into the wheel, so I just removed it.

PiperOrigin-RevId: 445446611
pull/13171/head
Joshua Haberman 3 years ago committed by Copybara-Service
parent 823ed18316
commit fbcd42b9f0
  1. 2
      .github/workflows/python_tests.yml
  2. 5
      python/dist/BUILD.bazel
  3. 10
      python/dist/dist.bzl
  4. 2
      python/protobuf.c
  5. 14
      python/py_extension.bzl

@ -70,3 +70,5 @@ jobs:
echo "VIRTUAL ENV:" $VIRTUAL_ENV echo "VIRTUAL ENV:" $VIRTUAL_ENV
- name: Install Wheels - name: Install Wheels
run: pip install -vvv --no-index --find-links wheels protobuf run: pip install -vvv --no-index --find-links wheels protobuf
- name: Test that module is importable
run: python -c 'from google._upb import _message; assert "google._upb._message.MessageMeta" in str(_message.MessageMeta)'

@ -104,7 +104,10 @@ py_wheel(
"//python:limited_api_3.10": "cp310", "//python:limited_api_3.10": "cp310",
"//conditions:default": "system", "//conditions:default": "system",
}), }),
strip_path_prefixes = ["python/"], strip_path_prefixes = [
"python/dist/",
"python/",
],
version = PROTOBUF_VERSION, version = PROTOBUF_VERSION,
deps = [ deps = [
":message_mod", ":message_mod",

@ -76,20 +76,12 @@ _py_dist_module_rule = rule(
) )
def py_dist_module(name, module_name, extension): def py_dist_module(name, module_name, extension):
file_rule = name + "_file"
_py_dist_module_rule( _py_dist_module_rule(
name = file_rule, name = name,
module_name = module_name, module_name = module_name,
extension = extension, extension = extension,
) )
# TODO(haberman): needed?
native.py_library(
name = name,
data = [":" + file_rule],
imports = ["."],
)
def _py_dist_transition_impl(settings, attr): def _py_dist_transition_impl(settings, attr):
_ignore = (settings) # @unused _ignore = (settings) # @unused
transitions = [] transitions = []

@ -322,7 +322,7 @@ PyObject* PyUpb_Forbidden_New(PyObject* cls, PyObject* args, PyObject* kwds) {
// Module Entry Point // Module Entry Point
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
PyMODINIT_FUNC PyInit__message(void) { __attribute__((visibility("default"))) PyMODINIT_FUNC PyInit__message(void) {
PyObject* m = PyModule_Create(&module_def); PyObject* m = PyModule_Create(&module_def);
if (!m) return NULL; if (!m) return NULL;

@ -11,27 +11,17 @@ def py_extension(name, srcs, copts, deps = []):
deps: Libraries that the target depends on deps: Libraries that the target depends on
""" """
version_script = name + "_version_script.lds"
symbol = "PyInit_" + name
native.genrule(
name = "gen_" + version_script,
outs = [version_script],
cmd = "echo 'message { global: " + symbol + "; local: *; };' > $@",
)
native.cc_binary( native.cc_binary(
name = name + "_binary", name = name + "_binary",
srcs = srcs, srcs = srcs,
copts = copts, copts = copts + ["-fvisibility=hidden"],
linkopts = select({ linkopts = select({
"//python/dist:osx-x86_64_cpu": ["-undefined", "dynamic_lookup"], "//python/dist:osx-x86_64_cpu": ["-undefined", "dynamic_lookup"],
"//conditions:default": [], "//conditions:default": [],
}), }),
linkshared = True, linkshared = True,
linkstatic = True, linkstatic = True,
deps = deps + [ deps = deps + select({
":" + version_script,
] + select({
"//python:limited_api_3.7": ["@python-3.7.0//:python_headers"], "//python:limited_api_3.7": ["@python-3.7.0//:python_headers"],
"//python:full_api_3.7_win32": ["@nuget_python_i686_3.7.0//:python_full_api"], "//python:full_api_3.7_win32": ["@nuget_python_i686_3.7.0//:python_full_api"],
"//python:full_api_3.7_win64": ["@nuget_python_x86-64_3.7.0//:python_full_api"], "//python:full_api_3.7_win64": ["@nuget_python_x86-64_3.7.0//:python_full_api"],

Loading…
Cancel
Save