Skip to content

Commit 29b74c7

Browse files
dhirschfeldfilmor
authored andcommitted
Allow passing None for nullable args (#460)
1 parent 67650a2 commit 29b74c7

File tree

3 files changed

+23
-0
lines changed

3 files changed

+23
-0
lines changed

src/runtime/converter.cs

+11
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,17 @@ internal static bool ToManagedValue(IntPtr value, Type obType,
318318
return true;
319319
}
320320

321+
if (obType.IsGenericType && obType.GetGenericTypeDefinition() == typeof(Nullable<>))
322+
{
323+
if( value == Runtime.PyNone )
324+
{
325+
result = null;
326+
return true;
327+
}
328+
// Set type to underlying type
329+
obType = obType.GetGenericArguments()[0];
330+
}
331+
321332
if (obType.IsArray)
322333
{
323334
return ToArray(value, obType, out result, setError);

src/testing/conversiontest.cs

+6
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,14 @@ public ConversionTest()
3232

3333
public byte[] ByteArrayField;
3434
public sbyte[] SByteArrayField;
35+
36+
public T? Echo<T>(T? arg) where T: struct {
37+
return arg;
38+
}
39+
3540
}
3641

42+
3743

3844
public interface ISpam
3945
{

src/tests/test_conversion.py

+6
Original file line numberDiff line numberDiff line change
@@ -641,6 +641,8 @@ def test_enum_conversion():
641641

642642
def test_null_conversion():
643643
"""Test null conversion."""
644+
import System
645+
644646
ob = ConversionTest()
645647

646648
ob.StringField = None
@@ -652,6 +654,10 @@ def test_null_conversion():
652654
ob.SpamField = None
653655
assert ob.SpamField is None
654656

657+
pi = 22/7
658+
assert ob.Echo[System.Double](pi) == pi
659+
assert ob.Echo[System.DateTime](None) is None
660+
655661
# Primitive types and enums should not be set to null.
656662

657663
with pytest.raises(TypeError):

0 commit comments

Comments
 (0)