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 fromall commits
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
1 change: 1 addition & 0 deletionsAUTHORS.md
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -44,6 +44,7 @@
- Jan Krivanek ([@jakrivan](https://github.com/jakrivan))
- Jeff Reback ([@jreback](https://github.com/jreback))
- Jeff Robbins ([@jeff17robbins](https://github.com/jeff17robbins))
- João Neves ([@joaompneves](https://github.com/joaompneves))
- Joe Frayne ([@jfrayne](https://github.com/jfrayne))
- Joe Lidbetter ([@jmlidbetter](https://github.com/jmlidbetter))
- Joe Savage ([@s4v4g3](https://github.com/s4v4g3))
Expand Down
2 changes: 2 additions & 0 deletionsCHANGELOG.md
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -9,6 +9,8 @@ This document follows the conventions laid out in [Keep a CHANGELOG][].

### Added

- Added `ToPythonAs()` extension method to allow for explicit conversion using a specific type not known at build time. ([#2419][i2419])

- Added `ToPythonAs<T>()` extension method to allow for explicit conversion using a specific type. ([#2311][i2311])

- Added `IComparable` and `IEquatable` implementations to `PyInt`, `PyFloat`, and `PyString`
Expand Down
29 changes: 29 additions & 0 deletionssrc/embed_tests/Codecs.cs
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -339,6 +339,23 @@ public void ExceptionDecoded()
Assert.AreEqual(TestExceptionMessage, error.Message);
}

[Test(Description = "Tests encoding of an object that explicitly implements an interface.")]
public void ExplicitObjectInterfaceEncoded()
{
var obj = new ExplicitInterfaceObject();
using var scope = Py.CreateScope();
scope.Exec(@"
def call(obj):
return dir(obj)
");
var callFunc = scope.Get("call");
// explicitly pass an interface (but not a generic type argument) to simulate a scenario where the type is not know at build time
var callArg = obj.ToPythonAs(typeof(IObjectInterface));
var members = callFunc.Invoke(callArg).As<string[]>();
CollectionAssert.Contains(members, nameof(IObjectInterface.MemberFromInterface));
CollectionAssert.DoesNotContain(members, nameof(ExplicitInterfaceObject.MemberFromObject));
}

[Test]
public void DateTimeDecoded()
{
Expand DownExpand Up@@ -533,4 +550,16 @@ public bool TryDecode<T>(PyObject pyObj, out T value)
return true;
}
}

interface IObjectInterface
{
int MemberFromInterface { get; }
}

class ExplicitInterfaceObject : IObjectInterface
{
int IObjectInterface.MemberFromInterface { get; }

public int MemberFromObject { get; }
}
}
1 change: 1 addition & 0 deletionssrc/embed_tests/Python.EmbeddingTest.csproj
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -2,6 +2,7 @@

<PropertyGroup>
<TargetFrameworks>net472;net6.0</TargetFrameworks>
<RollForward>LatestMajor</RollForward>
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

This allows anyone that has recent versions of .net to run these tests. I can revert but I think its usefull, otherwise one needs to install .net6 which is already deprecated

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

I think this should be conditioned on not running in CI

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Is there any config/env var which I can check?

<AssemblyOriginatorKeyFile>..\pythonnet.snk</AssemblyOriginatorKeyFile>
<SignAssembly>true</SignAssembly>
</PropertyGroup>
Expand Down
7 changes: 6 additions & 1 deletionsrc/runtime/Converter.cs
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -983,9 +983,14 @@ public static PyObject ToPython(this object? o)
}

public static PyObject ToPythonAs<T>(this T? o)
{
return ToPythonAs(o, typeof(T));
}

public static PyObject ToPythonAs(this object? o, Type type)
{
if (o is null) return Runtime.None;
return Converter.ToPython(o,typeof(T)).MoveToPyObject();
return Converter.ToPython(o,type).MoveToPyObject();
}
}
}
Loading

[8]ページ先頭

©2009-2025 Movatter.jp