Limit respawn to supported python versions (#83662)

* Limit respawn to supported python versions
pull/84774/head
Matt Martz 9 months ago committed by GitHub
parent 96a8c04207
commit 00067f1d2e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,2 @@
bugfixes:
- module respawn - limit to supported Python versions

@ -4,12 +4,15 @@
from __future__ import annotations from __future__ import annotations
import os import os
import pathlib
import subprocess import subprocess
import sys import sys
import typing as t import typing as t
from ansible.module_utils.common.text.converters import to_bytes from ansible.module_utils.common.text.converters import to_bytes
_ANSIBLE_PARENT_PATH = pathlib.Path(__file__).parents[3]
def has_respawned(): def has_respawned():
return hasattr(sys.modules['__main__'], '_respawned') return hasattr(sys.modules['__main__'], '_respawned')
@ -55,11 +58,20 @@ def probe_interpreters_for_module(interpreter_paths, module_name):
be returned (or ``None`` if probing fails for all supplied paths). be returned (or ``None`` if probing fails for all supplied paths).
:arg module_name: fully-qualified Python module name to probe for (eg, ``selinux``) :arg module_name: fully-qualified Python module name to probe for (eg, ``selinux``)
""" """
PYTHONPATH = os.getenv('PYTHONPATH', '')
env = os.environ | {'PYTHONPATH': f'{_ANSIBLE_PARENT_PATH}:{PYTHONPATH}'.rstrip(': ')}
for interpreter_path in interpreter_paths: for interpreter_path in interpreter_paths:
if not os.path.exists(interpreter_path): if not os.path.exists(interpreter_path):
continue continue
try: try:
rc = subprocess.call([interpreter_path, '-c', 'import {0}'.format(module_name)]) rc = subprocess.call(
[
interpreter_path,
'-c',
f'import {module_name}, ansible.module_utils.basic',
],
env=env,
)
if rc == 0: if rc == 0:
return interpreter_path return interpreter_path
except Exception: except Exception:

Loading…
Cancel
Save