Skip to content

Commit

Permalink
ndiff: Fix support for Python 3.12
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 ddfa361 commit e87be0b
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions ndiff/ndifftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,23 @@

import xml.dom.minidom

import imp
# import os
import importlib.machinery

# Suggested conversion for imp.load_module from
# https://github.com/python/cpython/issues/104212
def load_module(module_name, filename):
# script_path = os.path.abspath(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 e87be0b

Please sign in to comment.