{"record":{"id":"cac8d2728dc77a9c","repo":"python/cpython","slug":"could-not-convert-pre-site-command-to-unicode-n","errorCode":null,"errorMessage":"Could not convert pre-site command to Unicode\\n","messagePattern":"Could not convert pre-site command to Unicode\\\\n","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"Python/pylifecycle.c","lineNumber":1293,"sourceCode":"    Py_DECREF(obj);\n    Py_DECREF(attr);\n    return NULL;\n}\n\n\nstatic void\nrun_presite(PyThreadState *tstate)\n{\n    PyInterpreterState *interp = tstate->interp;\n    const PyConfig *config = _PyInterpreterState_GetConfig(interp);\n\n    if (!config->run_presite) {\n        return;\n    }\n\n    PyObject *presite = PyUnicode_FromWideChar(config->run_presite, -1);\n    if (presite == NULL) {\n        fprintf(stderr, \"Could not convert pre-site command to Unicode\\n\");\n        _PyErr_Print(tstate);\n        return;\n    }\n\n    // Accept \"mod_name\" and \"mod_name:func_name\" entry point syntax\n    Py_ssize_t len = PyUnicode_GET_LENGTH(presite);\n    Py_ssize_t pos = PyUnicode_FindChar(presite, ':', 0, len, 1);\n    PyObject *mod_name = NULL;\n    PyObject *func_name = NULL;\n    PyObject *module = NULL;\n    if (pos > 0) {\n        mod_name = PyUnicode_Substring(presite, 0, pos);\n        if (mod_name == NULL) {\n            goto error;\n        }\n\n        func_name = PyUnicode_Substring(presite, pos + 1, len);\n        if (func_name == NULL) {","sourceCodeStart":1275,"sourceCodeEnd":1311,"githubUrl":"https://github.com/python/cpython/blob/bc6749cc3b5ae4a5e88a6cc2d5b3bebbe354eae6/Python/pylifecycle.c#L1275-L1311","documentation":"Emitted in run_presite (Python/pylifecycle.c) when PyUnicode_FromWideChar(config->run_presite, -1) returns NULL while converting the pre-site hook specification (set via -X presite=... / PYTHON_PRESITE, used by free-threading and debugging workflows to run a hook before site.py) from wchar_t to a Unicode object. The most common cause is MemoryError during interpreter bring-up; the pending exception is then printed via _PyErr_Print.","triggerScenarios":"Configuring config->run_presite / -X presite=modname:funcname and hitting allocation failure or a conversion error in PyUnicode_FromWideChar at a point where the exception machinery is barely usable.","commonSituations":"Extremely memory-constrained startup; malformed wide-character strings set by embedding code; free-threaded debug builds using PYTHON_PRESITE for sanitizer hooks.","solutions":["Verify the presite value is a short, valid ASCII module[/:func] string","Rule out memory pressure at startup (raise limits, check ulimits/containers)","Temporarily unset -X presite/PYTHONPRESITE to confirm the rest of startup is healthy"],"exampleFix":"# before\nPYTHON_PRESITE=$'ÿþ(bad)' python app.py\n# after\nPYTHON_PRESITE=myhook:setup python app.py","handlingStrategy":"validation","validationCode":"# bash: keep the hook spec short, ASCII, mod[:func]\ncase \"$PYTHON_PRESITE\" in\n  *[!a-zA-Z0-9_.:]*) echo 'presite spec must be ASCII mod[:func]' >&2; exit 1;;\nesac\nPYTHON_PRESITE=\"$PYTHON_PRESITE\" python app.py","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Use only ASCII module/function names in presite specs","Avoid -X presite in memory-starved environments; it runs during the most fragile startup phase","Confirm startup works without the hook before debugging hook-specific failures"],"tags":["cpython","startup","presite","memory","unicode"],"backgroundTag":null,"analyzedSha":"bc6749cc3b5ae4a5e88a6cc2d5b3bebbe354eae6","analyzedAt":"2026-08-14T22:01:13.976Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}