Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Jul 22, 2023. It is now read-only.
/pythonnetPublic archive
forked frompythonnet/pythonnet

Commit8029ffe

Browse files
authored
Merge pull requestpythonnet#219 from matthid/myfixes
Some fixes.
2 parents5f21a85 +c07e9fe commit8029ffe

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=newPyObject[args.Length];
787787
for(inti=0;i<args.Length;++i)
788788
{
789-
pyargs[i]=newPyObject(Converter.ToPython(args[i],args[i].GetType()));
789+
pyargs[i]=newPyObject(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(objectoin(IEnumerable)value)
230230
{
231-
using(varp=newPyObject(ToPython(o,o.GetType())))
231+
using(varp=newPyObject(ToPython(o,o?.GetType())))
232232
resultlist.Append(p);
233233
}
234234
Runtime.Incref(resultlist.Handle);
@@ -962,7 +962,7 @@ public static class ConverterExtension
962962
{
963963
publicstaticPyObjectToPython(thisobjecto)
964964
{
965-
returnnewPyObject(Converter.ToPython(o,o.GetType()));
965+
returnnewPyObject(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
stringdocstring="Namespace containing types from the following assemblies:\n\n";
3636
foreach(AssemblyainAssemblyManager.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
returntrue;
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
thrownewPythonException();
@@ -999,7 +999,7 @@ public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, o
999999
try
10001000
{
10011001
GetArgs(args,outpyargs,outkwargs);
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,outpyargs,outkwargs);
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
returnfalse;
11351135
}
1136-
result=newPyObject(res);
1136+
result=CheckNone(newPyObject(res));
11371137
returntrue;
11381138
}
11391139

1140+
// Workaround for https://bugzilla.xamarin.com/show_bug.cgi?id=41509
1141+
// See https://github.com/pythonnet/pythonnet/pull/219
1142+
privatestaticobjectCheckNone(PyObjectpyObj)
1143+
{
1144+
if(pyObj!=null)
1145+
{
1146+
if(pyObj.obj==Runtime.PyNone)
1147+
{
1148+
returnnull;
1149+
}
1150+
}
1151+
1152+
returnpyObj;
1153+
}
1154+
11401155
publicoverrideboolTryUnaryOperation(UnaryOperationBinderbinder,outObjectresult)
11411156
{
11421157
intr;
@@ -1170,7 +1185,7 @@ public override bool TryUnaryOperation(UnaryOperationBinder binder, out Object r
11701185
result=null;
11711186
returnfalse;
11721187
}
1173-
result=newPyObject(res);
1188+
result=CheckNone(newPyObject(res));
11741189
returntrue;
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]isPyObject)
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
thrownewArgumentException(string.Format("Cannot add key '{0}' to dictionary.",(string)kv[i]));
493493
if(!(kv[i+1]isPyObject))

‎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
<CompileInclude="arraytest.cs" />
111+
<CompileInclude="callbacktest.cs" />
111112
<CompileInclude="classtest.cs" />
112113
<CompileInclude="constructortests.cs" />
113114
<CompileInclude="conversiontest.cs" />
@@ -127,6 +128,7 @@
127128
<CompileInclude="subclasstest.cs" />
128129
</ItemGroup>
129130
<ItemGroup>
131+
<ReferenceInclude="Microsoft.CSharp" />
130132
<ReferenceInclude="System" />
131133
<ReferenceInclude="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+
usingSystem;
2+
usingSystem.Collections.Generic;
3+
usingSystem.Linq;
4+
usingSystem.Text;
5+
6+
namespacePython.Test
7+
{
8+
//========================================================================
9+
// Tests callbacks into python code.
10+
//========================================================================
11+
12+
publicclassCallbackTest
13+
{
14+
publicstringCall_simpleDefaultArg_WithNull(stringmoduleName)
15+
{
16+
using(Runtime.Py.GIL())
17+
{
18+
dynamicmodule=Runtime.Py.Import(moduleName);
19+
returnmodule.simpleDefaultArg(null);
20+
}
21+
}
22+
publicstringCall_simpleDefaultArg_WithEmptyArgs(stringmoduleName)
23+
{
24+
using(Runtime.Py.GIL())
25+
{
26+
dynamicmodule=Runtime.Py.Import(moduleName);
27+
returnmodule.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+
importunittest
2+
3+
__all__= ['test_suite']
4+
5+
from .test_importimporttest_suiteasimport_tests
6+
from .test_callbackimporttest_suiteascallback_tests
7+
8+
deftest_suite():
9+
suite=unittest.TestSuite()
10+
suite.addTests((import_tests(),))
11+
suite.addTests((callback_tests(),))
12+
returnsuite
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
2+
importthis_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+
importunittest,sys
2+
importclr
3+
4+
this_module=sys.modules[__name__]
5+
clr.AddReference("Python.Test")
6+
importPython.TestasTest
7+
fromPython.TestimportCallbackTest
8+
test_instance=CallbackTest()
9+
10+
defsimpleDefaultArg(arg='test'):
11+
returnarg
12+
13+
classCallbackTests(unittest.TestCase):
14+
"""Test that callbacks from C# into python work."""
15+
16+
deftestDefaultForNull(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+
deftestDefaultForNone(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+
deftest_suite():
29+
returnunittest.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+
importunittest
2+
3+
classImportTests(unittest.TestCase):
4+
"""Test the import statement."""
5+
6+
deftestRealtiveMissingImport(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+
exceptImportError:
11+
pass
12+
13+
14+
deftest_suite():
15+
returnunittest.makeSuite(ImportTests)
16+

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp