Skip to content

Commit

Permalink
ndiff: convert ndifftest to importlib for Python 3.12 support
Browse files Browse the repository at this point in the history
Python 3.12 deprecated and removed the already deprecated and not
documented imp library (the load_source function was never documented)

Replace this with a modern alternative suggested by
python/cpython#104212.

Signed-off-by: Christian Marangi <[email protected]>
  • Loading branch information
Ansuel committed Apr 28, 2024
1 parent f5ef3a4 commit 1d8c0e1
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions ndiff/ndifftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,22 @@

import xml.dom.minidom

import imp
import types
import importlib.machinery

# Suggested conversion for imp.load_module from
# https://github.com/python/cpython/issues/104212
def load_module(module_name, filename):
loader = importlib.machinery.SourceFileLoader(module_name, filename)
module = types.ModuleType(loader.name)
module.__file__ = filename
sys.modules[module.__name__] = module
loader.exec_module(module)
return module

dont_write_bytecode = sys.dont_write_bytecode
sys.dont_write_bytecode = True
ndiff = imp.load_source("ndiff", "ndiff.py")
ndiff = load_module("ndiff", "ndiff.py")
for x in dir(ndiff):
if not x.startswith("_"):
globals()[x] = getattr(ndiff, x)
Expand Down

0 comments on commit 1d8c0e1

Please sign in to comment.