-
Notifications
You must be signed in to change notification settings - Fork 747
Codecs: customize set of Py<->C# conversions via PyObjectConversions #1022
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
Conversation
e840a4b
to
3dff8b5
Compare
@@ -23,7 +23,7 @@ | |||
<SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">..\..\</SolutionDir> | |||
<PythonBuildDir Condition="'$(TargetFramework)'=='net40' AND '$(PythonBuildDir)' == ''">$(SolutionDir)\bin\</PythonBuildDir> | |||
<PublishDir Condition="'$(TargetFramework)'!='net40'">$(OutputPath)\$(TargetFramework)_publish</PublishDir> | |||
<LangVersion>6</LangVersion> | |||
<LangVersion>7.3</LangVersion> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this intentional? @lostmsu
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I think I am using a few features. Is there a C# compiler, that does not support 7.3 yet?
It is 7.3 in Runtime for a few months now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@lostmsu just wanted to make sure it wasn't added by mistake.
/// <summary> | ||
/// Increase Python's ref counter for the given object, and get the object back. | ||
/// </summary> | ||
internal static IntPtr SelfIncRef(IntPtr op) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is neat and might simplify other code in the project
I like the conciseness of the terminology (encode/decode) but for me it isn't entirely obvious which direction (py <-> C#) is encoding and decoding. It is ok with me as long as we document it well enough. Which other encoders should exist inside of Python.Runtime aside from the Tuple one you've added? Would you be OK with the List and Action ones I am interested in eventually being distributed by PythonNet? I am writing the below snippet to document how it'd be available for external codecs to see how we like the syntax. To me it is not too bad:
|
@@ -135,8 +135,16 @@ internal static IntPtr ToPython(object value, Type type) | |||
return result; | |||
} | |||
|
|||
if (value is IList && !(value is INotifyPropertyChanged) && value.GetType().IsGenericType) | |||
{ | |||
if (Type.GetTypeCode(type) == TypeCode.Object && value.GetType() != typeof(object)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
glad this comes before other builtin conversions but ultimately the built-in conversions should probably use this codec system internally.
Pushed a fix to failing test |
src/runtime/Codecs/TupleCodecs.cs
Outdated
|
||
public bool CanEncode(Type type) | ||
=> type.Namespace == typeof(TTuple).Namespace && type.Name.StartsWith(typeof(TTuple).Name + '`') | ||
|| type == typeof(object) || type == typeof(TTuple); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe write this one out as a normal function block, it's a bit tedious to read.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, can't you just use type.IsGenericType && type.GetGenericTypeDefinition() == typeof(TTuple)
or something like that?
added sample TupleCodec (only supporting ValueTuple)
…ion taking priority
Mark methods as obsolete until they are stable. |
Codecov Report
@@ Coverage Diff @@
## master #1022 +/- ##
=======================================
Coverage 86.75% 86.75%
=======================================
Files 1 1
Lines 302 302
=======================================
Hits 262 262
Misses 40 40
Continue to review full report at Codecov.
|
@filmor I think this can be merged |
… as the decoded object might store a reference to the original PyObject
…return a new reference
The code style of this PR is totally different by the current code, is it really good for this? |
The issue #823 has been marked as solved by this PR. From what I can see, this new feature didn't make it to the latest release, is there a plan for for a 2.5.3 or there will be only the v3.0.0? |
@igiona There's no support for |
Thank you @lostmsu for the quick answer. What's the rational behind the decision to not support |
The rational is that pythonnet is an open source project, and nobody stepped up to implement this feature. I am not sure what you mean by "PythonNet assemblies needs to imported in the .NET application, which is not always possible/desirable". You can develop and register codecs from Python (though development in Python might be tricky). You can also develop codecs in C#, compile them to a DLL, and register them from it. e.g. clr.AddReference('MyCodecs.dll')
from MyCodecs import MyMegaCodecForTypeX
codec = MyMegaCodecForTypeX()
PyObjectConversions.RegisterEncoder(codec)
PyObjectConversions.RegisterDecoder(codec)
... work with TypeX ... |
This makes sense 👍 I did not checked yet the implementation above into details, but I assumed that the codecs have to implement the interfaces |
Again, the app does not have to import pythonnet assemblies. Codecs can be put into a separate library. |
What does this implement/fix? Explain your changes.
This enables registering new automatic Py->C# and C#->Py conversions (codecs), that will run when Python calls .NET code and vice versa.
Does this close any currently open issues?
This is a new feature
Any other comments?
...
Checklist
Check all those that are applicable and complete.
TupleCodec
AUTHORS
CHANGELOG