8000 Added a few cleanups based on feedback. · pythonnet/pythonnet@4f42458 · GitHub
[go: up one dir, main page]

Skip to content

Commit 4f42458

Browse files
committed
Added a few cleanups based on feedback.
1 parent 000504a commit 4f42458

File tree

3 files changed

+19
-24
lines changed

3 files changed

+19
-24
lines changed

src/runtime/Runtime.cs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1836,12 +1836,6 @@ internal static void SetNoSiteFlag()
18361836
return *Delegates.Py_NoSiteFlag;
18371837
});
18381838
}
1839-
1840-
internal static uint PyTuple_GetSize(BorrowedReference tuple)
1841-
{
1842-
IntPtr r = Delegates.PyTuple_Size(tuple);
1843-
return (uint)r.ToInt32();
1844-
}
18451839
}
18461840

18471841
internal class BadPythonDllException : MissingMethodException

src/runtime/TypeManager.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ static PyTuple GetBaseTypeTuple(Type clrType)
374374
return new PyTuple(bases);
375375
}
376376

377-
internal static NewReference CreateSubType(BorrowedReference py_name, IList<ClassBase> py_base_type, IList<Type> interfaces, BorrowedReference dictRef)
377+
internal static NewReference CreateSubType(BorrowedReference py_name, ClassBase py_base_type, IList<Type> interfaces, BorrowedReference dictRef)
378378
{
379379
// Utility to create a subtype of a managed type with the ability for the
380380
// a python subtype able to override the managed implementation
@@ -415,9 +415,7 @@ internal static NewReference CreateSubType(BorrowedReference py_name, IList<Clas
415415
}
416416

417417
// create the new managed type subclassing the base managed type
418-
var baseClass = py_base_type.FirstOrDefault();
419-
420-
return ReflectedClrType.CreateSubclass(baseClass, interfaces, name,
418+
return ReflectedClrType.CreateSubclass(py_base_type, interfaces, name,
421419
ns: (string?)namespaceStr,
422420
assembly: (string?)assembly,
423421
dict: dictRef);

src/runtime/Types/MetaType.cs

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -84,34 +84,37 @@ public static NewReference tp_new(BorrowedReference tp, BorrowedReference args,
8484

8585
// Extract interface types and base class types.
8686
var interfaces = new List<Type>();
87-
var baseType = new List<ClassBase>();
8887

89-
var cnt = Runtime.PyTuple_GetSize(bases);
88+
// More than one base type case be declared, but an exception will be thrown
89+
// if more than one is a class/not an interface.
90+
var baseTypes = new List<ClassBase>();
9091

91-
for (uint i = 0; i < cnt; i++)
92+
var baseClassCount = Runtime.PyTuple_Size(bases);
93+
94+
for (nint i = 0; i < baseClassCount; i++)
9295
{
93-
var base_type2 = Runtime.PyTuple_GetItem(bases, (int)i);
94-
var cb2 = (ClassBase) GetManagedObject(base_type2);
95-
if (cb2 != null)
96+
var baseTypeIt = Runtime.PyTuple_GetItem(bases, (int)i);
97+
98+
if (GetManagedObject(baseTypeIt) is ClassBase classBaseIt)
9699
{
97-
if (cb2.type.Valid && cb2.type.Value.IsInterface)
98-
interfaces.Add(cb2.type.Value);
99-
else baseType.Add(cb2);
100+
if (classBaseIt.type.Valid && classBaseIt.type.Value.IsInterface)
101+
interfaces.Add(classBaseIt.type.Value);
102+
else baseTypes.Add(classBaseIt);
100103
}
101104
}
102105
// if the base type count is 0, there might still be interfaces to implement.
103-
if (baseType.Count == 0)
106+
if (baseTypes.Count == 0)
104107
{
105-
baseType.Add(new ClassBase(typeof(object)));
108+
baseTypes.Add(new ClassBase(typeof(object)));
106109
}
107110

108111
// Multiple inheritance is not supported, unless the other types are interfaces
109-
if (baseType.Count > 1)
112+
if (baseTypes.Count > 1)
110113
{
111114
return Exceptions.RaiseTypeError("cannot use multiple inheritance with managed classes");
112115
}
113116

114-
var cb = baseType[0];
117+
var cb = baseTypes[0];
115118
try
116119
{
117120
if (!cb.CanSubclass())
@@ -140,7 +143,7 @@ public static NewReference tp_new(BorrowedReference tp, BorrowedReference args,
140143
using var clsDict = new PyDict(dict);
141144
if (clsDict.HasKey("__assembly__") || clsDict.HasKey("__namespace__"))
142145
{
143-
return TypeManager.CreateSubType(name, baseType, interfaces, clsDict);
146+
return TypeManager.CreateSubType(name, baseTypes[0], interfaces, clsDict);
144147
}
145148
}
146149

0 commit comments

Comments
 (0)
0