FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
pythonnet/src/embed_tests/TestConverter.cs at fix-enum-codec · QSLee-Net/pythonnet · GitHub
QSLee-Net
pythonnet
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
pythonnet
/
src
/
embed_tests
/
TestConverter.cs
Copy path
More file actions
More file actions
Latest commit
History
History
History
192 lines (169 loc) · 5.88 KB
Breadcrumbs
pythonnet
/
src
/
embed_tests
/
TestConverter.cs
Copy path
File metadata and controls
192 lines (169 loc) · 5.88 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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
using
System
;
using
System
.
Collections
.
Generic
;
using
System
.
Linq
;
using
NUnit
.
Framework
;
using
Python
.
Runtime
;
using
PyRuntime
=
Python
.
Runtime
.
Runtime
;
namespace
Python
.
EmbeddingTest
{
public
class
TestConverter
{
static
readonly
Type
[
]
_numTypes
=
new
Type
[
]
{
typeof
(
short
)
,
typeof
(
ushort
)
,
typeof
(
int
)
,
typeof
(
uint
)
,
typeof
(
long
)
,
typeof
(
ulong
)
}
;
[
OneTimeSetUp
]
public
void
SetUp
(
)
{
PythonEngine
.
Initialize
(
)
;
}
[
OneTimeTearDown
]
public
void
Dispose
(
)
{
PythonEngine
.
Shutdown
(
)
;
}
[
Test
]
public
void
TestConvertSingleToManaged
(
[
Values
(
float
.
PositiveInfinity
,
float
.
NegativeInfinity
,
float
.
MinValue
,
float
.
MaxValue
,
float
.
NaN
,
float
.
Epsilon
)
]
float
testValue
)
{
var
pyFloat
=
new
PyFloat
(
testValue
)
;
object
convertedValue
;
var
converted
=
Converter
.
ToManaged
(
pyFloat
.
Handle
,
typeof
(
float
)
,
out
convertedValue
,
false
)
;
Assert
.
IsTrue
(
converted
)
;
Assert
.
IsTrue
(
(
(
float
)
convertedValue
)
.
Equals
(
testValue
)
)
;
}
[
Test
]
public
void
TestConvertDoubleToManaged
(
[
Values
(
double
.
PositiveInfinity
,
double
.
NegativeInfinity
,
double
.
MinValue
,
double
.
MaxValue
,
double
.
NaN
,
double
.
Epsilon
)
]
double
testValue
)
{
var
pyFloat
=
new
PyFloat
(
testValue
)
;
object
convertedValue
;
var
converted
=
Converter
.
ToManaged
(
pyFloat
.
Handle
,
typeof
(
double
)
,
out
convertedValue
,
false
)
;
Assert
.
IsTrue
(
converted
)
;
Assert
.
IsTrue
(
(
(
double
)
convertedValue
)
.
Equals
(
testValue
)
)
;
}
[
Test
]
public
void
CovertTypeError
(
)
{
Type
[
]
floatTypes
=
new
Type
[
]
{
typeof
(
float
)
,
typeof
(
double
)
}
;
using
(
var
s
=
new
PyString
(
"abc"
)
)
{
foreach
(
var
type
in
_numTypes
.
Union
(
floatTypes
)
)
{
object
value
;
try
{
bool
res
=
Converter
.
ToManaged
(
s
.
Handle
,
type
,
out
value
,
true
)
;
Assert
.
IsFalse
(
res
)
;
var
bo
=
Exceptions
.
ExceptionMatches
(
Exceptions
.
TypeError
)
;
Assert
.
IsTrue
(
Exceptions
.
ExceptionMatches
(
Exceptions
.
TypeError
)
||
Exceptions
.
ExceptionMatches
(
Exceptions
.
ValueError
)
)
;
}
finally
{
Exceptions
.
Clear
(
)
;
}
}
}
}
[
Test
]
public
void
ConvertOverflow
(
)
{
using
(
var
num
=
new
PyInt
(
ulong
.
MaxValue
)
)
{
IntPtr
largeNum
=
PyRuntime
.
PyNumber_Add
(
num
.
Handle
,
num
.
Handle
)
;
try
{
object
value
;
foreach
(
var
type
in
_numTypes
)
{
bool
res
=
Converter
.
ToManaged
(
largeNum
,
type
,
out
value
,
true
)
;
Assert
.
IsFalse
(
res
)
;
Assert
.
IsTrue
(
Exceptions
.
ExceptionMatches
(
Exceptions
.
OverflowError
)
)
;
Exceptions
.
Clear
(
)
;
}
}
finally
{
Exceptions
.
Clear
(
)
;
PyRuntime
.
XDecref
(
largeNum
)
;
}
}
}
[
Test
]
public
void
NoImplicitConversionToBool
(
)
{
var
pyObj
=
new
PyList
(
items
:
new
[
]
{
1
.
ToPython
(
)
,
2
.
ToPython
(
)
}
)
.
ToPython
(
)
;
Assert
.
Throws
<
InvalidCastException
>
(
(
)
=>
pyObj
.
As
<
bool
>
(
)
)
;
}
[
Test
]
public
void
ToNullable
(
)
{
const
int
Const
=
42
;
var
i
=
new
PyInt
(
Const
)
;
var
ni
=
i
.
As
<
int
?
>
(
)
;
Assert
.
AreEqual
(
Const
,
ni
)
;
}
[
Test
]
public
void
ToPyList
(
)
{
var
list
=
new
PyList
(
)
;
list
.
Append
(
"hello"
.
ToPython
(
)
)
;
list
.
Append
(
"world"
.
ToPython
(
)
)
;
var
back
=
list
.
ToPython
(
)
.
As
<
PyList
>
(
)
;
Assert
.
AreEqual
(
list
.
Length
(
)
,
back
.
Length
(
)
)
;
}
[
Test
]
public
void
RawListProxy
(
)
{
var
list
=
new
List
<
string
>
{
"hello"
,
"world"
}
;
var
listProxy
=
PyObject
.
FromManagedObject
(
list
)
;
var
clrObject
=
(
CLRObject
)
ManagedType
.
GetManagedObject
(
listProxy
.
Handle
)
;
Assert
.
AreSame
(
list
,
clrObject
.
inst
)
;
}
[
Test
]
public
void
RawPyObjectProxy
(
)
{
var
pyObject
=
"hello world!"
.
ToPython
(
)
;
var
pyObjectProxy
=
PyObject
.
FromManagedObject
(
pyObject
)
;
var
clrObject
=
(
CLRObject
)
ManagedType
.
GetManagedObject
(
pyObjectProxy
.
Handle
)
;
Assert
.
AreSame
(
pyObject
,
clrObject
.
inst
)
;
var
proxiedHandle
=
pyObjectProxy
.
GetAttr
(
"Handle"
)
.
As
<
IntPtr
>
(
)
;
Assert
.
AreEqual
(
pyObject
.
Handle
,
proxiedHandle
)
;
}
// regression for https://github.com/pythonnet/pythonnet/issues/451
[
Test
]
public
void
CanGetListFromDerivedClass
(
)
{
using
var
scope
=
Py
.
CreateScope
(
)
;
scope
.
Import
(
typeof
(
GetListImpl
)
.
Namespace
,
asname
:
"test"
)
;
scope
.
Exec
(
@"
class PyGetListImpl(test.GetListImpl):
pass
"
)
;
var
pyImpl
=
scope
.
Get
(
"PyGetListImpl"
)
;
dynamic
inst
=
pyImpl
.
Invoke
(
)
;
List
<
string
>
result
=
inst
.
GetList
(
)
;
CollectionAssert
.
AreEqual
(
new
[
]
{
"testing"
}
,
result
)
;
}
}
public
interface
IGetList
{
List
<
string
>
GetList
(
)
;
}
public
class
GetListImpl
:
IGetList
{
public
List
<
string
>
GetList
(
)
=>
new
(
)
{
"testing"
}
;
}
}
Back
|
FazBrowse Home
|
New Git URL