Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 545c549

Browse files
jbw3den-run-ai
authored andcommitted
Implementing GetDynamicMemberNames() for PyObject (pythonnet#690)
* Implementing GetDynamicMemberNames() for PyObject * Added unit test * Addressing code review comments
1 parent f60dc32 commit 545c549

File tree

5 files changed

+80
-0
lines changed

5 files changed

+80
-0
lines changed

AUTHORS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
-   Jeff Reback ([@jreback](https://github.com/jreback))
2929
- Joe Frayne ([@jfrayne](https://github.com/jfrayne))
3030
- John Burnett ([@johnburnett](https://github.com/johnburnett))
31+
- John Wilkes ([@jbw3](https://github.com/jbw3))
3132
- Luke Stratman ([@lstratman](https://github.com/lstratman))
3233
- Konstantin Posudevskiy ([@konstantin-posudevskiy](https://github.com/konstantin-posudevskiy))
3334
- Matthias Dittrich ([@matthid](https://github.com/matthid))

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ This document follows the conventions laid out in [Keep a CHANGELOG][].
1616
- Added `clr.GetClrType` (#432, #433)
1717
- Allowed passing `None` for nullable args (#460)
1818
- Added keyword arguments based on C# syntax for calling CPython methods (#461)
19+
- Implemented GetDynamicMemberNames() for PyObject to allow dynamic object members to be visible in the debugger (#443)
1920

2021
### Changed
2122

src/embed_tests/Python.EmbeddingTest.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@
9393
<Compile Include="TestPyList.cs" />
9494
<Compile Include="TestPyLong.cs" />
9595
<Compile Include="TestPyNumber.cs" />
96+
<Compile Include="TestPyObject.cs" />
9697
<Compile Include="TestPySequence.cs" />
9798
<Compile Include="TestPyString.cs" />
9899
<Compile Include="TestPythonException.cs" />

src/embed_tests/TestPyObject.cs

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using NUnit.Framework;
5+
using Python.Runtime;
6+
7+
namespace Python.EmbeddingTest
8+
{
9+
public class TestPyObject
10+
{
11+
[OneTimeSetUp]
12+
public void SetUp()
13+
{
14+
PythonEngine.Initialize();
15+
}
16+
17+
[OneTimeTearDown]
18+
public void Dispose()
19+
{
20+
PythonEngine.Shutdown();
21+
}
22+
23+
[Test]
24+
public void TestGetDynamicMemberNames()
25+
{
26+
List<string> expectedMemberNames = new List<string>
27+
{
28+
"add",
29+
"getNumber",
30+
"member1",
31+
"member2"
32+
};
33+
34+
PyDict locals = new PyDict();
35+
36+
PythonEngine.Exec(@"
37+
class MemberNamesTest(object):
38+
def __init__(self):
39+
self.member1 = 123
40+
self.member2 = 'Test string'
41+
42+
def getNumber(self):
43+
return 123
44+
45+
def add(self, x, y):
46+
return x + y
47+
48+
a = MemberNamesTest()
49+
", null, locals.Handle);
50+
51+
PyObject a = locals.GetItem("a");
52+
53+
IEnumerable<string> memberNames = a.GetDynamicMemberNames();
54+
55+
foreach (string expectedName in expectedMemberNames)
56+
{
57+
Assert.IsTrue(memberNames.Contains(expectedName), "Could not find member '{0}'.", expectedName);
58+
}
59+
}
60+
}
61+
}

src/runtime/pyobject.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections;
3+
using System.Collections.Generic;
34
using System.Dynamic;
45
using System.Linq.Expressions;
56

@@ -1238,5 +1239,20 @@ public override bool TryUnaryOperation(UnaryOperationBinder binder, out object r
12381239
result = CheckNone(new PyObject(res));
12391240
return true;
12401241
}
1242+
1243+
/// <summary>
1244+
/// Returns the enumeration of all dynamic member names.
1245+
/// </summary>
1246+
/// <remarks>
1247+
/// This method exists for debugging purposes only.
1248+
/// </remarks>
1249+
/// <returns>A sequence that contains dynamic member names.</returns>
1250+
public override IEnumerable<string> GetDynamicMemberNames()
1251+
{
1252+
foreach (PyObject pyObj in Dir())
1253+
{
1254+
yield return pyObj.ToString();
1255+
}
1256+
}
12411257
}
12421258
}

0 commit comments

Comments
 (0)