Skip to content

Commit 03cf4ac

Browse files
authored
Non-delegate types should not be callable (#1247)
Removed the ClassObject.tp_call method which seemed to be unused. Also added a test case.
1 parent 50d947f commit 03cf4ac

File tree

4 files changed

+12
-31
lines changed

4 files changed

+12
-31
lines changed

AUTHORS.md

+1
Original file line numberDiff line numberDiff line change
@@ -77,3 +77,4 @@
7777
- ([@stonebig](https://github.com/stonebig))
7878
- ([@testrunner123](https://github.com/testrunner123))
7979
- ([@DanBarzilian](https://github.com/DanBarzilian))
80+
- ([@alxnull](https://github.com/alxnull))

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ details about the cause of the failure
2828
- Fix `object[]` parameters taking precedence when should not in overload resolution
2929
- Fixed a bug where all .NET class instances were considered Iterable
3030
- Fix incorrect choice of method to invoke when using keyword arguments.
31+
- Fix non-delegate types incorrectly appearing as callable.
3132

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

src/runtime/classobject.cs

-31
Original file line numberDiff line numberDiff line change
@@ -278,36 +278,5 @@ public static int mp_ass_subscript(IntPtr ob, IntPtr idx, IntPtr v)
278278

279279
return 0;
280280
}
281-
282-
283-
/// <summary>
284-
/// This is a hack. Generally, no managed class is considered callable
285-
/// from Python - with the exception of System.Delegate. It is useful
286-
/// to be able to call a System.Delegate instance directly, especially
287-
/// when working with multicast delegates.
288-
/// </summary>
289-
public static IntPtr tp_call(IntPtr ob, IntPtr args, IntPtr kw)
290-
{
291-
//ManagedType self = GetManagedObject(ob);
292-
IntPtr tp = Runtime.PyObject_TYPE(ob);
293-
var cb = (ClassBase)GetManagedObject(tp);
294-
295-
if (cb.type != typeof(Delegate))
296-
{
297-
Exceptions.SetError(Exceptions.TypeError, "object is not callable");
298-
return IntPtr.Zero;
299-
}
300-
301-
var co = (CLRObject)GetManagedObject(ob);
302-
var d = co.inst as Delegate;
303-
BindingFlags flags = BindingFlags.Public |
304-
BindingFlags.NonPublic |
305-
BindingFlags.Instance |
306-
BindingFlags.Static;
307-
308-
MethodInfo method = d.GetType().GetMethod("Invoke", flags);
309-
var binder = new MethodBinder(method);
310-
return binder.Invoke(ob, args, kw);
311-
}
312281
}
313282
}

src/tests/test_class.py

+10
Original file line numberDiff line numberDiff line change
@@ -315,3 +315,13 @@ def test_method_inheritance():
315315

316316
assert base.IsBase() == True
317317
assert derived.IsBase() == False
318+
319+
320+
def test_callable():
321+
"""Test that only delegate subtypes are callable"""
322+
323+
def foo():
324+
pass
325+
326+
assert callable(System.String("foo")) == False
327+
assert callable(System.Action(foo)) == True

0 commit comments

Comments
 (0)