Skip to content

#### Changes done to the dependent packages - #77

Open
copybara-service[bot] wants to merge 1 commit into
masterfrom
test_986596476
Open

copybara-service[bot] wants to merge 1 commit into
masterfrom
test_986596476

Conversation

@copybara-service

Copy link
Copy Markdown

Changes done to the dependent packages

Pybind11 changed the way modules are loaded and initialized, by introducing PEP 489 Multi-Phase Initialization.
Previously PYBIND11_MODULE returned valid, ready to use PyModuleObject.
Now it returns a PyModuleDef object - not initialized definition of a module, intended for an internal pybind import machinery to process it further.

Packages that relied on the raw pointer returned by the pybind started to crash with the error: "AttributeError: 'moduledef' object has no attribute"

Fixes:

  • abuse/ares/logica/internal/python/python_module.cc - Replaced manual module loading by injecting the raw module pointer to ModuleDict in init_re2_python_cc_library_for_embedding(), with the call to official Python API function inteded for that: PyImport_AppendInittab

    Note: PyImport_AppendInittab is also used by the pybind11 under the hood to load a module pybind11/embed.h

  • devtools/python/concurrent_access_detector/thread_tracker.py - Override exec_module method in _DelegateLoader, forwarding calls to underlying module. Otherwise, the modules were left uninitialized

  • devtools/python/lazy_imports/lazy_imports.py - assigning __spec__ attirbute to a module in exec_module(). Otherwise, python interpreter crashed when trying to lazy initialize modules, due to the __spec__ being None type.

  • googlex/gcam/creative_camera/video_storytelling/actions/composite_action_builder.h - Pybind now allows to automatically cast int to float even in the strict mode.
    Because float were higher than int in BuilderInput variant, ints passed to python interpreter were casted to floats. When later interpreter forwarded it back to a C++ function, an error is thrown about inexplicit casting. Putting int higher that float in the variant, allows it to take precedence and not be casted to float.

  • ipc/slicer/public/contrib/python/state_transfer_listener.cc - pybind11 v3 requires classes that derives from trampoline_self_life_support to also declare smart pointer type. Declared py::smart_holder.

  • storage/lightning/shared/voxel/python/gil_guard.cc - PYBIND11_CATCH_INIT_EXCEPTIONS macro now takes an argument of return value in case of an exception being handled. Added empty parentheses to PYBIND11_CATCH_INIT_EXCEPTIONS as nothing needs to be returned.

  • learning/brain/engprod/source_understanding/clang_parsers/pybind_matcher_test.cc - Pybind renamed ::pybind11::module_::module_def type to PyModuleDef. Renamed it as well in the test cases to match that.

  • learning/brain/engprod/source_understanding/clang_parsers/pybind_matcher.cc
    learning/brain/engprod/source_understanding/clang_parsers/pybind_matcher.h -
    Existing pybind matchers were created to capture and validate module object return by the pybind11. As mentioned before, pybind11 redesigned how modules are handled and what is being returned to represent a module.
    Matchers were rebuilded to handle modules structure returned from pybind v2 as well as the v3.

    MatchPybindV2Module captures arguments (like a module name) passed to a function creating a module.

    MatchPybindV3Module captures single structure with the module data in the fields and extracts it.

  • learning/deepmind/research/iig/open_spiel/pybind_step_annotations.h -
    AnnotateStep is registered as pybind module via py::classh which automatically assigns py::smart_holder as a smart pointer type. Pybind11 v3 requires classes managed by py::smart_holder to derive from pybind11::trampoline_self_life_support.

  • storage/lightning/shared/voxel/python/gil_guard.cc -
    pybind11 internals.tstate variable were wrapped in thread_specific_storage class, making it incompatible with the existing PYBIND11_TLS_REPLACE_VALUE macro. Instead, .get(), .set() and .reset() methods were provided to manage tstate

  • third_party/cel/python/policy/py_cel_policy.cc
    third_party/librealsense2/v2_50_0/wrappers/python/pyrs_frame.cpp
    third_party/py/reverb/pybind.cc
    third_party/py/tosa/serialization/python/bindings/tosa_serializer_pybind.cpp - when defining def_property_readonly, properties not wrapped in py::cpp_function were previously ignored. Recent pybind11 version added static_assert to be verbouse about ignoring them and requiring wrapping them in said py::cpp_function

  • third_party/ortools/ortools/linear_solver/python/model_builder_helper.cc
    third_party/ortools/ortools/sat/python/pybind_linearexpr.cc -
    pybind11 v3 reduced the amount of temporary objects created when processing expresions, like equations - essentialy reducing reference count tracked in model_builder_helper.cc by one.
    Without adjusting refcound treshold values, parsed expresions were malformed, as instead of appending next expresion term to flat vector [1 + 2 + 3 + 4], expresion vectors started to contain other vectors: [1 + [2 + [3 + [4]]]]

  • third_party/py/grain/_src/cpp/bindings/array_caster.cc
    third_party/py/grain/_src/cpp/bindings/array_tree_caster.cc - pybind11::detail::get_internals() in recent version became more complicated that a simple C++11 static singleton getter, and started using __cxa_guard_acquire under the hood. Not calling it at the beggining, and therefore not initializing it like pybind11::detail::get_local_internals(), resulted in a deadlock with fibers that tried to access it.

  • third_party/py/halide/src/halide/halide_/PyBinaryOperators.h -
    pybind11 started to automatically convert int64 to double. Since C++ tests expected to get back int64, the cast had to be explicitly defined.

  • third_party/py/koladata/g3doc/koda_v1_site_auto_files.py -
    pybind11 changed error message captured in koda_v1_site_auto_files.py. To still be able to capture same error while maintaining compatibility with old pybind11, new error message susbstring was added to if statement after the or operator.

@copybara-service
copybara-service Bot force-pushed the test_986596476 branch 2 times, most recently from f3bf685 to d85a151 Compare October 6, 2026 11:02
Pybind11 changed the way modules are loaded and initialized, by introducing [PEP 489 Multi-Phase Initialization](https://peps.python.org/pep-0489/).
 Previously `PYBIND11_MODULE` returned valid, ready to use `PyModuleObject`.
Now it returns a `PyModuleDef` object - not initialized definition of a module, intended for an internal pybind import machinery to process it further.

Packages that relied on the raw pointer returned by the pybind started to crash with the error: "AttributeError: 'moduledef' object has no attribute"

Fixes:
 - **abuse/ares/logica/internal/python/python_module.cc** - Replaced manual module loading by injecting the raw module pointer to `ModuleDict` in [init_re2_python_cc_library_for_embedding()](https://source.corp.google.com/piper///depot/google3/abuse/ares/logica/internal/python/python_module.cc;l=77), with the call to official Python API function inteded for that: [PyImport_AppendInittab](https://docs.python.org/3/c-api/import.html#c.PyImport_AppendInittab)

   *Note*: PyImport_AppendInittab is also used by the pybind11 under the hood to load a module [pybind11/embed.h](http://cs///depot/google3/third_party/pybind11/include/pybind11/embed.h;ws=rkurczak%2F376990;l=75)

- **devtools/python/concurrent_access_detector/thread_tracker.py** - Override `exec_module` method in `_DelegateLoader`, forwarding calls to underlying module. Otherwise, the modules were left uninitialized

- **devtools/python/lazy_imports/lazy_imports.py** - assigning `__spec__` attirbute to a module in `exec_module()`. Otherwise, python interpreter crashed when trying to lazy initialize modules, due to the `__spec__` being `None` type.

- **googlex/gcam/creative_camera/video_storytelling/actions/composite_action_builder.h** - Pybind now allows to automatically cast int to float even in the `strict mode`.
Because `float` were higher than `int` in [BuilderInput](https://source.corp.google.com/piper///depot/google3/googlex/gcam/creative_camera/video_storytelling/actions/composite_action_builder.h;l=37) variant, ints passed to python interpreter were casted to floats. When later interpreter forwarded it back to a C++ function, an error is thrown about inexplicit casting. Putting `int` higher that `float` in the variant, allows it to take precedence and not be casted to `float`.

- **ipc/slicer/public/contrib/python/state_transfer_listener.cc** - pybind11 v3 requires classes that derives from `trampoline_self_life_support` to also declare smart pointer type. Declared `py::smart_holder`.

- **storage/lightning/shared/voxel/python/gil_guard.cc** - `PYBIND11_CATCH_INIT_EXCEPTIONS` macro now takes an argument of return value in case of an exception being handled. Added empty parentheses to `PYBIND11_CATCH_INIT_EXCEPTIONS` as nothing needs to be returned.

- **learning/brain/engprod/source_understanding/clang_parsers/pybind_matcher_test.cc** - Pybind renamed `::pybind11::module_::module_def` type to `PyModuleDef`. Renamed it as well in the test cases to match that.

- **learning/brain/engprod/source_understanding/clang_parsers/pybind_matcher.cc**
**learning/brain/engprod/source_understanding/clang_parsers/pybind_matcher.h** -
Existing pybind matchers were created to capture and validate module object return by the pybind11. As mentioned before, pybind11 redesigned how modules are handled and what is being returned to represent a module.
Matchers were rebuilded to handle modules structure returned from pybind v2 as well as the v3.

    `MatchPybindV2Module` captures arguments (like a module name) passed to a function creating a module.

    `MatchPybindV3Module` captures single structure with the module data in the fields and extracts it.

- **learning/deepmind/research/iig/open_spiel/pybind_step_annotations.h** -
`AnnotateStep` is registered as pybind module via [py::classh](google3/learning/deepmind/research/iig/open_spiel/pybind_step_annotations.cc) which automatically assigns `py::smart_holder` as a smart pointer type. Pybind11 v3 requires classes managed by `py::smart_holder` to derive from `pybind11::trampoline_self_life_support`.

- **storage/lightning/shared/voxel/python/gil_guard.cc** -
pybind11 `internals.tstate` variable were wrapped in `thread_specific_storage` class, making it incompatible with the existing `PYBIND11_TLS_REPLACE_VALUE` macro. Instead, `.get()`, `.set()` and `.reset()` methods were provided to manage `tstate`

- **third_party/cel/python/policy/py_cel_policy.cc**
**third_party/librealsense2/v2_50_0/wrappers/python/pyrs_frame.cpp**
**third_party/py/reverb/pybind.cc**
**third_party/py/tosa/serialization/python/bindings/tosa_serializer_pybind.cpp** - when defining `def_property_readonly`, properties not wrapped in `py::cpp_function` were previously ignored. Recent pybind11 version added static_assert to be verbouse about ignoring them and requiring wrapping them in said `py::cpp_function`

- **third_party/ortools/ortools/linear_solver/python/model_builder_helper.cc**
**third_party/ortools/ortools/sat/python/pybind_linearexpr.cc** -
pybind11 v3 reduced the amount of temporary objects created when processing expresions, like equations - essentialy reducing reference count tracked in [model_builder_helper.cc](https://source.corp.google.com/piper///depot/google3/third_party/ortools/ortools/linear_solver/python/model_builder_helper.cc;l=405) by one.
Without adjusting refcound treshold values, parsed expresions were malformed, as instead of appending next expresion term to flat vector `[1 + 2 + 3 + 4]`, expresion vectors started to contain other vectors: `[1 + [2 + [3 + [4]]]]`

- **third_party/py/grain/_src/cpp/bindings/array_caster.cc**
**third_party/py/grain/_src/cpp/bindings/array_tree_caster.cc** - `pybind11::detail::get_internals()` in recent version became more complicated that a simple C++11 static singleton getter, and started using `__cxa_guard_acquire` under the hood. Not calling it at the beggining, and therefore not initializing it like `pybind11::detail::get_local_internals()`, resulted in a deadlock with fibers that tried to access it.

- **third_party/py/halide/src/halide/halide_/PyBinaryOperators.h** -
pybind11 started to automatically convert `int64` to `double`. Since C++ tests expected to get back `int64`, the cast had to be explicitly defined.

- **third_party/py/koladata/g3doc/koda_v1_site_auto_files.py** -
pybind11 changed error message captured in [koda_v1_site_auto_files.py](https://source.corp.google.com/piper///depot/google3/third_party/py/koladata/g3doc/koda_v1_site_auto_files.py;l=244). To still be able to capture same error while maintaining compatibility with old pybind11, new error message susbstring was added to `if` statement after the `or` operator.

PiperOrigin-RevId: 986596476
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

0 participants