Skip to content

getfilesystemencoding() returns incorrect value on Linux #2664

@iFreilicht

Description

@iFreilicht

There seems to be a discrepancy between pythonnet and system-installed python when determining the file-system-encoding.

This is my pythonnet code:

        Runtime.PythonDLL = "/usr/lib/x86_64-linux-gnu/libpython3.12.so";
        PythonEngine.Initialize();
        PythonEngine.BeginAllowThreads();

        using (Py.GIL())
        {
            dynamic sys = Py.Import("sys");
            Console.WriteLine("Filesystem encoding: {0}", sys.getfilesystemencoding());
            Console.WriteLine("Default encoding: {0}", sys.getdefaultencoding());
        }

Which prints:

Filesystem encoding: ascii
Default encoding: utf-8

However, this is not correct! First, it only prints this on Linux. On windows, the filesystem-encoding is correctly determined to be 'utf-8'. Running a simple oneliner in python itself returns the correct values on both platforms:

$ python -c "import sys; print(sys.getfilesystemencoding()); print(sys.getdefaultencoding())"
utf-8
utf-8

The python-version is 3.12.3, the dotnet version is 8.0.408 on both platforms as well.

I've tried to work around this with environment variables, but those aren't recognized as expected, see #2539 (comment)

Another option might be to call PySys_AddXOption with "utf8" to set UTF-8 Mode manually, but pythonnet has no binding for this function.

I'm at a loss about what to do next. This seems like a genuine bug, so I opened this here instead of starting a discussion.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions