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
This repository was archived by the owner on Jul 22, 2023. It is now read-only.
/pythonnetPublic archive
forked frompythonnet/pythonnet

Commitf707698

Browse files
authored
Add RawProxyEncoder (pythonnet#1122)
Now Python host can force raw encoding for autoconverted .NET types.Enables workaround forpythonnet#514
1 parent8522b5f commitf707698

File tree

6 files changed

+54
-11
lines changed

6 files changed

+54
-11
lines changed

‎src/embed_tests/CodecGroups.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ public class CodecGroups
1111
[Test]
1212
publicvoidGetEncodersByType()
1313
{
14-
varencoder1=newObjectToRawProxyEncoder<Uri>();
15-
varencoder2=newObjectToRawProxyEncoder<Uri>();
14+
varencoder1=newObjectToEncoderInstanceEncoder<Uri>();
15+
varencoder2=newObjectToEncoderInstanceEncoder<Uri>();
1616
vargroup=newEncoderGroup{
17-
newObjectToRawProxyEncoder<Tuple<int>>(),
17+
newObjectToEncoderInstanceEncoder<Tuple<int>>(),
1818
encoder1,
1919
encoder2,
2020
};
@@ -27,8 +27,8 @@ public void GetEncodersByType()
2727
publicvoidCanEncode()
2828
{
2929
vargroup=newEncoderGroup{
30-
newObjectToRawProxyEncoder<Tuple<int>>(),
31-
newObjectToRawProxyEncoder<Uri>(),
30+
newObjectToEncoderInstanceEncoder<Tuple<int>>(),
31+
newObjectToEncoderInstanceEncoder<Uri>(),
3232
};
3333

3434
Assert.IsTrue(group.CanEncode(typeof(Tuple<int>)));
@@ -39,9 +39,9 @@ public void CanEncode()
3939
[Test]
4040
publicvoidEncodes()
4141
{
42-
varencoder0=newObjectToRawProxyEncoder<Tuple<int>>();
43-
varencoder1=newObjectToRawProxyEncoder<Uri>();
44-
varencoder2=newObjectToRawProxyEncoder<Uri>();
42+
varencoder0=newObjectToEncoderInstanceEncoder<Tuple<int>>();
43+
varencoder1=newObjectToEncoderInstanceEncoder<Uri>();
44+
varencoder2=newObjectToEncoderInstanceEncoder<Uri>();
4545
vargroup=newEncoderGroup{
4646
encoder0,
4747
encoder1,

‎src/embed_tests/Codecs.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,9 @@ static void TupleRoundtripGeneric<T, TTuple>() {
8686

8787
/// <summary>
8888
/// "Decodes" only objects of exact type <typeparamref name="T"/>.
89-
/// Result is justa rawPython object proxy.
89+
/// Result is justthe rawproxy to the encoder instance itself.
9090
/// </summary>
91-
classObjectToRawProxyEncoder<T>:IPyObjectEncoder
91+
classObjectToEncoderInstanceEncoder<T>:IPyObjectEncoder
9292
{
9393
publicboolCanEncode(Typetype)=>type==typeof(T);
9494
publicPyObjectTryEncode(objectvalue)=>this.GetRawPythonProxy();

‎src/runtime/Codecs/RawProxyEncoder.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
usingSystem;
2+
3+
namespacePython.Runtime.Codecs
4+
{
5+
/// <summary>
6+
/// A .NET object encoder, that returns raw proxies (e.g. no conversion to Python types).
7+
/// <para>You must inherit from this class and override <see cref="CanEncode"/>.</para>
8+
/// </summary>
9+
[Obsolete(Util.UnstableApiMessage)]
10+
publicclassRawProxyEncoder:IPyObjectEncoder
11+
{
12+
publicPyObjectTryEncode(objectvalue)
13+
{
14+
if(valueisnull)thrownewArgumentNullException(nameof(value));
15+
16+
returnvalue.GetRawPythonProxy();
17+
}
18+
19+
publicvirtualboolCanEncode(Typetype)=>false;
20+
}
21+
}

‎src/runtime/Python.Runtime.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@
7878
<ItemGroup>
7979
<CompileInclude="Codecs\EncoderGroup.cs" />
8080
<CompileInclude="Codecs\DecoderGroup.cs" />
81+
<CompileInclude="Codecs\RawProxyEncoder.cs" />
8182
<CompileInclude="Codecs\TupleCodecs.cs" />
8283
<CompileInclude="converterextensions.cs" />
8384
<CompileInclude="finalizer.cs" />

‎src/testing/conversiontest.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
namespacePython.Test
22
{
3+
usingSystem.Collections.Generic;
4+
35
/// <summary>
4-
/// Supportsunits tests for field access.
6+
/// Supportsunit tests for field access.
57
/// </summary>
68
publicclassConversionTest
79
{
@@ -32,6 +34,7 @@ public ConversionTest()
3234

3335
publicbyte[]ByteArrayField;
3436
publicsbyte[]SByteArrayField;
37+
publicreadonlyList<int>ListField=newList<int>();
3538

3639
publicT?Echo<T>(T?arg)whereT:struct{
3740
returnarg;

‎src/tests/test_conversion.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
importSystem
77
importpytest
88
fromPython.TestimportConversionTest,UnicodeString
9+
fromPython.RuntimeimportPyObjectConversions
10+
fromPython.Runtime.CodecsimportRawProxyEncoder
911

1012
from ._compatimportindexbytes,long,unichr,text_type,PY2,PY3
1113

@@ -700,3 +702,19 @@ def test_sbyte_array_conversion():
700702
array=ob.SByteArrayField
701703
fori,_inenumerate(value):
702704
assertarray[i]==indexbytes(value,i)
705+
706+
deftest_codecs():
707+
"""Test codec registration from Python"""
708+
classListAsRawEncoder(RawProxyEncoder):
709+
__namespace__="Python.Test"
710+
defCanEncode(self,clr_type):
711+
returnclr_type.Name=="List`1"andclr_type.Namespace=="System.Collections.Generic"
712+
713+
list_raw_encoder=ListAsRawEncoder()
714+
PyObjectConversions.RegisterEncoder(list_raw_encoder)
715+
716+
ob=ConversionTest()
717+
718+
l=ob.ListField
719+
l.Add(42)
720+
assertob.ListField.Count==1

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp