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

Appearance settings
69 changes: 69 additions & 0 deletions 69 src/embed_tests/TestConverter.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,27 @@
using System;
using System.Collections.Generic;
using System.Linq;

using NUnit.Framework;

using Python.Runtime;

using PyRuntime = Python.Runtime.Runtime;

namespace Python.EmbeddingTest
{
public class TestConverter
{
static readonly Type[] _numTypes = new Type[]
{
typeof(short),
typeof(ushort),
typeof(int),
typeof(uint),
typeof(long),
typeof(ulong)
};

[OneTimeSetUp]
public void SetUp()
{
Expand Down Expand Up @@ -47,6 +62,60 @@ public void TestConvertDoubleToManaged(
Assert.IsTrue(((double) convertedValue).Equals(testValue));
}

[Test]
public void CovertTypeError()
{
Type[] floatTypes = new Type[]
{
typeof(float),
typeof(double)
};
using (var s = new PyString("abc"))
{
foreach (var type in _numTypes.Union(floatTypes))
{
object value;
try
{
bool res = Converter.ToManaged(s.Handle, type, out value, true);
Assert.IsFalse(res);
var bo = Exceptions.ExceptionMatches(Exceptions.TypeError);
Assert.IsTrue(Exceptions.ExceptionMatches(Exceptions.TypeError)
|| Exceptions.ExceptionMatches(Exceptions.ValueError));
}
finally
{
Exceptions.Clear();
}
}
}
}

[Test]
public void ConvertOverflow()
{
using (var num = new PyLong(ulong.MaxValue))
{
IntPtr largeNum = PyRuntime.PyNumber_Add(num.Handle, num.Handle);
try
{
object value;
foreach (var type in _numTypes)
{
bool res = Converter.ToManaged(largeNum, type, out value, true);
Assert.IsFalse(res);
Assert.IsTrue(Exceptions.ExceptionMatches(Exceptions.OverflowError));
Exceptions.Clear();
}
}
finally
{
Exceptions.Clear();
PyRuntime.XDecref(largeNum);
}
}
}

[Test]
public void RawListProxy()
{
Expand Down
10 changes: 7 additions & 3 deletions 10 src/runtime/NewReference.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,18 @@ public PyObject MoveToPyObject()
this.pointer = IntPtr.Zero;
return result;
}

/// <summary>
/// Removes this reference to a Python object, and sets it to <c>null</c>.
/// </summary>
public void Dispose()
{
if (!this.IsNull())
Runtime.XDecref(this.pointer);
this.pointer = IntPtr.Zero;
if (this.IsNull())
{
return;
}
Runtime.XDecref(pointer);
pointer = IntPtr.Zero;
}

/// <summary>
Expand Down
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.