From 3ceddb787bd5e905c5637779067df990e66fefe6 Mon Sep 17 00:00:00 2001 From: Eliot Robson Date: Wed, 18 Nov 2026 17:26:30 -0500 Subject: [PATCH 1/2] Collapse duplicated student and workspace code paths Refactor only; no behavior change. Unifies the near-identical student and workspace implementations behind one shared, parameterized path in each layer: - _student_code_runner.py: student_code_runner and workspace_runner now wrap a single _run_sandbox helper; workspace mode is selected by workspace_dir and the main code object to exec is supplied by a load_main callback. - fixture.py: lift helper_modules into _SandboxBase and extract the shared subprocess/connect/send/read sequence into _SandboxBase._start_server. - plugin.py: _initialize_sandbox_fixture and _initialize_workspace_fixture share a new _resolve_sandbox_config helper that resolves the common config. Public API (sandbox, module_sandbox, workspace_sandbox fixtures, ConfigObject, StudentFixture, WorkspaceFixture) is unchanged. Co-Authored-By: Claude Opus 4.8 --- .../_student_code_runner.py | 183 +++++++++--------- src/pytest_prairielearn_grader/fixture.py | 145 ++++++++------ src/pytest_prairielearn_grader/plugin.py | 153 +++++++-------- 3 files changed, 235 insertions(+), 246 deletions(-) diff --git a/src/pytest_prairielearn_grader/_student_code_runner.py b/src/pytest_prairielearn_grader/_student_code_runner.py index 53cb6e5..5607775 100644 --- a/src/pytest_prairielearn_grader/_student_code_runner.py +++ b/src/pytest_prairielearn_grader/_student_code_runner.py @@ -447,10 +447,9 @@ def _find_helper_name_clashes(workspace_dir: str, helper_modules: dict[str, str] return clashes -async def student_code_runner( +async def _run_sandbox( + *, setup_code: str | None, - student_code: str, - student_file_name: str, timeout: float, import_whitelist: list[str] | None, import_blacklist: list[str] | None, @@ -458,7 +457,21 @@ async def student_code_runner( builtin_whitelist: list[str] | None, names_for_user_list: list[str] | None, helper_modules: dict[str, str] | None, + workspace_dir: str | None, + load_main: Callable[[], tuple[str, str]] | None, + main_timeout_message: str, ) -> tuple[dict[str, Any], dict[str, Any], ProcessStartResponse]: + """ + Shared implementation behind student_code_runner and workspace_runner. + + Runs the setup code, injects the names_for_user variables, and then runs a main code object: + the student submission for student mode, or the workspace entry-point file for workspace mode. + *load_main*, called inside the capture/timeout region, returns that code's (source, file_name); + pass None to skip the main step. When *workspace_dir* is given, the importer also replaces the + real builtins.__import__ and the directory is put at the front of sys.path, so that student + modules loaded via importlib.import_module() go through the same restrictions and resolve + against the workspace. + """ stdout_capture = io.StringIO() stderr_capture = io.StringIO() execution_error: Exception | None = None @@ -468,16 +481,27 @@ async def student_code_runner( student_code_vars: dict[str, Any] = {} student_code_vars["__builtins__"] = get_builtins(builtin_whitelist) - student_code_vars["__builtins__"]["__name__"] = "__main__" # Set __name__ to "__main__" to mimic the main module server_data = ServerDataImport(local_vars.get("__data_params", {})) - student_code_vars["__builtins__"]["__import__"] = get_custom_importer( + importer = get_custom_importer( import_whitelist, import_blacklist, + workspace_dir, helper_modules=None if helper_modules is None else _HelperModules(helper_modules, student_code_vars["__builtins__"]), server_data=server_data, ) _block_blacklisted_functions(import_blacklist) + student_code_vars["__builtins__"]["__import__"] = importer + if workspace_dir is not None: + # Patch the real builtins.__import__ so that workspace modules loaded via + # importlib.import_module() (which use the real builtins, not student_code_vars) + # also go through the same restrictions. The subprocess is dedicated to one + # grading session so this global patch is safe. + builtins.__import__ = importer # type: ignore[assignment] + # Insert workspace_dir at the front of sys.path so imports resolve against it. + # Use insert(0, ...) to take precedence over any previously added paths. + if workspace_dir not in sys.path: + sys.path.insert(0, workspace_dir) try: # First, execute the setup code if provided @@ -513,19 +537,19 @@ async def student_code_runner( # If not in local_vars (setup_code), try getting from starting_vars student_code_vars[var_name] = deepcopy(starting_vars[var_name]) - if execution_error is None: + if execution_error is None and load_main is not None: try: - # Next, compile student code. Make sure to handle errors in this later - # TODO have a better filename - code_setup = compile(student_code, student_file_name, "exec") + # Compile and run the main code (student submission or workspace entry point). + main_source, main_file_name = load_main() + main_code = compile(main_source, main_file_name, "exec") with redirect_stdout(stdout_capture), redirect_stderr(stderr_capture): await asyncio.wait_for( - asyncio.get_event_loop().run_in_executor(executor, exec, code_setup, student_code_vars, student_code_vars), + asyncio.get_event_loop().run_in_executor(executor, exec, main_code, student_code_vars, student_code_vars), timeout=timeout, ) except asyncio.TimeoutError: - execution_error = asyncio.TimeoutError("Student code execution timed out") + execution_error = asyncio.TimeoutError(main_timeout_message) except Exception as e: execution_error = e # TODO this traceback only shows the last line with the exception. @@ -554,6 +578,33 @@ async def student_code_runner( return local_vars, student_code_vars, result_dict +async def student_code_runner( + setup_code: str | None, + student_code: str, + student_file_name: str, + timeout: float, + import_whitelist: list[str] | None, + import_blacklist: list[str] | None, + starting_vars: dict[str, Any] | None, + builtin_whitelist: list[str] | None, + names_for_user_list: list[str] | None, + helper_modules: dict[str, str] | None, +) -> tuple[dict[str, Any], dict[str, Any], ProcessStartResponse]: + return await _run_sandbox( + setup_code=setup_code, + timeout=timeout, + import_whitelist=import_whitelist, + import_blacklist=import_blacklist, + starting_vars=starting_vars, + builtin_whitelist=builtin_whitelist, + names_for_user_list=names_for_user_list, + helper_modules=helper_modules, + workspace_dir=None, + load_main=lambda: (student_code, student_file_name), + main_timeout_message="Student code execution timed out", + ) + + async def workspace_runner( workspace_dir: str, exec_entry: str | None, @@ -577,14 +628,9 @@ async def workspace_runner( helper_clashes = _find_helper_name_clashes(workspace_dir, helper_modules) shadowed_files = [f for f in _find_shadowed_workspace_files(workspace_dir) if f not in helper_clashes] - stdout_capture = io.StringIO() - stderr_capture = io.StringIO() - execution_error: Exception | None = None - exception_traceback = None - local_vars = deepcopy(starting_vars) if starting_vars else {} - local_vars["__from_server_json"] = from_server_json - if helper_clashes or shadowed_files: + local_vars = deepcopy(starting_vars) if starting_vars else {} + local_vars["__from_server_json"] = from_server_json reasons = [] if helper_clashes: reasons.append(f"These names are reserved for modules that this question provides: {', '.join(helper_clashes)}.") @@ -601,88 +647,33 @@ async def workspace_runner( } return local_vars, {}, start_response - student_code_vars: dict[str, Any] = {} - student_code_vars["__builtins__"] = get_builtins(builtin_whitelist) - student_code_vars["__builtins__"]["__name__"] = "__main__" - server_data = ServerDataImport(local_vars.get("__data_params", {})) - workspace_importer = get_custom_importer( - import_whitelist, - import_blacklist, - workspace_dir, - helper_modules=None if helper_modules is None else _HelperModules(helper_modules, student_code_vars["__builtins__"]), - server_data=server_data, - ) - _block_blacklisted_functions(import_blacklist) - student_code_vars["__builtins__"]["__import__"] = workspace_importer - # Patch the real builtins.__import__ so that workspace modules loaded via - # importlib.import_module() (which use the real builtins, not student_code_vars) - # also go through the same restrictions. The subprocess is dedicated to one - # grading session so this global patch is safe. - builtins.__import__ = workspace_importer # type: ignore[assignment] - - # Insert workspace_dir at the front of sys.path so imports resolve against it. - # Use insert(0, ...) to take precedence over any previously added paths. - if workspace_dir not in sys.path: - sys.path.insert(0, workspace_dir) - - try: - if setup_code: - code_setup = compile(setup_code, "", "exec") - with redirect_stdout(stdout_capture), redirect_stderr(stderr_capture): - await asyncio.wait_for( - asyncio.get_event_loop().run_in_executor(executor, exec, code_setup, student_code_vars, local_vars), - timeout=timeout, - ) - except asyncio.TimeoutError: - execution_error = asyncio.TimeoutError("Setup code execution timed out") - except Exception as e: - execution_error = e - - server_data.available = False - - if names_for_user_list is not None: - for var_name in names_for_user_list: - if var_name in local_vars: - student_code_vars[var_name] = deepcopy(local_vars[var_name]) - elif starting_vars is not None and var_name in starting_vars: - student_code_vars[var_name] = deepcopy(starting_vars[var_name]) - # Optionally exec an entry-point file at startup (e.g. "main.py") so that # module-level side effects in the student's root script are applied. - if execution_error is None and exec_entry is not None: - entry_path = pathlib.Path(workspace_dir) / exec_entry - try: + load_main: Callable[[], tuple[str, str]] | None = None + if exec_entry is not None: + entry = exec_entry + + def load_entry() -> tuple[str, str]: + entry_path = pathlib.Path(workspace_dir) / entry entry_source = entry_path.read_text(encoding="utf-8") - entry_code = compile(entry_source, str(entry_path), "exec") populate_linecache(entry_source, str(entry_path)) - with redirect_stdout(stdout_capture), redirect_stderr(stderr_capture): - await asyncio.wait_for( - asyncio.get_event_loop().run_in_executor(executor, exec, entry_code, student_code_vars, student_code_vars), - timeout=timeout, - ) - except asyncio.TimeoutError: - execution_error = asyncio.TimeoutError("Entry-point file execution timed out") - except Exception as e: - execution_error = e - exception_traceback = traceback.format_exc(limit=-1) - - if execution_error is None: - status = ProcessStatusCode.SUCCESS - elif isinstance(execution_error, asyncio.TimeoutError): - status = ProcessStatusCode.TIMEOUT - else: - status = ProcessStatusCode.EXCEPTION - - result_dict: ProcessStartResponse = { - "status": status, - "stdout": stdout_capture.getvalue(), - "stderr": stderr_capture.getvalue(), - "execution_error": type(execution_error).__name__ if execution_error else None, - "execution_message": str(execution_error) if execution_error else None, - "execution_traceback": str(exception_traceback), - } - - return local_vars, student_code_vars, result_dict + return entry_source, str(entry_path) + + load_main = load_entry + + return await _run_sandbox( + setup_code=setup_code, + timeout=timeout, + import_whitelist=import_whitelist, + import_blacklist=import_blacklist, + starting_vars=starting_vars, + builtin_whitelist=builtin_whitelist, + names_for_user_list=names_for_user_list, + helper_modules=helper_modules, + workspace_dir=workspace_dir, + load_main=load_main, + main_timeout_message="Entry-point file execution timed out", + ) def encode_server_failure(message: str) -> bytes: diff --git a/src/pytest_prairielearn_grader/fixture.py b/src/pytest_prairielearn_grader/fixture.py index 596062d..6b87ef8 100644 --- a/src/pytest_prairielearn_grader/fixture.py +++ b/src/pytest_prairielearn_grader/fixture.py @@ -9,6 +9,7 @@ import subprocess import sys import tempfile +from collections.abc import Callable from importlib.resources import files from pathlib import Path from typing import Any @@ -224,6 +225,7 @@ class _SandboxBase: builtin_whitelist: list[str] | None names_for_user_list: list[str] | None worker_username: str | None + helper_modules: dict[str, str] | None data_files: dict[str, Path] | None _data_files_dir: Path | None _accumulated_stdout: list[str] @@ -238,6 +240,7 @@ def __init__( builtin_whitelist: list[str] | None, names_for_user_list: list[str] | None, worker_username: str | None, + helper_modules: dict[str, str] | None = None, data_files: dict[str, Path] | None = None, ) -> None: self.import_whitelist = import_whitelist @@ -246,6 +249,7 @@ def __init__( self.builtin_whitelist = builtin_whitelist self.names_for_user_list = names_for_user_list self.worker_username = worker_username + self.helper_modules = helper_modules self.data_files = data_files self._data_files_dir = None self.process = None @@ -254,6 +258,14 @@ def __init__( self._server_failure = None self._timed_out_call = None + def _start_server(self, initialization_timeout: float, build_start_message: Callable[[], Any]) -> ProcessStartResponse: + """Spawn the subprocess, send the start message it builds, and read the start response.""" + self._start_subprocess() + json_message = build_start_message() + self._connect_to_server(initialization_timeout) + self._send_json_object(json_message) + return self._read_start_response(initialization_timeout) + def _assert_process_running(self) -> None: assert self.process is not None, "Sandbox process is not running. Please start it first." process_return_code = self.process.poll() @@ -488,14 +500,20 @@ def __init__( data_files: dict[str, Path] | None = None, ) -> None: super().__init__( - import_whitelist, import_blacklist, starting_vars, builtin_whitelist, names_for_user_list, worker_username, data_files + import_whitelist, + import_blacklist, + starting_vars, + builtin_whitelist, + names_for_user_list, + worker_username, + helper_modules=helper_modules, + data_files=data_files, ) self.leading_file = file_names.leading_file self.trailing_file = file_names.trailing_file self.student_code_file = file_names.student_code_file self.setup_code_file = file_names.setup_code_file self.notebook_cell_tag = notebook_cell_tag - self.helper_modules = helper_modules def start_student_code_server(self, *, initialization_timeout: float = DEFAULT_TIMEOUT) -> ProcessStartResponse: if self.worker_username is not None: @@ -503,43 +521,40 @@ def start_student_code_server(self, *, initialization_timeout: float = DEFAULT_T else: logger.debug("Starting student code server without dropping privileges.") - self._start_subprocess() - - student_code = "" - if self.leading_file.is_file(): - student_code += self.leading_file.read_text(encoding="utf-8") - student_code += os.linesep - if self.student_code_file.is_file(): - if self.student_code_file.suffix.lower() == ".ipynb": - student_code += _extract_notebook_code(self.student_code_file, self.notebook_cell_tag) - else: - student_code += self.student_code_file.read_text(encoding="utf-8") - if self.trailing_file.is_file(): - student_code += os.linesep - student_code += self.trailing_file.read_text(encoding="utf-8") - - setup_code = None - if self.setup_code_file.is_file(): - setup_code = self.setup_code_file.read_text(encoding="utf-8") - - self._connect_to_server(initialization_timeout) - - # TODO make this a shared type - json_message = ProcessStartRequest( - message_type=MessageType.START, - student_code=student_code, - student_file_name=str(self.student_code_file), - setup_code=setup_code, - initialization_timeout=initialization_timeout, - import_whitelist=self.import_whitelist, - import_blacklist=self.import_blacklist, - starting_vars=self.starting_vars, - builtin_whitelist=self.builtin_whitelist, - names_for_user_list=self.names_for_user_list, - helper_modules=self.helper_modules, - ) - self._send_json_object(json_message) - return self._read_start_response(initialization_timeout) + def build_start_message() -> ProcessStartRequest: + student_code = "" + if self.leading_file.is_file(): + student_code += self.leading_file.read_text(encoding="utf-8") + student_code += os.linesep + if self.student_code_file.is_file(): + if self.student_code_file.suffix.lower() == ".ipynb": + student_code += _extract_notebook_code(self.student_code_file, self.notebook_cell_tag) + else: + student_code += self.student_code_file.read_text(encoding="utf-8") + if self.trailing_file.is_file(): + student_code += os.linesep + student_code += self.trailing_file.read_text(encoding="utf-8") + + setup_code = None + if self.setup_code_file.is_file(): + setup_code = self.setup_code_file.read_text(encoding="utf-8") + + # TODO make this a shared type + return ProcessStartRequest( + message_type=MessageType.START, + student_code=student_code, + student_file_name=str(self.student_code_file), + setup_code=setup_code, + initialization_timeout=initialization_timeout, + import_whitelist=self.import_whitelist, + import_blacklist=self.import_blacklist, + starting_vars=self.starting_vars, + builtin_whitelist=self.builtin_whitelist, + names_for_user_list=self.names_for_user_list, + helper_modules=self.helper_modules, + ) + + return self._start_server(initialization_timeout, build_start_message) def query_setup_raw(self, var_to_query: str) -> SetupQueryResponse: self._assert_no_server_failure() @@ -602,38 +617,42 @@ def __init__( data_files: dict[str, Path] | None = None, ) -> None: super().__init__( - import_whitelist, import_blacklist, starting_vars, builtin_whitelist, names_for_user_list, worker_username, data_files + import_whitelist, + import_blacklist, + starting_vars, + builtin_whitelist, + names_for_user_list, + worker_username, + helper_modules=helper_modules, + data_files=data_files, ) self.workspace_dir = workspace_dir self.setup_code_file = setup_code_file self.exec_entry = exec_entry - self.helper_modules = helper_modules def start_workspace_server(self, *, initialization_timeout: float = DEFAULT_TIMEOUT) -> ProcessStartResponse: """Start the sandbox subprocess and initialise the workspace.""" - self._start_subprocess() - setup_code: str | None = None - if self.setup_code_file.is_file(): - setup_code = self.setup_code_file.read_text(encoding="utf-8") - - self._connect_to_server(initialization_timeout) - - json_message = WorkspaceStartRequest( - message_type=MessageType.START_WORKSPACE, - workspace_dir=str(self.workspace_dir), - exec_entry=self.exec_entry, - setup_code=setup_code, - initialization_timeout=initialization_timeout, - import_whitelist=self.import_whitelist, - import_blacklist=self.import_blacklist, - starting_vars=self.starting_vars, - builtin_whitelist=self.builtin_whitelist, - names_for_user_list=self.names_for_user_list, - helper_modules=self.helper_modules, - ) - self._send_json_object(json_message) - return self._read_start_response(initialization_timeout) + def build_start_message() -> WorkspaceStartRequest: + setup_code: str | None = None + if self.setup_code_file.is_file(): + setup_code = self.setup_code_file.read_text(encoding="utf-8") + + return WorkspaceStartRequest( + message_type=MessageType.START_WORKSPACE, + workspace_dir=str(self.workspace_dir), + exec_entry=self.exec_entry, + setup_code=setup_code, + initialization_timeout=initialization_timeout, + import_whitelist=self.import_whitelist, + import_blacklist=self.import_blacklist, + starting_vars=self.starting_vars, + builtin_whitelist=self.builtin_whitelist, + names_for_user_list=self.names_for_user_list, + helper_modules=self.helper_modules, + ) + + return self._start_server(initialization_timeout, build_start_message) def __repr__(self) -> str: return f"WorkspaceFixture(workspace_dir={self.workspace_dir})" diff --git a/src/pytest_prairielearn_grader/plugin.py b/src/pytest_prairielearn_grader/plugin.py index 6352b15..603043d 100644 --- a/src/pytest_prairielearn_grader/plugin.py +++ b/src/pytest_prairielearn_grader/plugin.py @@ -293,46 +293,50 @@ def tests_dir(request: pytest.FixtureRequest) -> Path: return path -def _initialize_sandbox_fixture( +@dataclass(frozen=True, slots=True) +class _ResolvedSandboxConfig: + """The configuration shared by the student and workspace fixtures, resolved once per fixture.""" + + initialization_timeout: float + import_whitelist: list[str] | None + import_blacklist: list[str] | None + builtin_whitelist: list[str] | None + starting_vars: dict[str, Any] + names_for_user_list: list[str] | None + helper_module_names: list[str] | None + data_file_names: list[str] | None + # The module-level ConfigObject, kept so each fixture can read its own extra fields + # (notebook_cell_tag for the student fixture, workspace_exec_entry for the workspace one). + config_object: ConfigObject | None + + +def _resolve_sandbox_config( request: pytest.FixtureRequest, data_json: dict[str, Any] | None, - file_names: StudentFiles, -) -> tuple[StudentFixture, float]: +) -> _ResolvedSandboxConfig: """ - Common initialization logic for both sandbox and module_sandbox fixtures. - Handles parameter parsing, timeout configuration, and StudentFixture creation. - Returns the fixture and the initialization timeout. + Resolve the configuration common to the sandbox, module_sandbox and workspace_sandbox fixtures. - Args: - request: The pytest fixture request - data_json: Data loaded from data.json file - file_names: StudentFiles namedtuple with file paths + Reads params from data.json and the module-level ConfigObject (or module-level timeout/mark + when there's no ConfigObject), merges them with the ConfigObject taking precedence, and injects + the names_for_user values into starting_vars. """ - # Default timeout TODO make this a command line option? initialization_timeout: float = DEFAULT_SANDBOX_TIMEOUT - - if data_json is None: - params_dict = {} - logger.debug("No data.json provided, using empty params") - else: - params_dict = data_json.get("params", {}) - logger.debug(f"Loaded params from data.json: {list(params_dict.keys())}") + params_dict = data_json.get("params", {}) if data_json is not None else {} # Check for module-level ConfigObject that overrides all other configuration config_dict: dict[str, Any] = {} - notebook_cell_tag: str | None = None + config_object: ConfigObject | None = None helper_module_names: list[str] | None = None data_file_names: list[str] | None = None if hasattr(request, "module") and hasattr(request.module, "autograder_config"): config_object = request.module.autograder_config if not isinstance(config_object, ConfigObject): pytest.fail(f"autograder_config must be a ConfigObject instance, got {type(config_object).__name__}") - logger.debug("Using module-level autograder_config - overriding all other configuration sources") config_dict = config_object.to_dict() # Handle timeout and starting_vars separately as they're not in to_dict() initialization_timeout = config_object.sandbox_timeout config_starting_vars = config_object.starting_vars - notebook_cell_tag = config_object.notebook_cell_tag helper_module_names = config_object.helper_modules data_file_names = config_object.data_files else: @@ -340,13 +344,11 @@ def _initialize_sandbox_fixture( # This allows setting: sandbox_timeout = 0.5 at module level if hasattr(request, "module") and hasattr(request.module, "sandbox_timeout"): initialization_timeout = request.module.sandbox_timeout - logger.debug(f"Using module-level timeout: {initialization_timeout}s") # Check for the custom mark (overrides module-level setting) marker = request.node.get_closest_marker("sandbox_timeout") if marker and marker.args: initialization_timeout = marker.args[0] - logger.debug(f"Using test-specific timeout override: {initialization_timeout}s") config_starting_vars = {} @@ -374,8 +376,6 @@ def _initialize_sandbox_fixture( **config_starting_vars, } - logger.debug(f"Import restrictions - whitelist: {import_whitelist}, blacklist: {import_blacklist}") - # Process names_for_user_list to inject variables from params_dict or starting_vars if names_for_user_list is not None: for name in names_for_user_list: @@ -387,20 +387,45 @@ def _initialize_sandbox_fixture( starting_vars[name] = value - fixture = StudentFixture( - file_names=file_names, + return _ResolvedSandboxConfig( + initialization_timeout=initialization_timeout, import_whitelist=import_whitelist, import_blacklist=import_blacklist, - starting_vars=starting_vars, builtin_whitelist=builtin_whitelist, + starting_vars=starting_vars, names_for_user_list=names_for_user_list, + helper_module_names=helper_module_names, + data_file_names=data_file_names, + config_object=config_object, + ) + + +def _initialize_sandbox_fixture( + request: pytest.FixtureRequest, + data_json: dict[str, Any] | None, + file_names: StudentFiles, +) -> tuple[StudentFixture, float]: + """ + Common initialization logic for both sandbox and module_sandbox fixtures. + Returns the StudentFixture and the initialization timeout. + """ + config = _resolve_sandbox_config(request, data_json) + notebook_cell_tag = config.config_object.notebook_cell_tag if config.config_object is not None else None + + fixture = StudentFixture( + file_names=file_names, + import_whitelist=config.import_whitelist, + import_blacklist=config.import_blacklist, + starting_vars=config.starting_vars, + builtin_whitelist=config.builtin_whitelist, + names_for_user_list=config.names_for_user_list, worker_username=request.config.getoption("--worker-username"), notebook_cell_tag=notebook_cell_tag, - helper_modules=_load_helper_modules(request, helper_module_names, import_blacklist), - data_files=_resolve_data_files(request, data_file_names), + helper_modules=_load_helper_modules(request, config.helper_module_names, config.import_blacklist), + data_files=_resolve_data_files(request, config.data_file_names), ) - return fixture, initialization_timeout + return fixture, config.initialization_timeout def _get_config_output_level(module: ModuleType | None) -> str | None: @@ -655,54 +680,8 @@ def _initialize_workspace_fixture( workspace_dir: Path, ) -> tuple[WorkspaceFixture, float]: """Initialise a WorkspaceFixture analogously to _initialize_sandbox_fixture.""" - initialization_timeout: float = DEFAULT_SANDBOX_TIMEOUT - - params_dict = data_json.get("params", {}) if data_json is not None else {} - - config_dict: dict[str, Any] = {} - exec_entry: str | None = None - helper_module_names: list[str] | None = None - data_file_names: list[str] | None = None - if hasattr(request, "module") and hasattr(request.module, "autograder_config"): - config_object = request.module.autograder_config - if not isinstance(config_object, ConfigObject): - pytest.fail(f"autograder_config must be a ConfigObject instance, got {type(config_object).__name__}") - config_dict = config_object.to_dict() - initialization_timeout = config_object.sandbox_timeout - config_starting_vars = config_object.starting_vars - exec_entry = config_object.workspace_exec_entry - helper_module_names = config_object.helper_modules - data_file_names = config_object.data_files - else: - if hasattr(request, "module") and hasattr(request.module, "sandbox_timeout"): - initialization_timeout = request.module.sandbox_timeout - marker = request.node.get_closest_marker("sandbox_timeout") - if marker and marker.args: - initialization_timeout = marker.args[0] - config_starting_vars = {} - - names_for_user_list: list[str] | None = None - if "names_for_user" in config_dict: - names_for_user_list = config_dict["names_for_user"] - elif "names_for_user" in params_dict: - names_for_user_list = [item["name"] for item in params_dict["names_for_user"]] - - merged_params = {**params_dict, **config_dict} - import_whitelist = merged_params.get("import_whitelist") - import_blacklist = merged_params.get("import_blacklist", DEFAULT_IMPORT_BLACKLIST) - builtin_whitelist = merged_params.get("builtin_whitelist") - - starting_vars: dict[str, Any] = { - "__data_params": deepcopy(params_dict) if data_json is not None else {}, - **config_starting_vars, - } - if names_for_user_list is not None: - for name in names_for_user_list: - if name in starting_vars and name != "__data_params": - value = starting_vars[name] - else: - value = params_dict.get(name, None) - starting_vars[name] = value + config = _resolve_sandbox_config(request, data_json) + exec_entry = config.config_object.workspace_exec_entry if config.config_object is not None else None # Resolve setup_code_file from the test module's tests folder tests_dir = get_tests_dir(request.module) @@ -711,17 +690,17 @@ def _initialize_workspace_fixture( fixture = WorkspaceFixture( workspace_dir=workspace_dir, setup_code_file=setup_code_file, - import_whitelist=import_whitelist, - import_blacklist=import_blacklist, - starting_vars=starting_vars, - builtin_whitelist=builtin_whitelist, - names_for_user_list=names_for_user_list, + import_whitelist=config.import_whitelist, + import_blacklist=config.import_blacklist, + starting_vars=config.starting_vars, + builtin_whitelist=config.builtin_whitelist, + names_for_user_list=config.names_for_user_list, worker_username=request.config.getoption("--worker-username"), exec_entry=exec_entry, - helper_modules=_load_helper_modules(request, helper_module_names, import_blacklist), - data_files=_resolve_data_files(request, data_file_names), + helper_modules=_load_helper_modules(request, config.helper_module_names, config.import_blacklist), + data_files=_resolve_data_files(request, config.data_file_names), ) - return fixture, initialization_timeout + return fixture, config.initialization_timeout @pytest.fixture From 09c6e19070d903a416ba4cd99e2b324afd99f407 Mon Sep 17 00:00:00 2001 From: Eliot Robson Date: Wed, 18 Nov 2026 17:59:21 -0500 Subject: [PATCH 2/2] Restore config-source debug logging in _resolve_sandbox_config The collapse dropped the debug lines that recorded which source each setting came from. Restore them in the unified helper so both the student and workspace paths log them (run.sh runs pytest at --log-cli-level=DEBUG). Logging only; no behavior change. Co-Authored-By: Claude Opus 4.8 --- src/pytest_prairielearn_grader/plugin.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/pytest_prairielearn_grader/plugin.py b/src/pytest_prairielearn_grader/plugin.py index 603043d..0f25504 100644 --- a/src/pytest_prairielearn_grader/plugin.py +++ b/src/pytest_prairielearn_grader/plugin.py @@ -322,7 +322,13 @@ def _resolve_sandbox_config( the names_for_user values into starting_vars. """ initialization_timeout: float = DEFAULT_SANDBOX_TIMEOUT - params_dict = data_json.get("params", {}) if data_json is not None else {} + + if data_json is None: + params_dict = {} + logger.debug("No data.json provided, using empty params") + else: + params_dict = data_json.get("params", {}) + logger.debug(f"Loaded params from data.json: {list(params_dict.keys())}") # Check for module-level ConfigObject that overrides all other configuration config_dict: dict[str, Any] = {} @@ -333,6 +339,7 @@ def _resolve_sandbox_config( config_object = request.module.autograder_config if not isinstance(config_object, ConfigObject): pytest.fail(f"autograder_config must be a ConfigObject instance, got {type(config_object).__name__}") + logger.debug("Using module-level autograder_config - overriding all other configuration sources") config_dict = config_object.to_dict() # Handle timeout and starting_vars separately as they're not in to_dict() initialization_timeout = config_object.sandbox_timeout @@ -344,11 +351,13 @@ def _resolve_sandbox_config( # This allows setting: sandbox_timeout = 0.5 at module level if hasattr(request, "module") and hasattr(request.module, "sandbox_timeout"): initialization_timeout = request.module.sandbox_timeout + logger.debug(f"Using module-level timeout: {initialization_timeout}s") # Check for the custom mark (overrides module-level setting) marker = request.node.get_closest_marker("sandbox_timeout") if marker and marker.args: initialization_timeout = marker.args[0] + logger.debug(f"Using test-specific timeout override: {initialization_timeout}s") config_starting_vars = {} @@ -370,6 +379,8 @@ def _resolve_sandbox_config( # TODO make sure this contains only valid builtins builtin_whitelist = merged_params.get("builtin_whitelist") + logger.debug(f"Import restrictions - whitelist: {import_whitelist}, blacklist: {import_blacklist}") + # Merge starting_vars: __data_params always present, then config_starting_vars starting_vars: dict[str, Any] = { "__data_params": deepcopy(params_dict) if data_json is not None else {},