Skip to content

Commit 10e5037

Browse files
jhonabreulclaude
andcommitted
Avoid recomputing TypeCode in integral-float checks
Add a TypeCode-based IsInteger overload and reuse the TypeCode already computed by the callers in Converter.ToPrimitive and MethodBinder, instead of fetching it again inside IsInteger(Type). Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent e3640ac commit 10e5037

3 files changed

Lines changed: 13 additions & 3 deletions

File tree

src/runtime/Converter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -900,7 +900,7 @@ internal static bool ToPrimitive(BorrowedReference value, Type obType, out objec
900900
// accepted. This keeps single- and multi-overload binding consistent:
901901
// MethodBinder only treats integral floats as candidates for integer
902902
// parameters, and this guard enforces the same rule at conversion time.
903-
if (obType.IsInteger() && Runtime.PyFloat_Check(value))
903+
if (tc.IsInteger() && Runtime.PyFloat_Check(value))
904904
{
905905
double dbl = Runtime.PyFloat_AsDouble(value);
906906
if (double.IsNaN(dbl) || double.IsInfinity(dbl) || Math.Truncate(dbl) != dbl)

src/runtime/MethodBinder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -683,7 +683,7 @@ internal Binding Bind(BorrowedReference inst, BorrowedReference args, BorrowedRe
683683
// parameters. Converter.ToManaged rejects non-integral floats
684684
// (e.g. 5.5) so we don't silently truncate. Enums are excluded
685685
// on purpose.
686-
else if (Runtime.PyFloat_Check(op) && underlyingType.IsInteger() && !underlyingType.IsEnum)
686+
else if (Runtime.PyFloat_Check(op) && argtypecode.IsInteger() && !underlyingType.IsEnum)
687687
{
688688
clrtype = parameter.ParameterType;
689689
typematch = Converter.ToManaged(op, clrtype, out arg, false);

src/runtime/Util/Util.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,17 @@ public static bool IsDelegate(this Type type)
311311
/// </summary>
312312
public static bool IsInteger(this Type type)
313313
{
314-
switch (Type.GetTypeCode(type))
314+
return Type.GetTypeCode(type).IsInteger();
315+
}
316+
317+
/// <summary>
318+
/// Determines whether the specified type code is a CLR integer type (signed or unsigned).
319+
/// Enums report an integral <see cref="TypeCode"/> too, so callers that want to
320+
/// exclude them must check <see cref="Type.IsEnum"/> separately.
321+
/// </summary>
322+
public static bool IsInteger(this TypeCode typeCode)
323+
{
324+
switch (typeCode)
315325
{
316326
case TypeCode.Byte:
317327
case TypeCode.SByte:

0 commit comments

Comments
 (0)