Repository navigation
Register Windows code pages when the engine starts - #851
dronov-dmitry wants to merge 2 commits into
Conversation
|
@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.
9da6ac2 to
9d54bdd
Compare
|
@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:
One correction to the review, for the record: on .NET Framework and .NET Core 3.1, Verification: a minimal, self-contained reproducer is published at https://github.com/dronov-dmitry/ipy-866-repro and needs no special environment: Without the change, CI: the workflow on |
Problem
IronPython 2 on .NET Core cannot use Windows code pages (cp866, cp1251, ...)
unless the host registers an
EncodingProviderfirst.IronPython does register
CodePagesEncodingProvider, but lazily, inStringOps.CodecsInfo— on the first string codec use. Anything that runsearlier sees a process without those code pages:
Python.CreateEngine()the code pages are still missing;PythonContextconstructor and readsConsole.InputEncodingbefore 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 866fromConsole.InputEncoding, and engine creation fails withInvalidOperationException: 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
CodePagesEncodingProviderin thePythonContextstatic constructor.It is the earliest point shared by every engine creation path
(
Python.CreateEngine,ScriptRuntimeSetup, hosting APIs) and it runs beforeSetStandardIO()touches the console.No project file change is needed:
System.Text.Encoding.CodePagesis alreadyreferenced for
netstandard2.0and is part of thenetcoreapp3.1targetingpack.
Verification
Built
net45,netstandard2.0andnetcoreapp3.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 ReleaseWithout the change:
With the change (same program, IronPython DLLs replaced with the build from
this branch):
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:
No in-process unit test is included:
Encoding.RegisterProviderisprocess-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
masteras well (deprecatedaction 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 inIronLanguages/dlr#365.