Skip to content

Reworked Enum marshaling #1392

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
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ when .NET expects an integer [#1342][i1342]
- BREAKING: to call Python from .NET `Runtime.PythonDLL` property must be set to Python DLL name
or the DLL must be loaded in advance. This must be done before calling any other Python.NET functions.
- BREAKING: `PyObject.Length()` now raises a `PythonException` when object does not support a concept of length.
- BREAKING: disabled implicit conversion from C# enums to Python `int` and back.
One must now either use enum members (e.g. `MyEnum.Option`), or use enum constructor
(e.g. `MyEnum(42)` or `MyEnum(42, True)` when `MyEnum` does not have a member with value 42).
- Sign Runtime DLL with a strong name
- Implement loading through `clr_loader` instead of the included `ClrModule`, enables
support for .NET Core
Expand Down
8 changes: 8 additions & 0 deletions src/embed_tests/TestOperator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,14 @@ public void SymmetricalOperatorOverloads()
");
}

[Test]
public void EnumOperator()
{
PythonEngine.Exec($@"
from System.IO import FileAccess
c = FileAccess.Read | FileAccess.Write");
}

[Test]
public void OperatorOverloadMissingArgument()
{
Expand Down
68 changes: 68 additions & 0 deletions src/runtime/Codecs/EnumPyLongCodec.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
using System;

namespace Python.Runtime.Codecs
{
[Obsolete]
public sealed class EnumPyLongCodec : IPyObjectEncoder, IPyObjectDecoder
{
public static EnumPyLongCodec Instance { get; } = new EnumPyLongCodec();

public bool CanDecode(PyObject objectType, Type targetType)
{
return targetType.IsEnum
&& objectType.IsSubclass(new BorrowedReference(Runtime.PyLongType));
}

public bool CanEncode(Type type)
{
return type == typeof(object) || type == typeof(ValueType) || type.IsEnum;
}

public bool TryDecode<T>(PyObject pyObj, out T value)
{
value = default;
if (!typeof(T).IsEnum) return false;

Type etype = Enum.GetUnderlyingType(typeof(T));

if (!PyLong.IsLongType(pyObj)) return false;

object result;
try
{
result = pyObj.AsManagedObject(etype);
}
catch (InvalidCastException)
{
return false;
}

if (Enum.IsDefined(typeof(T), result) || typeof(T).IsFlagsEnum())
{
value = (T)Enum.ToObject(typeof(T), result);
return true;
}

return false;
}

public PyObject TryEncode(object value)
{
if (value is null) return null;

var enumType = value.GetType();
if (!enumType.IsEnum) return null;

try
{
return new PyLong((long)value);
}
catch (InvalidCastException)
{
return new PyLong((ulong)value);
}
}

private EnumPyLongCodec() { }
}
}
11 changes: 11 additions & 0 deletions src/runtime/classmanager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,17 @@ private static ClassInfo GetClassInfo(Type type)
}
}

// only [Flags] enums support bitwise operations
if (type.IsEnum && type.IsFlagsEnum())
{
var opsImpl = typeof(EnumOps<>).MakeGenericType(type);
foreach (var op in opsImpl.GetMethods(OpsHelper.BindingFlags))
{
local[op.Name] = 1;
}
info = info.Concat(opsImpl.GetMethods(OpsHelper.BindingFlags)).ToArray();
}

// Now again to filter w/o losing overloaded member info
for (i = 0; i < info.Length; i++)
{
Expand Down
47 changes: 42 additions & 5 deletions src/runtime/classobject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,9 @@ internal NewReference GetDocString()
/// <summary>
/// Implements __new__ for reflected classes and value types.
/// </summary>
public static IntPtr tp_new(IntPtr tp, IntPtr args, IntPtr kw)
public static IntPtr tp_new(IntPtr tpRaw, IntPtr args, IntPtr kw)
{
var tp = new BorrowedReference(tpRaw);
var self = GetManagedObject(tp) as ClassObject;

// Sanity check: this ensures a graceful error if someone does
Expand Down Expand Up @@ -87,7 +88,7 @@ public static IntPtr tp_new(IntPtr tp, IntPtr args, IntPtr kw)
return IntPtr.Zero;
}

return CLRObject.GetInstHandle(result, tp);
return CLRObject.GetInstHandle(result, tp).DangerousMoveToPointerOrNull();
}

if (type.IsAbstract)
Expand All @@ -98,8 +99,7 @@ public static IntPtr tp_new(IntPtr tp, IntPtr args, IntPtr kw)

if (type.IsEnum)
{
Exceptions.SetError(Exceptions.TypeError, "cannot instantiate enumeration");
return IntPtr.Zero;
return NewEnum(type, new BorrowedReference(args), tp).DangerousMoveToPointerOrNull();
}

object obj = self.binder.InvokeRaw(IntPtr.Zero, args, kw);
Expand All @@ -108,7 +108,44 @@ public static IntPtr tp_new(IntPtr tp, IntPtr args, IntPtr kw)
return IntPtr.Zero;
}

return CLRObject.GetInstHandle(obj, tp);
return CLRObject.GetInstHandle(obj, tp).DangerousMoveToPointerOrNull();
}

private static NewReference NewEnum(Type type, BorrowedReference args, BorrowedReference tp)
{
nint argCount = Runtime.PyTuple_Size(args);
bool allowUnchecked = false;
if (argCount == 2)
{
var allow = Runtime.PyTuple_GetItem(args, 1);
if (!Converter.ToManaged(allow, typeof(bool), out var allowObj, true) || allowObj is null)
{
Exceptions.RaiseTypeError("second argument to enum constructor must be a boolean");
return default;
}
allowUnchecked |= (bool)allowObj;
}

if (argCount < 1 || argCount > 2)
{
Exceptions.SetError(Exceptions.TypeError, "no constructors match given arguments");
return default;
}

var op = Runtime.PyTuple_GetItem(args, 0);
if (!Converter.ToManaged(op, type.GetEnumUnderlyingType(), out object result, true))
{
return default;
}

if (!allowUnchecked && !Enum.IsDefined(type, result) && !type.IsFlagsEnum())
{
Exceptions.SetError(Exceptions.ValueError, "Invalid enumeration value. Pass True as the second argument if unchecked conversion is desired");
return default;
}

object enumValue = Enum.ToObject(type, result);
return CLRObject.GetInstHandle(enumValue, tp);
}


Expand Down
74 changes: 30 additions & 44 deletions src/runtime/converter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ private Converter()
private static Type int16Type;
private static Type int32Type;
private static Type int64Type;
private static Type flagsType;
private static Type boolType;
private static Type typeType;

Expand All @@ -42,7 +41,6 @@ static Converter()
singleType = typeof(Single);
doubleType = typeof(Double);
decimalType = typeof(Decimal);
flagsType = typeof(FlagsAttribute);
boolType = typeof(Boolean);
typeType = typeof(Type);
}
Expand Down Expand Up @@ -148,7 +146,8 @@ internal static IntPtr ToPython(object value, Type type)
return result;
}

if (Type.GetTypeCode(type) == TypeCode.Object && value.GetType() != typeof(object)) {
if (Type.GetTypeCode(type) == TypeCode.Object && value.GetType() != typeof(object)
|| type.IsEnum) {
var encoded = PyObjectConversions.TryEncode(value, type);
if (encoded != null) {
result = encoded.Handle;
Expand Down Expand Up @@ -203,6 +202,11 @@ internal static IntPtr ToPython(object value, Type type)

type = value.GetType();

if (type.IsEnum)
{
return CLRObject.GetInstHandle(value, type);
}

TypeCode tc = Type.GetTypeCode(type);

switch (tc)
Expand Down Expand Up @@ -317,6 +321,18 @@ internal static bool ToManaged(IntPtr value, Type type,
}
return Converter.ToManagedValue(value, type, out result, setError);
}
/// <summary>
/// Return a managed object for the given Python object, taking funny
/// byref types into account.
/// </summary>
/// <param name="value">A Python object</param>
/// <param name="type">The desired managed type</param>
/// <param name="result">Receives the managed object</param>
/// <param name="setError">If true, call <c>Exceptions.SetError</c> with the reason for failure.</param>
/// <returns>True on success</returns>
internal static bool ToManaged(BorrowedReference value, Type type,
out object result, bool setError)
=> ToManaged(value.DangerousGetAddress(), type, out result, setError);

internal static bool ToManagedValue(BorrowedReference value, Type obType,
out object result, bool setError)
Expand Down Expand Up @@ -398,11 +414,6 @@ internal static bool ToManagedValue(IntPtr value, Type obType,
return ToArray(value, obType, out result, setError);
}

if (obType.IsEnum)
{
return ToEnum(value, obType, out result, setError);
}

// Conversion to 'Object' is done based on some reasonable default
// conversions (Python string -> managed string, Python int -> Int32 etc.).
if (obType == objectType)
Expand Down Expand Up @@ -497,7 +508,7 @@ internal static bool ToManagedValue(IntPtr value, Type obType,
}

TypeCode typeCode = Type.GetTypeCode(obType);
if (typeCode == TypeCode.Object)
if (typeCode == TypeCode.Object || obType.IsEnum)
{
IntPtr pyType = Runtime.PyObject_TYPE(value);
if (PyObjectConversions.TryDecode(value, pyType, obType, out result))
Expand All @@ -516,8 +527,17 @@ internal static bool ToManagedValue(IntPtr value, Type obType,
/// </summary>
private static bool ToPrimitive(IntPtr value, Type obType, out object result, bool setError)
{
TypeCode tc = Type.GetTypeCode(obType);
result = null;
if (obType.IsEnum)
{
if (setError)
{
Exceptions.SetError(Exceptions.TypeError, "since Python.NET 3.0 int can not be converted to Enum implicitly. Use Enum(int_value)");
}
return false;
}

TypeCode tc = Type.GetTypeCode(obType);
IntPtr op = IntPtr.Zero;

switch (tc)
Expand Down Expand Up @@ -876,40 +896,6 @@ private static bool ToArray(IntPtr value, Type obType, out object result, bool s
result = items;
return true;
}


/// <summary>
/// Convert a Python value to a correctly typed managed enum instance.
/// </summary>
private static bool ToEnum(IntPtr value, Type obType, out object result, bool setError)
{
Type etype = Enum.GetUnderlyingType(obType);
result = null;

if (!ToPrimitive(value, etype, out result, setError))
{
return false;
}

if (Enum.IsDefined(obType, result))
{
result = Enum.ToObject(obType, result);
return true;
}

if (obType.GetCustomAttributes(flagsType, true).Length > 0)
{
result = Enum.ToObject(obType, result);
return true;
}

if (setError)
{
Exceptions.SetError(Exceptions.ValueError, "invalid enumeration value");
}

return false;
}
}

public static class ConverterExtension
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/exceptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ public static void Clear()
public static void warn(string message, IntPtr exception, int stacklevel)
{
if (exception == IntPtr.Zero ||
(Runtime.PyObject_IsSubclass(exception, Exceptions.Warning) != 1))
(Runtime.PyObject_IsSubclass(new BorrowedReference(exception), new BorrowedReference(Exceptions.Warning)) != 1))
{
Exceptions.RaiseTypeError("Invalid exception");
}
Expand Down
16 changes: 11 additions & 5 deletions src/runtime/operatormethod.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ static OperatorMethod()
["op_OnesComplement"] = new SlotDefinition("__invert__", TypeOffset.nb_invert),
["op_UnaryNegation"] = new SlotDefinition("__neg__", TypeOffset.nb_negative),
["op_UnaryPlus"] = new SlotDefinition("__pos__", TypeOffset.nb_positive),
["op_OneComplement"] = new SlotDefinition("__invert__", TypeOffset.nb_invert),
};
ComparisonOpMap = new Dictionary<string, string>
{
Expand Down Expand Up @@ -80,7 +79,7 @@ public static void Shutdown()

public static bool IsOperatorMethod(MethodBase method)
{
if (!method.IsSpecialName)
if (!method.IsSpecialName && !method.IsOpsHelper())
{
return false;
}
Expand All @@ -102,7 +101,12 @@ public static void FixupSlots(IntPtr pyType, Type clrType)
{
const BindingFlags flags = BindingFlags.Public | BindingFlags.Static;
Debug.Assert(_opType != null);
foreach (var method in clrType.GetMethods(flags))

var staticMethods =
clrType.IsEnum ? typeof(EnumOps<>).MakeGenericType(clrType).GetMethods(flags)
: clrType.GetMethods(flags);

foreach (var method in staticMethods)
{
// We only want to override slots for operators excluding
// comparison operators, which are handled by ClassBase.tp_richcompare.
Expand Down Expand Up @@ -170,9 +174,11 @@ public static string ReversePyMethodName(string pyName)
/// <returns></returns>
public static bool IsReverse(MethodInfo method)
{
Type declaringType = method.DeclaringType;
Type primaryType = method.IsOpsHelper()
? method.DeclaringType.GetGenericArguments()[0]
: method.DeclaringType;
Type leftOperandType = method.GetParameters()[0].ParameterType;
return leftOperandType != declaringType;
return leftOperandType != primaryType;
}

public static void FilterMethods(MethodInfo[] methods, out MethodInfo[] forwardMethods, out MethodInfo[] reverseMethods)
Expand Down
Loading