FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
pythonnet/src/embed_tests/TestConverter.cs at master · sdpython/pythonnet · GitHub
This repository was archived by the owner on Jul 22, 2023. It is now read-only.
sdpython
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
140 lines (123 loc) · 4.37 KB
Breadcrumbs
pythonnet
/
src
/
embed_tests
/
TestConverter.cs
Copy path
File metadata and controls
140 lines (123 loc) · 4.37 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
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
PyLong
(
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
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
)
;
}
}
}
Back
|
FazBrowse Home
|
New Git URL