| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
| using System; | ||
| using System.Collections; | ||
| using System.Collections.Generic; | ||
| using System.Diagnostics; | ||
| using System.Runtime.InteropServices; | ||
| using System.Reflection; | ||
| using System.Text; | ||
| Expand Down Expand Up | @@ -67,11 +68,47 @@ public ModulePropertyAttribute() | |
| } | ||
| } | ||
|
|
||
| internal static class ManagedDataOffsets | ||
| { | ||
| static ManagedDataOffsets() | ||
| { | ||
| FieldInfo[] fi = typeof(ManagedDataOffsets).GetFields(BindingFlags.Static | BindingFlags.Public); | ||
| for (int i = 0; i < fi.Length; i++) | ||
| { | ||
| fi[i].SetValue(null, -(i * IntPtr.Size) - IntPtr.Size); | ||
| } | ||
|
|
||
| [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)] | ||
| internal class ObjectOffset | ||
| size = fi.Length * IntPtr.Size; | ||
| } | ||
|
|
||
| public static readonly int ob_data; | ||
| public static readonly int ob_dict; | ||
|
Comment thread
Copy link
Copy Markdown
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. Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low QualityI think ob_dict and ob_data are never being assigned?
Sorry, something went wrong.
All reactions
Copy link
Copy Markdown
Author
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. Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low QualityThese are assigned via reflection in the static class constructor.
Sorry, something went wrong.
All reactions
Copy link
Copy Markdown
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. Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low QualityThese should likely be private and perhaps a comment stating that they're assigned/accessed via reflection.
Sorry, something went wrong.
All reactions
Copy link
Copy Markdown
Author
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. Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality@mchandschuh the visibility on these follows visibility on similar fields in other *Offset classes. One of the reasons for that is for reflection to be able to discern between fields mapping to python object fields, and fields, that are necessary for this class methods to function properly. I don't think comment here will do them any good, as in the next implementation they might be set by different means, and comments like this tend to be overlooked. I intend to keep this part as is, if there are no further objections.
Sorry, something went wrong.
All reactions
|
||
|
|
||
| private static int BaseOffset(IntPtr type) | ||
| { | ||
| Debug.Assert(type != IntPtr.Zero); | ||
|
Comment thread
Copy link
Copy Markdown
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. Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality@Martin-Molinero -- Do we build w/ #DEBUG compiler flag in production?
Sorry, something went wrong.
All reactions
Copy link
Copy Markdown
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. Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low QualityNo 👍 we build targeting the ReleaseWin/Mono configuration that doesn't use the #Debug flag nor do we add it
Sorry, something went wrong.
All reactions
|
||
| int typeSize = Marshal.ReadInt32(type, TypeOffset.tp_basicsize); | ||
| Debug.Assert(typeSize > 0 && typeSize <= ExceptionOffset.Size()); | ||
| return typeSize; | ||
| } | ||
| public static int DataOffset(IntPtr type) | ||
| { | ||
| return BaseOffset(type) + ob_data; | ||
| } | ||
|
|
||
| public static int DictOffset(IntPtr type) | ||
| { | ||
| return BaseOffset(type) + ob_dict; | ||
| } | ||
|
|
||
| public static int Size { get { return size; } } | ||
|
|
||
| private static readonly int size; | ||
| } | ||
|
|
||
| internal static class OriginalObjectOffsets | ||
| { | ||
| static ObjectOffset() | ||
| static OriginalObjectOffsets() | ||
| { | ||
| int size = IntPtr.Size; | ||
| var n = 0; // Py_TRACE_REFS add two pointers to PyObject_HEAD | ||
| Expand All | @@ -82,39 +119,58 @@ static ObjectOffset() | |
| #endif | ||
| ob_refcnt = (n + 0) * size; | ||
| ob_type = (n + 1) * size; | ||
| ob_dict = (n + 2) * size; | ||
| ob_data = (n + 3) * size; | ||
| } | ||
|
|
||
| public static int magic(IntPtr ob) | ||
| public static int Size { get { return size; } } | ||
|
|
||
| private static readonly int size = | ||
| #if PYTHON_WITH_PYDEBUG | ||
| 4 * IntPtr.Size; | ||
| #else | ||
| 2 * IntPtr.Size; | ||
| #endif | ||
|
|
||
| #if PYTHON_WITH_PYDEBUG | ||
| public static int _ob_next; | ||
| public static int _ob_prev; | ||
| #endif | ||
| public static int ob_refcnt; | ||
| public static int ob_type; | ||
| } | ||
|
|
||
| [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)] | ||
| internal class ObjectOffset | ||
| { | ||
| static ObjectOffset() | ||
| { | ||
| #if PYTHON_WITH_PYDEBUG | ||
| _ob_next = OriginalObjectOffsets._ob_next; | ||
| _ob_prev = OriginalObjectOffsets._ob_prev; | ||
| #endif | ||
| ob_refcnt = OriginalObjectOffsets.ob_refcnt; | ||
| ob_type = OriginalObjectOffsets.ob_type; | ||
|
|
||
| size = OriginalObjectOffsets.Size + ManagedDataOffsets.Size; | ||
| } | ||
|
|
||
| public static int magic(IntPtr type) | ||
| { | ||
| if (IsException(ob)) | ||
| { | ||
| return ExceptionOffset.ob_data; | ||
| } | ||
| return ob_data; | ||
| return ManagedDataOffsets.DataOffset(type); | ||
| } | ||
|
|
||
| public static int DictOffset(IntPtr ob) | ||
| public static int TypeDictOffset(IntPtr type) | ||
| { | ||
| if (IsException(ob)) | ||
| { | ||
| return ExceptionOffset.ob_dict; | ||
| } | ||
| return ob_dict; | ||
| return ManagedDataOffsets.DictOffset(type); | ||
|
Comment thread
Copy link
Copy Markdown
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. Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low QualityCould you please provide more detailed comments on why TypeDictOffset() and magic() new implementations are equivalent to the previous?
Sorry, something went wrong.
All reactions
Copy link
Copy Markdown
Author
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. Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low QualityI mentioned that in the PR: Python.NET always adds these fields to the end of the objects it allocates. So offsets to them can be computed as objAddr + objSize - const, no matter the type of the object.
Sorry, something went wrong.
All reactions
Copy link
Copy Markdown
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. Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low QualityIt's nice to leave comments describing the why in the source code and even in the commit messages as it makes understanding the code later much easier. Consider in a time, far far away, where github ceases to exist, this perfect comment describing the why of these offsets would be lost forever :)
Sorry, something went wrong.
All reactions
Copy link
Copy Markdown
Author
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. Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low QualityThat's why I put the explanation into the commit message of the squashed commit.
Sorry, something went wrong.
All reactions
|
||
| } | ||
|
|
||
| public static int Size(IntPtr ob) | ||
| public static int Size(IntPtr pyType) | ||
| { | ||
| if (IsException(ob)) | ||
| if (IsException(pyType)) | ||
| { | ||
| return ExceptionOffset.Size(); | ||
| } | ||
| #if PYTHON_WITH_PYDEBUG | ||
| return 6 * IntPtr.Size; | ||
| #else | ||
| return 4 * IntPtr.Size; | ||
| #endif | ||
|
|
||
| return size; | ||
| } | ||
|
|
||
| #if PYTHON_WITH_PYDEBUG | ||
| Expand All | @@ -123,8 +179,7 @@ public static int Size(IntPtr ob) | |
| #endif | ||
| public static int ob_refcnt; | ||
| public static int ob_type; | ||
| private static int ob_dict; | ||
| private static int ob_data; | ||
| private static readonly int size; | ||
|
|
||
| private static bool IsException(IntPtr pyObject) | ||
| { | ||
| Expand All | @@ -141,19 +196,17 @@ internal class ExceptionOffset | |
| static ExceptionOffset() | ||
| { | ||
| Type type = typeof(ExceptionOffset); | ||
| FieldInfo[] fi = type.GetFields(); | ||
| int size = IntPtr.Size; | ||
| FieldInfo[] fi = type.GetFields(BindingFlags.Static | BindingFlags.Public); | ||
| for (int i = 0; i < fi.Length; i++) | ||
| { | ||
| fi[i].SetValue(null, (i * size) + ObjectOffset.ob_type + size); | ||
| fi[i].SetValue(null, (i * IntPtr.Size) + OriginalObjectOffsets.Size); | ||
|
Comment thread
Copy link
Copy Markdown
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. Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Qualitynit - why inline IntPtr.Size here -- it certainly safe to assume that the value doesn't change over the coarse of the loop.
Sorry, something went wrong.
All reactions
Copy link
Copy Markdown
Author
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. Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low QualitySaves a jump to the original line to figure out what this size is supposed to be.
Sorry, something went wrong.
All reactions
|
||
| } | ||
| } | ||
|
|
||
| public static int Size() | ||
| { | ||
| return ob_data + IntPtr.Size; | ||
| size = fi.Length * IntPtr.Size + OriginalObjectOffsets.Size + ManagedDataOffsets.Size; | ||
| } | ||
|
|
||
| public static int Size() { return size; } | ||
|
|
||
| // PyException_HEAD | ||
| // (start after PyObject_HEAD) | ||
| public static int dict = 0; | ||
| Expand All | @@ -167,9 +220,7 @@ public static int Size() | |
| public static int suppress_context = 0; | ||
| #endif | ||
|
|
||
| // extra c# data | ||
| public static int ob_dict; | ||
| public static int ob_data; | ||
| private static readonly int size; | ||
| } | ||
|
|
||
|
|
||
| Expand Down | ||
| Back | FazBrowse Home | New Git URL |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality👍 -- always nice to get credit!
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.