1- using System . Diagnostics ;
21using System ;
3- using System . Runtime . InteropServices ;
42using System . Text ;
5- using System . Threading ;
63
74namespace Python . Runtime
85{
@@ -13,7 +10,6 @@ static class Loader
1310 {
1411 public unsafe static int Initialize ( IntPtr data , int size )
1512 {
16- IntPtr gs = IntPtr . Zero ;
1713 try
1814 {
1915 var dllPath = Encoding . UTF8 . GetString ( ( byte * ) data . ToPointer ( ) , size ) ;
@@ -27,11 +23,18 @@ public unsafe static int Initialize(IntPtr data, int size)
2723 PythonDLL = null ;
2824 }
2925
30- gs = PyGILState_Ensure ( ) ;
26+ var gs = PyGILState_Ensure ( ) ;
3127
32- // Console.WriteLine("Startup thread");
33- PythonEngine . InitExt ( ) ;
34- // Console.WriteLine("Startup finished");
28+ try
29+ {
30+ // Console.WriteLine("Startup thread");
31+ PythonEngine . InitExt ( ) ;
32+ // Console.WriteLine("Startup finished");
33+ }
34+ finally
35+ {
36+ PyGILState_Release ( gs ) ;
37+ }
3538 }
3639 catch ( Exception exc )
3740 {
@@ -40,27 +43,27 @@ public unsafe static int Initialize(IntPtr data, int size)
4043 ) ;
4144 return 1 ;
4245 }
43- finally
44- {
45- if ( gs != IntPtr . Zero )
46- {
47- PyGILState_Release ( gs ) ;
48- }
49- }
46+
5047 return 0 ;
5148 }
5249
5350 public unsafe static int Shutdown ( IntPtr data , int size )
5451 {
55- IntPtr gs = IntPtr . Zero ;
5652 try
5753 {
5854 var command = Encoding . UTF8 . GetString ( ( byte * ) data . ToPointer ( ) , size ) ;
5955
6056 if ( command == "full_shutdown" )
6157 {
62- gs = PyGILState_Ensure ( ) ;
63- PythonEngine . Shutdown ( ) ;
58+ var gs = PyGILState_Ensure ( ) ;
59+ try
60+ {
61+ PythonEngine . Shutdown ( ) ;
62+ }
63+ finally
64+ {
65+ PyGILState_Release ( gs ) ;
66+ }
6467 }
6568 }
6669 catch ( Exception exc )
@@ -70,13 +73,7 @@ public unsafe static int Shutdown(IntPtr data, int size)
7073 ) ;
7174 return 1 ;
7275 }
73- finally
74- {
75- if ( gs != IntPtr . Zero )
76- {
77- PyGILState_Release ( gs ) ;
78- }
79- }
76+
8077 return 0 ;
8178 }
8279 }
0 commit comments