Skip to content

Commit 24b0ffc

Browse files
author
dhirschf
committed
Allow passing None for nullable args
1 parent 67650a2 commit 24b0ffc

File tree

3 files changed

+22
-0
lines changed

3 files changed

+22
-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

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

642642
def test_null_conversion():
643643
"""Test null conversion."""
644+
import clr
645+
import System
646+
644647
ob = ConversionTest()
645648

646649
ob.StringField = None
@@ -652,6 +655,8 @@ def test_null_conversion():
652655
ob.SpamField = None
653656
assert ob.SpamField is None
654657

658+
assert ob.Echo[System.DateTime](None) is None
659+
655660
# Primitive types and enums should not be set to null.
656661

657662
with pytest.raises(TypeError):

0 commit comments

Comments
 (0)