Skip to content

RawProxyEncoder #1122

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions src/embed_tests/CodecGroups.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ public class CodecGroups
[Test]
public void GetEncodersByType()
{
var encoder1 = new ObjectToRawProxyEncoder<Uri>();
var encoder2 = new ObjectToRawProxyEncoder<Uri>();
var encoder1 = new ObjectToEncoderInstanceEncoder<Uri>();
var encoder2 = new ObjectToEncoderInstanceEncoder<Uri>();
var group = new EncoderGroup {
new ObjectToRawProxyEncoder<Tuple<int>>(),
new ObjectToEncoderInstanceEncoder<Tuple<int>>(),
encoder1,
encoder2,
};
Expand All @@ -27,8 +27,8 @@ public void GetEncodersByType()
public void CanEncode()
{
var group = new EncoderGroup {
new ObjectToRawProxyEncoder<Tuple<int>>(),
new ObjectToRawProxyEncoder<Uri>(),
new ObjectToEncoderInstanceEncoder<Tuple<int>>(),
new ObjectToEncoderInstanceEncoder<Uri>(),
};

Assert.IsTrue(group.CanEncode(typeof(Tuple<int>)));
Expand All @@ -39,9 +39,9 @@ public void CanEncode()
[Test]
public void Encodes()
{
var encoder0 = new ObjectToRawProxyEncoder<Tuple<int>>();
var encoder1 = new ObjectToRawProxyEncoder<Uri>();
var encoder2 = new ObjectToRawProxyEncoder<Uri>();
var encoder0 = new ObjectToEncoderInstanceEncoder<Tuple<int>>();
var encoder1 = new ObjectToEncoderInstanceEncoder<Uri>();
var encoder2 = new ObjectToEncoderInstanceEncoder<Uri>();
var group = new EncoderGroup {
encoder0,
encoder1,
Expand Down
4 changes: 2 additions & 2 deletions src/embed_tests/Codecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,9 @@ static void TupleRoundtripGeneric<T, TTuple>() {

/// <summary>
/// "Decodes" only objects of exact type <typeparamref name="T"/>.
/// Result is just a raw Python object proxy.
/// Result is just the raw proxy to the encoder instance itself.
/// </summary>
class ObjectToRawProxyEncoder<T> : IPyObjectEncoder
class ObjectToEncoderInstanceEncoder<T> : IPyObjectEncoder
{
public bool CanEncode(Type type) => type == typeof(T);
public PyObject TryEncode(object value) => this.GetRawPythonProxy();
Expand Down
21 changes: 21 additions & 0 deletions src/runtime/Codecs/RawProxyEncoder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System;

namespace Python.Runtime.Codecs
{
/// <summary>
/// A .NET object encoder, that returns raw proxies (e.g. no conversion to Python types).
/// <para>You must inherit from this class and override <see cref="CanEncode"/>.</para>
/// </summary>
[Obsolete(Util.UnstableApiMessage)]
public class RawProxyEncoder: IPyObjectEncoder
{
public PyObject TryEncode(object value)
{
if (value is null) throw new ArgumentNullException(nameof(value));

return value.GetRawPythonProxy();
}

public virtual bool CanEncode(Type type) => false;
}
}
1 change: 1 addition & 0 deletions src/runtime/Python.Runtime.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
<ItemGroup>
<Compile Include="Codecs\EncoderGroup.cs" />
<Compile Include="Codecs\DecoderGroup.cs" />
<Compile Include="Codecs\RawProxyEncoder.cs" />
<Compile Include="Codecs\TupleCodecs.cs" />
<Compile Include="converterextensions.cs" />
<Compile Include="finalizer.cs" />
Expand Down
5 changes: 4 additions & 1 deletion src/testing/conversiontest.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
namespace Python.Test
{
using System.Collections.Generic;

/// <summary>
/// Supports units tests for field access.
/// Supports unit tests for field access.
/// </summary>
public class ConversionTest
{
Expand Down Expand Up @@ -32,6 +34,7 @@ public ConversionTest()

public byte[] ByteArrayField;
public sbyte[] SByteArrayField;
public readonly List<int> ListField = new List<int>();

public T? Echo<T>(T? arg) where T: struct {
return arg;
Expand Down
18 changes: 18 additions & 0 deletions src/tests/test_conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import System
import pytest
from Python.Test import ConversionTest, UnicodeString
from Python.Runtime import PyObjectConversions
from Python.Runtime.Codecs import RawProxyEncoder

from ._compat import indexbytes, long, unichr, text_type, PY2, PY3

Expand Down Expand Up @@ -700,3 +702,19 @@ def test_sbyte_array_conversion():
array = ob.SByteArrayField
for i, _ in enumerate(value):
assert array[i] == indexbytes(value, i)

def test_codecs():
"""Test codec registration from Python"""
class ListAsRawEncoder(RawProxyEncoder):
__namespace__ = "Python.Test"
def CanEncode(self, clr_type):
return clr_type.Name == "List`1" and clr_type.Namespace == "System.Collections.Generic"

list_raw_encoder = ListAsRawEncoder()
PyObjectConversions.RegisterEncoder(list_raw_encoder)

ob = ConversionTest()

l = ob.ListField
l.Add(42)
assert ob.ListField.Count == 1