FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
pythonnet/src/embed_tests/dynamic.cs at net8.0 · pythonnet/pythonnet · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
pythonnet
/
pythonnet
Public
Notifications
You must be signed in to change notification settings
Fork
780
Star
5.5k
Code
Issues
153
Pull requests
12
Discussions
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Discussions
Actions
Projects
Wiki
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
163 lines (142 loc) · 4.81 KB
Breadcrumbs
pythonnet
/
src
/
embed_tests
/
dynamic.cs
Copy path
File metadata and controls
163 lines (142 loc) · 4.81 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
using
System
;
using
System
.
Collections
.
Generic
;
using
System
.
Text
;
using
NUnit
.
Framework
;
using
Python
.
Runtime
;
namespace
Python
.
EmbeddingTest
{
public
class
DynamicTest
{
[
OneTimeSetUp
]
public
void
SetUp
(
)
{
PythonEngine
.
Initialize
(
)
;
}
[
OneTimeTearDown
]
public
void
Dispose
(
)
{
PythonEngine
.
Shutdown
(
)
;
}
/// <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
)
)
;
}
// regression test for https://github.com/pythonnet/pythonnet/issues/1848
[
Test
]
public
void
EnumEquality
(
)
{
using
var
scope
=
Py
.
CreateScope
(
)
;
scope
.
Exec
(
@"
import enum
class MyEnum(enum.IntEnum):
OK = 1
ERROR = 2
def get_status():
return MyEnum.OK
"
)
;
dynamic
MyEnum
=
scope
.
Get
(
"MyEnum"
)
;
dynamic
status
=
scope
.
Get
(
"get_status"
)
.
Invoke
(
)
;
Assert
.
IsTrue
(
status
==
MyEnum
.
OK
)
;
}
// regression test for https://github.com/pythonnet/pythonnet/issues/1680
[
Test
]
public
void
ForEach
(
)
{
dynamic
pyList
=
PythonEngine
.
Eval
(
"[1,2,3]"
)
;
var
list
=
new
List
<
int
>
(
)
;
foreach
(
int
item
in
pyList
)
list
.
Add
(
item
)
;
}
}
}
Back
|
FazBrowse Home
|
New Git URL