LykosAI/StabilityMatrix · error · InvalidDataException

pyvenv.cfg contains NUL bytes; expected UTF-8/ASCII

Error message

pyvenv.cfg contains NUL bytes; expected UTF-8/ASCII: {path}

What it means

PyVenvCfg.Load also rejects pyvenv.cfg content containing NUL bytes after UTF-8 decoding. NUL bytes mean the file was written in an incompatible encoding (typically UTF-16 without a BOM, or corruption), so an InvalidDataException naming the path is thrown to avoid silently parsing garbage.

Solutions

  1. Rewrite pyvenv.cfg as UTF-8/ASCII without NUL bytes
  2. Recreate the venv to regenerate a clean config file
  3. Inspect the file bytes to confirm the encoding and convert with an explicit Unicode->UTF-8 conversion
  4. Catch InvalidDataException and rebuild the environment or regenerate the config

Example fix

// before
[IO.File]::WriteAllText($path, $content, [Text.Encoding]::Unicode)  # UTF-16, maybe no BOM
// after
[IO.File]::WriteAllText($path, $content, new-object Text.UTF8Encoding($false))
Defensive patterns

Strategy: validation

Validate before calling

var text = new UTF8Encoding(false).GetString(File.ReadAllBytes(cfgPath));
if (text.Contains('\0'))
    throw new InvalidDataException($"{cfgPath} is not valid UTF-8/ASCII; rewrite as UTF-8");

Try / catch

try
{
    var cfg = PyVenvCfg.Load(cfgPath);
}
catch (InvalidDataException ex)
{
    logger.LogWarning(ex, "pyvenv.cfg contains NUL bytes; recreating venv config");
    RecreateVenvConfig(cfgPath);
}

Prevention

When it happens

Trigger: Loading a pyvenv.cfg whose raw bytes decode (as UTF-8) with embedded '\0' characters — UTF-16-encoded files lacking a BOM, or corrupted/truncated binary writes.

Common situations: UTF-16 files written without BOM by custom scripts, disk corruption, tools that wrote the config with wrong encoding settings.

Related errors


AI-assisted analysis of LykosAI/StabilityMatrix@af93d6ef57 (2026-09-12). Data as JSON: /api/errors/06851ba69c368b9b. Report an issue: GitHub.

Appendix: source

Thrown at StabilityMatrix.Core/Python/PyVenvCfg.cs:69

    /// </summary>
    public static PyVenvCfg Load(string path)
    {
        var bytes = File.ReadAllBytes(path);

        // pyvenv.cfg is UTF-8/ASCII; reject UTF-16 BOMs and NUL bytes, which
        // indicate the file was read with the wrong encoding.
        if (
            bytes.Length >= 2
            && ((bytes[0] == 0xFF && bytes[1] == 0xFE) || (bytes[0] == 0xFE && bytes[1] == 0xFF))
        )
        {
            throw new InvalidDataException($"pyvenv.cfg is UTF-16 encoded; expected UTF-8/ASCII: {path}");
        }

        var content = new UTF8Encoding(false).GetString(bytes);
        if (content.Contains('\0'))
        {
            throw new InvalidDataException($"pyvenv.cfg contains NUL bytes; expected UTF-8/ASCII: {path}");
        }

        return Parse(content);
    }

    /// <summary>
    /// Gets the value of the last matching key (CPython is last-wins), or null.
    /// Setting rewrites every matching key, appending a new key when absent.
    /// </summary>
    public string? this[string key]
    {
        get
        {
            for (var i = _entries.Count - 1; i >= 0; i--)
            {
                if (_entries[i].Key is { } k && k.Equals(key, StringComparison.OrdinalIgnoreCase))
                {
                    return _entries[i].Value;

View on GitHub (pinned to af93d6ef57)