From 6c850f58d9d2d0d00bb3db03573de10ea33df073 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?The=20Vinh=20LUONG=20=28L=C6=AF=C6=A0NG=20Th=E1=BA=BF=20Vi?= =?UTF-8?q?nh=29?= Date: Wed, 8 Nov 2023 10:03:09 -0800 Subject: [PATCH] update xqueue_watcher.jailedgrader to comply with Python 3.12's removal of `imp` standard library module --- xqueue_watcher/jailedgrader.py | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/xqueue_watcher/jailedgrader.py b/xqueue_watcher/jailedgrader.py index b9c4f45..e354965 100644 --- a/xqueue_watcher/jailedgrader.py +++ b/xqueue_watcher/jailedgrader.py @@ -4,7 +4,8 @@ import codecs import os import sys -import imp +import importlib.util +import importlib.machinery import json import random import gettext @@ -21,6 +22,19 @@ TIMEOUT = 1 + +# Python 3.12: `imp` module removed: docs.python.org/3/whatsnew/3.12.html#imp +def load_source(modname, filename): + 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. + # Uncomment the following line to cache the module. + # sys.modules[module.__name__] = module + loader.exec_module(module) + return module + + def path_to_six(): """ Return the full path to six.py @@ -122,7 +136,7 @@ def grade(self, grader_path, grader_config, submission): # Import the grader, straight from the original file. (It probably isn't in # sys.path, and we may be in a long running gunicorn process, so we don't # want to add stuff to sys.path either.) - grader_module = imp.load_source("grader_module", str(grader_path)) + grader_module = load_source("grader_module", str(grader_path)) grader = grader_module.grader # Preprocess for grader-specified errors