FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
pythonnet/src/embed_tests/dynamic.cs at master · AdsTable/pythonnet · GitHub
AdsTable
pythonnet
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
pythonnet
/
src
/
embed_tests
/
dynamic.cs
Copy path
More file actions
More file actions
Latest commit
History
History
History
142 lines (125 loc) · 4.28 KB
Breadcrumbs
pythonnet
/
src
/
embed_tests
/
dynamic.cs
Copy path
File metadata and controls
142 lines (125 loc) · 4.28 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
using
System
;
using
System
.
Text
;
using
NUnit
.
Framework
;
using
Python
.
Runtime
;
namespace
Python
.
EmbeddingTest
{
public
class
DynamicTest
{
private
Py
.
GILState
_gs
;
[
SetUp
]
public
void
SetUp
(
)
{
try
{
_gs
=
Py
.
GIL
(
)
;
}
catch
(
Exception
e
)
{
Console
.
WriteLine
(
$
"exception in SetUp:
{
e
}
"
)
;
throw
;
}
}
[
TearDown
]
public
void
Dispose
(
)
{
try
{
_gs
.
Dispose
(
)
;
}
catch
(
Exception
e
)
{
Console
.
WriteLine
(
$
"exception in TearDown:
{
e
}
"
)
;
throw
;
}
}
/// <summary>
/// Set the attribute of a PyObject with a .NET object.
/// </summary>
[
Test
]
public
void
AssignObject
(
)
{
var
stream
=
new
StringBuilder
(
)
;
dynamic
sys
=
Py
.
Import
(
"sys"
)
;
sys
.
testattr
=
stream
;
// Check whether there are the same object.
dynamic
_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>
/// <remarks>
/// FIXME: Issue on Travis PY27: Error : Python.EmbeddingTest.dynamicTest.AssignPyObject
/// Python.Runtime.PythonException : ImportError : /home/travis/virtualenv/python2.7.9/lib/python2.7/lib-dynload/_io.so: undefined symbol: _PyLong_AsInt
/// </remarks>
[
Test
]
public
void
AssignPyObject
(
)
{
if
(
Environment
.
GetEnvironmentVariable
(
"TRAVIS"
)
==
"true"
&&
Environment
.
GetEnvironmentVariable
(
"TRAVIS_PYTHON_VERSION"
)
==
"2.7"
)
{
Assert
.
Ignore
(
"Fails on Travis/PY27: ImportError: ... undefined symbol: _PyLong_AsInt"
)
;
}
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
(
)
{
var
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
.
IsTrue
(
sys
.
testattr1
.
Equals
(
sys
.
testattr2
)
)
;
}
/// <summary>
/// Pass the PyObject in .NET side
/// </summary>
[
Test
]
public
void
PassPyObjectInNet
(
)
{
var
stream
=
new
StringBuilder
(
)
;
dynamic
sys
=
Py
.
Import
(
"sys"
)
;
sys
.
testattr1
=
stream
;
sys
.
testattr2
=
sys
.
testattr1
;
// 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
.
IsTrue
(
sys
.
testattr1
.
Equals
(
sys
.
testattr2
)
)
;
}
}
}
Back
|
FazBrowse Home
|
New Git URL