@@ -244,26 +244,39 @@ public void SetReadExec(IntPtr mappedMemory, int numBytes)
244244 /// </summary>
245245 public static void InitializePlatformData ( )
246246 {
247- IntPtr op ;
248- IntPtr fn ;
249- IntPtr platformModule = Runtime . PyImport_ImportModule ( "platform" ) ;
250- IntPtr emptyTuple = Runtime . PyTuple_New ( 0 ) ;
251-
252- fn = Runtime . PyObject_GetAttrString ( platformModule , "system" ) ;
253- op = Runtime . PyObject_Call ( fn , emptyTuple , IntPtr . Zero ) ;
254- PythonException . ThrowIfIsNull ( op ) ;
255- OperatingSystemName = Runtime . GetManagedString ( op ) ;
256- Runtime . XDecref ( op ) ;
257- Runtime . XDecref ( fn ) ;
258-
259- fn = Runtime . PyObject_GetAttrString ( platformModule , "machine" ) ;
260- op = Runtime . PyObject_Call ( fn , emptyTuple , IntPtr . Zero ) ;
261- MachineName = Runtime . GetManagedString ( op ) ;
262- Runtime . XDecref ( op ) ;
263- Runtime . XDecref ( fn ) ;
264-
265- Runtime . XDecref ( emptyTuple ) ;
266- Runtime . XDecref ( platformModule ) ;
247+ IntPtr op = IntPtr . Zero ;
248+ IntPtr fn = IntPtr . Zero ;
249+ IntPtr platformModule = IntPtr . Zero ;
250+ IntPtr emptyTuple = IntPtr . Zero ;
251+ try
252+ {
253+ platformModule = Runtime . PyImport_ImportModule ( "platform" ) ;
254+ PythonException . ThrowIfIsNull ( platformModule ) ;
255+
256+ fn = Runtime . PyObject_GetAttrString ( platformModule , "system" ) ;
257+ PythonException . ThrowIfIsNull ( fn ) ;
258+
259+ emptyTuple = Runtime . PyTuple_New ( 0 ) ;
260+ op = Runtime . PyObject_Call ( fn , emptyTuple , IntPtr . Zero ) ;
261+ PythonException . ThrowIfIsNull ( op ) ;
262+
263+ OperatingSystemName = Runtime . GetManagedString ( op ) ;
264+
265+ fn = Runtime . PyObject_GetAttrString ( platformModule , "machine" ) ;
266+ PythonException . ThrowIfIsNull ( fn ) ;
267+
268+ op = Runtime . PyObject_Call ( fn , emptyTuple , IntPtr . Zero ) ;
269+ PythonException . ThrowIfIsNull ( op ) ;
270+ MachineName = Runtime . GetManagedString ( op ) ;
271+ }
272+ finally
273+ {
274+ Runtime . XDecref ( op ) ;
275+ Runtime . XDecref ( fn ) ;
276+
277+ Runtime . XDecref ( emptyTuple ) ;
278+ Runtime . XDecref ( platformModule ) ;
279+ }
267280
268281 // Now convert the strings into enum values so we can do switch
269282 // statements rather than constant parsing.
0 commit comments