Skip to content

.NET class instances considered Iterable #1234

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
danabr opened this issue Sep 23, 2020 · 1 comment · Fixed by #1241
Closed

.NET class instances considered Iterable #1234

danabr opened this issue Sep 23, 2020 · 1 comment · Fixed by #1241

Comments

@danabr
Copy link
Contributor

danabr commented Sep 23, 2020

Environment

  • Pythonnet version: 2.5.0
  • Python version: 3.7
  • Operating System: Windows

Details

All .NET class instances are considered instances of collections.abc.Iterable.

namespace Python.Test {
  public class Empty { }
}
def test_not_Iterable():
    """Test regular .NET objects are not iterable"""
    from Python.Test import Empty
    from collections.abc import Iterable

    assert (not isinstance(Empty(), Iterable))

The test is expected to pass, but fails:

    def test_not_Iterable():
        """Test regular .NET objects are not iterable"""
        from Python.Test import Empty
        from collections.abc import Iterable

>       assert (not isinstance(Empty(), Iterable))
E       AssertionError: assert not True
E        +  where True = isinstance(<Python.Test.Empty object at 0x0000022D3E6DE5C8>, <class 'collections.abc.Iterable'>)
E        +    where <Python.Test.Empty object at 0x0000022D3E6DE5C8> = <class 'Python.Test.Empty'>()
@filmor
Copy link
Member

filmor commented Sep 24, 2020

This is because the tp_iter slot is always filled and just throws an error for non-IEnumerable types:

public static IntPtr tp_iter(IntPtr ob)
{
var co = GetManagedObject(ob) as CLRObject;
if (co == null)
{
return Exceptions.RaiseTypeError("invalid object");
}
var e = co.inst as IEnumerable;
IEnumerator o;
if (e != null)
{
o = e.GetEnumerator();
}
else
{
o = co.inst as IEnumerator;
if (o == null)
{
return Exceptions.RaiseTypeError("iteration over non-sequence");
}
}
return new Iterator(o).pyHandle;
}

We could do the respective check in the "constructor" of the type here:

/// <summary>
/// Given a newly allocated Python type object and a managed Type that
/// provides the implementation for the type, connect the type slots of
/// the Python object to the managed methods of the implementing Type.
/// </summary>
internal static void InitializeSlots(IntPtr type, Type impl)
{
// We work from the most-derived class up; make sure to get
// the most-derived slot and not to override it with a base
// class's slot.
var seen = new HashSet<string>();
while (impl != null)
{
MethodInfo[] methods = impl.GetMethods(tbFlags);
foreach (MethodInfo method in methods)
{
string name = method.Name;
if (!(name.StartsWith("tp_") ||
name.StartsWith("nb_") ||
name.StartsWith("sq_") ||
name.StartsWith("mp_") ||
name.StartsWith("bf_")
))
{
continue;
}
if (seen.Contains(name))
{
continue;
}
var thunkInfo = Interop.GetThunk(method);
InitializeSlot(type, thunkInfo.Address, name);
seen.Add(name);
}
impl = impl.BaseType;
}

danabr added a commit to danabr/pythonnet that referenced this issue Sep 26, 2020
danabr added a commit to danabr/pythonnet that referenced this issue Sep 28, 2020
lostmsu pushed a commit that referenced this issue Sep 29, 2020
… Iterable (#1241)

Fixes #1234.

Previously `tp_iter` slot was set for all reflected .NET types. This restricts it to types, that implement `IEnumerable` or `IEnumerator`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants