Skip to content
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
26 changes: 19 additions & 7 deletions src/runtime/interop.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System;
using System.Collections;
using System.Collections.Specialized;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Reflection;
using System.Text;
Expand Down Expand Up @@ -88,8 +88,7 @@ static ObjectOffset()

public static int magic(IntPtr ob)
{
if ((Runtime.PyObject_TypeCheck(ob, Exceptions.BaseException) ||
(Runtime.PyType_Check(ob) && Runtime.PyType_IsSubtype(ob, Exceptions.BaseException))))
if (IsException(ob))
{
return ExceptionOffset.ob_data;
}
Expand All @@ -98,8 +97,7 @@ public static int magic(IntPtr ob)

public static int DictOffset(IntPtr ob)
{
if ((Runtime.PyObject_TypeCheck(ob, Exceptions.BaseException) ||
(Runtime.PyType_Check(ob) && Runtime.PyType_IsSubtype(ob, Exceptions.BaseException))))
if (IsException(ob))
{
return ExceptionOffset.ob_dict;
}
Expand All @@ -108,8 +106,7 @@ public static int DictOffset(IntPtr ob)

public static int Size(IntPtr ob)
{
if ((Runtime.PyObject_TypeCheck(ob, Exceptions.BaseException) ||
(Runtime.PyType_Check(ob) && Runtime.PyType_IsSubtype(ob, Exceptions.BaseException))))
if (IsException(ob))
{
return ExceptionOffset.Size();
}
Expand All @@ -128,6 +125,21 @@ public static int Size(IntPtr ob)
public static int ob_type;
private static int ob_dict;
private static int ob_data;
private static readonly Dictionary<IntPtr, bool> ExceptionTypeCache = new Dictionary<IntPtr, bool>();

private static bool IsException(IntPtr pyObject)
{
bool res;
var type = Runtime.PyObject_TYPE(pyObject);
if (!ExceptionTypeCache.TryGetValue(type, out res))
{
res = Runtime.PyObjectType_TypeCheck(type, Exceptions.BaseException)
|| Runtime.PyObjectType_TypeCheck(type, Runtime.PyTypeType)
&& Runtime.PyType_IsSubtype(pyObject, Exceptions.BaseException);
ExceptionTypeCache.Add(type, res);
}
return res;
}
}

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
Expand Down
93 changes: 90 additions & 3 deletions src/runtime/propertyobject.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Linq.Expressions;
using System.Reflection;
using System.Security.Permissions;

Expand All @@ -12,6 +13,10 @@ internal class PropertyObject : ExtensionType
private PropertyInfo info;
private MethodInfo getter;
private MethodInfo setter;
private bool getterCacheFailed;
private bool setterCacheFailed;
private Func<object, object> getterCache;
private Action<object, object> setterCache;

[StrongNameIdentityPermission(SecurityAction.Assert)]
public PropertyObject(PropertyInfo md)
Expand Down Expand Up @@ -67,7 +72,21 @@ public static IntPtr tp_descr_get(IntPtr ds, IntPtr ob, IntPtr tp)

try
{
result = self.info.GetValue(co.inst, null);
if (self.getterCache == null && !self.getterCacheFailed)
{
// if the getter is not public 'GetGetMethod' will not find it
// but calling 'GetValue' will work, so for backwards compatibility
// we will use it instead
self.getterCache = BuildGetter(self.info);
if (self.getterCache == null)
{
self.getterCacheFailed = true;
}
}

result = self.getterCacheFailed ?
self.info.GetValue(co.inst, null)
: self.getterCache(co.inst);
return Converter.ToPython(result, self.info.PropertyType);
}
catch (Exception e)
Expand All @@ -81,7 +100,6 @@ public static IntPtr tp_descr_get(IntPtr ds, IntPtr ob, IntPtr tp)
}
}


/// <summary>
/// Descriptor __set__ implementation. This method sets the value of
/// a property based on the given Python value. The Python value must
Expand Down Expand Up @@ -132,7 +150,27 @@ public static IntPtr tp_descr_get(IntPtr ds, IntPtr ob, IntPtr tp)
Exceptions.RaiseTypeError("invalid target");
return -1;
}
self.info.SetValue(co.inst, newval, null);

if (self.setterCache == null && !self.setterCacheFailed)
{
// if the setter is not public 'GetSetMethod' will not find it
// but calling 'SetValue' will work, so for backwards compatibility
// we will use it instead
self.setterCache = BuildSetter(self.info);
if (self.setterCache == null)
{
self.setterCacheFailed = true;
}
}

if (self.setterCacheFailed)
{
self.info.SetValue(co.inst, newval, null);
}
else
{
self.setterCache(co.inst, newval);
}
}
else
{
Expand Down Expand Up @@ -160,5 +198,54 @@ public static IntPtr tp_repr(IntPtr ob)
var self = (PropertyObject)GetManagedObject(ob);
return Runtime.PyString_FromString($"<property '{self.info.Name}'>");
}

private static Func<object, object> BuildGetter(PropertyInfo propertyInfo)
{
var methodInfo = propertyInfo.GetGetMethod();
if (methodInfo == null)
{
// if the getter is not public 'GetGetMethod' will not find it
return null;
}
var obj = Expression.Parameter(typeof(object), "o");
// Require because we will know at runtime the declaring type
// so 'obj' is declared as typeof(object)
var instance = Expression.Convert(obj, methodInfo.DeclaringType);

var expressionCall = Expression.Call(instance, methodInfo);

return Expression.Lambda<Func<object, object>>(
Expression.Convert(expressionCall, typeof(object)),
obj).Compile();
}

private static Action<object, object> BuildSetter(PropertyInfo propertyInfo)
{
var methodInfo = propertyInfo.GetSetMethod();
if (methodInfo == null)
{
// if the setter is not public 'GetSetMethod' will not find it
return null;
}
var obj = Expression.Parameter(typeof(object), "o");
// Require because we will know at runtime the declaring type
// so 'obj' is declared as typeof(object)
var instance = Expression.Convert(obj, methodInfo.DeclaringType);

var parameters = methodInfo.GetParameters();
if (parameters.Length != 1)
{
return null;
}
var value = Expression.Parameter(typeof(object));
var argument = Expression.Convert(value, parameters[0].ParameterType);

var expressionCall = Expression.Call(instance, methodInfo, argument);

return Expression.Lambda<Action<object, object>>(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❤️

expressionCall,
obj,
value).Compile();
}
}
}
11 changes: 8 additions & 3 deletions src/runtime/runtime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public static IntPtr GetProcAddress(IntPtr dllHandle, string name)
public class Runtime
{
// C# compiler copies constants to the assemblies that references this library.
// We needs to replace all public constants to static readonly fields to allow
// We needs to replace all public constants to static readonly fields to allow
// binary substitution of different Python.Runtime.dll builds in a target application.

public static int UCS => _UCS;
Expand All @@ -130,7 +130,7 @@ public class Runtime
#endif

// C# compiler copies constants to the assemblies that references this library.
// We needs to replace all public constants to static readonly fields to allow
// We needs to replace all public constants to static readonly fields to allow
// binary substitution of different Python.Runtime.dll builds in a target application.

public static string pyversion => _pyversion;
Expand Down Expand Up @@ -173,7 +173,7 @@ public class Runtime
#endif

// C# compiler copies constants to the assemblies that references this library.
// We needs to replace all public constants to static readonly fields to allow
// We needs to replace all public constants to static readonly fields to allow
// binary substitution of different Python.Runtime.dll builds in a target application.

public static readonly string PythonDLL = _PythonDll;
Expand Down Expand Up @@ -1565,6 +1565,11 @@ internal static bool PyObject_TypeCheck(IntPtr ob, IntPtr tp)
return (t == tp) || PyType_IsSubtype(t, tp);
}

internal static bool PyObjectType_TypeCheck(IntPtr type, IntPtr tp)
{
return (type == tp) || PyType_IsSubtype(type, tp);
}

[DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)]
internal static extern IntPtr PyType_GenericNew(IntPtr type, IntPtr args, IntPtr kw);

Expand Down