Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Non-delegate types should not be callable #1247

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

Merged
merged 1 commit into from
Oct 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions AUTHORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,4 @@
- ([@stonebig](https://github.com/stonebig))
- ([@testrunner123](https://github.com/testrunner123))
- ([@DanBarzilian](https://github.com/DanBarzilian))
- ([@alxnull](https://github.com/alxnull))
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ details about the cause of the failure
- Fix `object[]` parameters taking precedence when should not in overload resolution
- Fixed a bug where all .NET class instances were considered Iterable
- Fix incorrect choice of method to invoke when using keyword arguments.
- Fix non-delegate types incorrectly appearing as callable.

## [2.5.0][] - 2020-06-14

Expand Down
31 changes: 0 additions & 31 deletions src/runtime/classobject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -278,36 +278,5 @@ public static int mp_ass_subscript(IntPtr ob, IntPtr idx, IntPtr v)

return 0;
}


/// <summary>
/// This is a hack. Generally, no managed class is considered callable
/// from Python - with the exception of System.Delegate. It is useful
/// to be able to call a System.Delegate instance directly, especially
/// when working with multicast delegates.
/// </summary>
public static IntPtr tp_call(IntPtr ob, IntPtr args, IntPtr kw)
{
//ManagedType self = GetManagedObject(ob);
IntPtr tp = Runtime.PyObject_TYPE(ob);
var cb = (ClassBase)GetManagedObject(tp);

if (cb.type != typeof(Delegate))
{
Exceptions.SetError(Exceptions.TypeError, "object is not callable");
return IntPtr.Zero;
}

var co = (CLRObject)GetManagedObject(ob);
var d = co.inst as Delegate;
BindingFlags flags = BindingFlags.Public |
BindingFlags.NonPublic |
BindingFlags.Instance |
BindingFlags.Static;

MethodInfo method = d.GetType().GetMethod("Invoke", flags);
var binder = new MethodBinder(method);
return binder.Invoke(ob, args, kw);
}
}
}
10 changes: 10 additions & 0 deletions src/tests/test_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -315,3 +315,13 @@ def test_method_inheritance():

assert base.IsBase() == True
assert derived.IsBase() == False


def test_callable():
"""Test that only delegate subtypes are callable"""

def foo():
pass

assert callable(System.String("foo")) == False
assert callable(System.Action(foo)) == True