FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Implementing GetDynamicMemberNames() for PyObject by jbw3 · Pull Request #690 · pythonnet/pythonnet · GitHub

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .cs  (2) .csproj  (1) .md  (2) All 3 file types selected
Only manifest files
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
1 change: 1 addition & 0 deletions AUTHORS.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
-   Jeff Reback ([@jreback](https://github.com/jreback))
- Joe Frayne ([@jfrayne](https://github.com/jfrayne))
- John Burnett ([@johnburnett](https://github.com/johnburnett))
- John Wilkes ([@jbw3](https://github.com/jbw3))
- Luke Stratman ([@lstratman](https://github.com/lstratman))
- Konstantin Posudevskiy ([@konstantin-posudevskiy](https://github.com/konstantin-posudevskiy))
- Matthias Dittrich ([@matthid](https://github.com/matthid))
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ This document follows the conventions laid out in [Keep a CHANGELOG][].
- Added `clr.GetClrType` (#432, #433)
- Allowed passing `None` for nullable args (#460)
- Added keyword arguments based on C# syntax for calling CPython methods (#461)
- Implemented GetDynamicMemberNames() for PyObject to allow dynamic object members to be visible in the debugger (#443)

### Changed

Expand Down
1 change: 1 addition & 0 deletions src/embed_tests/Python.EmbeddingTest.csproj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@
<Compile Include="TestPyList.cs" />
<Compile Include="TestPyLong.cs" />
<Compile Include="TestPyNumber.cs" />
<Compile Include="TestPyObject.cs" />
<Compile Include="TestPySequence.cs" />
<Compile Include="TestPyString.cs" />
<Compile Include="TestPythonException.cs" />
Expand Down
61 changes: 61 additions & 0 deletions src/embed_tests/TestPyObject.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
using System;
using System.Collections.Generic;
using System.Linq;
using NUnit.Framework;
using Python.Runtime;

namespace Python.EmbeddingTest
{
public class TestPyObject
{
[OneTimeSetUp]
public void SetUp()
{
PythonEngine.Initialize();
}

[OneTimeTearDown]
public void Dispose()
{
PythonEngine.Shutdown();
}

[Test]
public void TestGetDynamicMemberNames()
{
List<string> expectedMemberNames = new List<string>
{
"add",
"getNumber",
"member1",
"member2"
};

PyDict locals = new PyDict();

PythonEngine.Exec(@"
class MemberNamesTest(object):
def __init__(self):
self.member1 = 123
self.member2 = 'Test string'

def getNumber(self):
return 123

def add(self, x, y):
return x + y

a = MemberNamesTest()
", null, locals.Handle);

PyObject a = locals.GetItem("a");

IEnumerable<string> memberNames = a.GetDynamicMemberNames();

foreach (string expectedName in expectedMemberNames)
{
Assert.IsTrue(memberNames.Contains(expectedName), "Could not find member '{0}'.", expectedName);
}
}
}
}
16 changes: 16 additions & 0 deletions src/runtime/pyobject.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Dynamic;
using System.Linq.Expressions;

Expand Down Expand Up @@ -1238,5 +1239,20 @@ public override bool TryUnaryOperation(UnaryOperationBinder binder, out object r
result = CheckNone(new PyObject(res));
return true;
}

/// <summary>
/// Returns the enumeration of all dynamic member names.
/// </summary>
/// <remarks>
/// This method exists for debugging purposes only.
/// </remarks>
/// <returns>A sequence that contains dynamic member names.</returns>
public override IEnumerable<string> GetDynamicMemberNames()
{
foreach (PyObject pyObj in Dir())
{
yield return pyObj.ToString();
}
}
}
}

Back | FazBrowse Home | New Git URL