Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit3cd685e

Browse files
committed
Use the BinaryFormatter only on .NET FW and Mono
1 parentdfd746b commit3cd685e

File tree

3 files changed

+30
-28
lines changed

3 files changed

+30
-28
lines changed

‎src/embed_tests/StateSerialization/MethodSerialization.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ static T SerializationRoundtrip<T>(T item)
3333
{
3434
usingvarbuf=newMemoryStream();
3535
varformatter=RuntimeData.CreateFormatter();
36+
37+
if(formatter==null)
38+
Assert.Inconclusive("Failed to create formatter for state serialization");
39+
3640
formatter.Serialize(buf,item);
3741
buf.Position=0;
3842
return(T)formatter.Deserialize(buf);

‎src/runtime/StateSerialization/NoopFormatter.cs

Lines changed: 0 additions & 14 deletions
This file was deleted.

‎src/runtime/StateSerialization/RuntimeData.cs

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,23 @@ namespace Python.Runtime
1616
publicstaticclassRuntimeData
1717
{
1818

19-
publicreadonlystaticFunc<IFormatter>DefaultFormatterFactory=()=>
19+
publicreadonlystaticFunc<IFormatter?>DefaultFormatterFactory=()=>
2020
{
2121
try
2222
{
23-
returnnewBinaryFormatter();
24-
}
25-
catch
26-
{
27-
returnnewNoopFormatter();
23+
varfw=RuntimeInformation.FrameworkDescription;
24+
if(fw.StartsWith(".NET Framework")||fw.StartsWith("Mono"))
25+
{
26+
returnnewBinaryFormatter();
27+
}
2828
}
29+
catch{}
30+
returnnull;
2931
};
3032

31-
privatestaticFunc<IFormatter>_formatterFactory{get;set;}=DefaultFormatterFactory;
33+
privatestaticFunc<IFormatter?>_formatterFactory{get;set;}=DefaultFormatterFactory;
3234

33-
publicstaticFunc<IFormatter>FormatterFactory
35+
publicstaticFunc<IFormatter?>FormatterFactory
3436
{
3537
get=>_formatterFactory;
3638
set
@@ -82,6 +84,14 @@ static void ClearCLRData ()
8284

8385
internalstaticvoidStash()
8486
{
87+
ClearCLRData();
88+
89+
IFormatter?formatter=CreateFormatter();
90+
91+
if(formatter==null)
92+
// No formatter defined, exit early
93+
return;
94+
8595
varruntimeStorage=newPythonNetState
8696
{
8797
Metatype=MetaType.SaveRuntimeData(),
@@ -91,7 +101,6 @@ internal static void Stash()
91101
SharedObjects=SaveRuntimeDataObjects(),
92102
};
93103

94-
IFormatterformatter=CreateFormatter();
95104
varms=newMemoryStream();
96105
formatter.Serialize(ms,runtimeStorage);
97106

@@ -102,11 +111,10 @@ internal static void Stash()
102111
Marshal.WriteIntPtr(mem,(IntPtr)ms.Length);
103112
Marshal.Copy(data,0,mem+IntPtr.Size,(int)ms.Length);
104113

105-
ClearCLRData();
106-
107114
usingNewReferencecapsule=PyCapsule_New(mem,IntPtr.Zero,IntPtr.Zero);
108115
intres=PySys_SetObject("clr_data",capsule.BorrowOrThrow());
109116
PythonException.ThrowIfIsNotZero(res);
117+
110118
PostStashHook?.Invoke();
111119
}
112120

@@ -124,6 +132,12 @@ internal static void RestoreRuntimeData()
124132

125133
privatestaticvoidRestoreRuntimeDataImpl()
126134
{
135+
IFormatter?formatter=CreateFormatter();
136+
137+
if(formatter==null)
138+
// No formatter defined, exit early
139+
return;
140+
127141
PreRestoreHook?.Invoke();
128142
BorrowedReferencecapsule=PySys_GetObject("clr_data");
129143
if(capsule.IsNull)
@@ -135,7 +149,6 @@ private static void RestoreRuntimeDataImpl()
135149
byte[]data=newbyte[length];
136150
Marshal.Copy(mem+IntPtr.Size,data,0,length);
137151
varms=newMemoryStream(data);
138-
varformatter=CreateFormatter();
139152
varstorage=(PythonNetState)formatter.Deserialize(ms);
140153

141154
PyCLRMetaType=MetaType.RestoreRuntimeData(storage.Metatype);
@@ -373,9 +386,8 @@ public static MemoryStream GetSerializationData(string key)
373386
returnnewMemoryStream(buffer,writable:false);
374387
}
375388

376-
internalstaticIFormatterCreateFormatter()
389+
internalstaticIFormatter?CreateFormatter()
377390
{
378-
379391
if(FormatterType!=null)
380392
{
381393
return(IFormatter)Activator.CreateInstance(FormatterType);

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp