#955·monty

REPL string literals still exhaust the u16 name id space in long-lived sessions

Author: rsr5Created Sep 30, 2026Updated Sep 30, 2026

Summary

After #896, REPL input filenames no longer use up the string table that name-bearing opcodes address with u16 operands. String literals still do. They are interned into the same strings table and never released, so a long-lived session that compiles a unique literal in each input fills the 65,536 ids and then fails to compile any snippet that needs a new name:

SyntaxError: module has too many distinct names; the bytecode format supports up to 65536 interned strings

Reproduction

On the #896 branch:

rust
use monty::MontyRepl;
use monty_types::{CompileOptions, PrintWriter, ResourceTracker};

fn main() {
    let mut repl = MontyRepl::new("repl.py", ResourceTracker::default(), CompileOptions::default());
    repl.feed_run("class P:\n    pass\np = P()", vec![], PrintWriter::Disabled).unwrap();
    for i in 0..70_000 {
        repl.feed_run(&format!("s = 'frame-{i}'"), vec![], PrintWriter::Disabled).unwrap();
    }
    // Fails with the SyntaxError above.
    repl.feed_run("p.fresh_attr = 1\np.fresh_attr", vec![], PrintWriter::Disabled).unwrap();
}

The same happens to a host that builds snippets containing literal values, for example formatting per-frame data into the source instead of passing it as an input.

Why it happens

Literals and identifiers share one table. Only identifiers (attribute, method, keyword and module names, and global names in calls) ever appear as u16 operands, but every literal takes an id from the same range.

Possible directions

  • Give names their own id space, or intern literals into a separate table that bytecode reaches through the constant pool rather than a u16 name operand.
  • Or keep one table but hand out name-bearing ids from a reserved low range.

Either is a bigger change than #896, which is why it was left out of that PR.

Related clean-ups noted while reviewing #896

  • SnippetSource stores each <python-input-N> filename as a heap string for the session's lifetime (about 20 bytes per input, and in every dump). Storing the input number and formatting the name on demand would shrink it, but Interns::get_filename returns &str, so it would need to return Cow<str> or String.
  • ExceptionRaise::into_python_exception's source_for closure is now only needed for a whole-program run's script and for REPL inputs restored from a 1.0.0 dump. It could become a plain Option<&str> plus the legacy map lookup.