Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Allow passing None for nullable args #460

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 10, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/runtime/converter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,17 @@ internal static bool ToManagedValue(IntPtr value, Type obType,
return true;
}

if (obType.IsGenericType && obType.GetGenericTypeDefinition() == typeof(Nullable<>))
{
if( value == Runtime.PyNone )
{
result = null;
return true;
}
// Set type to underlying type
obType = obType.GetGenericArguments()[0];
}

if (obType.IsArray)
{
return ToArray(value, obType, out result, setError);
Expand Down
6 changes: 6 additions & 0 deletions src/testing/conversiontest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,14 @@ public ConversionTest()

public byte[] ByteArrayField;
public sbyte[] SByteArrayField;

public T? Echo<T>(T? arg) where T: struct {
return arg;
}

}



public interface ISpam
{
Expand Down
6 changes: 6 additions & 0 deletions src/tests/test_conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -641,6 +641,8 @@ def test_enum_conversion():

def test_null_conversion():
"""Test null conversion."""
import System

ob = ConversionTest()

ob.StringField = None
Expand All @@ -652,6 +654,10 @@ def test_null_conversion():
ob.SpamField = None
assert ob.SpamField is None

pi = 22/7
assert ob.Echo[System.Double](pi) == pi
assert ob.Echo[System.DateTime](None) is None

# Primitive types and enums should not be set to null.

with pytest.raises(TypeError):
Expand Down