Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
respond to PR feedback
  • Loading branch information
koubaa committed Feb 18, 2021
commit bf2d03896079ebbc14bccbe93aedbbc42c077d77
24 changes: 1 addition & 23 deletions src/embed_tests/Codecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,34 +98,12 @@ static void TupleRoundtripGeneric<T, TTuple>()
}
}

static string GetIntIterableCommands(string instanceName)
{
var builder = new System.Text.StringBuilder();
builder.AppendLine(@"
class foo():
def __init__(self):
self.counter = 0
def __iter__(self):
return self
def __next__(self):
if self.counter == 3:
raise StopIteration
self.counter = self.counter + 1
return self.counter");

builder.AppendLine(instanceName + " = foo()");
return builder.ToString();
}

static PyObject GetPythonIterable()
{
var locals = new PyDict();
using (Py.GIL())
{
PythonEngine.Exec(GetIntIterableCommands("foo_instance"), null, locals.Handle);
return PythonEngine.Eval("map(lambda x: x, [1,2,3])");
}

return locals.GetItem("foo_instance");
}

[Test]
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/Codecs/IterableDecoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ internal static bool IsIterable(Type targetType)

internal static bool IsIterable(PyObject objectType)
{
return objectType.HasAttr("__iter__");
return Runtime.PyIter_Check(objectType.Handle);
}

public bool CanDecode(PyObject objectType, Type targetType)
Expand Down
5 changes: 3 additions & 2 deletions src/runtime/Codecs/ListDecoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@ private static bool IsList(Type targetType)

private static bool IsList(PyObject objectType)
{
//TODO accept any python object that implements the sequence and list protocols
//must implement sequence protocol to fully implement list protocol
if (!SequenceDecoder.IsSequence(objectType)) return false;
//if (!SequenceDecoder.IsSequence(objectType)) return false;

//returns wheter the type is a list.
//returns wheter the type is a list.
return objectType.Handle == Runtime.PyListType;
Comment thread
koubaa marked this conversation as resolved.
}

Expand Down
2 changes: 1 addition & 1 deletion src/runtime/CollectionWrappers/IterableWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public IterableWrapper(PyObject pyObj)
{
if (pyObj == null)
throw new ArgumentNullException();
pyObject = pyObj;
pyObject = new PyObject(pyObj.Reference);
}

IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/CollectionWrappers/SequenceWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public int Count
{
get
{
var size = Runtime.PySequence_Size(pyObject.Handle);
var size = Runtime.PySequence_Size(pyObject.Reference);
if (size == -1)
{
Runtime.CheckExceptionOccurred();
Expand Down