Skip to content

Commit

Permalink
Replace imp module (removed in Python 3.12)
Browse files Browse the repository at this point in the history
  • Loading branch information
mkow committed May 4, 2024
1 parent a1575a1 commit 123bb36
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
16 changes: 8 additions & 8 deletions codecut/cc_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import cc_base
import basicutils_7x as basicutils
import snap_cg
import imp
import importlib

try:
import modnaming
Expand Down Expand Up @@ -57,12 +57,12 @@ def go():
return True

if __name__ == "__main__":
imp.reload(modnaming)
imp.reload(module)
imp.reload(cc_base)
imp.reload(lfa)
imp.reload(maxcut)
imp.reload(snap_cg)
imp.reload(basicutils)
importlib.reload(modnaming)
importlib.reload(module)
importlib.reload(cc_base)
importlib.reload(lfa)
importlib.reload(maxcut)
importlib.reload(snap_cg)
importlib.reload(basicutils)
go()

13 changes: 11 additions & 2 deletions diaphora.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import os
import re
import sys
import imp
import time
import json
import decimal
Expand Down Expand Up @@ -120,6 +119,16 @@
fmt = "[Diaphora: %(asctime)s] %(levelname)s: %(message)s"
logging.basicConfig(format=fmt, level=logging.INFO)

#-------------------------------------------------------------------------------
def load_source(modname, filename):
# Copied from https://docs.python.org/3.12/whatsnew/3.12.html#imp as a
# replacement for the removed imp.load_source().
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.
loader.exec_module(module)
return module

#-------------------------------------------------------------------------------
def result_iter(cursor, arraysize=1000):
Expand Down Expand Up @@ -498,7 +507,7 @@ def load_hooks(self):

try:
log(f"Loading project specific Python script {self.project_script}")
module = imp.load_source("diaphora_hooks", self.project_script)
module = load_source("diaphora_hooks", self.project_script)
except:
err = str(sys.exc_info()[1])
print(f"Error loading project specific Python script: {err}")
Expand Down

0 comments on commit 123bb36

Please sign in to comment.