forked from pythonnet/pythonnet
-
Notifications
You must be signed in to change notification settings - Fork 10
Uni 56832 another crash fix attempt #6
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
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -454,7 +454,7 @@ internal static IntPtr AllocateTypeObject(string name) | |
/// </summary> | ||
internal static void InitializeSlots(IntPtr type, Type impl) | ||
{ | ||
var seen = new Hashtable(8); | ||
var seen = new HashSet<string>(); | ||
Type offsetType = typeof(TypeOffset); | ||
|
||
while (impl != null) | ||
|
@@ -473,7 +473,7 @@ internal static void InitializeSlots(IntPtr type, Type impl) | |
continue; | ||
} | ||
|
||
if (seen[name] != null) | ||
if (seen.Contains(name)) | ||
{ | ||
continue; | ||
} | ||
|
@@ -484,11 +484,40 @@ internal static void InitializeSlots(IntPtr type, Type impl) | |
IntPtr slot = Interop.GetThunk(method); | ||
Marshal.WriteIntPtr(type, offset, slot); | ||
|
||
seen[name] = 1; | ||
seen.Add(name); | ||
} | ||
|
||
impl = impl.BaseType; | ||
} | ||
|
||
// Hack: for some objects (Classbase and ExtensionType) we need gc | ||
// support. We need to provide three functions: | ||
// - tp_traverse(object, ...) returns 0 | ||
// - tp_clear(object) returns 0 | ||
// - tp_is_gc(type) returns non-zero | ||
// In addition, these functions can get called after a domain reload, | ||
// so they must be implemented in native code. | ||
// | ||
// Py_GetVersion reliably returns non-zero and uses no arguments. | ||
// | ||
// PyAST_Check reads the object and returns zero unless it's a | ||
// python AST node. You could violate this assumption by making a C# | ||
// type that derives from ast.AST -- but it's not clear why you'd do that. | ||
if (seen.Contains("tp_traverse")) | ||
{ | ||
var lib = NativeMethods.LoadLibrary(Runtime.PythonDLL); | ||
var zeroslot = NativeMethods.GetProcAddress(lib, "PyAST_Check"); | ||
var trueslot = NativeMethods.GetProcAddress(lib, "Py_GetVersion"); | ||
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. What if the procs don't exist? I know it is unlikely, but should you catch an exception of some sort in that case? Also, I think it would be good to make sure PyAST_Check returns 0 and Py_GetVersion returns non-zero. Is there a way you could add an assertion in Debug perhaps? |
||
|
||
var offset = (int)offsetType.GetField("tp_traverse").GetValue(offsetType); | ||
Marshal.WriteIntPtr(type, offset, zeroslot); | ||
|
||
offset = (int)offsetType.GetField("tp_clear").GetValue(offsetType); | ||
Marshal.WriteIntPtr(type, offset, zeroslot); | ||
|
||
offset = (int)offsetType.GetField("tp_is_gc").GetValue(offsetType); | ||
Marshal.WriteIntPtr(type, offset, trueslot); | ||
} | ||
} | ||
|
||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Small detail but I think we should say "domain unload". Domain reload is kind of application-specific, it is not something that is part of the C# framework. In other words, it might be too "Unity-specific"