- Notifications
You must be signed in to change notification settings - Fork756
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
base:master
Are you sure you want to change the base?
Uh oh!
There was an error while loading.Please reload this page.
Conversation
filmor commentedJul 10, 2025 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
Sorry for the late reply. Could you give a sketch of how one would fix 2360 with this? |
We implemented such method for that: publicstaticvoidCoerceBindHandler(Dictionary<string,PyObject>arguments,MethodBase[]methods,refMethodBasefoundMethod){varfb=foundMethod;if(foundMethodis notnull&&argumentsis notnull){foreach(varargNameinarguments.Keys){if(!IsValid(argName)){Exceptions.SetError(Exceptions.TypeError,$"Invalid parameter name:{argName}");foundMethod=null;return;}}}boolIsValid(stringparamName){foreach(ParameterInfoparaminfb.GetParameters()){if(param.Name==paramName)returntrue;}returnfalse;}} 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 commentedJul 10, 2025 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
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? |
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: PublicFunctionSomeFunction(nameasString,ageasString)asStringReturn $"{name} - {age}"EndFunction 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 |
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 tofixissue #2360 by implementing a custom coercion handler.