FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
pythonnet/src/clrmodule/ClrModule.cs at fix_stackoverflow · matthid/pythonnet · GitHub
matthid
/
pythonnet
Public
forked from
pythonnet/pythonnet
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
pythonnet
/
src
/
clrmodule
/
ClrModule.cs
Copy path
More file actions
More file actions
Latest commit
History
History
History
115 lines (104 loc) · 5.13 KB
Breadcrumbs
pythonnet
/
src
/
clrmodule
/
ClrModule.cs
Copy path
File metadata and controls
115 lines (104 loc) · 5.13 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
//============================================================================
// This file replaces the hand-maintained stub that used to implement clr.dll.
// This is a line-by-line port from IL back to C#.
// We now use RGiesecke.DllExport on the required static init method so it can be
// loaded by a standard CPython interpreter as an extension module. When it
// is loaded, it bootstraps the managed runtime integration layer and defers
// to it to do initialization and put the clr module into sys.modules, etc.
// The "USE_PYTHON_RUNTIME_*" defines control what extra evidence is used
// to help the CLR find the appropriate Python.Runtime assembly.
// If defined, the "pythonRuntimeVersionString" variable must be set to
// Python.Runtime's current version.
#define USE_PYTHON_RUNTIME_VERSION
// If defined, the "PythonRuntimePublicKeyTokenData" data array must be
// set to Python.Runtime's public key token. (sn -T Python.Runtin.dll)
#define USE_PYTHON_RUNTIME_PUBLIC_KEY_TOKEN
// If DEBUG_PRINT is defined in the Build Properties, a few System.Console.WriteLine
// calls are made to indicate what's going on during the load...
//============================================================================
using
System
;
public
class
clrModule
{
#if
(
PYTHON32
||
PYTHON33
||
PYTHON34
||
PYTHON35
)
[
RGiesecke
.
DllExport
.
DllExport
(
"PyInit_clr"
,
System
.
Runtime
.
InteropServices
.
CallingConvention
.
StdCall
)
]
public
static
IntPtr
PyInit_clr
(
)
#else
[
RGiesecke
.
DllExport
.
DllExport
(
"initclr"
,
System
.
Runtime
.
InteropServices
.
CallingConvention
.
StdCall
)
]
public
static
void
initclr
(
)
#endif
{
#if
DEBUG_PRINT
System
.
Console
.
WriteLine
(
"Attempting to load Python.Runtime using standard binding rules... "
)
;
#endif
#if
USE_PYTHON_RUNTIME_PUBLIC_KEY_TOKEN
var
pythonRuntimePublicKeyTokenData
=
new
byte
[
]
{
0x50
,
0x00
,
0xfe
,
0xa6
,
0xcb
,
0xa7
,
0x02
,
0xdd
}
;
#endif
// Attempt to find and load Python.Runtime using standard assembly binding rules.
// This roughly translates into looking in order:
// - GAC
// - ApplicationBase
// - A PrivateBinPath under ApplicationBase
// With an unsigned assembly, the GAC is skipped.
var
pythonRuntimeName
=
new
System
.
Reflection
.
AssemblyName
(
"Python.Runtime"
)
{
#if
USE_PYTHON_RUNTIME_VERSION
Version
=
new
System
.
Version
(
"4.0.0.1"
)
,
#endif
CultureInfo
=
System
.
Globalization
.
CultureInfo
.
InvariantCulture
,
}
;
#if
USE_PYTHON_RUNTIME_PUBLIC_KEY_TOKEN
pythonRuntimeName
.
SetPublicKeyToken
(
pythonRuntimePublicKeyTokenData
)
;
#endif
// We've got the AssemblyName with optional features; try to load it.
System
.
Reflection
.
Assembly
pythonRuntime
;
try
{
pythonRuntime
=
System
.
Reflection
.
Assembly
.
Load
(
pythonRuntimeName
)
;
#if
DEBUG_PRINT
System
.
Console
.
WriteLine
(
"Success!"
)
;
#endif
}
catch
(
System
.
IO
.
IOException
)
{
try
{
// If the above fails for any reason, we fallback to attempting to load "Python.Runtime.dll"
// from the directory this assembly is running in. "This assembly" is probably "clr.pyd",
// sitting somewhere in PYTHONPATH. This is using Assembly.LoadFrom, and inherits all the
// caveats of that call. See MSDN docs for details.
// Suzanne Cook's blog is also an excellent source of info on this:
// http://blogs.msdn.com/suzcook/
// http://blogs.msdn.com/suzcook/archive/2003/05/29/57143.aspx
// http://blogs.msdn.com/suzcook/archive/2003/06/13/57180.aspx
var
executingAssembly
=
System
.
Reflection
.
Assembly
.
GetExecutingAssembly
(
)
;
var
assemblyDirectory
=
System
.
IO
.
Path
.
GetDirectoryName
(
executingAssembly
.
Location
)
;
if
(
assemblyDirectory
==
null
)
throw
new
System
.
InvalidOperationException
(
executingAssembly
.
Location
)
;
var
pythonRuntimeDllPath
=
System
.
IO
.
Path
.
Combine
(
assemblyDirectory
,
"Python.Runtime.dll"
)
;
#if
DEBUG_PRINT
System
.
Console
.
WriteLine
(
"Attempting to load Python.Runtime from: '{0}'..."
,
pythonRuntimeDllPath
)
;
#endif
pythonRuntime
=
System
.
Reflection
.
Assembly
.
LoadFrom
(
pythonRuntimeDllPath
)
;
}
catch
(
System
.
InvalidOperationException
)
{
#if
DEBUG_PRINT
System
.
Console
.
WriteLine
(
"Could not load Python.Runtime, so sad."
)
;
#endif
#if
(
PYTHON32
||
PYTHON33
||
PYTHON34
||
PYTHON35
)
return
IntPtr
.
Zero
;
#else
return
;
#endif
}
}
// Once here, we've successfully loaded SOME version of Python.Runtime
// So now we get the PythonEngine and execute the InitExt method on it.
var
pythonEngineType
=
pythonRuntime
.
GetType
(
"Python.Runtime.PythonEngine"
)
;
#if
(
PYTHON32
||
PYTHON33
||
PYTHON34
||
PYTHON35
)
return
(
IntPtr
)
pythonEngineType
.
InvokeMember
(
"InitExt"
,
System
.
Reflection
.
BindingFlags
.
InvokeMethod
,
null
,
null
,
null
)
;
#else
pythonEngineType
.
InvokeMember
(
"InitExt"
,
System
.
Reflection
.
BindingFlags
.
InvokeMethod
,
null
,
null
,
null
)
;
#endif
}
}
Back
|
FazBrowse Home
|
New Git URL