- Notifications
You must be signed in to change notification settings - Fork768
1783 Implement Interface And Inherit Class#2028
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes from1 commit
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
Added support for multiple inheritance when inheriting from one base class and/or multiple interfaces.Added a unit test verifying that it works with a simple class and interface. This unit test would previously have failed since multiple types are in the class super class list.
- Loading branch information
Uh oh!
There was an error while loading.Please reload this page.
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -374,7 +374,7 @@ static PyTuple GetBaseTypeTuple(Type clrType) | ||
| return new PyTuple(bases); | ||
| } | ||
| internal static NewReference CreateSubType(BorrowedReference py_name,IList<ClassBase> py_base_type, IList<Type> interfaces, BorrowedReference dictRef) | ||
| { | ||
| // Utility to create a subtype of a managed type with the ability for the | ||
| // a python subtype able to override the managed implementation | ||
| @@ -415,17 +415,12 @@ internal static NewReference CreateSubType(BorrowedReference py_name, BorrowedRe | ||
| } | ||
| // create the new managed type subclassing the base managed type | ||
| var baseClass = py_base_type.FirstOrDefault(); | ||
rmadsen-ks marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| return ReflectedClrType.CreateSubclass(baseClass, interfaces, name, | ||
| ns: (string?)namespaceStr, | ||
| assembly: (string?)assembly, | ||
| dict: dictRef); | ||
| } | ||
| internal static IntPtr WriteMethodDef(IntPtr mdef, IntPtr name, IntPtr func, PyMethodFlags flags, IntPtr doc) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,8 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Diagnostics; | ||
| using System.Linq; | ||
| using System.Reflection; | ||
| using System.Runtime.InteropServices; | ||
| using System.Runtime.Serialization; | ||
| @@ -79,49 +82,56 @@ public static NewReference tp_new(BorrowedReference tp, BorrowedReference args, | ||
| BorrowedReference bases = Runtime.PyTuple_GetItem(args, 1); | ||
| BorrowedReference dict = Runtime.PyTuple_GetItem(args, 2); | ||
| // Extract interface types and base class types. | ||
| var interfaces = new List<Type>(); | ||
Member There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. IMHO, repeating the same base interface multiple times should be an error. And it might be, add a test. ContributorAuthor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Test added. | ||
| var baseType = new List<ClassBase>(); | ||
rmadsen-ks marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| var cnt = Runtime.PyTuple_GetSize(bases); | ||
rmadsen-ks marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| for (uint i = 0; i < cnt; i++) | ||
| { | ||
| var base_type2 = Runtime.PyTuple_GetItem(bases, (int)i); | ||
| var cb2 = (ClassBase) GetManagedObject(base_type2); | ||
rmadsen-ks marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| if (cb2 != null) | ||
rmadsen-ks marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| { | ||
| if (cb2.type.Valid && cb2.type.Value.IsInterface) | ||
| interfaces.Add(cb2.type.Value); | ||
| else baseType.Add(cb2); | ||
| ||
| } | ||
Member There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. What if it is not a .NET class or interface? You can't simply ignore it. ContributorAuthor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Added exceptions here in the latest version. | ||
| } | ||
| // if the base type count is 0, there might still be interfaces to implement. | ||
| if (baseType.Count == 0) | ||
| { | ||
| baseType.Add(new ClassBase(typeof(object))); | ||
| } | ||
| // Multiple inheritance is not supported, unless the other types are interfaces | ||
| if (baseType.Count > 1) | ||
| { | ||
| return Exceptions.RaiseTypeError("cannot use multiple inheritance with managed classes"); | ||
| ||
| } | ||
| var cb = baseType[0]; | ||
| try | ||
| { | ||
| if (!cb.CanSubclass()) | ||
| { | ||
| return Exceptions.RaiseTypeError("delegates, enums and array types cannot be subclassed"); | ||
| } | ||
| } | ||
| catch (SerializationException) | ||
| { | ||
| return Exceptions.RaiseTypeError($"Underlying C# Base class {cb.type} has been deleted"); | ||
| } | ||
| BorrowedReference slots = Runtime.PyDict_GetItem(dict, PyIdentifier.__slots__); | ||
| if (slots != null) | ||
| { | ||
| return Exceptions.RaiseTypeError("subclasses of managed classes do not support __slots__"); | ||
| } | ||
| // If the base class has a parameterless constructor, or | ||
| ||
| // if __assembly__ or __namespace__ are in the class dictionary then create | ||
| // a managed sub type. | ||
| // This creates a new managed type that can be used from .net to call back | ||
| // into python. | ||
| @@ -130,10 +140,12 @@ public static NewReference tp_new(BorrowedReference tp, BorrowedReference args, | ||
| using var clsDict = new PyDict(dict); | ||
| if (clsDict.HasKey("__assembly__") || clsDict.HasKey("__namespace__")) | ||
| { | ||
| return TypeManager.CreateSubType(name,baseType, interfaces, clsDict); | ||
| } | ||
| } | ||
| var base_type = Runtime.PyTuple_GetItem(bases, 0); | ||
Member There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more.
ContributorAuthor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. How can bases be length 0? This should only be called when a C# type is subclassed, right? In this case, it should at least be length 1. | ||
| // otherwise just create a basic type without reflecting back into the managed side. | ||
| IntPtr func = Util.ReadIntPtr(Runtime.PyTypeType, TypeOffset.tp_new); | ||
| NewReference type = NativeCall.Call_3(func, tp, args, kw); | ||