1
1
//============================================================================
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.
3
3
// This is a line-by-line port from IL back to C#.
4
4
// We now use RGiesecke.DllExport on the required static init method so it can be
5
5
// loaded by a standard CPython interpreter as an extension module. When it
11
11
12
12
// If defined, the "pythonRuntimeVersionString" variable must be set to
13
13
// Python.Runtime's current version.
14
-
15
14
#define USE_PYTHON_RUNTIME_VERSION
16
15
17
16
// If defined, the "PythonRuntimePublicKeyTokenData" data array must be
18
17
// set to Python.Runtime's public key token. (sn -T Python.Runtin.dll)
19
18
#define USE_PYTHON_RUNTIME_PUBLIC_KEY_TOKEN
20
19
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
22
21
// calls are made to indicate what's going on during the load...
23
22
//============================================================================
24
23
using System ;
25
-
24
+ using System . Globalization ;
25
+ using System . IO ;
26
+ using System . Reflection ;
27
+ using System . Runtime . InteropServices ;
28
+ using RGiesecke . DllExport ;
26
29
27
30
public class clrModule
28
31
{
29
32
#if ( PYTHON32 || PYTHON33 || PYTHON34 || PYTHON35 || PYTHON36 )
30
- [ RGiesecke . DllExport . DllExport ( "PyInit_clr" , System . Runtime . InteropServices . CallingConvention . StdCall ) ]
33
+ [ DllExport ( "PyInit_clr" , CallingConvention . StdCall ) ]
31
34
public static IntPtr PyInit_clr ( )
32
35
#else
33
- [ RGiesecke . DllExport . DllExport ( "initclr" , System . Runtime . InteropServices . CallingConvention . StdCall ) ]
36
+ [ DllExport ( "initclr" , CallingConvention . StdCall ) ]
34
37
public static void initclr ( )
35
38
#endif
36
39
{
37
40
#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... " ) ;
39
42
#endif
40
43
#if USE_PYTHON_RUNTIME_PUBLIC_KEY_TOKEN
41
44
var pythonRuntimePublicKeyTokenData = new byte [ ] { 0x50 , 0x00 , 0xfe , 0xa6 , 0xcb , 0xa7 , 0x02 , 0xdd } ;
@@ -47,26 +50,26 @@ public static void initclr()
47
50
// - ApplicationBase
48
51
// - A PrivateBinPath under ApplicationBase
49
52
// With an unsigned assembly, the GAC is skipped.
50
- var pythonRuntimeName = new System . Reflection . AssemblyName ( "Python.Runtime" )
53
+ var pythonRuntimeName = new AssemblyName ( "Python.Runtime" )
51
54
{
52
55
#if USE_PYTHON_RUNTIME_VERSION
53
- Version = new System . Version ( "4.0.0.1" ) ,
56
+ Version = new Version ( "4.0.0.1" ) ,
54
57
#endif
55
- CultureInfo = System . Globalization . CultureInfo . InvariantCulture ,
58
+ CultureInfo = CultureInfo. InvariantCulture
56
59
} ;
57
60
#if USE_PYTHON_RUNTIME_PUBLIC_KEY_TOKEN
58
61
pythonRuntimeName . SetPublicKeyToken ( pythonRuntimePublicKeyTokenData ) ;
59
62
#endif
60
63
// We've got the AssemblyName with optional features; try to load it.
61
- System . Reflection . Assembly pythonRuntime ;
64
+ Assembly pythonRuntime ;
62
65
try
63
66
{
64
- pythonRuntime = System . Reflection . Assembly . Load ( pythonRuntimeName ) ;
67
+ pythonRuntime = Assembly . Load ( pythonRuntimeName ) ;
65
68
#if DEBUG_PRINT
66
- System . Console . WriteLine ( "Success!" ) ;
69
+ Console . WriteLine ( "Success!" ) ;
67
70
#endif
68
71
}
69
- catch ( System . IO . IOException )
72
+ catch ( IOException )
70
73
{
71
74
try
72
75
{
@@ -79,20 +82,22 @@ public static void initclr()
79
82
// http://blogs.msdn.com/suzcook/archive/2003/05/29/57143.aspx
80
83
// http://blogs.msdn.com/suzcook/archive/2003/06/13/57180.aspx
81
84
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 ) ;
84
87
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" ) ;
87
92
#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 ) ;
89
94
#endif
90
- pythonRuntime = System . Reflection . Assembly . LoadFrom ( pythonRuntimeDllPath ) ;
95
+ pythonRuntime = Assembly . LoadFrom ( pythonRuntimeDllPath ) ;
91
96
}
92
- catch ( System . InvalidOperationException )
97
+ catch ( InvalidOperationException )
93
98
{
94
99
#if DEBUG_PRINT
95
- System . Console . WriteLine ( "Could not load Python.Runtime, so sad. " ) ;
100
+ Console . WriteLine ( "Could not load Python.Runtime" ) ;
96
101
#endif
97
102
#if ( PYTHON32 || PYTHON33 || PYTHON34 || PYTHON35 || PYTHON36 )
98
103
return IntPtr . Zero ;
@@ -104,12 +109,12 @@ public static void initclr()
104
109
105
110
// Once here, we've successfully loaded SOME version of Python.Runtime
106
111
// 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" ) ;
108
113
109
114
#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 ) ;
111
116
#else
112
- pythonEngineType . InvokeMember ( "InitExt" , System . Reflection . BindingFlags . InvokeMethod , null , null , null ) ;
117
+ pythonEngineType . InvokeMember ( "InitExt" , BindingFlags . InvokeMethod , null , null , null ) ;
113
118
#endif
114
119
}
115
120
}
0 commit comments