rohitg00/ai-engineering-from-scratch · error · OSError

os.system denied

Error message

os.system denied

What it means

The sandbox replaces os.system with a stub that always raises OSError, so candidate code attempting shell execution is blocked before any process spawns; this is a deliberate denial of the process-execution surface, triggered by the candidate calling os.system(cmd).

Solutions

  1. Rewrite the candidate to accomplish the task in pure Python without shelling out
  2. Catch OSError in the runner and score the attempt as a blocked/failed execution
  3. Similarly avoid os.popen and subprocess since the module itself is denied
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at phases/19-capstone-projects/72-code-exec-metric/code/main.py:84 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of rohitg00/ai-engineering-from-scratch@39ea8a1c6d (2026-08-26). Data as JSON: /api/errors/5b1b16ca7e52e12d. Report an issue: GitHub.

Appendix: source

Thrown at phases/19-capstone-projects/72-code-exec-metric/code/main.py:84

import builtins as _builtins

_DENIED_MODULES = {
    'subprocess', 'socket', 'shutil', 'requests', 'urllib',
    'urllib.request', 'urllib.error', 'urllib.parse', 'ctypes',
    'http.client', 'asyncio.subprocess',
}

_real_import = _builtins.__import__
def _guarded_import(name, *args, **kwargs):
    base = name.split('.')[0]
    if name in _DENIED_MODULES or base in _DENIED_MODULES:
        raise ImportError("denied module: %s" % name)
    return _real_import(name, *args, **kwargs)
_builtins.__import__ = _guarded_import

import os as _os
def _denied_system(_cmd):
    raise OSError("os.system denied")
_os.system = _denied_system

CODE = __PLACEHOLDER_CODE__
ASSERTIONS = __PLACEHOLDER_ASSERTIONS__

ns = {}
status = {"exit_code": "pass", "results": [], "detail": ""}
try:
    compiled = compile(CODE, "<candidate>", "exec")
except SyntaxError as exc:
    status["exit_code"] = "syntax_error"
    status["detail"] = "SyntaxError: %s" % exc
    sys.stdout.write(json.dumps(status))
    sys.exit(0)
except Exception as exc:
    status["exit_code"] = "error"
    status["detail"] = "compile error: %r" % exc
    sys.stdout.write(json.dumps(status))

View on GitHub (pinned to 39ea8a1c6d)