IronLanguages / main

Work for this repo has moved to https://github.com/IronLanguages/ironpython2
1.16k stars 347 forks source link

Py3k: Overload Resolution Changes (was System.BitConverter.GetBytes(long(...)) wrong?) #439

Open ironpythonbot opened 9 years ago

ironpythonbot commented 9 years ago

--------------------------------------------------------------------------


IP VERSION AFFECTED: 2.6 Alpha 1
BUILD TYPE: All
FLAGS PASSED TO IPY.EXE: Any
OPERATING SYSTEM: All
CLR VERSION: .NET 2.0 SP1

--------------------------------------------------------------------------


BRIEF DESCRIPTION:
The number of bytes detected by the BCL static method, System.BitConverter.GetBytes,
for Python longs is always four regardless of how small/big the Python long is. Also,
the value returned by GetBytes for longs does not follow the same pattern as Int16, Int32,
etc.

--------------------------------------------------------------------------


REPRODUCTION SNIPPET:

Length of the ByteArray object returned by GetBytes looks wrong

IronPython 2.6 Alpha DEBUG (2.6.0.1) on .NET 2.0.50727.3053
Type "help", "copyright", "credits" or "license" for more information.

import System
System.BitConverter.GetBytes(11L)
Array[Byte](%28<System.Byte object at 0x000000000000002B [0]>, <System.Byte object at 0x000000000000002C [0]>, <System.Byte object at 0x000000
000000002D [48]>, <System.Byte object at 0x000000000000002E [65]>%29)
System.BitConverter.GetBytes(111111111111111111111L)
Array[Byte](%28<System.Byte object at 0x000000000000002F [63]>, <System.Byte object at 0x0000000000000030 [191]>, <System.Byte object at 0x000
0000000000031 [192]>, <System.Byte object at 0x0000000000000032 [96]>%29)

Wrong value returned by GetBytes?

System.BitConverter.GetBytes(System.Int16(1))
Array[Byte](%28<System.Byte object at 0x0000000000000067 [1]>, <System.Byte object at 0x0000000000000068 [0]>%29)
System.BitConverter.GetBytes(System.Int32(1))
Array[Byte](%28<System.Byte object at 0x0000000000000069 [1]>, <System.Byte object at 0x000000000000006A [0]>, <System.Byte object at 0x000000
000000006B [0]>, <System.Byte object at 0x000000000000006C [0]>%29)
System.BitConverter.GetBytes(System.Int64(1))
Array[Byte](%28<System.Byte object at 0x000000000000006D [1]>, <System.Byte object at 0x000000000000006E [0]>, <System.Byte object at 0x000000
000000006F [0]>, <System.Byte object at 0x0000000000000070 [0]>, <System.Byte object at 0x0000000000000071 [0]>, <System.Byte object at 0x00
00000000000072 [0]>, <System.Byte object at 0x0000000000000073 [0]>, <System.Byte object at 0x0000000000000074 [0]>%29)

System.BitConverter.GetBytes(long(1))
Array[Byte](%28<System.Byte object at 0x0000000000000075 [0]>, <System.Byte object at 0x0000000000000076 [0]>, <System.Byte object at 0x000000
0000000077 [128]>, <System.Byte object at 0x0000000000000078 [63]>%29)

Another weird one using System.Threading.Timer:

Timer(func, None, 100.1, 0) # throws
Timer(func, None, 100, 0) # ok
Timer(func, None, 100, 0).Change(100.1) # ok

Change has all the same numeric overloads as Timer. But the difference is that Timer also includes a function whose conversion is evaluated at a different level. Maybe we need to move function conversins up to an earlier narrowing level?

Work Item Details

Original CodePlex Issue: Issue 21676 Status: Active Reason Closed: Unassigned Assigned to: Unassigned Reported on: Mar 17, 2009 at 4:56 PM Reported by: dfugate Updated on: Feb 22, 2013 at 2:13 AM Updated by: skottmckay Test: Needed Thanks: Haibo Luo

ironpythonbot commented 9 years ago

On 2009-03-19 23:35:28 UTC, CurtHagenlocher commented:

This is no major surprise. BitConverter has no overload for Microsoft.Scripting.Math.BigInteger, so the binder looks for the type with the closest matching overload and selects System.BitConverter.GetBytes(System.Single).

ironpythonbot commented 9 years ago

On 2010-01-08 02:46:06 UTC, CurtHagenlocher commented:

Here's another overload resolution / conversion issue. The following C# + IronPython code results in the error

TypeError: unsupported operand type(s) for -: 'FloatParallelArray' and 'float' It should cast the Double to a Single. This already happens for non-operator calls.

public class FloatParallelArray { public static FloatParallelArray operator -(FloatParallelArray a1, FloatParallelArray a2); public static FloatParallelArray operator -(FloatParallelArray a, float f); public static FloatParallelArray operator -(float f, FloatParallelArray a); public static FloatParallelArray operator -(FloatParallelArray a); }

f = FloatParallelArray() g = f - 0.5 # Generates error

ironpythonbot commented 9 years ago

On 2011-08-31 12:27:28 UTC, skottmckay commented:

Note this can be worked around by explicitly choosing the overload

x = long(999000999000) System.BitConverter.GetBytes.OverloadsSystem.UInt64