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 pathCodecs.cs
More file actions
123 lines (111 loc) · 4.62 KB
/
Codecs.cs
File metadata and controls
123 lines (111 loc) · 4.62 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
namespace Python.EmbeddingTest {
using System;
using System.Collections.Generic;
using System.Text;
using NUnit.Framework;
using Python.Runtime;
using Python.Runtime.Codecs;
public class Codecs {
[SetUp]
public void SetUp() {
PythonEngine.Initialize();
}
[TearDown]
public void Dispose() {
PythonEngine.Shutdown();
}
[Test]
public void ConversionsGeneric() {
ConversionsGeneric<ValueTuple<int, string, object>, ValueTuple>();
}
static void ConversionsGeneric<T, TTuple>() {
TupleCodec<TTuple>.Register();
var tuple = Activator.CreateInstance(typeof(T), 42, "42", new object());
T restored = default;
using (Py.GIL())
using (var scope = Py.CreateScope()) {
void Accept(T value) => restored = value;
var accept = new Action<T>(Accept).ToPython();
scope.Set(nameof(tuple), tuple);
scope.Set(nameof(accept), accept);
scope.Exec($"{nameof(accept)}({nameof(tuple)})");
Assert.AreEqual(expected: tuple, actual: restored);
}
}
[Test]
public void ConversionsObject() {
ConversionsObject<ValueTuple<int, string, object>, ValueTuple>();
}
static void ConversionsObject<T, TTuple>() {
TupleCodec<TTuple>.Register();
var tuple = Activator.CreateInstance(typeof(T), 42, "42", new object());
T restored = default;
using (Py.GIL())
using (var scope = Py.CreateScope()) {
void Accept(object value) => restored = (T)value;
var accept = new Action<object>(Accept).ToPython();
scope.Set(nameof(tuple), tuple);
scope.Set(nameof(accept), accept);
scope.Exec($"{nameof(accept)}({nameof(tuple)})");
Assert.AreEqual(expected: tuple, actual: restored);
}
}
[Test]
public void TupleRoundtripObject() {
TupleRoundtripObject<ValueTuple<int, string, object>, ValueTuple>();
}
static void TupleRoundtripObject<T, TTuple>() {
var tuple = Activator.CreateInstance(typeof(T), 42, "42", new object());
using (Py.GIL()) {
var pyTuple = TupleCodec<TTuple>.Instance.TryEncode(tuple);
Assert.IsTrue(TupleCodec<TTuple>.Instance.TryDecode(pyTuple, out object restored));
Assert.AreEqual(expected: tuple, actual: restored);
}
}
[Test]
public void TupleRoundtripGeneric() {
TupleRoundtripGeneric<ValueTuple<int, string, object>, ValueTuple>();
}
static void TupleRoundtripGeneric<T, TTuple>() {
var tuple = Activator.CreateInstance(typeof(T), 42, "42", new object());
using (Py.GIL()) {
var pyTuple = TupleCodec<TTuple>.Instance.TryEncode(tuple);
Assert.IsTrue(TupleCodec<TTuple>.Instance.TryDecode(pyTuple, out T restored));
Assert.AreEqual(expected: tuple, actual: restored);
}
}
}
/// <summary>
/// "Decodes" only objects of exact type <typeparamref name="T"/>.
/// Result is just the raw proxy to the encoder instance itself.
/// </summary>
class ObjectToEncoderInstanceEncoder<T> : IPyObjectEncoder
{
public bool CanEncode(Type type) => type == typeof(T);
public PyObject TryEncode(object value) => PyObject.FromManagedObject(this);
}
/// <summary>
/// Decodes object of specified Python type to the predefined value <see cref="DecodeResult"/>
/// </summary>
/// <typeparam name="TTarget">Type of the <see cref="DecodeResult"/></typeparam>
class DecoderReturningPredefinedValue<TTarget> : IPyObjectDecoder
{
public PyObject TheOnlySupportedSourceType { get; }
public TTarget DecodeResult { get; }
public DecoderReturningPredefinedValue(PyObject objectType, TTarget decodeResult)
{
this.TheOnlySupportedSourceType = objectType;
this.DecodeResult = decodeResult;
}
public bool CanDecode(PyObject objectType, Type targetType)
=> objectType.Handle == TheOnlySupportedSourceType.Handle
&& targetType == typeof(TTarget);
public bool TryDecode<T>(PyObject pyObj, out T value)
{
if (typeof(T) != typeof(TTarget))
throw new ArgumentException(nameof(T));
value = (T)(object)DecodeResult;
return true;
}
}
}