@@ -111,24 +111,33 @@ internal static void ClrGcHandleOffsetAssertSanity(int offset)
111111 /// <summary>
112112 /// Returns dict offset in instances of the specified <paramref name="type"/>
113113 /// </summary>
114- public static int TypeDictOffset ( BorrowedReference type )
115- => TypeDictOffset ( type . DangerousGetAddress ( ) ) ;
116- public static int TypeDictOffset ( IntPtr type ) {
117- Debug . Assert ( Runtime . PyType_Check ( type ) ) ;
118- return Runtime . PyType_IsSubtype ( type , Exceptions . BaseException )
114+ public static int TypeDictOffset ( BorrowedReference type ) {
115+ if ( ! Runtime . PyType_Check ( type ) )
116+ throw new ArgumentException ( "Bad object type" ) ;
117+
118+ return IsExceptionSubtype ( type )
119119 ? ExceptionOffset . ob_dict
120120 : ob_dict ;
121121 }
122+ public static int TypeDictOffset ( IntPtr type )
123+ => TypeDictOffset ( new BorrowedReference ( type ) ) ;
122124
123125 public static int Size ( IntPtr ob ) {
124126 if ( ( Runtime . PyObject_TypeCheck ( ob , Exceptions . BaseException ) ||
125- ( Runtime . PyType_Check ( ob ) && Runtime . PyType_IsSubtype ( ob , Exceptions . BaseException ) ) ) ) {
127+ ( Runtime . PyType_Check ( new BorrowedReference ( ob ) ) && IsExceptionSubtype ( new BorrowedReference ( ob ) ) ) ) ) {
126128 return ExceptionOffset . Size ( ) ;
127129 }
128130
129131 return PyObject_HEAD_Size ( ) ;
130132 }
131133
134+ static bool IsExceptionSubtype ( BorrowedReference type )
135+ {
136+ bool isException = Runtime . PyType_FastSubclass ( type , TypeFlags . BaseExceptionSubclass ) ;
137+ Debug . Assert ( Runtime . PyType_IsSubtype ( type . DangerousGetAddress ( ) , Exceptions . BaseException ) == isException ) ;
138+ return isException ;
139+ }
140+
132141 public static int PyObject_HEAD_Size ( ) {
133142#if PYTHON_WITH_PYDEBUG
134143 return 6 * IntPtr . Size ;
0 commit comments