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

Skip to content

Commit 8029ffe

Browse files
authored
Merge pull request #219 from matthid/myfixes
Some fixes.
2 parents 5f21a85 + c07e9fe commit 8029ffe

File tree

12 files changed

+123
-11
lines changed

12 files changed

+123
-11
lines changed

src/runtime/classderived.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -786,7 +786,7 @@ public static void InvokeCtor(IPythonDerivedType obj, string origCtorName, Objec
786786
PyObject[] pyargs = new PyObject[args.Length];
787787
for (int i = 0; i < args.Length; ++i)
788788
{
789-
pyargs[i] = new PyObject(Converter.ToPython(args[i], args[i].GetType()));
789+
pyargs[i] = new PyObject(Converter.ToPython(args[i], args[i]?.GetType()));
790790
disposeList.Add(pyargs[i]);
791791
}
792792

src/runtime/converter.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ internal static IntPtr ToPython(Object value, Type type)
228228
{
229229
foreach (object o in (IEnumerable)value)
230230
{
231-
using (var p = new PyObject(ToPython(o, o.GetType())))
231+
using (var p = new PyObject(ToPython(o, o?.GetType())))
232232
resultlist.Append(p);
233233
}
234234
Runtime.Incref(resultlist.Handle);
@@ -962,7 +962,7 @@ public static class ConverterExtension
962962
{
963963
public static PyObject ToPython(this object o)
964964
{
965-
return new PyObject(Converter.ToPython(o, o.GetType()));
965+
return new PyObject(Converter.ToPython(o, o?.GetType()));
966966
}
967967
}
968968
}

src/runtime/moduleobject.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,10 @@ public ModuleObject(string name) : base()
3535
string docstring = "Namespace containing types from the following assemblies:\n\n";
3636
foreach (Assembly a in AssemblyManager.GetAssemblies(name))
3737
{
38-
filename = a.Location;
38+
if (!a.IsDynamic && a.Location != null)
39+
{
40+
filename = a.Location;
41+
}
3942
docstring += "- " + a.FullName + "\n";
4043
}
4144

src/runtime/pyobject.cs

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -939,7 +939,7 @@ public override bool TryGetMember(GetMemberBinder binder, out object result)
939939
{
940940
if (this.HasAttr(binder.Name))
941941
{
942-
result = this.GetAttr(binder.Name);
942+
result = CheckNone(this.GetAttr(binder.Name));
943943
return true;
944944
}
945945
else
@@ -972,7 +972,7 @@ private void GetArgs(object[] inargs, out PyTuple args, out PyDict kwargs)
972972
}
973973
else
974974
{
975-
ptr = Converter.ToPython(inargs[i], inargs[i].GetType());
975+
ptr = Converter.ToPython(inargs[i], inargs[i]?.GetType());
976976
}
977977
if (Runtime.PyTuple_SetItem(argtuple, i, ptr) < 0)
978978
throw new PythonException();
@@ -999,7 +999,7 @@ public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, o
999999
try
10001000
{
10011001
GetArgs(args, out pyargs, out kwargs);
1002-
result = InvokeMethod(binder.Name, pyargs, kwargs);
1002+
result = CheckNone(InvokeMethod(binder.Name, pyargs, kwargs));
10031003
}
10041004
finally
10051005
{
@@ -1023,7 +1023,7 @@ public override bool TryInvoke(InvokeBinder binder, object[] args, out object re
10231023
try
10241024
{
10251025
GetArgs(args, out pyargs, out kwargs);
1026-
result = Invoke(pyargs, kwargs);
1026+
result = CheckNone(Invoke(pyargs, kwargs));
10271027
}
10281028
finally
10291029
{
@@ -1133,10 +1133,25 @@ public override bool TryBinaryOperation(BinaryOperationBinder binder, Object arg
11331133
result = null;
11341134
return false;
11351135
}
1136-
result = new PyObject(res);
1136+
result = CheckNone(new PyObject(res));
11371137
return true;
11381138
}
11391139

1140+
// Workaround for https://bugzilla.xamarin.com/show_bug.cgi?id=41509
1141+
// See https://github.com/pythonnet/pythonnet/pull/219
1142+
private static object CheckNone(PyObject pyObj)
1143+
{
1144+
if (pyObj != null)
1145+
{
1146+
if (pyObj.obj == Runtime.PyNone)
1147+
{
1148+
return null;
1149+
}
1150+
}
1151+
1152+
return pyObj;
1153+
}
1154+
11401155
public override bool TryUnaryOperation(UnaryOperationBinder binder, out Object result)
11411156
{
11421157
int r;
@@ -1170,7 +1185,7 @@ public override bool TryUnaryOperation(UnaryOperationBinder binder, out Object r
11701185
result = null;
11711186
return false;
11721187
}
1173-
result = new PyObject(res);
1188+
result = CheckNone(new PyObject(res));
11741189
return true;
11751190
}
11761191
}

src/runtime/pythonengine.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -487,7 +487,7 @@ public static KeywordArguments kw(params object[] kv)
487487
if (kv[i + 1] is PyObject)
488488
value = ((PyObject)kv[i + 1]).Handle;
489489
else
490-
value = Converter.ToPython(kv[i + 1], kv[i + 1].GetType());
490+
value = Converter.ToPython(kv[i + 1], kv[i + 1]?.GetType());
491491
if (Runtime.PyDict_SetItemString(dict.Handle, (string)kv[i], value) != 0)
492492
throw new ArgumentException(string.Format("Cannot add key '{0}' to dictionary.", (string)kv[i]));
493493
if (!(kv[i + 1] is PyObject))

src/testing/Python.Test.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@
108108
</PropertyGroup>
109109
<ItemGroup>
110110
<Compile Include="arraytest.cs" />
111+
<Compile Include="callbacktest.cs" />
111112
<Compile Include="classtest.cs" />
112113
<Compile Include="constructortests.cs" />
113114
<Compile Include="conversiontest.cs" />
@@ -127,6 +128,7 @@
127128
<Compile Include="subclasstest.cs" />
128129
</ItemGroup>
129130
<ItemGroup>
131+
<Reference Include="Microsoft.CSharp" />
130132
<Reference Include="System" />
131133
<Reference Include="System.Core">
132134
<RequiredTargetFramework>3.5</RequiredTargetFramework>

src/testing/callbacktest.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
6+
namespace Python.Test
7+
{
8+
//========================================================================
9+
// Tests callbacks into python code.
10+
//========================================================================
11+
12+
public class CallbackTest
13+
{
14+
public string Call_simpleDefaultArg_WithNull(string moduleName)
15+
{
16+
using (Runtime.Py.GIL())
17+
{
18+
dynamic module = Runtime.Py.Import(moduleName);
19+
return module.simpleDefaultArg(null);
20+
}
21+
}
22+
public string Call_simpleDefaultArg_WithEmptyArgs(string moduleName)
23+
{
24+
using (Runtime.Py.GIL())
25+
{
26+
dynamic module = Runtime.Py.Import(moduleName);
27+
return module.simpleDefaultArg();
28+
}
29+
}
30+
}
31+
}

src/tests/runtests.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
# other test modules that import System.Windows.Forms
1919
# run first. They must not do module level import/AddReference()
2020
# of the System.Windows.Forms namespace.
21+
'test_suite',
2122
'test_event',
2223
'test_constructors',
2324
'test_enum',

src/tests/test_suite/__init__.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import unittest
2+
3+
__all__ = ['test_suite']
4+
5+
from .test_import import test_suite as import_tests
6+
from .test_callback import test_suite as callback_tests
7+
8+
def test_suite():
9+
suite = unittest.TestSuite()
10+
suite.addTests((import_tests(),))
11+
suite.addTests((callback_tests(),))
12+
return suite
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
2+
import this_package_should_never_exist_ever

src/tests/test_suite/test_callback.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import unittest, sys
2+
import clr
3+
4+
this_module = sys.modules[__name__]
5+
clr.AddReference("Python.Test")
6+
import Python.Test as Test
7+
from Python.Test import CallbackTest
8+
test_instance = CallbackTest()
9+
10+
def simpleDefaultArg(arg = 'test'):
11+
return arg
12+
13+
class CallbackTests(unittest.TestCase):
14+
"""Test that callbacks from C# into python work."""
15+
16+
def testDefaultForNull(self):
17+
"""Test that C# can use null for an optional python argument"""
18+
retVal = test_instance.Call_simpleDefaultArg_WithNull(__name__)
19+
pythonRetVal = simpleDefaultArg(None)
20+
self.assertEquals(retVal, pythonRetVal)
21+
22+
def testDefaultForNone(self):
23+
"""Test that C# can use no argument for an optional python argument"""
24+
retVal = test_instance.Call_simpleDefaultArg_WithEmptyArgs(__name__)
25+
pythonRetVal = simpleDefaultArg()
26+
self.assertEquals(retVal, pythonRetVal)
27+
28+
def test_suite():
29+
return unittest.makeSuite(CallbackTests)
30+

src/tests/test_suite/test_import.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import unittest
2+
3+
class ImportTests(unittest.TestCase):
4+
"""Test the import statement."""
5+
6+
def testRealtiveMissingImport(self):
7+
"""Test that a relative missing import doesn't crash. Some modules use this to check if a package is installed (realtive import in the site-packages folder"""
8+
try:
9+
from . import _missing_import
10+
except ImportError:
11+
pass
12+
13+
14+
def test_suite():
15+
return unittest.makeSuite(ImportTests)
16+

0 commit comments

Comments
 (0)