From 22899e6a7d9d58594dbbb93361534891fbf6a054 Mon Sep 17 00:00:00 2001 From: Stefan Wunsch Date: Tue, 31 Mar 2020 10:51:57 +0200 Subject: [PATCH 1/2] [PyROOT] Add test checking loaded libraries after import --- .../pythonizations/test/CMakeLists.txt | 3 + .../pythonizations/test/import_load_libs.py | 76 +++++++++++++++++++ 2 files changed, 79 insertions(+) create mode 100644 bindings/pyroot_experimental/pythonizations/test/import_load_libs.py diff --git a/bindings/pyroot_experimental/pythonizations/test/CMakeLists.txt b/bindings/pyroot_experimental/pythonizations/test/CMakeLists.txt index eb2ebbf0c3223..6a802207e987f 100644 --- a/bindings/pyroot_experimental/pythonizations/test/CMakeLists.txt +++ b/bindings/pyroot_experimental/pythonizations/test/CMakeLists.txt @@ -4,6 +4,9 @@ # For the licensing terms see $ROOTSYS/LICENSE. # For the list of contributors see $ROOTSYS/README/CREDITS. +# Test library loads during importing ROOT +ROOT_ADD_PYUNITTEST(pyroot_import_load_libs import_load_libs.py) + # Test ROOT module ROOT_ADD_PYUNITTEST(pyroot_root_module root_module.py) diff --git a/bindings/pyroot_experimental/pythonizations/test/import_load_libs.py b/bindings/pyroot_experimental/pythonizations/test/import_load_libs.py new file mode 100644 index 0000000000000..43597a9fd17f6 --- /dev/null +++ b/bindings/pyroot_experimental/pythonizations/test/import_load_libs.py @@ -0,0 +1,76 @@ +import unittest +import re + + +class ImportLoadLibs(unittest.TestCase): + """ + Test which libraries are loaded during importing ROOT + """ + + # The whitelist is a list of regex expressions that mark wanted libraries + # Note that the regex has to result in an exact match with the library name. + known_libs = [ + # ROOT libraries + 'libCore', + 'libCling', + 'libcppyy.*', + 'libROOTPythonizations.*', + # ROOT dependencies + '', + # System and Python libraries + 'libpthread', + 'libpython.*', + 'math', + 'libm', + 'libdl', + 'libc', + '_.*', + 'ld.*', + 'select', + 'binascii', + 'grp', + ] + + # Verbose mode of the test + verbose = True + + def test_import(self): + """ + Test libraries loaded after importing ROOT + """ + import ROOT + libs = str(ROOT.gSystem.GetLibraries()) + + # Split paths + libs = libs.split(' ') + + # Get library name without full path and .so* suffix + libs = [l.strip().split('/')[-1] for l in libs] + libs = [l.strip().split('.')[0] for l in libs] + + # Check that the loaded libraries are white listed + bad_libs = [] + good_libs = [] + matched_re = [] + for l in libs: + matched = False + for r in self.known_libs: + m = re.search(r, l) + if m: + if m[0] == l: + matched = True + good_libs.append(l) + matched_re.append(r) + break + if not matched: + bad_libs.append(l) + + if self.verbose: + print('Found whitelisted libraries after importing ROOT with the shown regex match:') + for l, r in zip(good_libs, matched_re): + print(' - {} ({})'.format(l, r)) + + if bad_libs: + raise Exception('Found not whitelisted libraries after importing ROOT:' \ + + '\n - ' + '\n - '.join(bad_libs) \ + + '\nIf the test fails with a library that is loaded on purpose, please add it to the whitelist.') From 87edc3ad153639f62da86e6d961f5095d5342039 Mon Sep 17 00:00:00 2001 From: Stefan Wunsch Date: Tue, 31 Mar 2020 12:35:38 +0200 Subject: [PATCH 2/2] [Squash me] Flush stdout so that we get the correct output --- .../pyroot_experimental/pythonizations/test/import_load_libs.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/bindings/pyroot_experimental/pythonizations/test/import_load_libs.py b/bindings/pyroot_experimental/pythonizations/test/import_load_libs.py index 43597a9fd17f6..7d5d8876ab48d 100644 --- a/bindings/pyroot_experimental/pythonizations/test/import_load_libs.py +++ b/bindings/pyroot_experimental/pythonizations/test/import_load_libs.py @@ -69,6 +69,8 @@ def test_import(self): print('Found whitelisted libraries after importing ROOT with the shown regex match:') for l, r in zip(good_libs, matched_re): print(' - {} ({})'.format(l, r)) + import sys + sys.stdout.flush() if bad_libs: raise Exception('Found not whitelisted libraries after importing ROOT:' \