GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Allow setting typed objects via PyModule.Set · Issue #2311 · pythonnet/pythonnet

Allow setting typed objects via PyModule.Set #2311

New issue
Copy link
New issue
Copy link
Closed
#2330
Closed
Allow setting typed objects via PyModule.Set#2311
#2330
Copy link

Description

@dmitry-medvedev
dmitry-medvedev
opened on Feb 1, 2024
Issue body actions

Environment

  • Pythonnet version: 3.0.1
  • Python version: 3.11
  • Operating System: Windows 11
  • .NET Runtime: .NET 4.8

Details

  • Right now, a .NET object can be passed to the Python scripting engine via PyModule.Set, which internally calls ToPythonDetectType(value)
    We'd like to pass a certain interface that the object supports, so only methods of this interface are available for Python scripting.

I suspect that adding the overloaded method PyModule.Set or ConverterExtension.ToPython that accepts the type of the object may resolve this issue.

Activity

  1. dmitry-medvedev commented on Feb 14, 2024

    @dmitry-medvedev
    dmitry-medvedev
    on Feb 14, 2024
    Author
    More actions

    We were able to achieve the desired effect by using the following code:

     internal static class PythonModuleExtensions
     {
         public static void Set(this PyModule module, string name, object value, Type type)
         {
             using var pyValue = Converter.ToPython(value, type);
             SetPyValue(module, name, pyValue.Borrow());
         }
    
         private static void SetPyValue(PyModule module, string name, BorrowedReference value)
         {
             Check(module);
             if (name is null)
                 throw new ArgumentNullException(nameof(name));
    
             using var pyKey = new PyString(name);
    
             var result = Runtime.PyObject_SetItem(module.variables, pyKey.obj, value);
             if (result < 0)
             {
                 throw PythonException.ThrowLastAsClrException();
             }
         }
    
         private static void Check(PyModule module)
         {
             if (module.Reference.IsNull)
             {
                 throw new ObjectDisposedException(nameof(PyModule));
             }
         }
     }
    

    However, as some of these types/methods are internal, this is only possible if we use IgnoresAccessChecksToGenerator, which we would really like to avoid.

  2. lostmsu commented on Feb 14, 2024

    @lostmsu
    lostmsu
    on Feb 14, 2024
    Member
    More actions

    PyModule inherits from PyObject, and PyObject has a Set method that accepts PyObject value.

    Sounds like we just need an overload for ToPython that takes type parameter. I think it would be better to have both generic and non-generic variants. PR would be welcome.

    Reacted by heartacker
  3. dmitry-medvedev commented on Feb 19, 2024

    @dmitry-medvedev
    dmitry-medvedev
    on Feb 19, 2024
    Author
    More actions

    We've sent PR via a separate ticket:
    #2323

  4. lostmsu
    added a commit that references this issue on Feb 27, 2024

    Added `ToPythonAs<T>()` extension method to allow for explicit conver…

    5be9bd5
  5. filmor
    closed this as completedin #2330on Feb 28, 2024
  6. filmor
    added a commit that references this issue on Feb 28, 2024

    Added `ToPythonAs<T>()` extension method to allow for explicit conver…

    9d18a24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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

      You are visitor 01402503 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.