Skip to content

Allow Python to overwrite .NET methods #1545

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
Sep 23, 2021
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
12 changes: 0 additions & 12 deletions src/runtime/extensiontype.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,18 +87,6 @@ public static int tp_setattro(IntPtr ob, IntPtr key, IntPtr val)
return -1;
}


/// <summary>
/// Default __set__ implementation - this prevents descriptor instances
/// being silently replaced in a type __dict__ by default __setattr__.
/// </summary>
public static int tp_descr_set(IntPtr ds, IntPtr ob, IntPtr val)
{
Exceptions.SetError(Exceptions.AttributeError, "attribute is read-only");
return -1;
}


public static void tp_dealloc(IntPtr ob)
{
// Clean up a Python instance of this extension type. This
Expand Down
2 changes: 2 additions & 0 deletions src/testing/methodtest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ public MethodTest()
{
}

public string OverwritableMethod() => "overwritable";

public string PublicMethod()
{
return "public";
Expand Down
35 changes: 5 additions & 30 deletions tests/test_method.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,37 +6,12 @@
import pytest
from Python.Test import MethodTest

def test_instance_method_overwritable():
"""Test instance method overwriting."""

def test_instance_method_descriptor():
"""Test instance method descriptor behavior."""

with pytest.raises(AttributeError):
MethodTest().PublicMethod = 0

with pytest.raises(AttributeError):
MethodTest.PublicMethod = 0

with pytest.raises(AttributeError):
del MethodTest().PublicMethod

with pytest.raises(AttributeError):
del MethodTest.PublicMethod


def test_static_method_descriptor():
"""Test static method descriptor behavior."""

with pytest.raises(AttributeError):
MethodTest().PublicStaticMethod = 0

with pytest.raises(AttributeError):
MethodTest.PublicStaticMethod = 0

with pytest.raises(AttributeError):
del MethodTest().PublicStaticMethod

with pytest.raises(AttributeError):
del MethodTest.PublicStaticMethod
ob = MethodTest()
ob.OverwritableMethod = lambda: "overwritten"
assert ob.OverwritableMethod() == "overwritten"


def test_public_instance_method():
Expand Down