agentleFS
jscott3201jscott3201/yet-more-skills/skills/pyo3-ownership-errors/SKILL.md

pyo3-ownership-errors

Skill4 starsChanged 2 months agoApache-2.0View on GitHub

Use for PyO3 Python/Rust conversions, object lifetime, pyclass borrows, callbacks, exception mapping, and signature/stub consistency. For ABI packaging or async runtime teardown use the corresponding specialist.

The file

We split this skill into the rules it gives an agent

3 rules. About 0 are common, 3 are distinctive, and 0 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈3

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 0

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Distinctive:

    Start from the actual PyO3 version, Python floor, generated module layout, and Rust crate policy.

    line 9
  2. Distinctive:

    Run relevant core tests plus installed Python consumer tests.

    line 27
  3. Distinctive:

    Read boundary checks for examples and source links.

    line 31

≈ “Common” is an estimate: at least 1 in 5 of the 350,495 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

PyO3 Ownership and Errors

Start from the actual PyO3 version, Python floor, generated module layout, and Rust crate policy. Keep Python-independent logic in the Rust core and Python conversion/exception policy in the binding. Do not weaken safe-core lints just because PyO3 macros need a separate boundary crate.

Map each crossing

For each changed argument, result, callback, or stored object, name the owner, borrow duration, mutability, thread/interpreter affinity, and error behavior. Bound<'py, T> is tied to an interpreter attachment lifetime; Py<T> owns a Python reference but does not make the object immutable, interpreter-independent, or safe to use without attachment. Do not extend lifetimes, store borrowed pointers in workers, or add unsafe Send/Sync to silence compiler constraints.

Convert or retain owned inputs before detaching or moving work into an async task. A safe owned copy is often the right boundary; eliminate it only with a proven lifetime and synchronization model plus performance evidence. Drop pyclass borrow guards before reentrant Python callbacks. Rust locks and Python attachment are separate resources with a lock order; avoid calling user code while holding a core lock.

A frozen pyclass can simplify ordinary field mutation but is not a blanket proof for interior mutability, native handles, caches, or callbacks. An unsendable class is an explicit thread-affinity contract, not an automatic fix for sharing. Keep extension module and interpreter lifetime in mind for globals and caches; use the version's interpreter-aware initialization facilities when warranted.

Preserve Python behavior

Choose explicit signatures/defaults and meaningful exceptions. Distinguish bad Python types, out-of-range values, invalid protocol data, transport failures, cancellation, and internal defects. Preserve native error context and public exception inheritance. Do not flatten every error into RuntimeError or turn a Rust panic into normal domain control flow. Panic handling does not make undefined behavior recoverable.

Test numeric narrowing, bool/int, missing/None, nested conversion, Unicode, and domain-specific sentinels. Conversion methods may execute Python and reenter the extension. Match .pyi files to runtime imports, classes, positional/keyword arguments, defaults, awaitables, context managers, and exceptions. Prefer real consumer tests when native signature introspection is incomplete.

Validate at both sides

Run relevant core tests plus installed Python consumer tests. Include repeated object creation/destruction, callback exceptions/reentry, invalid conversions, and explicit close. For resource-owning objects, verify ownership after owner deletion and avoid relying on garbage collection timing for correctness.

Report ownership decisions, failure contracts, tested Python surface, and unresolved lifetime or interpreter assumptions. Ask for policy guidance when supporting subinterpreters or changing public coercion rules would broaden the product.

Read boundary checks for examples and source links.

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.