Skip to content

Use InvariantCulture for implicit argument conversion - #5106

Closed
Dongbo Wang (daxian-dbw) wants to merge 3 commits into
PowerShell:masterfrom
daxian-dbw:argtype
Closed

Dongbo Wang (daxian-dbw) wants to merge 3 commits into
PowerShell:masterfrom
daxian-dbw:argtype

Conversation

@daxian-dbw

Copy link
Copy Markdown
Member

Fix #3348

PowerShell uses InvariantCulture for explicit type conversion. The implicit argument conversion for the script function/cmdlet also uses InvariantCulture. However, the implicit parameter argument type conversion for the C# cmdlet uses CurrentCulture. Change it to use 'InvariantCulture' too.

[CmdletBinding()]
param([DateTime] $date)
$date
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we add C# cmdlet?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can use Get-Date -Date instead, so no need to add a C# cmdlet.

PS:162> gcm Get-Date -Syntax
Get-Date [[-Date] <datetime>] ...

}

# Explict type cast use 'InvariantCulture' by default
$expected = [datetime] "1/11/1111"

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why we think that is 'InvariantCulture' ? I think we should have explicit constructor and explicit check that "Explict type cast use 'InvariantCulture' by default". Also it is another test then "Verify implicit argument conversion uses InvariantCulture".

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using invariant culture for a cast was a choice for consistency at the language level, casting a literal would always produce the same result in any culture.

If invariant culture conversions aren't appropriate, then one simply uses the appropriate code (constructor or whatever) and specifies how to do the conversion.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Jason Shirk (@lzybkr) Thanks for useful comment! But my question was just about the test - what if we break internally and replace InvariantCulture with other culture accidentally? So the line and comment will be wrong.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I see, I was thinking it was a test for the conversion. I agree then, we should not rely on the default when checking results.

If we don't already, we should have a test that the cast is culture invariant.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will change this line to LangaugePrimitives.ConvertTo explicitly with InvariantCulture. And also will add a test to validate if the explicit conversion in powershell language is using 'InvariantCulture'.

@daxian-dbw

Copy link
Copy Markdown
Member Author

There are some test failures, I will investigate whether they are test or code issues.

@iSazonov

Copy link
Copy Markdown
Collaborator

I see all test failures is culture-specific - the PR is really breaking change! :-)

@daxian-dbw

Copy link
Copy Markdown
Member Author

The early failures were because I didn't set the culture back after setting it to 'ru-RU'. It's now fixed.

$csharpCmdlet, $scriptFunction, $scriptCmdlet = @(
$csharpCmdlet, $scriptFunction, $scriptCmdlet = try {
$oldCulture = [CultureInfo]::CurrentCulture
[CultureInfo]::CurrentCulture = 'ru-RU'

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We use the 'ru-Ru' string in line 300 too - maybe put it in a variable?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

@iSazonov

Copy link
Copy Markdown
Collaborator

If we have so many culture-sensitive test should we fix them too?

$dateString = "1/11/1111"
$expected = [System.Management.Automation.LanguagePrimitives]::ConvertTo($dateString, [datetime], [cultureinfo]::InvariantCulture)
$result = try {
$oldCulture = [CultureInfo]::CurrentCulture

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should it be outside of try block?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

# Implict parameter argument conversion should also use 'InvariantCulture'
$csharpCmdlet, $scriptFunction, $scriptCmdlet = @(
$csharpCmdlet, $scriptFunction, $scriptCmdlet = try {
$oldCulture = [CultureInfo]::CurrentCulture

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should it be outside of try block?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

@PetSerAl

Copy link
Copy Markdown
Contributor

Ilya (@iSazonov)

If we have so many culture-sensitive test should we fix them too?

IMO, tests which valid only in certain culture, like this one:

 [-] using -format produces the correct output 41ms
   String lengths are both 9. Strings differ at index 0.
   Expected: {Jan-01-20}
   But was:  {янв-01-20}
   -----------^
   at line: 21 in D:\PowerShell\test\powershell\Modules\Microsoft.PowerShell.Utility\Get-Date.Tests.ps1
   21:         Get-date -Date:"Jan 1, 2020"  -Format:"MMM-dd-yy" | Should be "Jan-01-20"

should explicitly set that culture for themselves.

@daxian-dbw

Dongbo Wang (daxian-dbw) commented Oct 15, 2017 •

Copy link
Copy Markdown
Member Author

Some tests call ToString(), which makes them culture dependent. Maybe it's not really necessary to call the ToString method in the first place.

#5128 is opened for the culture-sensitive tests.

@iSazonov Ilya (iSazonov) added the Documentation Needed in this repo Documentation is needed in this repo label Oct 18, 2017
@daxian-dbw Dongbo Wang (daxian-dbw) added the Review - Committee The PR/Issue needs a review from the PowerShell Committee label Oct 18, 2017
@SteveL-MSFT Steve Lee (SteveL-MSFT) added Committee-Reviewed PS-Committee has reviewed this and made a decision and removed Review - Committee The PR/Issue needs a review from the PowerShell Committee labels Oct 19, 2017
@SteveL-MSFT

Copy link
Copy Markdown
Member

@PowerShell/powershell-committee reviewed this and due to the potential for breaking many existing modules, it's too risky to make this change so that c# and script cmdlets are alike for this case. We should keep the current behavior.

@daxian-dbw

Copy link
Copy Markdown
Member Author

Given the review decision of powershell committee, I'm closing this PR.

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

Labels

Breaking-Change breaking change that may affect users Committee-Reviewed PS-Committee has reviewed this and made a decision

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants