FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
pythonnet/src/embed_tests/TestConverter.cs at master · pythonnet/pythonnet · GitHub
pythonnet
pythonnet
Repository navigation
Code
Issues
147
(147)
Pull requests
11
(11)
Discussions
Actions
Projects
Wiki
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
211 lines (187 loc) · 6.74 KB
Breadcrumbs
pythonnet
/
src
/
embed_tests
/
TestConverter.cs
Copy path
File metadata and controls
211 lines (187 loc) · 6.74 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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
using
System
;
using
System
.
Collections
.
Generic
;
using
System
.
Linq
;
using
System
.
Numerics
;
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
)
}
;
[
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
,
typeof
(
float
)
,
out
convertedValue
,
false
)
;
Assert
.
That
(
converted
,
Is
.
True
)
;
Assert
.
That
(
(
(
float
)
convertedValue
)
.
Equals
(
testValue
)
,
Is
.
True
)
;
}
[
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
,
typeof
(
double
)
,
out
convertedValue
,
false
)
;
Assert
.
That
(
converted
,
Is
.
True
)
;
Assert
.
That
(
(
(
double
)
convertedValue
)
.
Equals
(
testValue
)
,
Is
.
True
)
;
}
[
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
,
type
,
out
value
,
true
)
;
Assert
.
That
(
res
,
Is
.
False
)
;
var
bo
=
Exceptions
.
ExceptionMatches
(
Exceptions
.
TypeError
)
;
Assert
.
That
(
Exceptions
.
ExceptionMatches
(
Exceptions
.
TypeError
)
||
Exceptions
.
ExceptionMatches
(
Exceptions
.
ValueError
)
,
Is
.
True
)
;
}
finally
{
Exceptions
.
Clear
(
)
;
}
}
}
}
[
Test
]
public
void
ConvertOverflow
(
)
{
using
(
var
num
=
new
PyInt
(
ulong
.
MaxValue
)
)
{
using
var
largeNum
=
PyRuntime
.
PyNumber_Add
(
num
,
num
)
;
try
{
object
value
;
foreach
(
var
type
in
_numTypes
)
{
bool
res
=
Converter
.
ToManaged
(
largeNum
.
BorrowOrThrow
(
)
,
type
,
out
value
,
true
)
;
Assert
.
That
(
res
,
Is
.
False
)
;
Assert
.
That
(
Exceptions
.
ExceptionMatches
(
Exceptions
.
OverflowError
)
,
Is
.
True
)
;
Exceptions
.
Clear
(
)
;
}
}
finally
{
Exceptions
.
Clear
(
)
;
}
}
}
[
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
.
That
(
ni
,
Is
.
EqualTo
(
Const
)
)
;
}
[
Test
]
public
void
BigIntExplicit
(
)
{
BigInteger
val
=
42
;
var
i
=
new
PyInt
(
val
)
;
var
ni
=
i
.
As
<
BigInteger
>
(
)
;
Assert
.
That
(
ni
,
Is
.
EqualTo
(
val
)
)
;
var
nullable
=
i
.
As
<
BigInteger
?
>
(
)
;
Assert
.
That
(
nullable
,
Is
.
EqualTo
(
val
)
)
;
}
[
Test
]
public
void
PyIntImplicit
(
)
{
var
i
=
new
PyInt
(
1
)
;
var
ni
=
(
PyObject
)
i
.
As
<
object
>
(
)
;
Assert
.
That
(
PythonReferenceComparer
.
Instance
.
Equals
(
i
,
ni
)
,
Is
.
True
)
;
}
[
Test
]
public
void
ToPyList
(
)
{
var
list
=
new
PyList
(
)
;
list
.
Append
(
"hello"
.
ToPython
(
)
)
;
list
.
Append
(
"world"
.
ToPython
(
)
)
;
var
back
=
list
.
ToPython
(
)
.
As
<
PyList
>
(
)
;
Assert
.
That
(
back
.
Length
(
)
,
Is
.
EqualTo
(
list
.
Length
(
)
)
)
;
}
[
Test
]
public
void
RawListProxy
(
)
{
var
list
=
new
List
<
string
>
{
"hello"
,
"world"
}
;
var
listProxy
=
PyObject
.
FromManagedObject
(
list
)
;
var
clrObject
=
(
CLRObject
)
ManagedType
.
GetManagedObject
(
listProxy
)
;
Assert
.
That
(
clrObject
.
inst
,
Is
.
SameAs
(
list
)
)
;
}
[
Test
]
public
void
RawPyObjectProxy
(
)
{
var
pyObject
=
"hello world!"
.
ToPython
(
)
;
var
pyObjectProxy
=
PyObject
.
FromManagedObject
(
pyObject
)
;
var
clrObject
=
(
CLRObject
)
ManagedType
.
GetManagedObject
(
pyObjectProxy
)
;
Assert
.
That
(
clrObject
.
inst
,
Is
.
SameAs
(
pyObject
)
)
;
#pragma warning disable
CS0612
// Type or member is obsolete
const
string
handlePropertyName
=
nameof
(
PyObject
.
Handle
)
;
#pragma warning restore
CS0612
// Type or member is obsolete
var
proxiedHandle
=
pyObjectProxy
.
GetAttr
(
handlePropertyName
)
.
As
<
IntPtr
>
(
)
;
Assert
.
That
(
proxiedHandle
,
Is
.
EqualTo
(
pyObject
.
DangerousGetAddressOrNull
(
)
)
)
;
}
[
Test
]
public
void
GenericToPython
(
)
{
int
i
=
42
;
var
pyObject
=
i
.
ToPythonAs
<
IConvertible
>
(
)
;
var
type
=
pyObject
.
GetPythonType
(
)
;
Assert
.
That
(
type
.
Name
,
Is
.
EqualTo
(
nameof
(
IConvertible
)
)
)
;
}
// 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
(
)
;
Assert
.
That
(
result
,
Is
.
EqualTo
(
new
[
]
{
"testing"
}
)
.
AsCollection
)
;
}
}
public
interface
IGetList
{
List
<
string
>
GetList
(
)
;
}
public
class
GetListImpl
:
IGetList
{
public
List
<
string
>
GetList
(
)
=>
new
(
)
{
"testing"
}
;
}
}
Back
|
FazBrowse Home
|
New Git URL