FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
pythonnet/src/runtime/StateSerialization/RuntimeData.cs at master · generalloki/pythonnet · GitHub
generalloki
pythonnet
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
pythonnet
/
src
/
runtime
/
StateSerialization
/
RuntimeData.cs
Copy path
More file actions
More file actions
Latest commit
History
History
History
388 lines (349 loc) · 14.2 KB
Breadcrumbs
pythonnet
/
src
/
runtime
/
StateSerialization
/
RuntimeData.cs
Copy path
File metadata and controls
388 lines (349 loc) · 14.2 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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
using
System
;
using
System
.
Collections
.
Generic
;
using
System
.
Diagnostics
;
using
System
.
IO
;
using
System
.
Linq
;
using
System
.
Runtime
.
InteropServices
;
using
System
.
Runtime
.
Serialization
;
using
System
.
Runtime
.
Serialization
.
Formatters
.
Binary
;
using
Python
.
Runtime
.
StateSerialization
;
using
static
Python
.
Runtime
.
Runtime
;
namespace
Python
.
Runtime
{
public
static
class
RuntimeData
{
public
readonly
static
Func
<
IFormatter
>
DefaultFormatterFactory
=
(
)
=>
{
try
{
var
res
=
new
BinaryFormatter
(
)
;
res
.
Serialize
(
new
MemoryStream
(
)
,
1
)
;
// test if BinaryFormatter is usable
return
res
;
}
catch
{
return
new
NoopFormatter
(
)
;
}
}
;
private
static
Func
<
IFormatter
>
_formatterFactory
{
get
;
set
;
}
=
DefaultFormatterFactory
;
public
static
Func
<
IFormatter
>
FormatterFactory
{
get
=>
_formatterFactory
;
set
{
if
(
value
==
null
)
throw
new
ArgumentNullException
(
nameof
(
value
)
)
;
_formatterFactory
=
value
;
}
}
private
static
Type
?
_formatterType
=
null
;
public
static
Type
?
FormatterType
{
get
=>
_formatterType
;
set
{
if
(
!
typeof
(
IFormatter
)
.
IsAssignableFrom
(
value
)
)
{
throw
new
ArgumentException
(
"Not a type implemented IFormatter"
)
;
}
_formatterType
=
value
;
}
}
/// <summary>
/// Callback called as a last step in the serialization process
/// </summary>
public
static
Action
?
PostStashHook
{
get
;
set
;
}
=
null
;
/// <summary>
/// Callback called as the first step in the deserialization process
/// </summary>
public
static
Action
?
PreRestoreHook
{
get
;
set
;
}
=
null
;
public
static
ICLRObjectStorer
?
WrappersStorer
{
get
;
set
;
}
/// <summary>
/// Clears the old "clr_data" entry if a previous one is present.
/// </summary>
static
void
ClearCLRData
(
)
{
BorrowedReference
capsule
=
PySys_GetObject
(
"clr_data"
)
;
if
(
!
capsule
.
IsNull
)
{
IntPtr
oldData
=
PyCapsule_GetPointer
(
capsule
,
IntPtr
.
Zero
)
;
PyMem_Free
(
oldData
)
;
PyCapsule_SetPointer
(
capsule
,
IntPtr
.
Zero
)
;
}
}
internal
static
void
Stash
(
)
{
var
runtimeStorage
=
new
PythonNetState
{
Metatype
=
MetaType
.
SaveRuntimeData
(
)
,
ImportHookState
=
ImportHook
.
SaveRuntimeData
(
)
,
Types
=
TypeManager
.
SaveRuntimeData
(
)
,
Classes
=
ClassManager
.
SaveRuntimeData
(
)
,
SharedObjects
=
SaveRuntimeDataObjects
(
)
,
}
;
IFormatter
formatter
=
CreateFormatter
(
)
;
var
ms
=
new
MemoryStream
(
)
;
formatter
.
Serialize
(
ms
,
runtimeStorage
)
;
Debug
.
Assert
(
ms
.
Length
<=
int
.
MaxValue
)
;
byte
[
]
data
=
ms
.
GetBuffer
(
)
;
// TODO: use buffer api instead
IntPtr
mem
=
PyMem_Malloc
(
ms
.
Length
+
IntPtr
.
Size
)
;
Marshal
.
WriteIntPtr
(
mem
,
(
IntPtr
)
ms
.
Length
)
;
Marshal
.
Copy
(
data
,
0
,
mem
+
IntPtr
.
Size
,
(
int
)
ms
.
Length
)
;
ClearCLRData
(
)
;
using
NewReference
capsule
=
PyCapsule_New
(
mem
,
IntPtr
.
Zero
,
IntPtr
.
Zero
)
;
int
res
=
PySys_SetObject
(
"clr_data"
,
capsule
.
BorrowOrThrow
(
)
)
;
PythonException
.
ThrowIfIsNotZero
(
res
)
;
PostStashHook
?
.
Invoke
(
)
;
}
internal
static
void
RestoreRuntimeData
(
)
{
try
{
RestoreRuntimeDataImpl
(
)
;
}
finally
{
ClearStash
(
)
;
}
}
private
static
void
RestoreRuntimeDataImpl
(
)
{
PreRestoreHook
?
.
Invoke
(
)
;
BorrowedReference
capsule
=
PySys_GetObject
(
"clr_data"
)
;
if
(
capsule
.
IsNull
)
{
return
;
}
IntPtr
mem
=
PyCapsule_GetPointer
(
capsule
,
IntPtr
.
Zero
)
;
int
length
=
(
int
)
Marshal
.
ReadIntPtr
(
mem
)
;
byte
[
]
data
=
new
byte
[
length
]
;
Marshal
.
Copy
(
mem
+
IntPtr
.
Size
,
data
,
0
,
length
)
;
var
ms
=
new
MemoryStream
(
data
)
;
var
formatter
=
CreateFormatter
(
)
;
var
storage
=
(
PythonNetState
)
formatter
.
Deserialize
(
ms
)
;
PyCLRMetaType
=
MetaType
.
RestoreRuntimeData
(
storage
.
Metatype
)
;
TypeManager
.
RestoreRuntimeData
(
storage
.
Types
)
;
ClassManager
.
RestoreRuntimeData
(
storage
.
Classes
)
;
RestoreRuntimeDataObjects
(
storage
.
SharedObjects
)
;
ImportHook
.
RestoreRuntimeData
(
storage
.
ImportHookState
)
;
}
public
static
bool
HasStashData
(
)
{
return
!
PySys_GetObject
(
"clr_data"
)
.
IsNull
;
}
public
static
void
ClearStash
(
)
{
PySys_SetObject
(
"clr_data"
,
default
)
;
}
static
bool
CheckSerializable
(
object
o
)
{
Type
type
=
o
.
GetType
(
)
;
do
{
if
(
!
type
.
IsSerializable
)
{
return
false
;
}
}
while
(
(
type
=
type
.
BaseType
)
!=
null
)
;
return
true
;
}
private
static
SharedObjectsState
SaveRuntimeDataObjects
(
)
{
var
contexts
=
new
Dictionary
<
PyObject
,
Dictionary
<
string
,
object
?
>
>
(
PythonReferenceComparer
.
Instance
)
;
var
extensionObjs
=
new
Dictionary
<
PyObject
,
ExtensionType
>
(
PythonReferenceComparer
.
Instance
)
;
// make a copy with strongly typed references to avoid concurrent modification
var
extensions
=
ExtensionType
.
loadedExtensions
.
Select
(
addr
=>
new
PyObject
(
new
BorrowedReference
(
addr
)
,
// if we don't skip collect, finalizer might modify loadedExtensions
skipCollect
:
true
)
)
.
ToArray
(
)
;
foreach
(
var
pyObj
in
extensions
)
{
var
extension
=
(
ExtensionType
)
ManagedType
.
GetManagedObject
(
pyObj
)
!
;
Debug
.
Assert
(
CheckSerializable
(
extension
)
)
;
var
context
=
extension
.
Save
(
pyObj
)
;
if
(
context
is
not
null
)
{
contexts
[
pyObj
]
=
context
;
}
extensionObjs
.
Add
(
pyObj
,
extension
)
;
}
var
wrappers
=
new
Dictionary
<
object
,
List
<
CLRObject
>
>
(
)
;
var
userObjects
=
new
CLRWrapperCollection
(
)
;
// make a copy with strongly typed references to avoid concurrent modification
var
reflectedObjects
=
CLRObject
.
reflectedObjects
.
Select
(
addr
=>
new
PyObject
(
new
BorrowedReference
(
addr
)
,
// if we don't skip collect, finalizer might modify reflectedObjects
skipCollect
:
true
)
)
.
ToList
(
)
;
foreach
(
var
pyObj
in
reflectedObjects
)
{
// Wrapper must be the CLRObject
var
clrObj
=
(
CLRObject
)
ManagedType
.
GetManagedObject
(
pyObj
)
!
;
object
inst
=
clrObj
.
inst
;
List
<
CLRObject
>
mappedObjs
;
if
(
!
userObjects
.
TryGetValue
(
inst
,
out
var
item
)
)
{
item
=
new
CLRMappedItem
(
inst
)
;
userObjects
.
Add
(
item
)
;
Debug
.
Assert
(
!
wrappers
.
ContainsKey
(
inst
)
)
;
mappedObjs
=
new
List
<
CLRObject
>
(
)
;
wrappers
.
Add
(
inst
,
mappedObjs
)
;
}
else
{
mappedObjs
=
wrappers
[
inst
]
;
}
item
.
AddRef
(
pyObj
)
;
mappedObjs
.
Add
(
clrObj
)
;
}
var
wrapperStorage
=
new
Dictionary
<
string
,
object
?
>
(
)
;
WrappersStorer
?
.
Store
(
userObjects
,
wrapperStorage
)
;
var
internalStores
=
new
Dictionary
<
PyObject
,
CLRObject
>
(
PythonReferenceComparer
.
Instance
)
;
foreach
(
var
item
in
userObjects
)
{
if
(
!
item
.
Stored
)
{
if
(
!
CheckSerializable
(
item
.
Instance
)
)
{
continue
;
}
var
clrO
=
wrappers
[
item
.
Instance
]
.
First
(
)
;
foreach
(
var
@ref
in
item
.
PyRefs
)
{
internalStores
.
Add
(
@ref
,
clrO
)
;
}
}
}
return
new
(
)
{
InternalStores
=
internalStores
,
Extensions
=
extensionObjs
,
Wrappers
=
wrapperStorage
,
Contexts
=
contexts
,
}
;
}
private
static
void
RestoreRuntimeDataObjects
(
SharedObjectsState
storage
)
{
var
extensions
=
storage
.
Extensions
;
var
internalStores
=
storage
.
InternalStores
;
var
contexts
=
storage
.
Contexts
;
foreach
(
var
extension
in
extensions
)
{
contexts
.
TryGetValue
(
extension
.
Key
,
out
var
context
)
;
extension
.
Value
.
Load
(
extension
.
Key
,
context
)
;
}
foreach
(
var
clrObj
in
internalStores
)
{
clrObj
.
Value
.
Load
(
clrObj
.
Key
,
null
)
;
}
if
(
WrappersStorer
!=
null
)
{
var
wrapperStorage
=
storage
.
Wrappers
;
var
handle2Obj
=
WrappersStorer
.
Restore
(
wrapperStorage
)
;
foreach
(
var
item
in
handle2Obj
)
{
object
obj
=
item
.
Instance
;
foreach
(
var
pyRef
in
item
.
PyRefs
??
new
List
<
PyObject
>
(
)
)
{
var
context
=
contexts
[
pyRef
]
;
CLRObject
.
Restore
(
obj
,
pyRef
,
context
)
;
}
}
}
}
static
readonly
string
serialization_key_namepsace
=
"pythonnet_serialization_"
;
/// <summary>
/// Removes the serialization capsule from the `sys` module object.
/// </summary>
/// <remarks>
/// The serialization data must have been set with <code>StashSerializationData</code>
/// </remarks>
/// <param name="key">The name given to the capsule on the `sys` module object</param>
public
static
void
FreeSerializationData
(
string
key
)
{
key
=
serialization_key_namepsace
+
key
;
BorrowedReference
oldCapsule
=
PySys_GetObject
(
key
)
;
if
(
!
oldCapsule
.
IsNull
)
{
IntPtr
oldData
=
PyCapsule_GetPointer
(
oldCapsule
,
IntPtr
.
Zero
)
;
Marshal
.
FreeHGlobal
(
oldData
)
;
PyCapsule_SetPointer
(
oldCapsule
,
IntPtr
.
Zero
)
;
PySys_SetObject
(
key
,
null
)
;
}
}
/// <summary>
/// Stores the data in the <paramref name="stream"/> argument in a Python capsule and stores
/// the capsule on the `sys` module object with the name <paramref name="key"/>.
/// </summary>
/// <remarks>
/// No checks on pre-existing names on the `sys` module object are made.
/// </remarks>
/// <param name="key">The name given to the capsule on the `sys` module object</param>
/// <param name="stream">A MemoryStream that contains the data to be placed in the capsule</param>
public
static
void
StashSerializationData
(
string
key
,
MemoryStream
stream
)
{
if
(
stream
.
TryGetBuffer
(
out
var
data
)
)
{
IntPtr
mem
=
Marshal
.
AllocHGlobal
(
IntPtr
.
Size
+
data
.
Count
)
;
// store the length of the buffer first
Marshal
.
WriteIntPtr
(
mem
,
(
IntPtr
)
data
.
Count
)
;
Marshal
.
Copy
(
data
.
Array
,
data
.
Offset
,
mem
+
IntPtr
.
Size
,
data
.
Count
)
;
try
{
using
NewReference
capsule
=
PyCapsule_New
(
mem
,
IntPtr
.
Zero
,
IntPtr
.
Zero
)
;
int
res
=
PySys_SetObject
(
key
,
capsule
.
BorrowOrThrow
(
)
)
;
PythonException
.
ThrowIfIsNotZero
(
res
)
;
}
catch
{
Marshal
.
FreeHGlobal
(
mem
)
;
}
}
else
{
throw
new
NotImplementedException
(
$
"
{
nameof
(
stream
)
}
must be exposable"
)
;
}
}
static
byte
[
]
emptyBuffer
=
new
byte
[
0
]
;
/// <summary>
/// Retreives the previously stored data on a Python capsule.
/// Throws if the object corresponding to the <paramref name="key"/> parameter
/// on the `sys` module object is not a capsule.
/// </summary>
/// <param name="key">The name given to the capsule on the `sys` module object</param>
/// <returns>A MemoryStream containing the previously saved serialization data.
/// The stream is empty if no name matches the key. </returns>
public
static
MemoryStream
GetSerializationData
(
string
key
)
{
BorrowedReference
capsule
=
PySys_GetObject
(
key
)
;
if
(
capsule
.
IsNull
)
{
// nothing to do.
return
new
MemoryStream
(
emptyBuffer
,
writable
:
false
)
;
}
var
ptr
=
PyCapsule_GetPointer
(
capsule
,
IntPtr
.
Zero
)
;
if
(
ptr
==
IntPtr
.
Zero
)
{
// The PyCapsule API returns NULL on error; NULL cannot be stored
// as a capsule's value
PythonException
.
ThrowIfIsNull
(
null
)
;
}
var
len
=
(
int
)
Marshal
.
ReadIntPtr
(
ptr
)
;
byte
[
]
buffer
=
new
byte
[
len
]
;
Marshal
.
Copy
(
ptr
+
IntPtr
.
Size
,
buffer
,
0
,
len
)
;
return
new
MemoryStream
(
buffer
,
writable
:
false
)
;
}
internal
static
IFormatter
CreateFormatter
(
)
{
if
(
FormatterType
!=
null
)
{
return
(
IFormatter
)
Activator
.
CreateInstance
(
FormatterType
)
;
}
return
FormatterFactory
(
)
;
}
}
}
Back
|
FazBrowse Home
|
New Git URL