| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
14 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,5 +1,5 @@ | |||
| 1 | 1 | [bumpversion] | |
| 2 | - current_version = 1.0.5.24 | ||
| 2 | + current_version = 1.0.5.25 | ||
| 3 | 3 | parse = (?P<major>\d+)\.(?P<minor>\d+)\.(?P<patch>\d+)(\.(?P<release>[a-z]+)(?P<dev>\d+))? | |
| 4 | 4 | serialize = | |
| 5 | 5 | {major}.{minor}.{patch}.{release}{dev} | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,6 +1,6 @@ | |||
| 1 | 1 | package: | |
| 2 | 2 | name: pythonnet | |
| 3 | - version: "1.0.5.24" | ||
| 3 | + version: "1.0.5.25" | ||
| 4 | 4 | ||
| 5 | 5 | build: | |
| 6 | 6 | skip: True # [not win] | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -485,7 +485,7 @@ def run(self): | |||
| 485 | 485 | ||
| 486 | 486 | setup( | |
| 487 | 487 | name="pythonnet", | |
| 488 | - version="1.0.5.24", | ||
| 488 | + version="1.0.5.25", | ||
| 489 | 489 | description=".Net and Mono integration for Python", | |
| 490 | 490 | url='https://pythonnet.github.io/', | |
| 491 | 491 | license='MIT', | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -25,4 +25,4 @@ | |||
| 25 | 25 | // Version Information. Keeping it simple. May need to revisit for Nuget | |
| 26 | 26 | // See: https://codingforsmarties.wordpress.com/2016/01/21/how-to-version-assemblies-destined-for-nuget/ | |
| 27 | 27 | // AssemblyVersion can only be numeric | |
| 28 | - [assembly: AssemblyVersion("1.0.5.24")] | ||
| 28 | + [assembly: AssemblyVersion("1.0.5.25")] | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -53,7 +53,7 @@ public static void initclr() | |||
| 53 | 53 | { | |
| 54 | 54 | #if USE_PYTHON_RUNTIME_VERSION | |
| 55 | 55 | // Has no effect until SNK works. Keep updated anyways. | |
| 56 | - Version = new Version("1.0.5.24"), | ||
| 56 | + Version = new Version("1.0.5.25"), | ||
| 57 | 57 | #endif | |
| 58 | 58 | CultureInfo = CultureInfo.InvariantCulture | |
| 59 | 59 | }; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -88,6 +88,7 @@ | |||
| 88 | 88 | <Compile Include="TestCustomMarshal.cs" /> | |
| 89 | 89 | <Compile Include="TestExample.cs" /> | |
| 90 | 90 | <Compile Include="TestFinalizer.cs" /> | |
| 91 | + <Compile Include="TestMethodBinder.cs" /> | ||
| 91 | 92 | <Compile Include="TestPyAnsiString.cs" /> | |
| 92 | 93 | <Compile Include="TestPyFloat.cs" /> | |
| 93 | 94 | <Compile Include="TestPyInt.cs" /> | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,151 @@ | |||
| 1 | + using NUnit.Framework; | ||
| 2 | + using Python.Runtime; | ||
| 3 | + | ||
| 4 | + namespace Python.EmbeddingTest | ||
| 5 | + { | ||
| 6 | + public class TestMethodBinder | ||
| 7 | + { | ||
| 8 | + [OneTimeSetUp] | ||
| 9 | + public void SetUp() | ||
| 10 | + { | ||
| 11 | + PythonEngine.Initialize(); | ||
| 12 | + } | ||
| 13 | + | ||
| 14 | + [OneTimeTearDown] | ||
| 15 | + public void Dispose() | ||
| 16 | + { | ||
| 17 | + PythonEngine.Shutdown(); | ||
| 18 | + } | ||
| 19 | + | ||
| 20 | + [Test] | ||
| 21 | + public void ImplicitConversionToString() | ||
| 22 | + { | ||
| 23 | + // create instance of python model | ||
| 24 | + dynamic pyMethodCall = PythonEngine.ModuleFromString("module", @" | ||
| 25 | + from clr import AddReference | ||
| 26 | + AddReference(""System"") | ||
| 27 | + AddReference(""Python.EmbeddingTest"") | ||
| 28 | + | ||
| 29 | + from Python.EmbeddingTest import * | ||
| 30 | + | ||
| 31 | + class PythonModel(TestMethodBinder.CSharpModel): | ||
| 32 | + def MethodCall(self): | ||
| 33 | + return self.OnlyString(TestMethodBinder.TestImplicitConversion()) | ||
| 34 | + ").GetAttr("PythonModel").Invoke(); | ||
| 35 | + | ||
| 36 | + using (Py.GIL()) | ||
| 37 | + { | ||
| 38 | + var data = (string)pyMethodCall.MethodCall(); | ||
| 39 | + // we assert implicit conversion took place | ||
| 40 | + Assert.AreEqual("OnlyString impl: implicit to string", data); | ||
| 41 | + } | ||
| 42 | + } | ||
| 43 | + | ||
| 44 | + [Test] | ||
| 45 | + public void ImplicitConversionToClass() | ||
| 46 | + { | ||
| 47 | + // create instance of python model | ||
| 48 | + dynamic pyMethodCall = PythonEngine.ModuleFromString("module", @" | ||
| 49 | + from clr import AddReference | ||
| 50 | + AddReference(""System"") | ||
| 51 | + AddReference(""Python.EmbeddingTest"") | ||
| 52 | + | ||
| 53 | + from Python.EmbeddingTest import * | ||
| 54 | + | ||
| 55 | + class PythonModel(TestMethodBinder.CSharpModel): | ||
| 56 | + def MethodCall(self): | ||
| 57 | + return self.OnlyClass('input string') | ||
| 58 | + ").GetAttr("PythonModel").Invoke(); | ||
| 59 | + | ||
| 60 | + using (Py.GIL()) | ||
| 61 | + { | ||
| 62 | + var data = (string)pyMethodCall.MethodCall(); | ||
| 63 | + // we assert implicit conversion took place | ||
| 64 | + Assert.AreEqual("OnlyClass impl", data); | ||
| 65 | + } | ||
| 66 | + } | ||
| 67 | + | ||
| 68 | + [Test] | ||
| 69 | + public void WillAvoidUsingImplicitConversionIfPossible_String() | ||
| 70 | + { | ||
| 71 | + // create instance of python model | ||
| 72 | + dynamic pyMethodCall = PythonEngine.ModuleFromString("module", @" | ||
| 73 | + from clr import AddReference | ||
| 74 | + AddReference(""System"") | ||
| 75 | + AddReference(""Python.EmbeddingTest"") | ||
| 76 | + | ||
| 77 | + from Python.EmbeddingTest import * | ||
| 78 | + | ||
| 79 | + class PythonModel(TestMethodBinder.CSharpModel): | ||
| 80 | + def MethodCall(self): | ||
| 81 | + return self.InvokeModel('input string') | ||
| 82 | + ").GetAttr("PythonModel").Invoke(); | ||
| 83 | + | ||
| 84 | + using (Py.GIL()) | ||
| 85 | + { | ||
| 86 | + var data = (string)pyMethodCall.MethodCall(); | ||
| 87 | + // we assert no implicit conversion took place | ||
| 88 | + Assert.AreEqual("string impl: input string", data); | ||
| 89 | + } | ||
| 90 | + } | ||
| 91 | + | ||
| 92 | + [Test] | ||
| 93 | + public void WillAvoidUsingImplicitConversionIfPossible_Class() | ||
| 94 | + { | ||
| 95 | + // create instance of python model | ||
| 96 | + dynamic pyMethodCall = PythonEngine.ModuleFromString("module", @" | ||
| 97 | + from clr import AddReference | ||
| 98 | + AddReference(""System"") | ||
| 99 | + AddReference(""Python.EmbeddingTest"") | ||
| 100 | + | ||
| 101 | + from Python.EmbeddingTest import * | ||
| 102 | + | ||
| 103 | + class PythonModel(TestMethodBinder.CSharpModel): | ||
| 104 | + def MethodCall(self): | ||
| 105 | + return self.InvokeModel(TestMethodBinder.TestImplicitConversion()) | ||
| 106 | + ").GetAttr("PythonModel").Invoke(); | ||
| 107 | + | ||
| 108 | + using (Py.GIL()) | ||
| 109 | + { | ||
| 110 | + var data = (string)pyMethodCall.MethodCall(); | ||
| 111 | + // we assert no implicit conversion took place | ||
| 112 | + Assert.AreEqual("TestImplicitConversion impl", data); | ||
| 113 | + } | ||
| 114 | + } | ||
| 115 | + | ||
| 116 | + public class CSharpModel | ||
| 117 | + { | ||
| 118 | + public virtual string OnlyClass(TestImplicitConversion data) | ||
| 119 | + { | ||
| 120 | + return "OnlyClass impl"; | ||
| 121 | + } | ||
| 122 | + | ||
| 123 | + public virtual string OnlyString(string data) | ||
| 124 | + { | ||
| 125 | + return "OnlyString impl: " + data; | ||
| 126 | + } | ||
| 127 | + | ||
| 128 | + public virtual string InvokeModel(string data) | ||
| 129 | + { | ||
| 130 | + return "string impl: " + data; | ||
| 131 | + } | ||
| 132 | + | ||
| 133 | + public virtual string InvokeModel(TestImplicitConversion data) | ||
| 134 | + { | ||
| 135 | + return "TestImplicitConversion impl"; | ||
| 136 | + } | ||
| 137 | + } | ||
| 138 | + | ||
| 139 | + public class TestImplicitConversion | ||
| 140 | + { | ||
| 141 | + public static implicit operator string(TestImplicitConversion symbol) | ||
| 142 | + { | ||
| 143 | + return "implicit to string"; | ||
| 144 | + } | ||
| 145 | + public static implicit operator TestImplicitConversion(string symbol) | ||
| 146 | + { | ||
| 147 | + return new TestImplicitConversion(); | ||
| 148 | + } | ||
| 149 | + } | ||
| 150 | + } | ||
| 151 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -31,9 +31,9 @@ internal ClassObject(Type tp) : base(tp) | |||
| 31 | 31 | /// </summary> | |
| 32 | 32 | internal IntPtr GetDocString() | |
| 33 | 33 | { | |
| 34 | - MethodBase[] methods = binder.GetMethods(); | ||
| 34 | + var methods = binder.GetMethods(); | ||
| 35 | 35 | var str = ""; | |
| 36 | - foreach (MethodBase t in methods) | ||
| 36 | + foreach (var t in methods) | ||
| 37 | 37 | { | |
| 38 | 38 | if (str.Length > 0) | |
| 39 | 39 | { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -119,10 +119,10 @@ public static IntPtr tp_repr(IntPtr ob) | |||
| 119 | 119 | Runtime.XIncref(self.repr); | |
| 120 | 120 | return self.repr; | |
| 121 | 121 | } | |
| 122 | - MethodBase[] methods = self.ctorBinder.GetMethods(); | ||
| 122 | + var methods = self.ctorBinder.GetMethods(); | ||
| 123 | 123 | string name = self.type.FullName; | |
| 124 | 124 | var doc = ""; | |
| 125 | - foreach (MethodBase t in methods) | ||
| 125 | + foreach (var t in methods) | ||
| 126 | 126 | { | |
| 127 | 127 | if (doc.Length > 0) | |
| 128 | 128 | { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -397,6 +397,17 @@ internal static bool ToManagedValue(IntPtr value, Type obType, | |||
| 397 | 397 | result = tmp; | |
| 398 | 398 | return true; | |
| 399 | 399 | } | |
| 400 | + else | ||
| 401 | + { | ||
| 402 | + var type = tmp.GetType(); | ||
| 403 | + // check implicit conversions that receive tmp type and return obType | ||
| 404 | + var conversionMethod = type.GetMethod("op_Implicit", new[] { type }); | ||
| 405 | + if (conversionMethod != null && conversionMethod.ReturnType == obType) | ||
| 406 | + { | ||
| 407 | + result = conversionMethod.Invoke(null, new[] { tmp }); | ||
| 408 | + return true; | ||
| 409 | + } | ||
| 410 | + } | ||
| 400 | 411 | Exceptions.SetError(Exceptions.TypeError, $"value cannot be converted to {obType}"); | |
| 401 | 412 | return false; | |
| 402 | 413 | } | |
| Back | FazBrowse Home | New Git URL |
0 commit comments