This repository was archived by the owner on Jul 22, 2023. It is now read-only.
forked from pythonnet/pythonnet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconverterextensions.cs
More file actions
189 lines (163 loc) · 6.6 KB
/
converterextensions.cs
File metadata and controls
189 lines (163 loc) · 6.6 KB
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
namespace Python.Runtime
{
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Python.Runtime.Codecs;
/// <summary>
/// Defines <see cref="PyObject"/> conversion to CLR types (unmarshalling)
/// </summary>
public interface IPyObjectDecoder
{
/// <summary>
/// Checks if this decoder can decode from <paramref name="objectType"/> to <paramref name="targetType"/>
/// </summary>
bool CanDecode(PyObject objectType, Type targetType);
/// <summary>
/// Attempts do decode <paramref name="pyObj"/> into a variable of specified type
/// </summary>
/// <typeparam name="T">CLR type to decode into</typeparam>
/// <param name="pyObj">Object to decode</param>
/// <param name="value">The variable, that will receive decoding result</param>
/// <returns></returns>
bool TryDecode<T>(PyObject pyObj, out T value);
}
/// <summary>
/// Defines conversion from CLR objects into Python objects (e.g. <see cref="PyObject"/>) (marshalling)
/// </summary>
public interface IPyObjectEncoder
{
/// <summary>
/// Checks if encoder can encode CLR objects of specified type
/// </summary>
bool CanEncode(Type type);
/// <summary>
/// Attempts to encode CLR object <paramref name="value"/> into Python object
/// </summary>
PyObject TryEncode(object value);
}
/// <summary>
/// This class allows to register additional marshalling codecs.
/// <para>Python.NET will pick suitable encoder/decoder registered first</para>
/// </summary>
public static class PyObjectConversions
{
static readonly DecoderGroup decoders = new DecoderGroup();
static readonly EncoderGroup encoders = new EncoderGroup();
/// <summary>
/// Registers specified encoder (marshaller)
/// <para>Python.NET will pick suitable encoder/decoder registered first</para>
/// </summary>
public static void RegisterEncoder(IPyObjectEncoder encoder)
{
if (encoder == null) throw new ArgumentNullException(nameof(encoder));
lock (encoders)
{
encoders.Add(encoder);
}
}
/// <summary>
/// Registers specified decoder (unmarshaller)
/// <para>Python.NET will pick suitable encoder/decoder registered first</para>
/// </summary>
public static void RegisterDecoder(IPyObjectDecoder decoder)
{
if (decoder == null) throw new ArgumentNullException(nameof(decoder));
lock (decoders)
{
decoders.Add(decoder);
}
}
#region Encoding
internal static PyObject TryEncode(object obj, Type type)
{
if (obj == null) throw new ArgumentNullException(nameof(obj));
if (type == null) throw new ArgumentNullException(nameof(type));
foreach (var encoder in clrToPython.GetOrAdd(type, GetEncoders))
{
var result = encoder.TryEncode(obj);
if (result != null) return result;
}
return null;
}
static readonly ConcurrentDictionary<Type, IPyObjectEncoder[]>
clrToPython = new ConcurrentDictionary<Type, IPyObjectEncoder[]>();
static IPyObjectEncoder[] GetEncoders(Type type)
{
lock (encoders)
{
return encoders.GetEncoders(type).ToArray();
}
}
#endregion
#region Decoding
static readonly ConcurrentDictionary<TypePair, Converter.TryConvertFromPythonDelegate>
pythonToClr = new ConcurrentDictionary<TypePair, Converter.TryConvertFromPythonDelegate>();
internal static bool TryDecode(IntPtr pyHandle, IntPtr pyType, Type targetType, out object result)
{
if (pyHandle == IntPtr.Zero) throw new ArgumentNullException(nameof(pyHandle));
if (pyType == IntPtr.Zero) throw new ArgumentNullException(nameof(pyType));
if (targetType == null) throw new ArgumentNullException(nameof(targetType));
var decoder = pythonToClr.GetOrAdd(new TypePair(pyType, targetType), pair => GetDecoder(pair.PyType, pair.ClrType));
result = null;
if (decoder == null) return false;
return decoder.Invoke(pyHandle, out result);
}
static Converter.TryConvertFromPythonDelegate GetDecoder(IntPtr sourceType, Type targetType)
{
IPyObjectDecoder decoder;
using (var pyType = new PyObject(Runtime.SelfIncRef(sourceType)))
{
lock (decoders)
{
decoder = decoders.GetDecoder(pyType, targetType);
if (decoder == null) return null;
}
}
var decode = genericDecode.MakeGenericMethod(targetType);
bool TryDecode(IntPtr pyHandle, out object result)
{
var pyObj = new PyObject(Runtime.SelfIncRef(pyHandle));
var @params = new object[] { pyObj, null };
bool success = (bool)decode.Invoke(decoder, @params);
if (!success)
{
pyObj.Dispose();
}
result = @params[1];
return success;
}
return TryDecode;
}
static readonly MethodInfo genericDecode = typeof(IPyObjectDecoder).GetMethod(nameof(IPyObjectDecoder.TryDecode));
#endregion
internal static void Reset()
{
lock (encoders)
lock (decoders)
{
clrToPython.Clear();
pythonToClr.Clear();
encoders.Clear();
decoders.Clear();
}
}
struct TypePair : IEquatable<TypePair>
{
internal readonly IntPtr PyType;
internal readonly Type ClrType;
public TypePair(IntPtr pyType, Type clrType)
{
this.PyType = pyType;
this.ClrType = clrType;
}
public override int GetHashCode()
=> this.ClrType.GetHashCode() ^ this.PyType.GetHashCode();
public bool Equals(TypePair other)
=> this.PyType == other.PyType && this.ClrType == other.ClrType;
public override bool Equals(object obj) => obj is TypePair other && this.Equals(other);
}
}
}