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

AddedToPythonAs() extension method for explicit conversion using an arbitrary type#2419

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
joaompneves wants to merge13 commits intopythonnet:master
base:master
Choose a base branch
Loading
fromjoaompneves:support-specific-type-encoding
Open
Show file tree
Hide file tree
Changes from1 commit
Commits
Show all changes
13 commits
Select commitHold shift + click to select a range
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
PrevPrevious commit
NextNext commit
Fix build
  • Loading branch information
@joaompneves
joaompneves committedJul 18, 2024
commit255201afbc81f382b74ebcb74ec9506ee67a83ba
4 changes: 2 additions & 2 deletionssrc/embed_tests/CodecGroups.cs
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -48,12 +48,12 @@ public void Encodes()
encoder2,
};

var uri = group.TryEncode(new Uri("data:"));
var uri = group.TryEncode(new Uri("data:"), typeof(Uri));
var clrObject = (CLRObject)ManagedType.GetManagedObject(uri);
Assert.AreSame(encoder1, clrObject.inst);
Assert.AreNotSame(encoder2, clrObject.inst);

var tuple = group.TryEncode(Tuple.Create(1));
var tuple = group.TryEncode(Tuple.Create(1), typeof(Tuple<int>));
clrObject = (CLRObject)ManagedType.GetManagedObject(tuple);
Assert.AreSame(encoder0, clrObject.inst);
}
Expand Down
8 changes: 4 additions & 4 deletionssrc/embed_tests/Codecs.cs
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -71,7 +71,7 @@ public void TupleRoundtripObject()
static void TupleRoundtripObject<T, TTuple>()
{
var tuple = Activator.CreateInstance(typeof(T), 42.0, "42", new object());
using var pyTuple = TupleCodec<TTuple>.Instance.TryEncode(tuple);
using var pyTuple = TupleCodec<TTuple>.Instance.TryEncode(tuple, typeof(T));
Assert.IsTrue(TupleCodec<TTuple>.Instance.TryDecode(pyTuple, out object restored));
Assert.AreEqual(expected: tuple, actual: restored);
}
Expand All@@ -85,7 +85,7 @@ public void TupleRoundtripGeneric()
static void TupleRoundtripGeneric<T, TTuple>()
{
var tuple = Activator.CreateInstance(typeof(T), 42, "42", new object());
using var pyTuple = TupleCodec<TTuple>.Instance.TryEncode(tuple);
using var pyTuple = TupleCodec<TTuple>.Instance.TryEncode(tuple, typeof(T));
Assert.IsTrue(TupleCodec<TTuple>.Instance.TryDecode(pyTuple, out T restored));
Assert.AreEqual(expected: tuple, actual: restored);
}
Expand DownExpand Up@@ -438,7 +438,7 @@ public bool TryDecode<T>(PyObject pyObj, out T value)
return true;
}

public PyObject TryEncode(object value)
public PyObject TryEncode(object value, Type type)
{
var error = (ValueErrorWrapper)value;
return PythonEngine.Eval("ValueError").Invoke(error.Message.ToPython());
Expand DownExpand Up@@ -478,7 +478,7 @@ public bool TryDecode<T>(PyObject pyObj, out T value)
class ObjectToEncoderInstanceEncoder<T> : IPyObjectEncoder
{
public bool CanEncode(Type type) => type == typeof(T);
public PyObject TryEncode(object value) => PyObject.FromManagedObject(this);
public PyObject TryEncode(object value, Type type) => PyObject.FromManagedObject(this);
}

/// <summary>
Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp