Skip to content

Fix encoding 866 crash: fallback to UTF-8 when console encoding unavailable - #364

Closed
dronov-dmitry wants to merge 1 commit into
IronLanguages:mainfrom
dronov-dmitry:fix/encoding-866-fallback
Closed

dronov-dmitry wants to merge 1 commit into
IronLanguages:mainfrom
dronov-dmitry:fix/encoding-866-fallback

Conversation

@dronov-dmitry

Copy link
Copy Markdown

Problem

SharedIO.InitializeInput() accesses Console.InputEncoding without error handling. On Russian Windows this returns cp866, but in hosting environments like Dynamo (Autodesk Revit) or on .NET Core, the codepage data is unavailable, causing:

ArgumentException: No data is available for encoding 866

This crashes Python.CreateEngine() in IronPython.

Fix

Added TryGetConsoleEncoding() helper that wraps console encoding access in try-catch with fallback:

private static Encoding TryGetConsoleEncoding(Func<Encoding> getter, Encoding fallback) {
    try { return getter(); } catch { return fallback; }
}

Applied to:

  • InitializeInput()Console.InputEncoding / Encoding.Default → fallback Encoding.UTF8
  • InitializeOutput()Console.Out → fallback TextWriter.Null
  • InitializeErrorOutput()Console.Error → fallback TextWriter.Null

Companion PR

This fix works together with IronLanguages/ironpython2#851 which adds Encoding.RegisterProvider(CodePagesEncodingProvider.Instance) in PythonContext static constructor for .NET Core/Standard.

Testing

Verified with C# host simulating Dynamo environment — all tests pass.

…coding unavailable

SharedIO.InitializeInput/Output/ErrorOutput accessed Console.InputEncoding
and Console.Out/Console.Error without error handling. On systems where cp866
(or other console encodings) are not registered (e.g. Dynamo/Revit hosting
environment, .NET Core), this threw ArgumentException:
  'No data is available for encoding 866'

Added TryGetConsoleEncoding() helper that wraps encoding access in try-catch
and falls back to Encoding.UTF8 / TextWriter.Null when the console encoding
is unavailable.
@dronov-dmitry

Copy link
Copy Markdown
Author

@dotnet-policy-service agree

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant