-
Notifications
You must be signed in to change notification settings - Fork 7.8k
Fix Default/OEM encoding behavior PowerShell Core #3467
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
9580b4a
64bceba
a26149c
b60e9eb
900311c
957f01b
595b728
0f04279
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -530,17 +530,26 @@ internal static object[] GetCustomAttributes<T>(Assembly assembly) | |
#region Encoding | ||
|
||
/// <summary> | ||
/// Facade for Encoding.Default | ||
/// Facade for getting Encoding.Default | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Depending on what we will do when There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree. |
||
/// </summary> | ||
internal static Encoding GetDefaultEncoding() | ||
{ | ||
if (s_defaultEncoding == null) | ||
{ | ||
#if CORECLR // Encoding.Default is not in CoreCLR | ||
// As suggested by CoreCLR team (tarekms), use latin1 (ISO-8859-1, CodePage 28591) as the default encoding. | ||
// We will revisit this if it causes any failures when running tests on Core PS. | ||
s_defaultEncoding = Encoding.GetEncoding(28591); | ||
#else | ||
#if CORECLR | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The if/def pattern used in other places is
Could you please follow the same? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
|
||
#if UNUX // PowerShell Core on Unix | ||
s_defaultEncoding = Encoding.GetEncoding(65001); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
#else // PowerShell Core on Windows | ||
uint aCp = NativeMethods.GetACP(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe rename the variable to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
if (s_oemEncoding == null) | ||
{ | ||
Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it better to separate this into a method to reduce code duplication? |
||
} | ||
s_defaultEncoding = Encoding.GetEncoding((int)aCp); | ||
#endif | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please remove the extra blank line. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
#else // Windows PowerShell | ||
s_defaultEncoding = Encoding.Default; | ||
#endif | ||
} | ||
|
@@ -555,10 +564,20 @@ internal static Encoding GetOEMEncoding() | |
{ | ||
if (s_oemEncoding == null) | ||
{ | ||
#if CORECLR // The OEM code page '437' is not supported by CoreCLR. | ||
// Use the default encoding (ISO-8859-1, CodePage 28591) as the OEM encoding in OneCore powershell. | ||
#if CORECLR | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here regarding the pattern. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
|
||
#if UNUX // PowerShell Core on Unix | ||
s_oemEncoding = GetDefaultEncoding(); | ||
#else | ||
#else // PowerShell Core on Windows | ||
uint oemCp = NativeMethods.GetOEMCP(); | ||
if (s_defaultEncoding == null) | ||
{ | ||
Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same here regarding the code duplication. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
} | ||
s_oemEncoding = Encoding.GetEncoding((int)oemCp); | ||
#endif | ||
|
||
#else // Windows PowerShell | ||
uint oemCp = NativeMethods.GetOEMCP(); | ||
s_oemEncoding = Encoding.GetEncoding((int)oemCp); | ||
#endif | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I didn't notice the duplicate code between coreclr and fullclr. In this case, maybe the following is better?
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I tried - it is difficult to read. In compile time no duplicate code is. So maybe leave as is? |
||
|
@@ -1033,6 +1052,12 @@ private static class NativeMethods | |
[DllImport(PinvokeDllNames.GetOEMCPDllName, SetLastError = false, CharSet = CharSet.Unicode)] | ||
internal static extern uint GetOEMCP(); | ||
|
||
/// <summary> | ||
/// Pinvoke for GetACP to get the Windows operating system code page. | ||
/// </summary> | ||
[DllImport(PinvokeDllNames.GetACPDllName, SetLastError = false, CharSet = CharSet.Unicode)] | ||
internal static extern uint GetACP(); | ||
|
||
public const int S_OK = 0x00000000; | ||
|
||
/// <summary> | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,7 @@ internal static class PinvokeDllNames | |
internal const string QueryDosDeviceDllName = "api-ms-win-core-file-l1-1-0.dll"; /*1*/ | ||
internal const string CreateSymbolicLinkDllName = "api-ms-win-core-file-l2-1-0.dll"; /*2*/ | ||
internal const string GetOEMCPDllName = "api-ms-win-core-localization-l1-2-0.dll"; /*3*/ | ||
internal const string GetACPDllName = "api-ms-win-core-localization-l1-2-0.dll"; /*3*/ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
internal const string DeviceIoControlDllName = "api-ms-win-core-io-l1-1-0.dll"; /*4*/ | ||
internal const string CreateFileDllName = "api-ms-win-core-file-l1-1-0.dll"; /*5*/ | ||
internal const string DeleteFileDllName = "api-ms-win-core-file-l1-1-0.dll"; /*6*/ | ||
|
@@ -139,6 +140,7 @@ internal static class PinvokeDllNames | |
internal const string QueryDosDeviceDllName = "kernel32.dll"; /*1*/ | ||
internal const string CreateSymbolicLinkDllName = "kernel32.dll"; /*2*/ | ||
internal const string GetOEMCPDllName = "kernel32.dll"; /*3*/ | ||
internal const string GetACPDllName = "kernel32.dll"; /*3*/ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
internal const string DeviceIoControlDllName = "kernel32.dll"; /*4*/ | ||
internal const string CreateFileDllName = "kernel32.dll"; /*5*/ | ||
internal const string DeleteFileDllName = "kernel32.dll"; /*6*/ | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
NativeMethods.GetOEMCP
in this file can be removed if it's no longer used.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is used in
ClrFacade.GetOEMEncoding
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ClrFacade
has its own copy ofGetOEMEncoding
, so the one inFileSystemProvider.cs
can be removed.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.