Skip to content

Commit 7604e39

Browse files
committed
allow Python to overwrite .NET methods
1 parent ee0ab7f commit 7604e39

File tree

2 files changed

+39
-12
lines changed

2 files changed

+39
-12
lines changed

src/embed_tests/CallableObject.cs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using NUnit.Framework;
2+
3+
using Python.Runtime;
4+
5+
namespace Python.EmbeddingTest
6+
{
7+
class CallableObject
8+
{
9+
[OneTimeSetUp]
10+
public void SetUp()
11+
{
12+
PythonEngine.Initialize();
13+
}
14+
15+
[OneTimeTearDown]
16+
public void Dispose()
17+
{
18+
PythonEngine.Shutdown();
19+
}
20+
21+
[Test]
22+
public void PythonCanOverwriteMethods()
23+
{
24+
var obj = new PublicCallMethod();
25+
using var _ = Py.GIL();
26+
using var scope = Py.CreateScope();
27+
scope.Set("o", obj);
28+
scope.Exec("orig_call = o.Call");
29+
scope.Exec("o.Call = lambda a: orig_call(a*7)");
30+
int result = scope.Eval<int>("o.Call(5)");
31+
Assert.AreEqual(105, result);
32+
}
33+
34+
class PublicCallMethod
35+
{
36+
public int Call(int arg) => 3 * arg;
37+
}
38+
}
39+
}

src/runtime/extensiontype.cs

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -87,18 +87,6 @@ public static int tp_setattro(IntPtr ob, IntPtr key, IntPtr val)
8787
return -1;
8888
}
8989

90-
91-
/// <summary>
92-
/// Default __set__ implementation - this prevents descriptor instances
93-
/// being silently replaced in a type __dict__ by default __setattr__.
94-
/// </summary>
95-
public static int tp_descr_set(IntPtr ds, IntPtr ob, IntPtr val)
96-
{
97-
Exceptions.SetError(Exceptions.AttributeError, "attribute is read-only");
98-
return -1;
99-
}
100-
101-
10290
public static void tp_dealloc(IntPtr ob)
10391
{
10492
// Clean up a Python instance of this extension type. This

0 commit comments

Comments
 (0)