Skip to content

Improve method binding #974

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 11 commits into from
Prev Previous commit
Next Next commit
share common code between ToArray and ToList
  • Loading branch information
koubaa committed Dec 2, 2019
commit a4599d922bf0742f35e76f720ba0fefcdadf86af
88 changes: 34 additions & 54 deletions src/runtime/converter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -864,44 +864,28 @@ private static void SetConversionError(IntPtr value, Type target)
Exceptions.SetError(Exceptions.TypeError, $"Cannot convert {src} to {target}");
}


/// <summary>
/// Convert a Python value to a correctly typed managed array instance.
/// The Python value must support the Python iterator protocol or and the
/// items in the sequence must be convertible to the target array type.
/// </summary>
private static bool ToArray(IntPtr value, Type obType, out object result, bool setError)
private static bool ToListOfT(IntPtr value, Type elementType, out object result)
{
Type elementType = obType.GetElementType();
result = null;

bool IsSeqObj = Runtime.PySequence_Check(value);
var len = IsSeqObj ? Runtime.PySequence_Size(value) : -1;

IntPtr IterObject = Runtime.PyObject_GetIter(value);

if(IterObject==IntPtr.Zero) {
if (setError)
{
SetConversionError(value, obType);
}
if (IterObject == IntPtr.Zero)
return false;
}

Array items;

var listType = typeof(List<>);
var constructedListType = listType.MakeGenericType(elementType);
IList list = IsSeqObj ? (IList) Activator.CreateInstance(constructedListType, new Object[] {(int) len}) :
(IList) Activator.CreateInstance(constructedListType);
IList list = IsSeqObj ? (IList)Activator.CreateInstance(constructedListType, new Object[] { (int)len }) :
(IList)Activator.CreateInstance(constructedListType);
IntPtr item;

while ((item = Runtime.PyIter_Next(IterObject)) != IntPtr.Zero)
{
while ((item = Runtime.PyIter_Next(IterObject)) != IntPtr.Zero) {
object obj = null;

if (!Converter.ToManaged(item, elementType, out obj, true))
{
if (!Converter.ToManaged(item, elementType, out obj, true)) {
Runtime.XDecref(item);
return false;
}
Expand All @@ -910,12 +894,34 @@ private static bool ToArray(IntPtr value, Type obType, out object result, bool s
Runtime.XDecref(item);
}
Runtime.XDecref(IterObject);
result = list;
return true;
}

/// <summary>
/// Convert a Python value to a correctly typed managed array instance.
/// The Python value must support the Python iterator protocol or and the
/// items in the sequence must be convertible to the target array type.
/// </summary>
private static bool ToArray(IntPtr value, Type obType, out object result, bool setError)
{
Type elementType = obType.GetElementType();
result = null;
bool success = ToListOfT(value, elementType, out result);
if (!success)
{
if (setError)
SetConversionError(value, obType);
return false;
}

items = Array.CreateInstance(elementType, list.Count);
IList list = (IList)result;
Array items = Array.CreateInstance(elementType, list.Count);
list.CopyTo(items, 0);

result = items;
return true;

}

/// <summary>
Expand Down Expand Up @@ -963,39 +969,13 @@ private static bool ToAction(IntPtr value, Type obType, out object result, bool
/// </summary>
private static bool ToList(IntPtr value, Type obType, out object result, bool setError) {
Type elementType = obType.GetGenericArguments()[0];
result = null;

bool IsSeqObj = Runtime.PySequence_Check(value);
var len = IsSeqObj ? Runtime.PySequence_Size(value) : -1;

IntPtr IterObject = Runtime.PyObject_GetIter(value);

if (IterObject == IntPtr.Zero) {
if (setError) {
var success = ToListOfT(value, elementType, out result);
if (!success)
{
if (setError)
SetConversionError(value, obType);
}
return false;
}

var listType = typeof(List<>);
var constructedListType = listType.MakeGenericType(elementType);
IList list = IsSeqObj ? (IList)Activator.CreateInstance(constructedListType, new Object[] { (int)len }) :
(IList)Activator.CreateInstance(constructedListType);
IntPtr item;

while ((item = Runtime.PyIter_Next(IterObject)) != IntPtr.Zero) {
object obj = null;

if (!Converter.ToManaged(item, elementType, out obj, true)) {
Runtime.XDecref(item);
return false;
}

list.Add(obj);
Runtime.XDecref(item);
}
Runtime.XDecref(IterObject);
result = list;
return true;
}

Expand Down