Skip to content

Commit 247c394

Browse files
author
Caspar van Leeuwen
committed
Add sanity check to see if a ctypes.CDLL call on libpython3.so works, to verify that the patch has been applied correctly when EasyBuild is configured to filter LD_LIBRARY_PATH
1 parent c622e77 commit 247c394

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

easybuild/easyblocks/p/python.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -709,6 +709,26 @@ def install_step(self):
709709
symlink(target_lib_dynload, lib_dynload)
710710
change_dir(cwd)
711711

712+
def _sanity_check_ctypes_ld_library_path_patch(self):
713+
"""Check that the patch for ctypes that should be applied when filtering LD_LIBRARY_PATH works"""
714+
cmd = "python -c 'import ctypes; print(ctypes.CDLL(\"libpython3.so\"))'"
715+
res = run_shell_cmd(cmd)
716+
out = res.output.strip()
717+
escaped_python_root = re.escape(self.installdir)
718+
pattern = rf"^<CDLL '{escaped_python_root}.*', handle [a-f0-9]+ at 0x[a-f0-9]+>$"
719+
match = re.match(pattern, out)
720+
self.log.debug(f"Matching regular expression pattern {pattern} to string {out}")
721+
if match:
722+
msg = "Call to ctypes.CDLL('libpython3.so') succesfully opened libpython3.so, indicating that the patch "
723+
msg += "for ctypes when EasyBuild is configured to filter LD_LIBRARY_PATH was applied succesfully."
724+
self.log.info(msg)
725+
else:
726+
msg = "Opening of libpython3.so using ctypes.CDLL('libpython3.so') failed. "
727+
msg += "Ctypes requires a patch when EasyBuild is configured to filter LD_LIBRARY_PATH. "
728+
msg += "Please check if you specified a patch through patch_ctypes_ld_library_path and check "
729+
msg += "the logs to see if it applied correctly."
730+
raise EasyBuildError(msg)
731+
712732
def _sanity_check_ebpythonprefixes(self):
713733
"""Check that EBPYTHONPREFIXES works"""
714734
temp_prefix = tempfile.mkdtemp(suffix='-tmp-prefix')
@@ -773,6 +793,18 @@ def sanity_check_step(self):
773793
if self.cfg.get('ebpythonprefixes'):
774794
self._sanity_check_ebpythonprefixes()
775795

796+
# If the conditions for applying the patch specified through patch_ctypes_ld_library_path are met, check that
797+
# the patch applied correctly (and fixed the issue). Note that the condition should be identical to the one
798+
# used to determine if the patch_ctypes_ld_library_path patch should be applied
799+
filtered_env_vars = build_option('filter_env_vars') or []
800+
patch_ctypes_ld_library_path = self.cfg.get('patch_ctypes_ld_library_path')
801+
if (
802+
'LD_LIBRARY_PATH' in filtered_env_vars and
803+
'LIBRARY_PATH' not in filtered_env_vars and
804+
patch_ctypes_ld_library_path
805+
):
806+
self._sanity_check_ctypes_ld_library_path_patch
807+
776808
pyver = 'python' + self.pyshortver
777809
custom_paths = {
778810
'files': [

0 commit comments

Comments
 (0)