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

Commit1d583c7

Browse files
yagwebvmuriart
authored andcommitted
Pass arbitrary .NET object as value of an attr of PyObject by dyn type(#373)
1 parent94c266e commit1d583c7

File tree

5 files changed

+144
-15
lines changed

5 files changed

+144
-15
lines changed

‎AUTHORS.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
- Sean Freitag ([@cowboygneox](https://github.com/cowboygneox))
3030
- Serge Weinstock ([@sweinst](https://github.com/sweinst))
3131
- Virgil Dupras ([@hsoft](https://github.com/hsoft))
32+
- Wenguang Yang ([@yagweb](https://github.com/yagweb))
3233
- Xavier Dupré ([@sdpython](https://github.com/sdpython))
3334
- Zane Purvis ([@zanedp](https://github.com/zanedp))
3435
- ([@ArvidJB](https://github.com/ArvidJB))

‎CHANGELOG.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ This document follows the conventions laid out in [Keep a CHANGELOG][].
1515
- Added Embedded tests on Appveyor (#353)
1616
- Added PY3 settings to configuration-manager (#346)
1717
- Added`Slack` chat (#384)(#383)(#386)
18+
- Added function of passing an arbitrary .NET object as the value
19+
of an attribute of PyObject (#370)(#373)
1820

1921
###Changed
2022

‎src/embed_tests/Python.EmbeddingTest.csproj‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
<DebugType>pdbonly</DebugType>
6666
</PropertyGroup>
6767
<ItemGroup>
68+
<ReferenceInclude="Microsoft.CSharp" />
6869
<ReferenceInclude="nunit.framework, Version=3.6.0.0, Culture=neutral, PublicKeyToken=2638cd05610744eb, processorArchitecture=MSIL">
6970
<HintPath>..\..\packages\NUnit.3.6.0\lib\net40\nunit.framework.dll</HintPath>
7071
</Reference>
@@ -76,6 +77,7 @@
7677
</ItemGroup>
7778
<ItemGroup>
7879
<CompileInclude="pyinitialize.cs" />
80+
<CompileInclude="dynamic.cs" />
7981
<CompileInclude="pyimport.cs" />
8082
<CompileInclude="pyiter.cs" />
8183
<CompileInclude="pylong.cs" />

‎src/embed_tests/dynamic.cs‎

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
usingSystem;
2+
usingSystem.Text;
3+
usingNUnit.Framework;
4+
usingPython.Runtime;
5+
6+
namespacePython.EmbeddingTest
7+
{
8+
[TestFixture]
9+
publicclassdynamicTest
10+
{
11+
privatePy.GILStategil;
12+
13+
[SetUp]
14+
publicvoidSetUp()
15+
{
16+
gil=Py.GIL();
17+
}
18+
19+
[TearDown]
20+
publicvoidTearDown()
21+
{
22+
gil.Dispose();
23+
}
24+
25+
/// <summary>
26+
/// Set the attribute of a pyobject with a .NET object.
27+
/// </summary>
28+
[Test]
29+
publicvoidAssignObject()
30+
{
31+
StringBuilderstream=newStringBuilder();
32+
dynamicsys=Py.Import("sys");
33+
sys.testattr=stream;
34+
// Check whether there are the same object.
35+
var_stream=sys.testattr.AsManagedObject(typeof(StringBuilder));
36+
Assert.AreEqual(_stream,stream);
37+
38+
PythonEngine.RunSimpleString(
39+
"import sys\n"+
40+
"sys.testattr.Append('Hello!')\n");
41+
Assert.AreEqual(stream.ToString(),"Hello!");
42+
}
43+
44+
/// <summary>
45+
/// Set the attribute of a pyobject to null.
46+
/// </summary>
47+
[Test]
48+
publicvoidAssignNone()
49+
{
50+
dynamicsys=Py.Import("sys");
51+
sys.testattr=newStringBuilder();
52+
Assert.IsNotNull(sys.testattr);
53+
54+
sys.testattr=null;
55+
Assert.IsNull(sys.testattr);
56+
}
57+
58+
/// <summary>
59+
/// Check whether we can get the attr of a python object when the
60+
/// value of attr is a PyObject.
61+
/// </summary>
62+
[Test]
63+
publicvoidAssignPyObject()
64+
{
65+
dynamicsys=Py.Import("sys");
66+
dynamicio=Py.Import("io");
67+
sys.testattr=io.StringIO();
68+
dynamicbb=sys.testattr;//Get the PyObject
69+
bb.write("Hello!");
70+
Assert.AreEqual(bb.getvalue().ToString(),"Hello!");
71+
}
72+
73+
/// <summary>
74+
/// Pass the .NET object in Python side.
75+
/// </summary>
76+
[Test]
77+
publicvoidPassObjectInPython()
78+
{
79+
StringBuilderstream=newStringBuilder();
80+
dynamicsys=Py.Import("sys");
81+
sys.testattr1=stream;
82+
83+
//Pass the .NET object in Python side
84+
PythonEngine.RunSimpleString(
85+
"import sys\n"+
86+
"sys.testattr2 = sys.testattr1\n"
87+
);
88+
89+
//Compare in Python
90+
PythonEngine.RunSimpleString(
91+
"import sys\n"+
92+
"sys.testattr3 = sys.testattr1 is sys.testattr2\n"
93+
);
94+
Assert.AreEqual(sys.testattr3.ToString(),"True");
95+
96+
//Compare in .NET
97+
Assert.AreEqual(sys.testattr1,sys.testattr2);
98+
}
99+
100+
/// <summary>
101+
/// Pass the PyObject in .NET side
102+
/// </summary>
103+
[Test]
104+
publicvoidPassPyObjectInNet()
105+
{
106+
StringBuilderstream=newStringBuilder();
107+
dynamicsys=Py.Import("sys");
108+
sys.testattr1=stream;
109+
sys.testattr2=sys.testattr1;
110+
111+
//Compare in Python
112+
PyObjectres=PythonEngine.RunString(
113+
"import sys\n"+
114+
"sys.testattr3 = sys.testattr1 is sys.testattr2\n"
115+
);
116+
Assert.AreEqual(sys.testattr3.ToString(),"True");
117+
118+
//Compare in .NET
119+
Assert.AreEqual(sys.testattr1,sys.testattr2);
120+
}
121+
}
122+
}

‎src/runtime/pyobject.cs‎

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -887,30 +887,32 @@ public override int GetHashCode()
887887
returnRuntime.PyObject_Hash(obj).ToInt32();
888888
}
889889

890-
publicoverrideboolTryGetMember(GetMemberBinderbinder,outobjectresult)
890+
891+
publiclongRefcount
891892
{
892-
if(this.HasAttr(binder.Name))
893-
{
894-
result=CheckNone(this.GetAttr(binder.Name));
895-
returntrue;
896-
}
897-
else
893+
get
898894
{
899-
returnbase.TryGetMember(binder,outresult);
895+
returnRuntime.Refcount(obj);
900896
}
901897
}
902898

899+
900+
publicoverrideboolTryGetMember(GetMemberBinderbinder,outobjectresult)
901+
{
902+
result=CheckNone(this.GetAttr(binder.Name));
903+
returntrue;
904+
}
905+
903906
publicoverrideboolTrySetMember(SetMemberBinderbinder,objectvalue)
904907
{
905-
if(this.HasAttr(binder.Name))
906-
{
907-
this.SetAttr(binder.Name,(PyObject)value);
908-
returntrue;
909-
}
910-
else
908+
IntPtrptr=Converter.ToPython(value,value?.GetType());
909+
intr=Runtime.PyObject_SetAttrString(obj,binder.Name,ptr);
910+
if(r<0)
911911
{
912-
returnbase.TrySetMember(binder,value);
912+
thrownewPythonException();
913913
}
914+
Runtime.XDecref(ptr);
915+
returntrue;
914916
}
915917

916918
privatevoidGetArgs(object[]inargs,outPyTupleargs,outPyDictkwargs)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp