Skip to content

Commit 3e1fc2e

Browse files
authored
Merge pull request #1175 from amos402/remove-redundant-calls
* Remove unnecessary `CheckExceptionOccurred` calls * Dispose safety * Refactor Converter.ToPrimitive * str to int raises `ValueError` * Test convert error for float types * Convert error for UInt32 * * Ignore package index * Install pycparser on Windows ci * BorrowedReference instead of `in NewReference` * Remove useless pip argument
2 parents 2d2b297 + 8ca0a62 commit 3e1fc2e

File tree

13 files changed

+305
-287
lines changed

13 files changed

+305
-287
lines changed

src/embed_tests/TestConverter.cs

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,27 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Linq;
4+
35
using NUnit.Framework;
6+
47
using Python.Runtime;
58

9+
using PyRuntime = Python.Runtime.Runtime;
10+
611
namespace Python.EmbeddingTest
712
{
813
public class TestConverter
914
{
15+
static readonly Type[] _numTypes = new Type[]
16+
{
17+
typeof(short),
18+
typeof(ushort),
19+
typeof(int),
20+
typeof(uint),
21+
typeof(long),
22+
typeof(ulong)
23+
};
24+
1025
[OneTimeSetUp]
1126
public void SetUp()
1227
{
@@ -47,6 +62,60 @@ public void TestConvertDoubleToManaged(
4762
Assert.IsTrue(((double) convertedValue).Equals(testValue));
4863
}
4964

65+
[Test]
66+
public void CovertTypeError()
67+
{
68+
Type[] floatTypes = new Type[]
69+
{
70+
typeof(float),
71+
typeof(double)
72+
};
73+
using (var s = new PyString("abc"))
74+
{
75+
foreach (var type in _numTypes.Union(floatTypes))
76+
{
77+
object value;
78+
try
79+
{
80+
bool res = Converter.ToManaged(s.Handle, type, out value, true);
81+
Assert.IsFalse(res);
82+
var bo = Exceptions.ExceptionMatches(Exceptions.TypeError);
83+
Assert.IsTrue(Exceptions.ExceptionMatches(Exceptions.TypeError)
84+
|| Exceptions.ExceptionMatches(Exceptions.ValueError));
85+
}
86+
finally
87+
{
88+
Exceptions.Clear();
89+
}
90+
}
91+
}
92+
}
93+
94+
[Test]
95+
public void ConvertOverflow()
96+
{
97+
using (var num = new PyLong(ulong.MaxValue))
98+
{
99+
IntPtr largeNum = PyRuntime.PyNumber_Add(num.Handle, num.Handle);
100+
try
101+
{
102+
object value;
103+
foreach (var type in _numTypes)
104+
{
105+
bool res = Converter.ToManaged(largeNum, type, out value, true);
106+
Assert.IsFalse(res);
107+
Assert.IsTrue(Exceptions.ExceptionMatches(Exceptions.OverflowError));
108+
Exceptions.Clear();
109+
}
110+
}
111+
finally
112+
{
113+
Exceptions.Clear();
114+
PyRuntime.XDecref(largeNum);
115+
}
116+
}
117+
}
118+
50119
[Test]
51120
public void RawListProxy()
52121
{

src/runtime/NewReference.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,18 @@ public PyObject MoveToPyObject()
2727
this.pointer = IntPtr.Zero;
2828
return result;
2929
}
30+
3031
/// <summary>
3132
/// Removes this reference to a Python object, and sets it to <c>null</c>.
3233
/// </summary>
3334
public void Dispose()
3435
{
35-
if (!this.IsNull())
36-
Runtime.XDecref(this.pointer);
37-
this.pointer = IntPtr.Zero;
36+
if (this.IsNull())
37+
{
38+
return;
39+
}
40+
Runtime.XDecref(pointer);
41+
pointer = IntPtr.Zero;
3842
}
3943

4044
/// <summary>

0 commit comments

Comments
 (0)