FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
pythonnet/src/clrmodule/ClrModule.cs at v2.5.1 · pythonnet/pythonnet · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
pythonnet
/
pythonnet
Public
Notifications
You must be signed in to change notification settings
Fork
780
Star
5.5k
Code
Issues
153
Pull requests
12
Discussions
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Discussions
Actions
Projects
Wiki
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
126 lines (116 loc) · 5.37 KB
Breadcrumbs
pythonnet
/
src
/
clrmodule
/
ClrModule.cs
Copy path
File metadata and controls
126 lines (116 loc) · 5.37 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
116
117
118
119
120
121
122
123
124
125
126
//============================================================================
// 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 is defined in the Build Properties, a few Console.WriteLine
// calls are made to indicate what's going on during the load...
//============================================================================
using
System
;
using
System
.
Diagnostics
;
using
System
.
Globalization
;
using
System
.
IO
;
using
System
.
Reflection
;
using
System
.
Runtime
.
InteropServices
;
using
RGiesecke
.
DllExport
;
public
class
clrModule
{
#if
PYTHON3
[
DllExport
(
"PyInit_clr"
,
CallingConvention
.
StdCall
)
]
public
static
IntPtr
PyInit_clr
(
)
#elif
PYTHON2
[
DllExport
(
"initclr"
,
CallingConvention
.
StdCall
)
]
public
static
void
initclr
(
)
#endif
{
DebugPrint
(
"Attempting to load 'Python.Runtime' using standard binding rules."
)
;
#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
AssemblyName
(
"Python.Runtime"
)
{
#if
USE_PYTHON_RUNTIME_VERSION
// Has no effect until SNK works. Keep updated anyways.
Version
=
new
Version
(
"2.5.1"
)
,
#endif
CultureInfo
=
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.
Assembly
pythonRuntime
;
try
{
pythonRuntime
=
Assembly
.
Load
(
pythonRuntimeName
)
;
DebugPrint
(
"Success loading 'Python.Runtime' using standard binding rules."
)
;
}
catch
(
IOException
)
{
DebugPrint
(
"'Python.Runtime' not found using standard binding rules."
)
;
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
Assembly
executingAssembly
=
Assembly
.
GetExecutingAssembly
(
)
;
string
assemblyDirectory
=
Path
.
GetDirectoryName
(
executingAssembly
.
Location
)
;
if
(
assemblyDirectory
==
null
)
{
throw
new
InvalidOperationException
(
executingAssembly
.
Location
)
;
}
string
pythonRuntimeDllPath
=
Path
.
Combine
(
assemblyDirectory
,
"Python.Runtime.dll"
)
;
DebugPrint
(
$
"Attempting to load Python.Runtime from: '
{
pythonRuntimeDllPath
}
'."
)
;
pythonRuntime
=
Assembly
.
LoadFrom
(
pythonRuntimeDllPath
)
;
DebugPrint
(
$
"Success loading 'Python.Runtime' from: '
{
pythonRuntimeDllPath
}
'."
)
;
}
catch
(
InvalidOperationException
)
{
DebugPrint
(
"Could not load 'Python.Runtime'."
)
;
#if
PYTHON3
return
IntPtr
.
Zero
;
#elif
PYTHON2
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.
Type
pythonEngineType
=
pythonRuntime
.
GetType
(
"Python.Runtime.PythonEngine"
)
;
#if
PYTHON3
return
(
IntPtr
)
pythonEngineType
.
InvokeMember
(
"InitExt"
,
BindingFlags
.
InvokeMethod
,
null
,
null
,
null
)
;
#elif
PYTHON2
pythonEngineType
.
InvokeMember
(
"InitExt"
,
BindingFlags
.
InvokeMethod
,
null
,
null
,
null
)
;
#endif
}
/// <summary>
/// Substitute for Debug.Writeline(...). Ideally we would use Debug.Writeline
/// but haven't been able to configure the TRACE from within Python.
/// </summary>
[
Conditional
(
"DEBUG"
)
]
private
static
void
DebugPrint
(
string
str
)
{
Console
.
WriteLine
(
str
)
;
}
}
Back
|
FazBrowse Home
|
New Git URL