Movatterモバイル変換


[0]ホーム

URL:


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

Sign up
Appearance settings

Commitad0d4e7

Browse files
committed
allow dynamic PyObject conversion to IEnumerable when the object is a Python iterable
this enables foreach loops over dynamic PyObject instancescloses#1680
1 parentdd3302a commitad0d4e7

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

‎src/embed_tests/dynamic.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
usingSystem;
2+
usingSystem.Collections.Generic;
23
usingSystem.Text;
34
usingNUnit.Framework;
45
usingPython.Runtime;
@@ -126,5 +127,15 @@ public void PassPyObjectInNet()
126127
// Compare in .NET
127128
Assert.IsTrue(sys.testattr1.Equals(sys.testattr2));
128129
}
130+
131+
// regression test for https://github.com/pythonnet/pythonnet/issues/1680
132+
[Test]
133+
publicvoidForEach()
134+
{
135+
dynamicpyList=PythonEngine.Eval("[1,2,3]");
136+
varlist=newList<int>();
137+
foreach(intiteminpyList)
138+
list.Add(item);
139+
}
129140
}
130141
}

‎src/runtime/PythonTypes/PyObject.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1296,6 +1296,12 @@ public override bool TryConvert(ConvertBinder binder, out object? result)
12961296
returnconverted;
12971297
}
12981298

1299+
if(binder.Type==typeof(System.Collections.IEnumerable)&&this.IsIterable())
1300+
{
1301+
result=newPyIterable(this.Reference);
1302+
returntrue;
1303+
}
1304+
12991305
returnfalse;
13001306
}
13011307

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp