Centralize string access and name conversions

Closed
JA
1mo ago
+795-532

Extract the string-handling refactoring from #994 so it can be reviewed and merged before inline storage.

Add optional Value::as_str / as_str_heap accessors, retain generic TypeError conversion as try_to_str / try_to_str_heap, and migrate callers to these accessors or PyValue views. Move attribute-name conversion to EitherStr::as_value, share existing name helpers, and remove duplicated string dispatch and impossible conversion errors.

String storage, identity, JSON cache policy, and the dump format remain unchanged. Inline storage follows in #994.

Validation: full Monty Rust tests and all 718 Monty fixtures pass with memory-model-checks and ref-count-return; all prek hooks pass.


Summary by cubic

Centralizes how Monty reads string values and converts attribute names, so the upcoming inline-storage change (#994) touches one place instead of scattered match arms.

  • Adds Value::as_str / as_str_heap, which return Option<&str> without allocating, and renames the fallible to_str / to_str_heap to try_to_str / try_to_str_heap.
  • Migrates callers that already know their value is a string to the new accessors, replacing is_str + to_str pairs and hand-written dispatch in module finder, JSON, bytes, and str.join paths.
  • Moves attribute-name conversion into EitherStr::as_value, replacing the attribute_name_value helper, and refactors open / path handling to borrow instead of copy.

Behavior changes

  • Computed (non-interned) attribute names now resolve on all modules, GenericAlias, and builtin types instead of always failing with AttributeError.
  • Type::call_class_method now takes EitherStr directly.
  • Error messages for non-string arguments now come from one shared path in try_to_str, and impossible conversion errors (e.g. non-str kwargs keys) were removed.

String storage, identity, JSON cache policy, and dump format are unchanged. New tests cover string argument errors, module attribute names, inline read/print behavior, and refcount retention.