|
| 1 | +using NUnit.Framework; |
| 2 | +using Python.Runtime; |
| 3 | + |
| 4 | +namespace Python.EmbeddingTest |
| 5 | +{ |
| 6 | + /// <summary> |
| 7 | + /// Passing a Python float where a .NET integer is expected. |
| 8 | + /// |
| 9 | + /// A float that holds an integral value (e.g. 5.0) is accepted and converted; |
| 10 | + /// a non-integral float (e.g. 5.5) is rejected rather than silently truncated. |
| 11 | + /// This must hold regardless of whether the target method/constructor has a |
| 12 | + /// single signature or several overloads (the latter reproduces Lean's |
| 13 | + /// RangeConsolidator(period), which has two int-first constructor overloads). |
| 14 | + /// </summary> |
| 15 | + public class TestFloatToIntConversion |
| 16 | + { |
| 17 | + private PyModule _module; |
| 18 | + |
| 19 | + private const string TestModule = @" |
| 20 | +from clr import AddReference |
| 21 | +AddReference(""Python.EmbeddingTest"") |
| 22 | +from Python.EmbeddingTest import IntTaker, OverloadedIntTaker |
| 23 | +
|
| 24 | +def single_ctor(value): |
| 25 | + return IntTaker(value).Value |
| 26 | +
|
| 27 | +def single_method(value): |
| 28 | + return IntTaker(0).Echo(value) |
| 29 | +
|
| 30 | +def overloaded_ctor(value): |
| 31 | + return OverloadedIntTaker(value).Value |
| 32 | +
|
| 33 | +def overloaded_method(value): |
| 34 | + return OverloadedIntTaker(0).Echo(value) |
| 35 | +"; |
| 36 | + |
| 37 | + [OneTimeSetUp] |
| 38 | + public void Setup() |
| 39 | + { |
| 40 | + PythonEngine.Initialize(); |
| 41 | + _module = PyModule.FromString("float_to_int_module", TestModule); |
| 42 | + } |
| 43 | + |
| 44 | + [OneTimeTearDown] |
| 45 | + public void TearDown() |
| 46 | + { |
| 47 | + _module.Dispose(); |
| 48 | + PythonEngine.Shutdown(); |
| 49 | + } |
| 50 | + |
| 51 | + private int Call(string func, double value) |
| 52 | + { |
| 53 | + using (Py.GIL()) |
| 54 | + using (var arg = value.ToPython()) |
| 55 | + { |
| 56 | + return _module.InvokeMethod(func, arg).As<int>(); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + // An integral-valued float is accepted and converted, single or overloaded. |
| 61 | + [TestCase("single_ctor")] |
| 62 | + [TestCase("single_method")] |
| 63 | + [TestCase("overloaded_ctor")] |
| 64 | + [TestCase("overloaded_method")] |
| 65 | + public void IntegralFloat_IsAccepted(string func) |
| 66 | + { |
| 67 | + Assert.AreEqual(5, Call(func, 5.0)); |
| 68 | + } |
| 69 | + |
| 70 | + // A non-integral float is rejected (no silent truncation) for every target. |
| 71 | + [TestCase("single_ctor")] |
| 72 | + [TestCase("single_method")] |
| 73 | + [TestCase("overloaded_ctor")] |
| 74 | + [TestCase("overloaded_method")] |
| 75 | + public void NonIntegralFloat_IsRejected(string func) |
| 76 | + { |
| 77 | + var ex = Assert.Throws<PythonException>(() => Call(func, 5.5)); |
| 78 | + Assert.AreEqual("TypeError", ex.Type.Name); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + public class IntTaker |
| 83 | + { |
| 84 | + public int Value { get; } |
| 85 | + |
| 86 | + public IntTaker(int value) |
| 87 | + { |
| 88 | + Value = value; |
| 89 | + } |
| 90 | + |
| 91 | + public int Echo(int value) => value; |
| 92 | + } |
| 93 | + |
| 94 | + /// <summary> |
| 95 | + /// Mimics Lean's RangeConsolidator: two overloads that both take an int first |
| 96 | + /// parameter, differing only in the (defaulted) later parameters. This forces the |
| 97 | + /// binder through its overload-disambiguation path. |
| 98 | + /// </summary> |
| 99 | + public class OverloadedIntTaker |
| 100 | + { |
| 101 | + public int Value { get; } |
| 102 | + |
| 103 | + public OverloadedIntTaker(int range, System.Func<int, int> selector = null) |
| 104 | + { |
| 105 | + Value = range; |
| 106 | + } |
| 107 | + |
| 108 | + public OverloadedIntTaker(int range, PyObject selector, PyObject volumeSelector = null) |
| 109 | + { |
| 110 | + Value = range; |
| 111 | + } |
| 112 | + |
| 113 | + public int Echo(int value, System.Func<int, int> selector = null) => value; |
| 114 | + |
| 115 | + public int Echo(int value, PyObject selector, PyObject other = null) => value; |
| 116 | + } |
| 117 | +} |
0 commit comments