← Winnitude! Search  ·  Reader Mode (1015 ms)
Links: Stay in Reader Retro Site Converter Retro Mode: OFF Original Page ↗
GITHUB · 39 LINKS

Implement coerce logic for net method binding by generalloki · Pull Request #2590 · pythonnet/pythonnet

Repository navigation

Implement coerce logic for net method binding - #2590


Open
generalloki wants to merge 2 commits into pythonnet:master from generalloki:master
Open

Implement coerce logic for net method binding #2590 generalloki wants to merge 2 commits into pythonnet:master from generalloki:master

Conversation

generalloki commented May 6, 2025



Copy link Copy Markdown

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 commented Jul 10, 2025 · edited Loading



Copy link Copy Markdown
Member

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

generalloki commented Jul 10, 2025



Copy link Copy Markdown
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 commented Jul 10, 2025 · edited Loading



Copy link Copy Markdown
Member

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 commented Jul 12, 2025



Copy link Copy Markdown
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

filmor force-pushed the master branch from aa8e833 to 16e4aaf
Compare
December 8, 2025 10:30 generalloki added 2 commits May 6, 2026 16:16 MethodBinderEvents.CoerceBind 0af0a52 Update MethodBinder.cs 64d06ab filmor force-pushed the master branch from 16e4aaf to 64d06ab
Compare
May 6, 2026 14:16

This branch has not been deployed


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

Labels

None yet

3 participants