Repository navigation
[Python] Fix crash unpickling a ROOT object without importing cppyy #22968
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
bindings/pyroot/pythonizations/test/regression_pickle_no_cppyy.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| import os | ||
| import pickle | ||
| import subprocess | ||
| import sys | ||
| import tempfile | ||
| import textwrap | ||
| import unittest | ||
|
|
||
|
|
||
| class RegressionPickleNoCppyy(unittest.TestCase): | ||
| """ | ||
| Reading back a pickled ROOT object in a fresh interpreter, where the cppyy | ||
| backend has not been imported yet, must not crash. | ||
|
|
||
| Unpickling a ROOT object only imports ``ROOT.libROOTPythonizations`` (to | ||
| resolve ``_CPPInstance__expand__``), which does not initialize the cppyy | ||
| backend. Entering CPyCppyy through the public API in that state used to | ||
| leave ``gThisModule`` null, so that ``CreateScopeProxy`` crashed with a | ||
| segmentation violation when using it as a fake scope. | ||
|
|
||
| See the ROOT forum report: | ||
| https://root-forum.cern.ch/t/issue-with-new-root-version-on-lxplus | ||
| """ | ||
|
|
||
| def test_load_without_prior_cppyy_import(self): | ||
| import ROOT | ||
|
|
||
| # Create a pickle of a ROOT object. Touching a class here imports the | ||
| # cppyy backend, but only in this (throw-away) process. | ||
| h = ROOT.TH1F("h", "h", 10, 0, 1) | ||
| h.Fill(0.5) | ||
|
|
||
| fname = tempfile.NamedTemporaryFile(suffix=".pkl", delete=False).name | ||
| try: | ||
| with open(fname, "wb") as f: | ||
| pickle.dump(h, f) | ||
|
|
||
| # Read it back in a *fresh* interpreter that only imports pickle, so | ||
| # that the cppyy backend is not initialized before the object is | ||
| # expanded. This reproduces the exact scenario from the bug report. | ||
| code = textwrap.dedent( | ||
| """ | ||
| import pickle | ||
| with open({fname!r}, "rb") as f: | ||
| h = pickle.load(f) | ||
| assert h.GetName() == "h", h.GetName() | ||
| assert h.GetEntries() == 1.0, h.GetEntries() | ||
| """ | ||
| ).format(fname=fname) | ||
|
|
||
| # Do not use check=True so we can give a helpful message on crash | ||
| # (a segfault shows up as a negative return code on POSIX). | ||
| proc = subprocess.run([sys.executable, "-c", code]) | ||
| self.assertEqual( | ||
| proc.returncode, | ||
| 0, | ||
| "Unpickling a ROOT object in a fresh interpreter crashed (return code {}).".format(proc.returncode), | ||
| ) | ||
| finally: | ||
| os.remove(fname) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.