Skip to content

Register Windows code pages when the engine starts - #851

Open
dronov-dmitry wants to merge 2 commits into
IronLanguages:masterfrom
dronov-dmitry:fix/encoding-866-fallback
Open

dronov-dmitry wants to merge 2 commits into
IronLanguages:masterfrom
dronov-dmitry:fix/encoding-866-fallback

Conversation

@dronov-dmitry

@dronov-dmitry dronov-dmitry commented Sep 10, 2026 •

Copy link
Copy Markdown

Problem

IronPython 2 on .NET Core cannot use Windows code pages (cp866, cp1251, ...)
unless the host registers an EncodingProvider first.

IronPython does register CodePagesEncodingProvider, but lazily, in
StringOps.CodecsInfo — on the first string codec use. Anything that runs
earlier sees a process without those code pages:

  • immediately after Python.CreateEngine() the code pages are still missing;
  • the DLR console setup runs inside the PythonContext constructor and reads
    Console.InputEncoding before any codec is touched.

A host that registers a provider answering code page 0 but not cp866 (the
scenario from IronLanguages/dlr#365) therefore gets
NotSupportedException: No data is available for encoding 866 from
Console.InputEncoding, and engine creation fails with
InvalidOperationException: Failed to load language 'IronPython 2.7.12': No data is available for encoding 866.

IronPython 3 registers the provider in its own startup code
(StringOps.CodecsInfo) exactly to avoid this.

Change

Register CodePagesEncodingProvider in the PythonContext static constructor.
It is the earliest point shared by every engine creation path
(Python.CreateEngine, ScriptRuntimeSetup, hosting APIs) and it runs before
SetStandardIO() touches the console.

No project file change is needed: System.Text.Encoding.CodePages is already
referenced for netstandard2.0 and is part of the netcoreapp3.1 targeting
pack.

Verification

Built net45, netstandard2.0 and netcoreapp3.1.

A standalone reproducer is published at
https://github.com/dronov-dmitry/ipy-866-repro (net8.0, IronPython 2.7.12 from
NuGet, no special environment needed):

git clone https://github.com/dronov-dmitry/ipy-866-repro.git
cd ipy-866-repro
dotnet run -c Release

Without the change:

Before CreateEngine:
[FAIL] Encoding.GetEncoding(866): NotSupportedException: No data is available for encoding 866. ...
After CreateEngine:
[FAIL] Encoding.GetEncoding(866): NotSupportedException: No data is available for encoding 866. ...

With the change (same program, IronPython DLLs replaced with the build from
this branch):

Before CreateEngine:
[FAIL] Encoding.GetEncoding(866): NotSupportedException: No data is available for encoding 866. ...
After CreateEngine:
[OK]   Encoding.GetEncoding(866): cp866 (code page 866)

On Windows the same program reproduces the original crash as well, by
registering a provider that answers code page 0 but not cp866 and forcing the
console code page to 866. The stack observed on the affected Windows machine
while probing the console:

System.NotSupportedException: No data is available for encoding 866.
   at System.Text.Encoding.GetEncoding(Int32 codepage)
   at System.Console.get_OutputEncoding()
   at System.ConsolePal.OpenStandardError()
   at System.Console.<get_Error>g__EnsureInitialized|28_0()

No in-process unit test is included: Encoding.RegisterProvider is
process-global and cannot be undone, so any test would depend on execution
order. The reproducer above is the deterministic check.

Note on CI: the repository CI is currently red on master as well (deprecated
action versions and analyzer errors in the pinned DLR submodule with current
SDKs), so the checks here may stay red for reasons unrelated to this change.
The first commit only makes the workflow runnable again.

This supersedes the earlier version of this PR, which also proposed a fallback
in DLR SharedIO. That part was withdrawn after the review in
IronLanguages/dlr#365.

@dronov-dmitry

Copy link
Copy Markdown
Author

@dotnet-policy-service agree

CI jobs were failing before running a single step:
1. actions/upload-artifact@v2 is deprecated and GitHub fails the whole job
   immediately; actions/checkout@v2 and actions/setup-dotnet@v1 are equally
   out of support. Bump to checkout@v7, setup-dotnet@v6 and upload-artifact@v7
   (inputs used here are unchanged).
2. NuGet.config pointed at https://api.nuget.org/api/v3/index.json which now
   returns 404, so restore failed for every package. Use the current
   https://api.nuget.org/v3/index.json instead.
Windows code pages such as cp866 and cp1251 are not available on .NET Core
unless an EncodingProvider is registered. IronPython 2 registers the provider
lazily in StringOps.CodecsInfo, which is too late for anything that runs
during engine startup (the DLR console setup reads Console.InputEncoding) and
forces every host to register it manually.

Register CodePagesEncodingProvider in the PythonContext static constructor,
which runs before the context is created. IronPython 3 does the same in
StringOps.CodecsInfo.

No project file change is needed: System.Text.Encoding.CodePages is already
referenced for netstandard2.0 and is part of the netcoreapp3.1 targeting pack.
@dronov-dmitry
dronov-dmitry force-pushed the fix/encoding-866-fallback branch from 9da6ac2 to 9d54bdd Compare November 22, 2026 11:26
@dronov-dmitry dronov-dmitry changed the title Fix encoding 866 crash: register CodePagesEncodingProvider + SharedIO fallback Register Windows code pages when the engine starts Nov 22, 2026
@dronov-dmitry

Copy link
Copy Markdown
Author

@BCSharp @slozier — I have reworked this PR based on the review in IronLanguages/dlr#365, and I would appreciate another look.

What changed since the first version:

  • The DLR-side fallback in SharedIO is gone. You were right that on .NET 5+ Console.OpenStandard* read the console encoding themselves, that the fallback would silently produce wrong I/O, and that a failed type initializer stays failed for the process. The DLR PR stays closed.
  • What is left is the root cause on the IronPython side: IronPython 2 registers CodePagesEncodingProvider only lazily in StringOps.CodecsInfo, which is too late for the console setup that runs inside the PythonContext constructor. The change is 11 lines in one file and mirrors what IronPython 3 already does.
  • The unrelated .csproj change is gone: System.Text.Encoding.CodePages is already referenced for netstandard2.0 and is part of the netcoreapp3.1 targeting pack, so the project builds unchanged for all three TFMs.

One correction to the review, for the record: on .NET Framework and .NET Core 3.1, ConsolePal.Windows.OpenStandard*() do not read the encoding (dotnet/corefx release/3.1 and the .NET Framework reference source), so the old patch did get past those calls there. That does not make it correct, but it explains why it appeared to work in the reported host.

Verification: a minimal, self-contained reproducer is published at https://github.com/dronov-dmitry/ipy-866-repro and needs no special environment:

git clone https://github.com/dronov-dmitry/ipy-866-repro.git
cd ipy-866-repro
dotnet run -c Release

Without the change, Encoding.GetEncoding(866) still throws after Python.CreateEngine(); with the build from this branch it returns cp866. Both outputs are in the repository's evidence/ folder.

CI: the workflow on master is currently red as well, so the checks here may stay red for reasons unrelated to this change. Locally net45, netstandard2.0 and netcoreapp3.1 build.

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