pythonnet icon indicating copy to clipboard operation
pythonnet copied to clipboard

Implement coerce logic for net method binding

Open generalloki opened this issue 7 months ago • 4 comments

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.

generalloki avatar May 06 '25 12:05 generalloki

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

filmor avatar Jul 10 '25 04:07 filmor

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

generalloki avatar Jul 10 '25 04:07 generalloki

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?

lostmsu avatar Jul 10 '25 16:07 lostmsu

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

generalloki avatar Jul 12 '25 02:07 generalloki