Skip to content

Commit

Permalink
Make CinderX work with optimized Python builds (again)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
jbower-fb authored and facebook-github-bot committed Nov 8, 2024
1 parent 20d2153 commit cf9e37a
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 23 deletions.
21 changes: 21 additions & 0 deletions Lib/init_cinderx.py
Original file line number Diff line number Diff line change
@@ -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
32 changes: 9 additions & 23 deletions Lib/site.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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()
Expand Down

0 comments on commit cf9e37a

Please sign in to comment.