Skip to content

fix type-object marshalling across the sandbox boundary#478

Open
samuelcolvin wants to merge 3 commits into
mainfrom
builtins-attrs
Open

fix type-object marshalling across the sandbox boundary#478
samuelcolvin wants to merge 3 commits into
mainfrom
builtins-attrs

Conversation

@samuelcolvin
Copy link
Copy Markdown
Member

@samuelcolvin samuelcolvin commented May 30, 2026

Modeled stdlib type objects (PosixPath, datetime., re., io.*, …) were reconstructed host-side via getattr(builtins, name), raising AttributeError since they don't live in builtins. They now resolve from their real defining module, each cached in its own PyOnceLock. The Path class maps to PurePosixPath, consistent with how Path instances round-trip.

Also fix the inverse direction: a type object passed into the sandbox (input or external-call return) previously degraded to a callable; recognized builtins and modeled stdlib types are now preserved as MontyObject::Type so they round-trip and isinstance works inside the sandbox.


Summary by cubic

Fixes type-object marshalling across the sandbox boundary so modeled stdlib types resolve from their real modules and incoming host types are preserved by identity. pathlib.Path maps to pathlib.PurePosixPath for consistency.

  • Bug Fixes
    • Sandbox → host: resolve modeled stdlib types from datetime, re, io, typing (not builtins); map pathlib.Pathpathlib.PurePosixPath; cache each resolved class in its own PyOnceLock.
    • Host → sandbox: preserve recognized builtins and modeled stdlib classes as type objects (MontyObject::Type), matching by identity; match all pathlib via issubclass(PurePath); also preserve NoneType and ellipsis; unmodeled classes still degrade to callables.
    • Added tests for type-object args/returns, identity round-trips, isinstance, and unconvertible-instance errors; updated limitations docs; exported Type from the crate root.

Written for commit b0d574d. Summary will update on new commits.

Review in cubic

Modeled stdlib type objects (PosixPath, datetime.*, re.*, io.*, …) were
reconstructed host-side via getattr(builtins, name), raising AttributeError
since they don't live in builtins. They now resolve from their real defining
module, each cached in its own PyOnceLock. The Path class maps to
PurePosixPath, consistent with how Path instances round-trip.

Also fix the inverse direction: a type object passed into the sandbox (input
or external-call return) previously degraded to a callable; recognized
builtins and modeled stdlib types are now preserved as MontyObject::Type so
they round-trip and isinstance works inside the sandbox.

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

codspeed-hq Bot commented May 30, 2026

Merging this PR will not alter performance

✅ 17 untouched benchmarks
⏩ 15 skipped benchmarks1


Comparing builtins-attrs (b0d574d) with main (7f7d08e)

Open in CodSpeed

Footnotes

  1. 15 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

Copy link
Copy Markdown
Contributor

@cubic-dev-ai cubic-dev-ai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2 issues found across 6 files

Reply with feedback, questions, or to request a fix.

Re-trigger cubic

Comment thread crates/monty-python/src/convert.rs Outdated
Comment thread crates/monty-python/src/convert.rs Outdated
@github-actions
Copy link
Copy Markdown

github-actions Bot commented May 30, 2026

Codecov Results 📊

✅ Patch coverage is 100.00%. Project has 24061 uncovered lines.
✅ Project coverage is 66.36%. Comparing base (base) to head (head).

Files with missing lines (1)
File Patch % Lines
crates/monty-python/src/convert.rs 100.00% ⚠️ 5 partials
Coverage diff
@@            Coverage Diff             @@
##          main       #PR       +/-##
==========================================
+ Coverage    66.09%    66.36%    +0.27%
==========================================
  Files          281       281         —
  Lines        71447     71521       +74
  Branches    153474    153638      +164
==========================================
+ Hits         47218     47460      +242
- Misses       24229     24061      -168
- Partials      4043      4067       +24

Generated by Codecov Action

@codecov
Copy link
Copy Markdown

codecov Bot commented May 30, 2026

Codecov Report

❌ Patch coverage is 93.58974% with 5 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
crates/monty-python/src/convert.rs 93.58% 0 Missing and 5 partials ⚠️

📢 Thoughts on this report? Let us know!

samuelcolvin and others added 2 commits May 30, 2026 12:06
The inbound (host → sandbox) type-object detection matched on
(__module__, __name__) strings, which is both spoofable and version-fragile:
pathlib path classes report __module__ as `pathlib._local` on Python 3.13,
so PurePosixPath wasn't recognised and the round-trip test failed in CI.

Switch to type-object identity matching against a cached table built from
type_object_to_py, so the two directions stay in lock-step and a class that
forges __name__/__module__ can't impersonate a builtin. The pathlib family is
matched via issubclass(PurePath). Also preserve NoneType and ellipsis, which
previously fell through to the callable fallback.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
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