Skip to content

Passing an arbitrary .NET object as the value of an attribute of PyObject #373

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

Merged
merged 1 commit into from
Feb 17, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions AUTHORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
- Sean Freitag ([@cowboygneox](https://github.com/cowboygneox))
- Serge Weinstock ([@sweinst](https://github.com/sweinst))
- Virgil Dupras ([@hsoft](https://github.com/hsoft))
- Wenguang Yang ([@yagweb](https://github.com/yagweb))
- Xavier Dupré ([@sdpython](https://github.com/sdpython))
- Zane Purvis ([@zanedp](https://github.com/zanedp))
- ([@ArvidJB](https://github.com/ArvidJB))
Expand Down
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ This document follows the conventions laid out in [Keep a CHANGELOG][].
- Added Embedded tests on Appveyor (#353)
- Added PY3 settings to configuration-manager (#346)
- Added `Slack` chat (#384)(#383)(#386)
- Added function of passing an arbitrary .NET object as the value
of an attribute of PyObject (#370)(#373)

### Changed

Expand Down
2 changes: 2 additions & 0 deletions src/embed_tests/Python.EmbeddingTest.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
<DebugType>pdbonly</DebugType>
</PropertyGroup>
<ItemGroup>
<Reference Include="Microsoft.CSharp" />
<Reference Include="nunit.framework, Version=3.6.0.0, Culture=neutral, PublicKeyToken=2638cd05610744eb, processorArchitecture=MSIL">
<HintPath>..\..\packages\NUnit.3.6.0\lib\net40\nunit.framework.dll</HintPath>
</Reference>
Expand All @@ -76,6 +77,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="pyinitialize.cs" />
<Compile Include="dynamic.cs" />
<Compile Include="pyimport.cs" />
<Compile Include="pyiter.cs" />
<Compile Include="pylong.cs" />
Expand Down
122 changes: 122 additions & 0 deletions src/embed_tests/dynamic.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
using System;
using System.Text;
using NUnit.Framework;
using Python.Runtime;

namespace Python.EmbeddingTest
{
[TestFixture]
public class dynamicTest
{
private Py.GILState gil;

[SetUp]
public void SetUp()
{
gil = Py.GIL();
}

[TearDown]
public void TearDown()
{
gil.Dispose();
}

/// <summary>
/// Set the attribute of a pyobject with a .NET object.
/// </summary>
[Test]
public void AssignObject()
{
StringBuilder stream = new StringBuilder();
dynamic sys = Py.Import("sys");
sys.testattr = stream;
// Check whether there are the same object.
var _stream = sys.testattr.AsManagedObject(typeof(StringBuilder));
Assert.AreEqual(_stream, stream);

PythonEngine.RunSimpleString(
"import sys\n" +
"sys.testattr.Append('Hello!')\n");
Assert.AreEqual(stream.ToString(), "Hello!");
}

/// <summary>
/// Set the attribute of a pyobject to null.
/// </summary>
[Test]
public void AssignNone()
{
dynamic sys = Py.Import("sys");
sys.testattr = new StringBuilder();
Assert.IsNotNull(sys.testattr);

sys.testattr = null;
Assert.IsNull(sys.testattr);
}

/// <summary>
/// Check whether we can get the attr of a python object when the
/// value of attr is a PyObject.
/// </summary>
[Test]
public void AssignPyObject()
{
dynamic sys = Py.Import("sys");
dynamic io = Py.Import("io");
sys.testattr = io.StringIO();
dynamic bb = sys.testattr; //Get the PyObject
bb.write("Hello!");
Assert.AreEqual(bb.getvalue().ToString(), "Hello!");
}

/// <summary>
/// Pass the .NET object in Python side.
/// </summary>
[Test]
public void PassObjectInPython()
{
StringBuilder stream = new StringBuilder();
dynamic sys = Py.Import("sys");
sys.testattr1 = stream;

//Pass the .NET object in Python side
PythonEngine.RunSimpleString(
"import sys\n" +
"sys.testattr2 = sys.testattr1\n"
);

//Compare in Python
PythonEngine.RunSimpleString(
"import sys\n" +
"sys.testattr3 = sys.testattr1 is sys.testattr2\n"
);
Assert.AreEqual(sys.testattr3.ToString(), "True");

//Compare in .NET
Assert.AreEqual(sys.testattr1, sys.testattr2);
}

/// <summary>
/// Pass the PyObject in .NET side
/// </summary>
[Test]
public void PassPyObjectInNet()
{
StringBuilder stream = new StringBuilder();
dynamic sys = Py.Import("sys");
sys.testattr1 = stream;
sys.testattr2 = sys.testattr1;

//Compare in Python
PyObject res = PythonEngine.RunString(
"import sys\n" +
"sys.testattr3 = sys.testattr1 is sys.testattr2\n"
);
Assert.AreEqual(sys.testattr3.ToString(), "True");

//Compare in .NET
Assert.AreEqual(sys.testattr1, sys.testattr2);
}
}
}
32 changes: 17 additions & 15 deletions src/runtime/pyobject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -887,30 +887,32 @@ public override int GetHashCode()
return Runtime.PyObject_Hash(obj).ToInt32();
}

public override bool TryGetMember(GetMemberBinder binder, out object result)

public long Refcount
{
if (this.HasAttr(binder.Name))
{
result = CheckNone(this.GetAttr(binder.Name));
return true;
}
else
get
{
return base.TryGetMember(binder, out result);
return Runtime.Refcount(obj);
}
}


public override bool TryGetMember(GetMemberBinder binder, out object result)
{
result = CheckNone(this.GetAttr(binder.Name));
return true;
}

public override bool TrySetMember(SetMemberBinder binder, object value)
{
if (this.HasAttr(binder.Name))
{
this.SetAttr(binder.Name, (PyObject)value);
return true;
}
else
IntPtr ptr = Converter.ToPython(value, value?.GetType());
int r = Runtime.PyObject_SetAttrString(obj, binder.Name, ptr);
if (r < 0)
{
return base.TrySetMember(binder, value);
throw new PythonException();
}
Runtime.XDecref(ptr);
return true;
}

private void GetArgs(object[] inargs, out PyTuple args, out PyDict kwargs)
Expand Down