Skip to content

Commit

Permalink
Remove imp module from kernel.py
Browse files Browse the repository at this point in the history
  • Loading branch information
Patrick Fox committed Nov 25, 2024
1 parent c28a4b6 commit bc66ab1
Showing 1 changed file with 7 additions and 11 deletions.
18 changes: 7 additions & 11 deletions ck/ck/kernel.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
import sys
import json
import os
import imp # Needed to load CK Python modules from CK repositories
import importlib # Needed to load CK Python modules from CK repositories

initialized = False # True if initialized
# Needed to suppress all output (useful for CK-based web services)
Expand Down Expand Up @@ -4093,17 +4093,14 @@ def load_module_from_path(i):
xcfg = i.get('cfg', None)

# Find module
try:
x = imp.find_module(n, [p])
except ImportError as e: # pragma: no cover
return {'return': 1, 'error': 'can\'t find module code (path='+p+', name='+n+', err='+format(e)+')'}
x = importlib.machinery.PathFinder().find_spec(n, [p])
if not x:
return {'return': 1, 'error': 'can\'t find module code (path='+p+', name='+n+')'}

ff = x[0]
full_path = x[1]
full_path = x.origin

# Check if code has been already loaded
if full_path in work['cached_module_by_path'] and work['cached_module_by_path_last_modification'][full_path] == os.path.getmtime(full_path):
ff.close()
# Code already loaded
return work['cached_module_by_path'][full_path]

Expand Down Expand Up @@ -4131,12 +4128,11 @@ def load_module_from_path(i):
ruid = 'rt-'+r['data_uid']

try:
c = imp.load_module(ruid, ff, full_path, x[2])
c = importlib.util.module_from_spec(x)
x.loader.exec_module(c)
except ImportError as e: # pragma: no cover
return {'return': 1, 'error': 'can\'t load module code (path='+p+', name='+n+', err='+format(e)+')'}

x[0].close()

# Initialize module with this CK instance
c.ck = sys.modules[__name__]
if xcfg != None:
Expand Down

0 comments on commit bc66ab1

Please sign in to comment.