Skip to content

Implement coerce logic for net method binding #2590

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

generalloki
Copy link

What does this implement/fix? Explain your changes.

This allows additional checks to be performed before .NET members are called from Python scripts.
It also enables the cancellation of a method call if it is not permitted by the application's security policies.

Does this close any currently open issues?

This allows to fix issue #2360 by implementing a custom coercion handler.

@filmor
Copy link
Member

filmor commented Jul 10, 2025

Sorry for the late reply. Could you give a sketch of how one would fix 2360 with this?

@generalloki
Copy link
Author

We implemented such method for that:

        public static void CoerceBindHandler(
            Dictionary<string, PyObject> arguments,
            MethodBase[] methods,
            ref MethodBase foundMethod)
        {
            var fb = foundMethod;

            if (foundMethod is not null && arguments is not null)
            {
                foreach (var argName in arguments.Keys)
                {
                    if (!IsValid(argName))
                    {
                        Exceptions.SetError(Exceptions.TypeError, $"Invalid parameter name: {argName}");
                        foundMethod = null;
                        return;
                    }
                }
            }

            bool IsValid(string paramName)
            {
                foreach (ParameterInfo param in fb.GetParameters())
                {
                    if (param.Name == paramName)
                        return true;
                }

                return false;
            }
        }

So, if coerce logic will be added to the library we could just use it

MethodBinderEvents.CoerceBind = ScriptEngine.CoerceBindHandler;

in order to fix #2360

@lostmsu
Copy link
Member

lostmsu commented Jul 10, 2025

What is the use case? Can you give your specific example? Why can't you just have an overload with explicit kwargs on C# side, and then route as needed?

@generalloki
Copy link
Author

I am not sure what you suggest to overload. Initially our customer reported the problem. Here is his request:


When accessing a .NET function using PythonNET bindings I noticed that if a kwarg is incorrect or doesn’t exist it is discarded by the PythonNET parser. For example

If I have a .NET function like this:

Public Function SomeFunction(name as String, age as String) as String
    Return $"{name} - {age}"
End Function

And I call it in PythonNET like this:

if __name__ == "__main__":
    name = "John"
    age = "3"
    SomeFunction(name, age=age, fake_arg="something")

The fake arg is discarded and no error or exception is thrown. The python compiles as if the additional kwarg is not there. In this situation normal python would be throwing a TypeError.

This is an issue for us when we have multiple kwargs in our methods/functions that a user can easily misspell and result in unexpected behaviour.


We posted an issue about kwargs but it is not fixed from that moment, so we suggest an improvement that will allow bind a customizable event which is fired before .net method is called. In the implementation of this event we can fix the issue and also add some other security related logic

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

PythonNET unknown kwargs being discarded
3 participants
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy