FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Allow passing None for nullable args by dhirschfeld · Pull Request #460 · pythonnet/pythonnet · GitHub

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .cs  (2) .py  (1) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
11 changes: 11 additions & 0 deletions src/runtime/converter.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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

Back | FazBrowse Home | New Git URL