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

A way to override Python.NET .NET <-> Python wrapping for custom classes #823

Closed
@lostmsu

Description

@lostmsu

Environment

  • Pythonnet version: 2.3.0
  • Python version: 3.6
  • Operating System: Windows 10 x64

Details

I am trying to generate statically-typed wrappers for Python libraries, particularly TensorFlow (seeGradient).

Since I am automatically generating .NET-side types for wrappers, I can make their methods to use my custom converter written in C# to convert function arguments toPyObject instances. And then for any result values, I can convertPyObject instances back into my wrapper types.

Problem I am facing is when a user of my wrapper wants to inherit from one of the Python-derived classes, and override and/or extend its behavior.

An artificial sample:

// this part of my library is auto-generated// represents tensorflow moduleclasstf{staticdynamictf=Py.Import("tensorflow");// set_default_session - made up methodstaticvoidSetDefaultSession(Sessionsession)=>tf.set_default_session(session.underlyingSession);// made up methodstaticvoidComputeUsingDefaultSession()=>tf.compute_using_default_session();}// represents tf.TensorclassTensor{PyObjectunderlyingTensor;}// represents tf.SessionclassSession{PyObjectunderlyingSession;virtualobjectRun(Tensortensor){varpyTensor=ConvertToPyObject(tensor);// simply does tensor.underlyingTensor heredynamicpySession=this.underlyingSession;varresult=pySession.Run(pyTensor);returnConvertFromPyObject(result);// wraps any returned object into one of generated classes}}// this code is what user of my library wants to do:classMyBetterSession:Session{overrideobjectRun(Tensortensor){    ... here he writes custom code to run aTensor ...}}// an attempts to use the above (functions made up):varsimpleSession=newSession();tf.SetDefaultSession(simpleSession);// OKtf.ComputeUsingDefaultSession();// SUCCEEDSvarbetterSession=newMyBetterSession();tf.SetDefaultSession(betterSession);// OKtf.ComputeUsingDefaultSession();// FAILS

The last line will fail, because Python will attempt to callMyBetterSession.Run with Python's classtf.Tensor, but the method actually expects wrapped classTensor. I need to somehow tell Python or rather Python.NET to invoke myConvertFromPyObject on the argument, before trying to find a matching overload.

I looked into Python.NET source, and the corresponding objects areMethodBinding andMethodObject, however, neither seem to provide any extensibility.

Now I am considering several approaches to the problem, and I just wanted to discuss them with Python.NET team, as some of them involve modification of Python.NET.

  1. Do not change anything in Python.NET, and for every user class likeMyBetterSession generate a pythonic wrapper with the same set of methods, but replacing all parameter and return types withPyObject. It would require one of the following:
  • System.Reflection.Emit, which is a very heavy dependency, and also not very pleasant to work with
  • Roslyn, which is much heavier, but moderately OK to work with
  1. I noticed, that while Python.NET supports representing any Python object asdynamic, it does not actually support passingdynamic objects (e.g.IDynamicMetaObjectProvider instances) back to Python. I mean, it would pass them, but if Python would try to access a dynamic attribute, it would not attempt to callTryGetMember. I could potentially implement something similar toClassBase specifically for wrappingIDynamicMetaObjectProvider instances. Then it is much easier to implement its members, that would simply wrap PyObject arguments before forwarding them to an instance ofMyBetterSession.

  2. Have Python.NET directly expose some low-level interface, that would enable hooking into Python.NET's marshaling and, possibly, also method binding processes.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


      [8]ページ先頭

      ©2009-2025 Movatter.jp