Skip to content

Multiple assertion failures for Python classes derived from System.Exception in debug builds #1218

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

Closed
lostmsu opened this issue Sep 11, 2020 · 6 comments

Comments

@lostmsu
Copy link
Member

lostmsu commented Sep 11, 2020

Environment

  • Pythonnet version: 2.5.1
  • Python version: 3.8
  • Operating System: Win 10 64

Details

In debug build (DebugWinPY3) this test:

class Sub(System.Exception):

fails this assertion:

Debug.Assert(typeSize > 0 && typeSize <= ExceptionOffset.Size());

Python type object for Sub class gets an unknown extra field of size IntPtr.Size, causing it to be larger than its base type object (reflected) System.Exception.

See also #1196 (comment) for previous discussion.

@koubaa @amos402

@lostmsu
Copy link
Member Author

lostmsu commented Sep 11, 2020

It seems that the reason is that System.Exception has no weak list associated with it:

Marshal.ReadInt32(type, TypeOffset.tp_weaklistoffset) // type == Sub
80
Marshal.ReadInt32(Marshal.ReadIntPtr(type, TypeOffset.tp_base), TypeOffset.tp_weaklistoffset)
0

@amos402
Copy link
Member

amos402 commented Sep 11, 2020

Because it used tp_basicsize as the slot to save GCHandle, you can test it on soft-shutdown branch, it should be fine.
https://github.com/amos402/pythonnet/blob/12c0206b94173935864e0434ec22898753ffb6b5/src/runtime/interop.cs#L116-L117
I changed the slot.

@lostmsu
Copy link
Member Author

lostmsu commented Sep 11, 2020

@amos402 not exactly sure what you mean. The reflected System.Exception should already have a slot for GCHandle. If that would be the reason, size of Sub would be equal to size of System.Exception, but it is not.

@amos402
Copy link
Member

amos402 commented Sep 11, 2020

image
Trust me, it fixed.
image

The reason is as I said. The size of Sub come from type(System.Exception)->tp_basicsize, they're not equal.

@koubaa
Copy link
Contributor

koubaa commented Sep 13, 2020

@lostmsu @amos402 I just took Amos's interop changes from the soft shutdown branch and applied them to master, and this issue goes away. I'll submit a PR soon with this change, even if it doesn't fix my PyType_FromSpec it could still make the soft shutdown branch easier to review without these changes

@koubaa
Copy link
Contributor

koubaa commented Oct 24, 2020

Fixed in master

@filmor filmor closed this as completed Oct 24, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants