python/cpython · error

memory error: could not allocate varname

Error message

memory error: could not allocate varname

What it means

Build-time error from Programs/_freeze_module.c: get_varname() (which builds the `_Py_M__<name>` array identifier for the frozen module) returned NULL — almost always an allocation failure while formatting the variable name. write_frozen() aborts after closing the output file.

Source

Thrown at Programs/_freeze_module.c:202

    fprintf(outfile, "};\n");
}

static int
write_frozen(const char *outpath, const char *inpath, const char *name,
             PyObject *marshalled)
{
    /* Open the file in text mode. The hg checkout should be using the eol extension,
       which in turn should cause the EOL style match the C library's text mode */
    FILE *outfile = fopen(outpath, "w");
    if (outfile == NULL) {
        fprintf(stderr, "cannot open '%s' for writing\n", outpath);
        return -1;
    }

    fprintf(outfile, "%s\n", header);
    char *arrayname = get_varname(name, "_Py_M__");
    if (arrayname == NULL) {
        fprintf(stderr, "memory error: could not allocate varname\n");
        fclose(outfile);
        return -1;
    }
    write_code(outfile, marshalled, arrayname);
    free(arrayname);

    if (ferror(outfile)) {
        fprintf(stderr, "error when writing to '%s'\n", outpath);
        fclose(outfile);
        return -1;
    }
    fclose(outfile);
    return 0;
}

int
main(int argc, char *argv[])
{

View on GitHub (pinned to bc6749cc3b)

Solutions

  1. Free memory / raise limits (container memory, `ulimit -v`) and rebuild
  2. Check the module name passed as argv[1] is sane ASCII and matches the expected identifier pattern
  3. Reduce build parallelism to lower peak memory
Defensive patterns

Strategy: validation

Validate before calling

# shell: validate the module name is a sane identifier before freezing
# case "$NAME" in ''|*[!a-zA-Z0-9_.]*) echo 'bad module name'; exit 1;; esac

Prevention

When it happens

Trigger: Memory exhaustion while freezing a module with an extremely long name; a module name containing characters that make the name-building routine fail; running the freezer under a restrictive memory ulimit.

Common situations: Same low-memory contexts as other freeze failures: constrained CI containers, huge parallel builds; exotic module names in patched trees.

Related errors


AI-assisted analysis of python/cpython@bc6749cc3b (2026-08-14). Data as JSON: /api/errors/b58407ecd5fc6672. Report an issue: GitHub.