Skip to content

Commit 6fa03af

Browse files
committed
Remove superfluous code from ClassBase, moved to TypeManager
1 parent 3ac46de commit 6fa03af

3 files changed

Lines changed: 6 additions & 129 deletions

File tree

src/runtime/Runtime.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,6 @@ internal static void Initialize(bool initSigs = false)
151151

152152
GenericUtil.Reset();
153153
ClassManager.Reset();
154-
ClassBase.Reset();
155154
ClassDerivedObject.Reset();
156155
TypeManager.Initialize();
157156
CLRObject.creationBlocked = false;
@@ -281,7 +280,6 @@ internal static void Shutdown()
281280

282281
NullGCHandles(ExtensionType.loadedExtensions);
283282
ClassManager.RemoveClasses();
284-
ClassBase.Reset();
285283
TypeManager.RemoveTypes();
286284
_typesInitialized = false;
287285

src/runtime/TypeManager.cs

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,7 @@ internal static void Initialize()
218218
{
219219
Debug.Assert(cache.Count == 0, "Cache should be empty",
220220
"Some errors may occurred on last shutdown");
221+
dynamicMemberAccessor.Clear();
221222
using (var plainType = SlotHelper.CreateObjectType())
222223
{
223224
subtype_traverse = Util.ReadIntPtr(plainType.Borrow(), TypeOffset.tp_traverse);
@@ -241,6 +242,8 @@ internal static void RemoveTypes()
241242
}
242243
}
243244

245+
dynamicMemberAccessor.Clear();
246+
244247
foreach (var type in cache.Values)
245248
{
246249
type.Dispose();
@@ -480,12 +483,6 @@ internal static void InitializeClass(PyType type, ClassBase impl, Type clrType)
480483

481484
impl.InitializeSlots(type, slotsHolder);
482485

483-
if (typeof(IDynamicMetaObjectProvider).IsAssignableFrom(clrType)
484-
&& !typeof(IPythonDerivedType).IsAssignableFrom(clrType))
485-
{
486-
InitializeSlot(type, TypeOffset.tp_getattro, new Interop.BB_N(tp_getattro_dlr_proxy), slotsHolder);
487-
}
488-
489486
OperatorMethod.FixupSlots(type, clrType);
490487
// Leverage followup initialization from the Python runtime. Note
491488
// that the type of the new type must PyType_Type at the time we
@@ -496,19 +493,10 @@ internal static void InitializeClass(PyType type, ClassBase impl, Type clrType)
496493
throw PythonException.ThrowLastAsClrException();
497494
}
498495

499-
if (typeof(IDynamicMetaObjectProvider).IsAssignableFrom(clrType)
500-
&& !typeof(IPythonDerivedType).IsAssignableFrom(clrType))
496+
if (typeof(IDynamicMetaObjectProvider).IsAssignableFrom(clrType))
501497
{
502-
MethodInfo? setMethod = typeof(TypeManager).GetMethod(
503-
nameof(tp_setattro_dlr_proxy),
504-
BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public);
505-
506-
if (setMethod is null)
507-
{
508-
throw new MissingMethodException("DLR attribute slot handlers were not found");
509-
}
510-
511-
InitializeSlot(type, TypeOffset.tp_setattro, setMethod, slotsHolder);
498+
InitializeSlot(type, TypeOffset.tp_getattro, new Interop.BB_N(tp_getattro_dlr_proxy), slotsHolder);
499+
InitializeSlot(type, TypeOffset.tp_setattro, new Interop.BBB_I32(tp_setattro_dlr_proxy), slotsHolder);
512500
Runtime.PyType_Modified(type.Reference);
513501
}
514502

src/runtime/Types/ClassBase.cs

Lines changed: 0 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,6 @@ namespace Python.Runtime
2323
[Serializable]
2424
internal class ClassBase : ManagedType, IDeserializationCallback
2525
{
26-
static readonly DynamicObjectMemberAccessor dynamicMemberAccessor = new();
27-
28-
internal static void Reset() => dynamicMemberAccessor.Clear();
29-
3026
[NonSerialized]
3127
internal List<string> dotNetMembers = new();
3228
internal Indexer? indexer;
@@ -608,105 +604,6 @@ static IEnumerable<MethodInfo> GetCallImplementations(Type type)
608604
=> type.GetMethods(BindingFlags.Public | BindingFlags.Instance)
609605
.Where(m => m.Name == "__call__");
610606

611-
static NewReference tp_getattro_dlr(BorrowedReference ob, BorrowedReference key)
612-
{
613-
var attr = Runtime.PyObject_GenericGetAttr(ob, key);
614-
if (!attr.IsNull())
615-
{
616-
return attr;
617-
}
618-
619-
// Only run the DLR binder if the error was AttributeError, otherwise preserve the original error
620-
if (Runtime.PyErr_ExceptionMatches(Exceptions.AttributeError) == 0)
621-
{
622-
return default;
623-
}
624-
625-
if (!Runtime.PyString_Check(key))
626-
{
627-
return default;
628-
}
629-
630-
if (GetManagedObject(ob) is not CLRObject co)
631-
{
632-
return default;
633-
}
634-
635-
// Slot registration already guarantees this type supports DLR
636-
var dynamicObject = (IDynamicMetaObjectProvider)co.inst;
637-
638-
string? memberName = Runtime.GetManagedString(key);
639-
if (memberName is null)
640-
{
641-
return default;
642-
}
643-
644-
if (!dynamicMemberAccessor.TryGetMember(dynamicObject, memberName, out object? value))
645-
{
646-
return default;
647-
}
648-
649-
// Clear the lingering AttributeError
650-
Runtime.PyErr_Clear();
651-
652-
using var pyValue = value.ToPython();
653-
return pyValue.NewReferenceOrNull();
654-
}
655-
656-
static int tp_setattro_dlr(BorrowedReference ob, BorrowedReference key, BorrowedReference val)
657-
{
658-
int result = Runtime.PyObject_GenericSetAttr(ob, key, val);
659-
if (result == 0)
660-
{
661-
return 0;
662-
}
663-
664-
// Preserve non-attribute errors exactly as they are.
665-
if (Runtime.PyErr_ExceptionMatches(Exceptions.AttributeError) == 0)
666-
{
667-
return -1;
668-
}
669-
670-
// Deletion fallback is intentionally not handled by DLR binder yet.
671-
if (val == null)
672-
{
673-
return -1;
674-
}
675-
676-
if (!Runtime.PyString_Check(key))
677-
{
678-
return -1;
679-
}
680-
681-
if (GetManagedObject(ob) is not CLRObject co)
682-
{
683-
return -1;
684-
}
685-
686-
// Slot registration already guarantees this type supports DLR.
687-
var dynamicObject = (IDynamicMetaObjectProvider)co.inst;
688-
689-
string? memberName = Runtime.GetManagedString(key);
690-
if (memberName is null)
691-
{
692-
return -1;
693-
}
694-
695-
if (!Converter.ToManaged(val, typeof(object), out object? managedValue, true))
696-
{
697-
return -1;
698-
}
699-
700-
if (!dynamicMemberAccessor.TrySetMember(dynamicObject, memberName, managedValue))
701-
{
702-
return -1;
703-
}
704-
705-
// Clear the lingering AttributeError
706-
Runtime.PyErr_Clear();
707-
return 0;
708-
}
709-
710607
public virtual void InitializeSlots(BorrowedReference pyType, SlotsHolder slotsHolder)
711608
{
712609
if (!this.type.Valid) return;
@@ -716,12 +613,6 @@ public virtual void InitializeSlots(BorrowedReference pyType, SlotsHolder slotsH
716613
TypeManager.InitializeSlotIfEmpty(pyType, TypeOffset.tp_call, new Interop.BBB_N(tp_call_impl), slotsHolder);
717614
}
718615

719-
if (typeof(IDynamicMetaObjectProvider).IsAssignableFrom(this.type.Value))
720-
{
721-
TypeManager.InitializeSlotIfEmpty(pyType, TypeOffset.tp_getattro, new Interop.BB_N(tp_getattro_dlr), slotsHolder);
722-
TypeManager.InitializeSlotIfEmpty(pyType, TypeOffset.tp_setattro, new Interop.BBB_I32(tp_setattro_dlr), slotsHolder);
723-
}
724-
725616
if (indexer is not null)
726617
{
727618
if (indexer.CanGet)

0 commit comments

Comments
 (0)