Skip to content

add context manager support#307

Open
samuelcolvin wants to merge 1 commit intomainfrom
context-manager
Open

add context manager support#307
samuelcolvin wants to merge 1 commit intomainfrom
context-manager

Conversation

@samuelcolvin
Copy link
Copy Markdown
Member

Implements the Python context manager protocol (__enter__/__exit__) and the open() builtin function for sandboxed file I/O via OsCall.

The with statement compiles to bytecode using existing opcodes (Dup, CallAttr, exception table entries) with FinallyTarget integration for return/break/continue. The open() builtin yields OsCall(FileOpen) to the host, which returns file content as MontyObject::FileData. A new FileObject heap type provides read(), readline(), write(), close(), and the context manager methods.

Key changes:

  • Parser: parse with statements, desugar multiple items to nested nodes
  • Compiler: compile_with() emits setup/body/normal-exit/exception-handler bytecode
  • Builtins: BuiltinsFunctions::call() returns CallResult (was Value) for OsCall support
  • New types: FileObject, OsFunction::FileOpen/FileClose, MontyObject::FileData
  • Test infra: virtual open() for CPython test compatibility

Implements the Python context manager protocol (`__enter__`/`__exit__`) and
the `open()` builtin function for sandboxed file I/O via OsCall.

The `with` statement compiles to bytecode using existing opcodes (Dup, CallAttr,
exception table entries) with FinallyTarget integration for return/break/continue.
The `open()` builtin yields OsCall(FileOpen) to the host, which returns file
content as MontyObject::FileData. A new FileObject heap type provides read(),
readline(), write(), close(), and the context manager methods.

Key changes:
- Parser: parse `with` statements, desugar multiple items to nested nodes
- Compiler: compile_with() emits setup/body/normal-exit/exception-handler bytecode
- Builtins: BuiltinsFunctions::call() returns CallResult (was Value) for OsCall support
- New types: FileObject, OsFunction::FileOpen/FileClose, MontyObject::FileData
- Test infra: virtual open() for CPython test compatibility

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@codspeed-hq
Copy link
Copy Markdown

codspeed-hq Bot commented Mar 31, 2026

Merging this PR will not alter performance

✅ 13 untouched benchmarks


Comparing context-manager (311a025) with main (a5e2266)

Open in CodSpeed

@github-actions
Copy link
Copy Markdown

Codecov Results 📊

❌ Patch coverage is 59.24%. Project has 4131 uncovered lines.
❌ Project coverage is 85.81%. Comparing base (base) to head (head).

Files with missing lines (8)
File Patch % Lines
crates/monty/src/types/file_object.rs 48.97% ⚠️ 99 Missing and 12 partials
crates/monty/src/bytecode/compiler.rs 64.34% ⚠️ 46 Missing and 7 partials
crates/monty/src/builtins/open.rs 63.41% ⚠️ 15 Missing and 8 partials
crates/monty/src/prepare.rs 52.38% ⚠️ 20 Missing and 2 partials
crates/monty/src/object.rs 55.56% ⚠️ 4 Missing and 2 partials
crates/monty/src/parse.rs 89.66% ⚠️ 3 Missing and 3 partials
crates/monty/src/heap_data.rs 42.86% ⚠️ 4 Missing and 1 partials
crates/monty/src/builtins/mod.rs 90.00% ⚠️ 1 Missing
Coverage diff
@@            Coverage Diff             @@
##          main       #PR       +/-##
==========================================
- Coverage    86.24%    85.81%    -0.43%
==========================================
  Files          108       110        +2
  Lines        28651     29106      +455
  Branches     61876     62908     +1032
==========================================
+ Hits         24709     24975      +266
- Misses        3942      4131      +189
- Partials      1845      1880       +35

Generated by Codecov Action

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