FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
pythonnet/src/embed_tests/TestPyBuffer.cs at master · quantfns/pythonnet · GitHub
quantfns
pythonnet
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
pythonnet
/
src
/
embed_tests
/
TestPyBuffer.cs
Copy path
More file actions
More file actions
Latest commit
History
History
History
125 lines (100 loc) · 3.57 KB
Breadcrumbs
pythonnet
/
src
/
embed_tests
/
TestPyBuffer.cs
Copy path
File metadata and controls
125 lines (100 loc) · 3.57 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
using
System
;
using
System
.
Runtime
.
CompilerServices
;
using
System
.
Text
;
using
NUnit
.
Framework
;
using
Python
.
Runtime
;
using
Python
.
Runtime
.
Codecs
;
namespace
Python
.
EmbeddingTest
{
class
TestPyBuffer
{
[
OneTimeSetUp
]
public
void
SetUp
(
)
{
PythonEngine
.
Initialize
(
)
;
TupleCodec
<
ValueTuple
>
.
Register
(
)
;
}
[
OneTimeTearDown
]
public
void
Dispose
(
)
{
PythonEngine
.
Shutdown
(
)
;
}
[
Test
]
public
void
TestBufferWrite
(
)
{
string
bufferTestString
=
"hello world! !$%&/()=?"
;
string
bufferTestString2
=
"h llo world! !$%&/()=?"
;
using
var
_
=
Py
.
GIL
(
)
;
using
var
pythonArray
=
ByteArrayFromAsciiString
(
bufferTestString
)
;
using
(
PyBuffer
buf
=
pythonArray
.
GetBuffer
(
PyBUF
.
WRITABLE
)
)
{
byte
[
]
managedArray
=
{
(
byte
)
' '
}
;
buf
.
Write
(
managedArray
,
0
,
managedArray
.
Length
,
1
)
;
}
string
result
=
pythonArray
.
InvokeMethod
(
"decode"
,
"utf-8"
.
ToPython
(
)
)
.
As
<
string
>
(
)
;
Assert
.
IsTrue
(
result
==
bufferTestString2
)
;
}
[
Test
]
public
void
TestBufferRead
(
)
{
string
bufferTestString
=
"hello world! !$%&/()=?"
;
using
var
_
=
Py
.
GIL
(
)
;
using
var
pythonArray
=
ByteArrayFromAsciiString
(
bufferTestString
)
;
byte
[
]
managedArray
=
new
byte
[
bufferTestString
.
Length
]
;
using
(
PyBuffer
buf
=
pythonArray
.
GetBuffer
(
)
)
{
managedArray
[
0
]
=
(
byte
)
' '
;
buf
.
Read
(
managedArray
,
1
,
managedArray
.
Length
-
1
,
1
)
;
}
string
result
=
new
UTF8Encoding
(
)
.
GetString
(
managedArray
)
;
Assert
.
IsTrue
(
result
==
" "
+
bufferTestString
.
Substring
(
1
)
)
;
}
[
Test
]
public
void
ArrayHasBuffer
(
)
{
var
array
=
new
[
,
]
{
{
1
,
2
}
,
{
3
,
4
}
}
;
var
memoryView
=
PythonEngine
.
Eval
(
"memoryview"
)
;
var
mem
=
memoryView
.
Invoke
(
array
.
ToPython
(
)
)
;
Assert
.
AreEqual
(
1
,
mem
[
(
0
,
0
)
.
ToPython
(
)
]
.
As
<
int
>
(
)
)
;
Assert
.
AreEqual
(
array
[
1
,
0
]
,
mem
[
(
1
,
0
)
.
ToPython
(
)
]
.
As
<
int
>
(
)
)
;
}
[
Test
]
public
void
RefCount
(
)
{
using
var
_
=
Py
.
GIL
(
)
;
using
var
arr
=
ByteArrayFromAsciiString
(
"hello world! !$%&/()=?"
)
;
Assert
.
AreEqual
(
1
,
arr
.
Refcount
)
;
using
(
PyBuffer
buf
=
arr
.
GetBuffer
(
)
)
{
Assert
.
AreEqual
(
2
,
arr
.
Refcount
)
;
}
Assert
.
AreEqual
(
1
,
arr
.
Refcount
)
;
}
[
Test
]
public
void
Finalization
(
)
{
if
(
Type
.
GetType
(
"Mono.Runtime"
)
is
not
null
)
{
Assert
.
Inconclusive
(
"test unreliable in Mono"
)
;
return
;
}
using
var
_
=
Py
.
GIL
(
)
;
using
var
arr
=
ByteArrayFromAsciiString
(
"hello world! !$%&/()=?"
)
;
Assert
.
AreEqual
(
1
,
arr
.
Refcount
)
;
MakeBufAndLeak
(
arr
)
;
GC
.
Collect
(
)
;
GC
.
WaitForPendingFinalizers
(
)
;
Finalizer
.
Instance
.
Collect
(
)
;
Assert
.
AreEqual
(
1
,
arr
.
Refcount
)
;
}
[
MethodImpl
(
MethodImplOptions
.
NoInlining
)
]
static
void
MakeBufAndLeak
(
PyObject
bufProvider
)
{
PyBuffer
buf
=
bufProvider
.
GetBuffer
(
)
;
}
static
PyObject
ByteArrayFromAsciiString
(
string
str
)
{
using
var
scope
=
Py
.
CreateScope
(
)
;
return
Runtime
.
Runtime
.
PyByteArray_FromStringAndSize
(
str
)
.
MoveToPyObject
(
)
;
}
}
}
Back
|
FazBrowse Home
|
New Git URL