FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
pythonnet/src/runtime/Interop.cs at v3.2.0-rc2 · pythonnet/pythonnet · GitHub
pythonnet
pythonnet
Repository navigation
Code
Issues
147
(147)
Pull requests
11
(11)
Discussions
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
pythonnet
/
src
/
runtime
/
Interop.cs
Copy path
More file actions
More file actions
Latest commit
History
History
History
222 lines (181 loc) · 7.23 KB
Breadcrumbs
pythonnet
/
src
/
runtime
/
Interop.cs
Copy path
File metadata and controls
222 lines (181 loc) · 7.23 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
using
System
;
using
System
.
Collections
.
Concurrent
;
using
System
.
Collections
.
Generic
;
using
System
.
Diagnostics
;
using
System
.
Linq
;
using
System
.
Runtime
.
InteropServices
;
using
Python
.
Runtime
.
Reflection
;
using
System
.
Reflection
;
namespace
Python
.
Runtime
{
/// <summary>
/// This file defines objects to support binary interop with the Python
/// runtime. Generally, the definitions here need to be kept up to date
/// when moving to new Python versions.
/// </summary>
[
Serializable
]
[
AttributeUsage
(
AttributeTargets
.
All
)
]
public
class
DocStringAttribute
:
Attribute
{
public
DocStringAttribute
(
string
docStr
)
{
DocString
=
docStr
;
}
public
string
DocString
{
get
;
}
}
[
Serializable
]
[
AttributeUsage
(
AttributeTargets
.
Method
|
AttributeTargets
.
Delegate
)
]
internal
class
ModuleFunctionAttribute
:
Attribute
{
public
ModuleFunctionAttribute
(
)
{
}
}
[
Serializable
]
[
AttributeUsage
(
AttributeTargets
.
Method
|
AttributeTargets
.
Delegate
)
]
internal
class
ForbidPythonThreadsAttribute
:
Attribute
{
public
ForbidPythonThreadsAttribute
(
)
{
}
}
[
Serializable
]
[
AttributeUsage
(
AttributeTargets
.
Property
)
]
internal
class
ModulePropertyAttribute
:
Attribute
{
public
ModulePropertyAttribute
(
)
{
}
}
/// <summary>
/// TypeFlags(): The actual bit values for the Type Flags stored
/// in a class.
/// Note that the two values reserved for stackless have been put
/// to good use as PythonNet specific flags (Managed and Subclass)
/// </summary>
// Py_TPFLAGS_*
[
Flags
]
public
enum
TypeFlags
:
long
{
HeapType
=
(
1
<<
9
)
,
BaseType
=
(
1
<<
10
)
,
Ready
=
(
1
<<
12
)
,
Readying
=
(
1
<<
13
)
,
HaveGC
=
(
1
<<
14
)
,
// 15 and 16 are reserved for stackless
HaveStacklessExtension
=
0
,
/* XXX Reusing reserved constants */
/// <remarks>PythonNet specific</remarks>
HasClrInstance
=
(
1
<<
15
)
,
/// <remarks>PythonNet specific</remarks>
Subclass
=
(
1
<<
16
)
,
/* Objects support nb_index in PyNumberMethods */
HaveVersionTag
=
(
1
<<
18
)
,
ValidVersionTag
=
(
1
<<
19
)
,
IsAbstract
=
(
1
<<
20
)
,
HaveNewBuffer
=
(
1
<<
21
)
,
// TODO: Implement FastSubclass functions
IntSubclass
=
(
1
<<
23
)
,
LongSubclass
=
(
1
<<
24
)
,
ListSubclass
=
(
1
<<
25
)
,
TupleSubclass
=
(
1
<<
26
)
,
StringSubclass
=
(
1
<<
27
)
,
UnicodeSubclass
=
(
1
<<
28
)
,
DictSubclass
=
(
1
<<
29
)
,
BaseExceptionSubclass
=
(
1
<<
30
)
,
TypeSubclass
=
(
1
<<
31
)
,
Default
=
(
HaveStacklessExtension
|
HaveVersionTag
)
,
}
// This class defines the function prototypes (delegates) used for low
// level integration with the CPython runtime. It also provides name
// based lookup of the correct prototype for a particular Python type
// slot and utilities for generating method thunks for managed methods.
internal
class
Interop
{
// Concurrent: type-slot installation can race past TryGetValue.
static
readonly
ConcurrentDictionary
<
MethodInfo
,
Type
>
delegateTypes
=
new
(
)
;
internal
static
Type
GetPrototype
(
MethodInfo
method
)
{
if
(
delegateTypes
.
TryGetValue
(
method
,
out
var
delegateType
)
)
return
delegateType
;
var
parameters
=
method
.
GetParameters
(
)
.
Select
(
p
=>
new
ParameterHelper
(
p
)
)
.
ToArray
(
)
;
foreach
(
var
candidate
in
typeof
(
Interop
)
.
GetNestedTypes
(
)
)
{
if
(
!
typeof
(
Delegate
)
.
IsAssignableFrom
(
candidate
)
)
continue
;
MethodInfo
invoke
=
candidate
.
GetMethod
(
"Invoke"
)
;
var
candiateParameters
=
invoke
.
GetParameters
(
)
;
if
(
candiateParameters
.
Length
!=
parameters
.
Length
)
continue
;
var
parametersMatch
=
parameters
.
Zip
(
candiateParameters
,
(
expected
,
actual
)
=>
expected
.
Matches
(
actual
)
)
.
All
(
matches
=>
matches
)
;
if
(
!
parametersMatch
)
continue
;
if
(
invoke
.
ReturnType
!=
method
.
ReturnType
)
continue
;
delegateTypes
.
TryAdd
(
method
,
candidate
)
;
return
candidate
;
}
throw
new
NotImplementedException
(
method
.
ToString
(
)
)
;
}
// Concurrent: documents the multi-writer contract previously enforced
// by callers happening to hold TypeManager._cacheCreateLock.
internal
static
ConcurrentDictionary
<
IntPtr
,
Delegate
>
allocatedThunks
=
new
(
)
;
internal
static
ThunkInfo
GetThunk
(
MethodInfo
method
)
{
Type
dt
=
GetPrototype
(
method
)
;
Delegate
d
=
Delegate
.
CreateDelegate
(
dt
,
method
)
;
return
GetThunk
(
d
)
;
}
internal
static
ThunkInfo
GetThunk
(
Delegate
@delegate
)
{
var
info
=
new
ThunkInfo
(
@delegate
)
;
allocatedThunks
[
info
.
Address
]
=
@delegate
;
return
info
;
}
[
UnmanagedFunctionPointer
(
CallingConvention
.
Cdecl
)
]
public
delegate
NewReference
B_N
(
BorrowedReference
ob
)
;
[
UnmanagedFunctionPointer
(
CallingConvention
.
Cdecl
)
]
public
delegate
NewReference
BB_N
(
BorrowedReference
ob
,
BorrowedReference
a
)
;
[
UnmanagedFunctionPointer
(
CallingConvention
.
Cdecl
)
]
public
delegate
NewReference
BBB_N
(
BorrowedReference
ob
,
BorrowedReference
a1
,
BorrowedReference
a2
)
;
[
UnmanagedFunctionPointer
(
CallingConvention
.
Cdecl
)
]
public
delegate
int
B_I32
(
BorrowedReference
ob
)
;
[
UnmanagedFunctionPointer
(
CallingConvention
.
Cdecl
)
]
public
delegate
int
BB_I32
(
BorrowedReference
ob
,
BorrowedReference
a
)
;
[
UnmanagedFunctionPointer
(
CallingConvention
.
Cdecl
)
]
public
delegate
int
BBB_I32
(
BorrowedReference
ob
,
BorrowedReference
a1
,
BorrowedReference
a2
)
;
[
UnmanagedFunctionPointer
(
CallingConvention
.
Cdecl
)
]
public
delegate
int
BP_I32
(
BorrowedReference
ob
,
IntPtr
arg
)
;
[
UnmanagedFunctionPointer
(
CallingConvention
.
Cdecl
)
]
public
delegate
IntPtr
B_P
(
BorrowedReference
ob
)
;
[
UnmanagedFunctionPointer
(
CallingConvention
.
Cdecl
)
]
public
delegate
NewReference
BBI32_N
(
BorrowedReference
ob
,
BorrowedReference
a1
,
int
a2
)
;
[
UnmanagedFunctionPointer
(
CallingConvention
.
Cdecl
)
]
public
delegate
NewReference
BP_N
(
BorrowedReference
ob
,
IntPtr
arg
)
;
[
UnmanagedFunctionPointer
(
CallingConvention
.
Cdecl
)
]
public
delegate
void
N_V
(
NewReference
ob
)
;
[
UnmanagedFunctionPointer
(
CallingConvention
.
Cdecl
)
]
public
delegate
int
BPP_I32
(
BorrowedReference
ob
,
IntPtr
a1
,
IntPtr
a2
)
;
}
internal
class
ThunkInfo
{
public
readonly
Delegate
Target
;
public
readonly
IntPtr
Address
;
public
ThunkInfo
(
Delegate
target
)
{
Debug
.
Assert
(
target
is
not
null
)
;
Target
=
target
!
;
Address
=
Marshal
.
GetFunctionPointerForDelegate
(
target
)
;
}
}
[
StructLayout
(
LayoutKind
.
Sequential
)
]
struct
PyMethodDef
{
public
IntPtr
ml_name
;
public
IntPtr
ml_meth
;
public
int
ml_flags
;
public
IntPtr
ml_doc
;
}
}
Back
|
FazBrowse Home
|
New Git URL