Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 13ce409

Browse files
committed
Some code review changes
* Revert line endings change in Python.Runtime.csproj * Split maybe serialize into respective class files * Name changes for consistency
1 parent 8aba1da commit 13ce409

File tree

9 files changed

+256
-243
lines changed

9 files changed

+256
-243
lines changed

src/runtime/Python.Runtime.csproj

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,46 +29,46 @@
2929
<PlatformTarget>x64</PlatformTarget>
3030
</PropertyGroup>-->
3131
<PropertyGroup Condition=" '$(Configuration)' == 'ReleaseMono'">
32-
<DefineConstants Condition="'$(DefineConstants)' == ''">PYTHON2;PYTHON27</DefineConstants>
32+
<DefineConstants Condition="'$(DefineConstants)' == ''">PYTHON2;PYTHON27;UCS4</DefineConstants>
3333
<Optimize>true</Optimize>
3434
<DebugType>pdbonly</DebugType>
3535
</PropertyGroup>
3636
<PropertyGroup Condition=" '$(Configuration)' == 'ReleaseMonoPY3'">
37-
<DefineConstants Condition="'$(DefineConstants)' == ''">PYTHON3;PYTHON38</DefineConstants>
37+
<DefineConstants Condition="'$(DefineConstants)' == ''">PYTHON3;PYTHON38;UCS4</DefineConstants>
3838
<Optimize>true</Optimize>
3939
<DebugType>pdbonly</DebugType>
4040
</PropertyGroup>
4141
<PropertyGroup Condition=" '$(Configuration)' == 'DebugMono'">
4242
<DebugSymbols>true</DebugSymbols>
43-
<DefineConstants Condition="'$(DefineConstants)' == ''">PYTHON2;PYTHON27;TRACE;DEBUG</DefineConstants>
43+
<DefineConstants Condition="'$(DefineConstants)' == ''">PYTHON2;PYTHON27;UCS4;TRACE;DEBUG</DefineConstants>
4444
<Optimize>false</Optimize>
4545
<DebugType>full</DebugType>
4646
</PropertyGroup>
4747
<PropertyGroup Condition=" '$(Configuration)' == 'DebugMonoPY3'">
4848
<DebugSymbols>true</DebugSymbols>
49-
<DefineConstants Condition="'$(DefineConstants)' == ''">PYTHON3;PYTHON38;TRACE;DEBUG</DefineConstants>
49+
<DefineConstants Condition="'$(DefineConstants)' == ''">PYTHON3;PYTHON38;UCS4;TRACE;DEBUG</DefineConstants>
5050
<Optimize>false</Optimize>
5151
<DebugType>full</DebugType>
5252
</PropertyGroup>
5353
<PropertyGroup Condition=" '$(Configuration)' == 'ReleaseWin'">
54-
<DefineConstants Condition="'$(DefineConstants)' == ''">PYTHON2;PYTHON27</DefineConstants>
54+
<DefineConstants Condition="'$(DefineConstants)' == ''">PYTHON2;PYTHON27;UCS2</DefineConstants>
5555
<Optimize>true</Optimize>
5656
<DebugType>pdbonly</DebugType>
5757
</PropertyGroup>
5858
<PropertyGroup Condition=" '$(Configuration)' == 'ReleaseWinPY3'">
59-
<DefineConstants Condition="'$(DefineConstants)' == ''">PYTHON3;PYTHON38</DefineConstants>
59+
<DefineConstants Condition="'$(DefineConstants)' == ''">PYTHON3;PYTHON38;UCS2</DefineConstants>
6060
<Optimize>true</Optimize>
6161
<DebugType>pdbonly</DebugType>
6262
</PropertyGroup>
6363
<PropertyGroup Condition=" '$(Configuration)' == 'DebugWin'">
6464
<DebugSymbols>true</DebugSymbols>
65-
<DefineConstants Condition="'$(DefineConstants)' == ''">PYTHON2;PYTHON27;TRACE;DEBUG</DefineConstants>
65+
<DefineConstants Condition="'$(DefineConstants)' == ''">PYTHON2;PYTHON27;UCS2;TRACE;DEBUG</DefineConstants>
6666
<Optimize>false</Optimize>
6767
<DebugType>full</DebugType>
6868
</PropertyGroup>
6969
<PropertyGroup Condition=" '$(Configuration)' == 'DebugWinPY3'">
7070
<DebugSymbols>true</DebugSymbols>
71-
<DefineConstants Condition="'$(DefineConstants)' == ''">PYTHON3;PYTHON38;TRACE;DEBUG</DefineConstants>
71+
<DefineConstants Condition="'$(DefineConstants)' == ''">PYTHON3;PYTHON38;UCS2;TRACE;DEBUG</DefineConstants>
7272
<Optimize>false</Optimize>
7373
<DebugType>full</DebugType>
7474
</PropertyGroup>
@@ -80,10 +80,6 @@
8080
<Compile Include="Codecs\DecoderGroup.cs" />
8181
<Compile Include="Codecs\RawProxyEncoder.cs" />
8282
<Compile Include="Codecs\TupleCodecs.cs" />
83-
<Compile Include="native\ABI.cs" />
84-
<Compile Include="native\GeneratedTypeOffsets.cs" />
85-
<Compile Include="native\ITypeOffsets.cs" />
86-
<Compile Include="native\TypeOffset.cs" />
8783
<Compile Include="converterextensions.cs" />
8884
<Compile Include="finalizer.cs" />
8985
<Compile Include="intern.cs" />
@@ -165,6 +161,9 @@
165161
<Compile Include="polyfill\ReflectionPolifills.cs" />
166162
<Compile Include="platform\NativeCodePage.cs" />
167163
<Compile Include="slots\mp_length.cs" />
164+
<Compile Include="StateSerialization\MaybeType.cs" />
165+
<Compile Include="StateSerialization\MaybeMethodInfo.cs" />
166+
<Compile Include="StateSerialization\MaybeMemberInfo.cs" />
168167
</ItemGroup>
169168
<ItemGroup Condition=" '$(PythonInteropFile)' != '' ">
170169
<Compile Include="$(PythonInteropFile)" />
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
using System;
2+
using System.Reflection;
3+
using System.Runtime.Serialization;
4+
using System.Runtime.Serialization.Formatters.Binary;
5+
using System.IO;
6+
7+
namespace Python.Runtime
8+
{
9+
[Serializable]
10+
internal struct MaybeMemberInfo<T> : ISerializable where T: MemberInfo
11+
{
12+
public static implicit operator MaybeMemberInfo<T> (T ob) => new MaybeMemberInfo<T>(ob);
13+
14+
string m_name;
15+
MemberInfo m_info;
16+
17+
// As seen in ClassManager.GetClassInfo
18+
const BindingFlags k_flags = BindingFlags.Static |
19+
BindingFlags.Instance |
20+
BindingFlags.Public ;
21+
22+
public string DeletedMessage
23+
{
24+
get
25+
{
26+
return $"The .NET {typeof(T)} {m_name} no longer exists";
27+
}
28+
}
29+
30+
public T Value
31+
{
32+
get
33+
{
34+
if (m_info == null)
35+
{
36+
throw new SerializationException(DeletedMessage);
37+
}
38+
return (T)m_info;
39+
}
40+
}
41+
42+
public string Name {get{return m_name;}}
43+
public bool Valid => m_info != null;
44+
45+
public override string ToString()
46+
{
47+
return (m_info != null ? m_info.ToString() : $"missing type: {m_name}");
48+
}
49+
50+
public MaybeMemberInfo(T fi)
51+
{
52+
m_info = fi;
53+
m_name = m_info?.ToString();
54+
}
55+
56+
internal MaybeMemberInfo(SerializationInfo info, StreamingContext context)
57+
{
58+
// Assumption: name is always stored in "s"
59+
m_name = info.GetString("s");
60+
m_info = null;
61+
try
62+
{
63+
var tp = Type.GetType(info.GetString("t"));
64+
if (tp != null)
65+
{
66+
var field_name = info.GetString("f");
67+
m_info = tp.GetField(field_name, k_flags);
68+
}
69+
}
70+
catch
71+
{
72+
}
73+
}
74+
75+
public void GetObjectData(SerializationInfo info, StreamingContext context)
76+
{
77+
info.AddValue("s", m_name);
78+
if (Valid)
79+
{
80+
info.AddValue("f", m_info.Name);
81+
info.AddValue("t", m_info.ReflectedType.AssemblyQualifiedName);
82+
}
83+
}
84+
}
85+
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
using System;
2+
using System.Reflection;
3+
using System.Runtime.Serialization;
4+
using System.Runtime.Serialization.Formatters.Binary;
5+
using System.IO;
6+
7+
namespace Python.Runtime
8+
{
9+
[Serializable]
10+
internal struct MaybeMethodBase<T> : ISerializable where T: MethodBase
11+
{
12+
public static implicit operator MaybeMethodBase<T> (T ob) => new MaybeMethodBase<T>(ob);
13+
14+
string m_name;
15+
MethodBase m_info;
16+
// As seen in ClassManager.GetClassInfo
17+
const BindingFlags k_flags = BindingFlags.Static |
18+
BindingFlags.Instance |
19+
BindingFlags.Public ;
20+
21+
public T Value
22+
{
23+
get
24+
{
25+
if (m_info == null)
26+
{
27+
throw new SerializationException($"The .NET {typeof(T)} {m_name} no longer exists");
28+
}
29+
return (T)m_info;
30+
}
31+
}
32+
33+
public T UnsafeValue { get { return (T)m_info; } }
34+
public string Name {get{return m_name;}}
35+
public bool Valid => m_info != null;
36+
37+
public override string ToString()
38+
{
39+
return (m_info != null ? m_info.ToString() : $"missing method info: {m_name}");
40+
}
41+
42+
public MaybeMethod(T mi)
43+
{
44+
m_info = mi;
45+
m_name = mi?.ToString();
46+
}
47+
48+
internal MaybeMethod(SerializationInfo info, StreamingContext context)
49+
{
50+
m_name = info.GetString("s");
51+
m_info = null;
52+
try
53+
{
54+
// Retrive the reflected type of the method;
55+
var tp = Type.GetType(info.GetString("t"));
56+
// Get the method's parameters types
57+
var field_name = info.GetString("f");
58+
var param = (string[])info.GetValue("p", typeof(string[]));
59+
Type[] types = new Type[param.Length];
60+
for (int i = 0; i < param.Length; i++)
61+
{
62+
types[i] = Type.GetType(param[i]);
63+
}
64+
// Try to get the method
65+
m_info = tp.GetMethod(field_name, k_flags, binder:null, types:types, modifiers:null);
66+
// Try again, may be a constructor
67+
if (m_info == null && m_name.Contains(".ctor"))
68+
{
69+
m_info = tp.GetConstructor(k_flags, binder:null, types:types, modifiers:null);
70+
}
71+
}
72+
catch
73+
{
74+
}
75+
}
76+
77+
public void GetObjectData(SerializationInfo info, StreamingContext context)
78+
{
79+
info.AddValue("s", m_name);
80+
if (Valid)
81+
{
82+
info.AddValue("f", m_info.Name);
83+
info.AddValue("t", m_info.ReflectedType.AssemblyQualifiedName);
84+
var p = m_info.GetParameters();
85+
string[] types = new string[p.Length];
86+
for (int i = 0; i < p.Length; i++)
87+
{
88+
types[i] = p[i].ParameterType.AssemblyQualifiedName;
89+
}
90+
info.AddValue("p", types, typeof(string[]));
91+
}
92+
}
93+
}
94+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
using System;
2+
using System.Reflection;
3+
using System.Runtime.Serialization;
4+
using System.Runtime.Serialization.Formatters.Binary;
5+
using System.IO;
6+
7+
namespace Python.Runtime
8+
{
9+
[Serializable]
10+
internal struct MaybeType : ISerializable
11+
{
12+
public static implicit operator MaybeType (Type ob) => new MaybeType(ob);
13+
14+
string m_name;
15+
Type m_type;
16+
17+
public string DeletedMessage
18+
{
19+
get
20+
{
21+
return $"The .NET Type {m_name} no longer exists";
22+
}
23+
}
24+
25+
public Type Value
26+
{
27+
get
28+
{
29+
if (m_type == null)
30+
{
31+
throw new SerializationException(DeletedMessage);
32+
}
33+
return m_type;
34+
}
35+
}
36+
37+
public string Name {get{return m_name;}}
38+
public bool Valid => m_type != null;
39+
40+
public override string ToString()
41+
{
42+
return (m_type != null ? m_type.ToString() : $"missing type: {m_name}");
43+
}
44+
45+
public MaybeType(Type tp)
46+
{
47+
m_type = tp;
48+
m_name = tp.AssemblyQualifiedName;
49+
}
50+
51+
private MaybeType(SerializationInfo info, StreamingContext context)
52+
{
53+
m_name = (string)info.GetValue("n", typeof(string));
54+
m_type = Type.GetType(m_name, throwOnError:false);
55+
}
56+
57+
public void GetObjectData(SerializationInfo info, StreamingContext context)
58+
{
59+
info.AddValue("n", m_name);
60+
}
61+
}
62+
}

0 commit comments

Comments
 (0)