Skip to content

allow .NET classes to override __getattr__ and __setattr__ #901

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

Closed
wants to merge 3 commits into from
Closed
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 CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ This document follows the conventions laid out in [Keep a CHANGELOG][].
### Added

- Added automatic NuGet package generation in appveyor and local builds
- Added IGetAttr and ISetAttr, so that .NET classes could override __getattr__ and __setattr__

### Changed

Expand Down
1 change: 1 addition & 0 deletions src/embed_tests/Python.EmbeddingTest.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@
<Compile Include="TestDomainReload.cs" />
<Compile Include="TestExample.cs" />
<Compile Include="TestFinalizer.cs" />
<Compile Include="TestInstanceWrapping.cs" />
<Compile Include="TestPyAnsiString.cs" />
<Compile Include="TestPyFloat.cs" />
<Compile Include="TestPyInt.cs" />
Expand Down
50 changes: 50 additions & 0 deletions src/embed_tests/TestInstanceWrapping.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using NUnit.Framework;
using Python.Runtime;
using Python.Runtime.Slots;

namespace Python.EmbeddingTest {
public class TestInstanceWrapping {
[Test]
public void GetAttrCanBeOverriden() {
var overloaded = new Overloaded();
using (Py.GIL()) {
var o = overloaded.ToPython();
dynamic getNonexistingAttr = PythonEngine.Eval("lambda o: o.non_existing_attr");
string nonexistentAttrValue = getNonexistingAttr(o);
Assert.AreEqual(GetAttrFallbackValue, nonexistentAttrValue);
}
}

[Test]
public void SetAttrCanBeOverriden() {
var overloaded = new Overloaded();
using (Py.GIL())
using (var scope = Py.CreateScope()) {
var o = overloaded.ToPython();
scope.Set(nameof(o), o);
scope.Exec($"{nameof(o)}.non_existing_attr = 42");
Assert.AreEqual(42, overloaded.Value);
}
}

const string GetAttrFallbackValue = "undefined";

class Base {}
class Derived: Base { }

class Overloaded: Derived, IGetAttr, ISetAttr
{
public int Value { get; private set; }

public bool TryGetAttr(string name, out PyObject value) {
value = GetAttrFallbackValue.ToPython();
return true;
}

public bool TrySetAttr(string name, PyObject value) {
this.Value = value.As<int>();
return true;
}
}
}
}
27 changes: 14 additions & 13 deletions src/runtime/Python.Runtime.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,19 @@
</PropertyGroup>
<!--We can relax binding to platform because code references no any platform dependent assemblies-->
<!--This will allows to use any build of this assebly as a compile ref assebly-->
<!--<PropertyGroup Condition=" '$(Platform)' == 'x86'">
<PlatformTarget>x86</PlatformTarget>
</PropertyGroup>
<PropertyGroup Condition=" '$(Platform)' == 'x64'">
<PlatformTarget>x64</PlatformTarget>
<!--<PropertyGroup Condition=" '$(Platform)' == 'x86'">
<PlatformTarget>x86</PlatformTarget>
</PropertyGroup>
<PropertyGroup Condition=" '$(Platform)' == 'x64'">
<PlatformTarget>x64</PlatformTarget>
</PropertyGroup>-->
<PropertyGroup Condition=" '$(Configuration)' == 'ReleaseMono'">
<DefineConstants Condition="'$(DefineConstants)' == ''">PYTHON2;PYTHON27;UCS4</DefineConstants>
<Optimize>true</Optimize>
<DebugType>pdbonly</DebugType>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)' == 'ReleaseMonoPY3'">
<DefineConstants Condition="'$(DefineConstants)' == ''">PYTHON3;PYTHON37;UCS4</DefineConstants>
<DefineConstants Condition="'$(DefineConstants)' == ''">PYTHON3;PYTHON37;UCS4</DefineConstants>
<Optimize>true</Optimize>
<DebugType>pdbonly</DebugType>
</PropertyGroup>
Expand All @@ -46,7 +46,7 @@
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)' == 'DebugMonoPY3'">
<DebugSymbols>true</DebugSymbols>
<DefineConstants Condition="'$(DefineConstants)' == ''">PYTHON3;PYTHON37;UCS4;TRACE;DEBUG</DefineConstants>
<DefineConstants Condition="'$(DefineConstants)' == ''">PYTHON3;PYTHON37;UCS4;TRACE;DEBUG</DefineConstants>
<Optimize>false</Optimize>
<DebugType>full</DebugType>
</PropertyGroup>
Expand All @@ -56,7 +56,7 @@
<DebugType>pdbonly</DebugType>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)' == 'ReleaseWinPY3'">
<DefineConstants Condition="'$(DefineConstants)' == ''">PYTHON3;PYTHON37;UCS2</DefineConstants>
<DefineConstants Condition="'$(DefineConstants)' == ''">PYTHON3;PYTHON37;UCS2</DefineConstants>
<Optimize>true</Optimize>
<DebugType>pdbonly</DebugType>
</PropertyGroup>
Expand All @@ -68,7 +68,7 @@
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)' == 'DebugWinPY3'">
<DebugSymbols>true</DebugSymbols>
<DefineConstants Condition="'$(DefineConstants)' == ''">PYTHON3;PYTHON37;UCS2;TRACE;DEBUG</DefineConstants>
<DefineConstants Condition="'$(DefineConstants)' == ''">PYTHON3;PYTHON37;UCS2;TRACE;DEBUG</DefineConstants>
<Optimize>false</Optimize>
<DebugType>full</DebugType>
</PropertyGroup>
Expand Down Expand Up @@ -137,11 +137,12 @@
<Compile Include="pythonexception.cs" />
<Compile Include="pytuple.cs" />
<Compile Include="runtime.cs" />
<Compile Include="slots.cs" />
<Compile Include="typemanager.cs" />
<Compile Include="typemethod.cs" />
<Compile Include="Util.cs" />
<Compile Include="platform\Types.cs" />
<Compile Include="platform\LibraryLoader.cs" />
<Compile Include="platform\Types.cs" />
<Compile Include="platform\LibraryLoader.cs" />
</ItemGroup>
<ItemGroup Condition=" '$(PythonInteropFile)' != '' ">
<Compile Include="$(PythonInteropFile)" />
Expand All @@ -151,7 +152,7 @@
<Compile Include="interop34.cs" />
<Compile Include="interop35.cs" />
<Compile Include="interop36.cs" />
<Compile Include="interop37.cs" />
<Compile Include="interop37.cs" />
</ItemGroup>
<ItemGroup>
<None Include="..\pythonnet.snk" />
Expand All @@ -170,4 +171,4 @@
<Copy SourceFiles="$(TargetAssembly)" DestinationFolder="$(PythonBuildDir)" />
<!--Copy SourceFiles="$(TargetAssemblyPdb)" Condition="Exists('$(TargetAssemblyPdb)')" DestinationFolder="$(PythonBuildDir)" /-->
</Target>
</Project>
</Project>
10 changes: 9 additions & 1 deletion src/runtime/runtime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public class Runtime
internal static object IsFinalizingLock = new object();
internal static bool IsFinalizing;

internal static bool Is32Bit = IntPtr.Size == 4;
internal static readonly bool Is32Bit = IntPtr.Size == 4;

// .NET core: System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(OSPlatform.Windows)
internal static bool IsWindows = Environment.OSVersion.Platform == PlatformID.Win32NT;
Expand Down Expand Up @@ -562,6 +562,14 @@ internal static unsafe void XIncref(IntPtr op)
#endif
}

/// <summary>
/// Increase Python's ref counter for the given object, and return the object back.
/// </summary>
internal static IntPtr SelfIncRef(IntPtr op) {
XIncref(op);
return op;
}

internal static unsafe void XDecref(IntPtr op)
{
#if PYTHON_WITH_PYDEBUG || NETSTANDARD
Expand Down
47 changes: 47 additions & 0 deletions src/runtime/slots.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using System;

namespace Python.Runtime.Slots
{
/// <summary>
/// Implement this interface to override Python's __getattr__ for your class
/// </summary>
public interface IGetAttr {
bool TryGetAttr(string name, out PyObject value);
}

/// <summary>
/// Implement this interface to override Python's __setattr__ for your class
/// </summary>
public interface ISetAttr {
bool TrySetAttr(string name, PyObject value);
}

static class SlotOverrides {
public static IntPtr tp_getattro(IntPtr ob, IntPtr key) {
IntPtr genericResult = Runtime.PyObject_GenericGetAttr(ob, key);
if (genericResult != IntPtr.Zero || !Runtime.PyString_Check(key)) {
return genericResult;
}

Exceptions.Clear();

var self = (IGetAttr)((CLRObject)ManagedType.GetManagedObject(ob)).inst;
string attr = Runtime.GetManagedString(key);
return self.TryGetAttr(attr, out var value)
? Runtime.SelfIncRef(value.Handle)
: Runtime.PyObject_GenericGetAttr(ob, key);
}

public static int tp_setattro(IntPtr ob, IntPtr key, IntPtr val) {
if (!Runtime.PyString_Check(key)) {
return Runtime.PyObject_GenericSetAttr(ob, key, val);
}

var self = (ISetAttr)((CLRObject)ManagedType.GetManagedObject(ob)).inst;
string attr = Runtime.GetManagedString(key);
return self.TrySetAttr(attr, new PyObject(Runtime.SelfIncRef(val)))
? 0
: Runtime.PyObject_GenericSetAttr(ob, key, val);
}
}
}
15 changes: 14 additions & 1 deletion src/runtime/typemanager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
using System.Reflection;
using System.Runtime.InteropServices;
using Python.Runtime.Platform;
using Python.Runtime.Slots;

namespace Python.Runtime
{

/// <summary>
/// The TypeManager class is responsible for building binary-compatible
/// Python type objects that are implemented in managed code.
Expand Down Expand Up @@ -155,6 +155,14 @@ internal static IntPtr CreateType(ManagedType impl, Type clrType)

InitializeSlots(type, impl.GetType());

if (typeof(IGetAttr).IsAssignableFrom(clrType)) {
InitializeSlot(type, TypeOffset.tp_getattro, typeof(SlotOverrides).GetMethod(nameof(SlotOverrides.tp_getattro)));
}

if (typeof(ISetAttr).IsAssignableFrom(clrType)) {
InitializeSlot(type, TypeOffset.tp_setattro, typeof(SlotOverrides).GetMethod(nameof(SlotOverrides.tp_setattro)));
}

if (base_ != IntPtr.Zero)
{
Marshal.WriteIntPtr(type, TypeOffset.tp_base, base_);
Expand Down Expand Up @@ -766,6 +774,11 @@ static void InitializeSlot(IntPtr type, IntPtr slot, string name)
Marshal.WriteIntPtr(type, offset, slot);
}

static void InitializeSlot(IntPtr type, int slotOffset, MethodInfo method) {
IntPtr thunk = Interop.GetThunk(method);
Marshal.WriteIntPtr(type, slotOffset, thunk);
}

/// <summary>
/// Given a newly allocated Python type object and a managed Type that
/// implements it, initialize any methods defined by the Type that need
Expand Down