Skip to content

Commit

Permalink
Replace "imp" module with "importlib" in parseTestRecord
Browse files Browse the repository at this point in the history
The "imp" module is deprecated in Python 3.4 and will be removed in
Python 3.12. Use it only if the current Python version is too old to use
the "importlib" module.
  • Loading branch information
anba authored and ptomato committed Nov 15, 2023
1 parent 60310b7 commit b218cf6
Showing 1 changed file with 39 additions and 2 deletions.
41 changes: 39 additions & 2 deletions tools/packaging/parseTestRecord.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import os
import re
import imp
import sys

# Matches trailing whitespace and any following blank lines.
_BLANK_LINES = r"([ \t]*[\r\n]{1,2})*"
Expand Down Expand Up @@ -114,7 +114,9 @@ def importYamlLoad():
monkeyYaml = loadMonkeyYaml()
yamlLoad = monkeyYaml.load

def loadMonkeyYaml():
def loadMonkeyYaml2():
import imp

f = None
try:
p = os.path.dirname(os.path.realpath(__file__))
Expand All @@ -126,3 +128,38 @@ def loadMonkeyYaml():
finally:
if f:
f.close()

def loadMonkeyYaml3():
import importlib.machinery
import importlib.util

packaging_dir = os.path.dirname(os.path.realpath(__file__))
module_name = "monkeyYaml"

# Create a FileFinder to load Python source files.
loader_details = (
importlib.machinery.SourceFileLoader,
importlib.machinery.SOURCE_SUFFIXES,
)
finder = importlib.machinery.FileFinder(packaging_dir, loader_details)

# Find the module spec.
spec = finder.find_spec(module_name)
if spec is None:
raise RuntimeError("Can't find monkeyYaml module")

# Create and execute the module.
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)

# Return the executed module
return module

def loadMonkeyYaml():
# The "imp" module is deprecated in Python 3.4 and will be removed in
# Python 3.12. Use it only if the current Python version is too old to use
# the "importlib" module.
if sys.version_info < (3, 4):
return loadMonkeyYaml2()

return loadMonkeyYaml3()

0 comments on commit b218cf6

Please sign in to comment.