Skip to content

Commit 70b8c04

Browse files
committed
allow Python to overwrite .NET methods
1 parent ee0ab7f commit 70b8c04

File tree

3 files changed

+39
-45
lines changed

3 files changed

+39
-45
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

tests/test_method.py

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -6,39 +6,6 @@
66
import pytest
77
from Python.Test import MethodTest
88

9-
10-
def test_instance_method_descriptor():
11-
"""Test instance method descriptor behavior."""
12-
13-
with pytest.raises(AttributeError):
14-
MethodTest().PublicMethod = 0
15-
16-
with pytest.raises(AttributeError):
17-
MethodTest.PublicMethod = 0
18-
19-
with pytest.raises(AttributeError):
20-
del MethodTest().PublicMethod
21-
22-
with pytest.raises(AttributeError):
23-
del MethodTest.PublicMethod
24-
25-
26-
def test_static_method_descriptor():
27-
"""Test static method descriptor behavior."""
28-
29-
with pytest.raises(AttributeError):
30-
MethodTest().PublicStaticMethod = 0
31-
32-
with pytest.raises(AttributeError):
33-
MethodTest.PublicStaticMethod = 0
34-
35-
with pytest.raises(AttributeError):
36-
del MethodTest().PublicStaticMethod
37-
38-
with pytest.raises(AttributeError):
39-
del MethodTest.PublicStaticMethod
40-
41-
429
def test_public_instance_method():
4310
"""Test public instance method visibility."""
4411
ob = MethodTest()

0 commit comments

Comments
 (0)