Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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

Open
generalloki wants to merge2 commits intopythonnet:master
base:master
Choose a base branch
Loading
fromgeneralloki:master

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 tofixissue #2360 by implementing a custom coercion handler.

@filmor
Copy link
Member

filmor commentedJul 10, 2025
edited
Loading

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:

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

lostmsu commentedJul 10, 2025
edited
Loading

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:

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

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers
No reviews
Assignees
No one assigned
Labels
None yet
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

PythonNET unknown kwargs being discarded
3 participants
@generalloki@filmor@lostmsu

[8]ページ先頭

©2009-2025 Movatter.jp