{"record":{"id":"b58407ecd5fc6672","repo":"python/cpython","slug":"memory-error-could-not-allocate-varname","errorCode":null,"errorMessage":"memory error: could not allocate varname\n","messagePattern":"memory error: could not allocate varname\n","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"Programs/_freeze_module.c","lineNumber":202,"sourceCode":"    fprintf(outfile, \"};\\n\");\n}\n\nstatic int\nwrite_frozen(const char *outpath, const char *inpath, const char *name,\n             PyObject *marshalled)\n{\n    /* Open the file in text mode. The hg checkout should be using the eol extension,\n       which in turn should cause the EOL style match the C library's text mode */\n    FILE *outfile = fopen(outpath, \"w\");\n    if (outfile == NULL) {\n        fprintf(stderr, \"cannot open '%s' for writing\\n\", outpath);\n        return -1;\n    }\n\n    fprintf(outfile, \"%s\\n\", header);\n    char *arrayname = get_varname(name, \"_Py_M__\");\n    if (arrayname == NULL) {\n        fprintf(stderr, \"memory error: could not allocate varname\\n\");\n        fclose(outfile);\n        return -1;\n    }\n    write_code(outfile, marshalled, arrayname);\n    free(arrayname);\n\n    if (ferror(outfile)) {\n        fprintf(stderr, \"error when writing to '%s'\\n\", outpath);\n        fclose(outfile);\n        return -1;\n    }\n    fclose(outfile);\n    return 0;\n}\n\nint\nmain(int argc, char *argv[])\n{","sourceCodeStart":184,"sourceCodeEnd":220,"githubUrl":"https://github.com/python/cpython/blob/bc6749cc3b5ae4a5e88a6cc2d5b3bebbe354eae6/Programs/_freeze_module.c#L184-L220","documentation":"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.","triggerScenarios":"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.","commonSituations":"Same low-memory contexts as other freeze failures: constrained CI containers, huge parallel builds; exotic module names in patched trees.","solutions":["Free memory / raise limits (container memory, `ulimit -v`) and rebuild","Check the module name passed as argv[1] is sane ASCII and matches the expected identifier pattern","Reduce build parallelism to lower peak memory"],"exampleFix":null,"handlingStrategy":"validation","validationCode":"# shell: validate the module name is a sane identifier before freezing\n# case \"$NAME\" in ''|*[!a-zA-Z0-9_.]*) echo 'bad module name'; exit 1;; esac","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Keep frozen module names short, ASCII identifiers","Give builds enough memory headroom","Fail fast on invalid names in your build scripts rather than inside the freezer"],"tags":["cpython","build","freeze-module","out-of-memory"],"backgroundTag":null,"analyzedSha":"bc6749cc3b5ae4a5e88a6cc2d5b3bebbe354eae6","analyzedAt":"2026-08-14T22:01:13.976Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}