Skip to content

Block blacklisted functions however student code reaches them - #39

Merged
eliotwrobson merged 2 commits into
mainfrom
claude/blacklist-functions
Nov 11, 2026
Merged

eliotwrobson merged 2 commits into
mainfrom
claude/blacklist-functions

Conversation

@eliotwrobson

Copy link
Copy Markdown
Collaborator

Lets questions block a single library function with import_blacklist, instead of overriding it in setup code. Before, import_blacklist=["numpy.linalg.inv"] only blocked from numpy.linalg import inv, so np.linalg.inv(A) and la.inv(A) still worked.

  • Startup: for each dotted entry whose parent is an importable module, and whose last part is a callable that isn't a module, the sandbox replaces it in that module with a function that raises RuntimeError("'numpy.linalg.inv' is blacklisted and cannot be used."). It does this before setup code runs, in both sandbox and workspace_sandbox.
  • Other entries: module entries ("os", "matplotlib.pyplot"), constants and entries whose module isn't installed are left to the importer as before.
  • Scope: modules are shared within the sandbox, so setup code and helper modules can't call the function either. The function's own module is imported at startup, which adds its import time to startup.

Ran the test suite inside the full grader image with this branch on PYTHONPATH. Before this change, the new scenario's np.linalg.inv and la.inv calls succeed.

🤖 Generated with Claude Code

An import_blacklist entry such as numpy.linalg.inv only blocked
'from numpy.linalg import inv', so np.linalg.inv(A) still worked. The
sandbox now replaces the function in its module with one that raises.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@eliotwrobson
eliotwrobson merged commit 73451f0 into main Nov 11, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant