11//============================================================================
2- // This file replaces the hand-maintained stub that used to implement clr.dll.
2+ // This file replaces the hand-maintained stub that used to implement clr.dll.
33// This is a line-by-line port from IL back to C#.
44// We now use RGiesecke.DllExport on the required static init method so it can be
55// loaded by a standard CPython interpreter as an extension module. When it
1111
1212// If defined, the "pythonRuntimeVersionString" variable must be set to
1313// Python.Runtime's current version.
14-
1514#define USE_PYTHON_RUNTIME_VERSION
1615
1716// If defined, the "PythonRuntimePublicKeyTokenData" data array must be
1817// set to Python.Runtime's public key token. (sn -T Python.Runtin.dll)
1918#define USE_PYTHON_RUNTIME_PUBLIC_KEY_TOKEN
2019
21- // If DEBUG_PRINT is defined in the Build Properties, a few System. Console.WriteLine
20+ // If DEBUG_PRINT is defined in the Build Properties, a few Console.WriteLine
2221// calls are made to indicate what's going on during the load...
2322//============================================================================
2423using System ;
25-
24+ using System . Globalization ;
25+ using System . IO ;
26+ using System . Reflection ;
27+ using System . Runtime . InteropServices ;
28+ using RGiesecke . DllExport ;
2629
2730public class clrModule
2831{
2932#if ( PYTHON32 || PYTHON33 || PYTHON34 || PYTHON35 || PYTHON36 )
30- [ RGiesecke . DllExport . DllExport ( "PyInit_clr" , System . Runtime . InteropServices . CallingConvention . StdCall ) ]
33+ [ DllExport ( "PyInit_clr" , CallingConvention . StdCall ) ]
3134 public static IntPtr PyInit_clr ( )
3235#else
33- [ RGiesecke . DllExport . DllExport ( "initclr" , System . Runtime . InteropServices . CallingConvention . StdCall ) ]
36+ [ DllExport ( "initclr" , CallingConvention . StdCall ) ]
3437 public static void initclr ( )
3538#endif
3639 {
3740#if DEBUG_PRINT
38- System . Console . WriteLine ( "Attempting to load Python.Runtime using standard binding rules... " ) ;
41+ Console . WriteLine ( "Attempting to load Python.Runtime using standard binding rules... " ) ;
3942#endif
4043#if USE_PYTHON_RUNTIME_PUBLIC_KEY_TOKEN
4144 var pythonRuntimePublicKeyTokenData = new byte [ ] { 0x50 , 0x00 , 0xfe , 0xa6 , 0xcb , 0xa7 , 0x02 , 0xdd } ;
@@ -47,26 +50,26 @@ public static void initclr()
4750 // - ApplicationBase
4851 // - A PrivateBinPath under ApplicationBase
4952 // With an unsigned assembly, the GAC is skipped.
50- var pythonRuntimeName = new System . Reflection . AssemblyName ( "Python.Runtime" )
53+ var pythonRuntimeName = new AssemblyName ( "Python.Runtime" )
5154 {
5255#if USE_PYTHON_RUNTIME_VERSION
53- Version = new System . Version ( "4.0.0.1" ) ,
56+ Version = new Version ( "4.0.0.1" ) ,
5457#endif
55- CultureInfo = System . Globalization . CultureInfo . InvariantCulture ,
58+ CultureInfo = CultureInfo. InvariantCulture
5659 } ;
5760#if USE_PYTHON_RUNTIME_PUBLIC_KEY_TOKEN
5861 pythonRuntimeName . SetPublicKeyToken ( pythonRuntimePublicKeyTokenData ) ;
5962#endif
6063 // We've got the AssemblyName with optional features; try to load it.
61- System . Reflection . Assembly pythonRuntime ;
64+ Assembly pythonRuntime ;
6265 try
6366 {
64- pythonRuntime = System . Reflection . Assembly . Load ( pythonRuntimeName ) ;
67+ pythonRuntime = Assembly . Load ( pythonRuntimeName ) ;
6568#if DEBUG_PRINT
66- System . Console . WriteLine ( "Success!" ) ;
69+ Console . WriteLine ( "Success!" ) ;
6770#endif
6871 }
69- catch ( System . IO . IOException )
72+ catch ( IOException )
7073 {
7174 try
7275 {
@@ -79,20 +82,22 @@ public static void initclr()
7982 // http://blogs.msdn.com/suzcook/archive/2003/05/29/57143.aspx
8083 // http://blogs.msdn.com/suzcook/archive/2003/06/13/57180.aspx
8184
82- var executingAssembly = System . Reflection . Assembly . GetExecutingAssembly ( ) ;
83- var assemblyDirectory = System . IO . Path . GetDirectoryName ( executingAssembly . Location ) ;
85+ Assembly executingAssembly = Assembly . GetExecutingAssembly ( ) ;
86+ string assemblyDirectory = Path . GetDirectoryName ( executingAssembly . Location ) ;
8487 if ( assemblyDirectory == null )
85- throw new System . InvalidOperationException ( executingAssembly . Location ) ;
86- var pythonRuntimeDllPath = System . IO . Path . Combine ( assemblyDirectory , "Python.Runtime.dll" ) ;
88+ {
89+ throw new InvalidOperationException ( executingAssembly . Location ) ;
90+ }
91+ string pythonRuntimeDllPath = Path . Combine ( assemblyDirectory , "Python.Runtime.dll" ) ;
8792#if DEBUG_PRINT
88- System . Console . WriteLine ( "Attempting to load Python.Runtime from: '{0}'..." , pythonRuntimeDllPath ) ;
93+ Console . WriteLine ( "Attempting to load Python.Runtime from: '{0}'..." , pythonRuntimeDllPath ) ;
8994#endif
90- pythonRuntime = System . Reflection . Assembly . LoadFrom ( pythonRuntimeDllPath ) ;
95+ pythonRuntime = Assembly . LoadFrom ( pythonRuntimeDllPath ) ;
9196 }
92- catch ( System . InvalidOperationException )
97+ catch ( InvalidOperationException )
9398 {
9499#if DEBUG_PRINT
95- System . Console . WriteLine ( "Could not load Python.Runtime, so sad. " ) ;
100+ Console . WriteLine ( "Could not load Python.Runtime" ) ;
96101#endif
97102#if ( PYTHON32 || PYTHON33 || PYTHON34 || PYTHON35 || PYTHON36 )
98103 return IntPtr . Zero ;
@@ -104,12 +109,12 @@ public static void initclr()
104109
105110 // Once here, we've successfully loaded SOME version of Python.Runtime
106111 // So now we get the PythonEngine and execute the InitExt method on it.
107- var pythonEngineType = pythonRuntime . GetType ( "Python.Runtime.PythonEngine" ) ;
112+ Type pythonEngineType = pythonRuntime . GetType ( "Python.Runtime.PythonEngine" ) ;
108113
109114#if ( PYTHON32 || PYTHON33 || PYTHON34 || PYTHON35 || PYTHON36 )
110- return ( IntPtr ) pythonEngineType . InvokeMember ( "InitExt" , System . Reflection . BindingFlags . InvokeMethod , null , null , null ) ;
115+ return ( IntPtr ) pythonEngineType . InvokeMember ( "InitExt" , BindingFlags . InvokeMethod , null , null , null ) ;
111116#else
112- pythonEngineType . InvokeMember ( "InitExt" , System . Reflection . BindingFlags . InvokeMethod , null , null , null ) ;
117+ pythonEngineType . InvokeMember ( "InitExt" , BindingFlags . InvokeMethod , null , null , null ) ;
113118#endif
114119 }
115120}
0 commit comments