@@ -24,19 +24,22 @@ public void TestBufferWrite()
2424
2525 string bufferTestString = "hello world! !$%&/()=?" ;
2626
27- using ( var scope = Py . CreateScope ( ) )
27+ using ( Py . GIL ( ) )
2828 {
29- scope . Exec ( $ "arr = bytearray({ bufferTestString . Length } )") ;
30- PyObject pythonArray = scope . Get ( "arr" ) ;
31- byte [ ] managedArray = new UTF8Encoding ( ) . GetBytes ( bufferTestString ) ;
32-
33- using ( PyBuffer buf = pythonArray . GetBuffer ( ) )
29+ using ( var scope = Py . CreateScope ( ) )
3430 {
35- buf . Write ( managedArray , 0 , managedArray . Length ) ;
36- }
31+ scope . Exec ( $ "arr = bytearray({ bufferTestString . Length } )") ;
32+ PyObject pythonArray = scope . Get ( "arr" ) ;
33+ byte [ ] managedArray = new UTF8Encoding ( ) . GetBytes ( bufferTestString ) ;
3734
38- string result = scope . Eval ( "arr.decode('utf-8')" ) . ToString ( ) ;
39- Assert . IsTrue ( result == bufferTestString ) ;
35+ using ( PyBuffer buf = pythonArray . GetBuffer ( ) )
36+ {
37+ buf . Write ( managedArray , 0 , managedArray . Length ) ;
38+ }
39+
40+ string result = scope . Eval ( "arr.decode('utf-8')" ) . ToString ( ) ;
41+ Assert . IsTrue ( result == bufferTestString ) ;
42+ }
4043 }
4144 }
4245
@@ -47,19 +50,22 @@ public void TestBufferRead()
4750
4851 string bufferTestString = "hello world! !$%&/()=?" ;
4952
50- using ( var scope = Py . CreateScope ( ) )
53+ using ( Py . GIL ( ) )
5154 {
52- scope . Exec ( $ "arr = b'{ bufferTestString } '") ;
53- PyObject pythonArray = scope . Get ( "arr" ) ;
54- byte [ ] managedArray = new byte [ bufferTestString . Length ] ;
55-
56- using ( PyBuffer buf = pythonArray . GetBuffer ( ) )
55+ using ( var scope = Py . CreateScope ( ) )
5756 {
58- buf . Read ( managedArray , 0 , managedArray . Length ) ;
59- }
57+ scope . Exec ( $ "arr = b'{ bufferTestString } '") ;
58+ PyObject pythonArray = scope . Get ( "arr" ) ;
59+ byte [ ] managedArray = new byte [ bufferTestString . Length ] ;
6060
61- string result = new UTF8Encoding ( ) . GetString ( managedArray ) ;
62- Assert . IsTrue ( result == bufferTestString ) ;
61+ using ( PyBuffer buf = pythonArray . GetBuffer ( ) )
62+ {
63+ buf . Read ( managedArray , 0 , managedArray . Length ) ;
64+ }
65+
66+ string result = new UTF8Encoding ( ) . GetString ( managedArray ) ;
67+ Assert . IsTrue ( result == bufferTestString ) ;
68+ }
6369 }
6470 }
6571 }
0 commit comments