This is an automated email from the ASF dual-hosted git repository. laszlog pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/impala.git
commit 2cf57778922aa0456eba077097be5d8ba8176e9d Author: Laszlo Gaal <[email protected]> AuthorDate: Wed Feb 5 20:06:16 2025 +0100 IMPALA-13826: Migrate from imp to importlib in the config generator Python has deprecated the 'imp' package in Python 3.4, and removed it in Python 3.12. The deprecation has also started throwing warnings in versions before 3.12. The template generator used a single call to imp.load_source to load the template Python file. This is now replaced with code snippet published in Python's official documentation. Change-Id: I472d093eeaac97a380d444a1756b54f825b2d031 Reviewed-on: http://gerrit.cloudera.org:8080/22582 Reviewed-by: Zoltan Borok-Nagy <[email protected]> Tested-by: Laszlo Gaal <[email protected]> --- bin/generate_xml_config.py | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/bin/generate_xml_config.py b/bin/generate_xml_config.py index d4e7e6561..dc6e5eca8 100755 --- a/bin/generate_xml_config.py +++ b/bin/generate_xml_config.py @@ -40,7 +40,6 @@ REPL: """ from __future__ import absolute_import, division, print_function -import imp import os import re import sys @@ -95,6 +94,33 @@ def dump_config(d, source_path, out): print("</configuration>", file=out) +def load_source_with_importlib(modname, filename): + """"Emulate imp.load_source() of Python2 for Python3 using importlib + Code taken from published Python documentation, see + https://docs.python.org/3/whatsnew/3.12.html#imp""" + import importlib.util + import importlib.machinery + + loader = importlib.machinery.SourceFileLoader(modname, filename) + spec = importlib.util.spec_from_file_location(modname, filename, loader=loader) + module = importlib.util.module_from_spec(spec) + # The module is always executed and not cached in sys.modules. + # Uncomment the following line to cache the module. + # sys.modules[module.__name__] = module + loader.exec_module(module) + return module + + +def import_template(name, module_path): + """Handle module import differences between Python2 and Python3""" + mod = None + if sys.version_info.major < 3: + import imp + mod = imp.load_source('template', module_path) + else: + mod = load_source_with_importlib(name, module_path) + return mod + def main(): if len(sys.argv) != 3: print("usage: {prog} <template> <out>".format(prog=sys.argv[0]), file=sys.stderr) @@ -102,7 +128,7 @@ def main(): _, in_path, out_path = sys.argv try: - mod = imp.load_source('template', in_path) + mod = import_template('template', in_path) except: # noqa print("Unable to load template: %s" % in_path, file=sys.stderr) raise
