Skip to content

Commit 65a6408

Browse files
committed
Add int constructor for (U)IntPtr
1 parent c88b1f6 commit 65a6408

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

src/runtime/Types/ClassObject.cs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ private static NewReference NewString(BorrowedReference args, BorrowedReference
129129

130130
/// <summary>
131131
/// Create a new Python object for a primitive type
132-
///
132+
///
133133
/// The primitive types are Boolean, Byte, SByte, Int16, UInt16,
134134
/// Int32, UInt32, Int64, UInt64, IntPtr, UIntPtr, Char, Double,
135135
/// and Single.
@@ -170,6 +170,14 @@ private static NewReference NewPrimitive(BorrowedReference tp, BorrowedReference
170170
break;
171171
}
172172
}
173+
else if (Runtime.PyInt_Check(op))
174+
{
175+
long? num = Runtime.PyLong_AsLongLong(op);
176+
if (num is long n)
177+
{
178+
result = new IntPtr(n);
179+
}
180+
}
173181
}
174182

175183
if (type == typeof(UIntPtr))
@@ -189,6 +197,14 @@ private static NewReference NewPrimitive(BorrowedReference tp, BorrowedReference
189197
break;
190198
}
191199
}
200+
else if (Runtime.PyInt_Check(op))
201+
{
202+
ulong? num = Runtime.PyLong_AsUnsignedLongLong(op);
203+
if (num is ulong n)
204+
{
205+
result = new UIntPtr(n);
206+
}
207+
}
192208
}
193209

194210
if (result == null && !Converter.ToManaged(op, type, out result, true))

tests/test_conversion.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -689,10 +689,13 @@ def test_intptr_construction():
689689
assert ob.UIntPtrField == UIntPtr.Zero
690690

691691
ob.IntPtrField = IntPtr(Int64(-1))
692+
assert ob.IntPtrField == IntPtr(-1)
692693
assert ob.IntPtrField.ToInt64() == -1
693694

694695
ob.IntPtrField = IntPtr(Int64(1024))
696+
assert ob.IntPtrField == IntPtr(1024)
695697
assert ob.IntPtrField.ToInt64() == 1024
696698

697699
ob.UIntPtrField = UIntPtr(UInt64(1024))
700+
assert ob.UIntPtrField == UIntPtr(1024)
698701
assert ob.UIntPtrField.ToUInt64() == 1024

0 commit comments

Comments
 (0)