From cf9e37adf7e2baf2444dbbd74c67829a188ec492 Mon Sep 17 00:00:00 2001 From: Jacob Bower Date: Thu, 7 Nov 2024 20:27:26 -0800 Subject: [PATCH] Make CinderX work with optimized Python builds (again) Summary: D62604340 introduced a step to the build where `sed` is used to edit `site.py` to *enable* CinderX in the 3.12 distribution as needed. Unfortunately this doesn't work in optimized builds. Those builds "deep-freeze" a number of startup Python libraries including `site.py` in a state *before* the edit is made. To fix this, move the CinderX initialization code into a Python module that's not in the frozen libraries, and only include this in the distribution if CinderX is desired. During Python startup, if the `init_cinderx` does not exist then proceed without it leaving the set of imported modules unchanged. This resolves the issue D62604340 was trying to address. Reviewed By: itamaro Differential Revision: D65584596 fbshipit-source-id: 212dbe22a93fa44d33750166b80d03bffa559502 --- Lib/init_cinderx.py | 21 +++++++++++++++++++++ Lib/site.py | 32 +++++++++----------------------- 2 files changed, 30 insertions(+), 23 deletions(-) create mode 100644 Lib/init_cinderx.py diff --git a/Lib/init_cinderx.py b/Lib/init_cinderx.py new file mode 100644 index 00000000000..c808dc07f1d --- /dev/null +++ b/Lib/init_cinderx.py @@ -0,0 +1,21 @@ +# This file is deliberately not included in the set of deep-frozen bootstrap +# libraries so it can easily be excluded from the distribution to avoid +# attempting to initialize CinderX. This is desirable because this code imports +# importlib.util and some tests are surprised when they find this unexpectedly +# loaded as part of bootstrap. + +import importlib.util + +# `cinderx` is the Python module in fbcode/cinderx/PythonLib which wraps the +# `_cinderx` C++ extension in fbcode/cinderx/_cinderx.cpp. Both need to be +# available to load CinderX. +if not ( + importlib.util.find_spec("cinderx") is None + or importlib.util.find_spec("_cinderx") is None +): + try: + import cinderx + + cinderx.init() + except Exception as e: + raise RuntimeError("Failed to initialize CinderX module") from e diff --git a/Lib/site.py b/Lib/site.py index 73b239414b6..5141016f3ea 100644 --- a/Lib/site.py +++ b/Lib/site.py @@ -600,25 +600,6 @@ def execusercustomize(): (err.__class__.__name__, err)) -def init_cinder(): - import importlib.util - - # `cinderx` is the Python module in fbcode/cinderx/PythonLib which wraps the - # `_cinderx` C++ extension in fbcode/cinderx/_cinderx.cpp. Both need to be - # available to load CinderX. - if ( - importlib.util.find_spec("cinderx") is None - or importlib.util.find_spec("_cinderx") is None - ): - return - try: - import cinderx - - cinderx.init() - except Exception as e: - raise RuntimeError("Failed to initialize CinderX module") from e - - def main(): """Add standard site-specific directories to the module search path. @@ -644,10 +625,15 @@ def main(): sethelper() if not sys.flags.isolated: enablerlcompleter() - # Do not attempt to initialize cinder *by default*, as it's not relevant - # for regular MetaPython users (and cinderx is not even available). - # This gets enabled at build-time when cinderx is enabled (see D62604340). - # init_cinder() + # If we've been built with CinderX support there will be an init_cinderx.py + # included in the default module search path. In this case we import it + # which will have the side-effect of initializing CinderX. If this module + # is missing then we weren't build with CinderX so ignoring the import + # error is fine. + try: + import init_cinderx + except ImportError: + pass execsitecustomize() if ENABLE_USER_SITE: execusercustomize()