-
Notifications
You must be signed in to change notification settings - Fork 762
Modernize import hook #1369
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
Modernize import hook #1369
Changes from 1 commit
a321daa
279b535
f92e95b
afffc18
d821c0f
e469a8a
685b972
be81364
73958ed
e71a0ef
2af066d
bb490bf
31ea876
c02d5c6
970a189
059605b
ff170e9
63de923
624f7e3
bd7e745
46a85fe
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -108,9 +108,9 @@ internal static void RestoreRuntimeData(RuntimeDataStorage storage) | |
storage.GetValue("py_clr_module", out py_clr_module); | ||
var rootHandle = storage.GetValue<IntPtr>("root"); | ||
root = (CLRModule)ManagedType.GetManagedObject(rootHandle); | ||
IntPtr dict = Runtime.PyImport_GetModuleDict(); | ||
Runtime.PyDict_SetItemString(dict, "CLR", py_clr_module); | ||
Runtime.PyDict_SetItemString(dict, "clr", py_clr_module); | ||
BorrowedReference dict = Runtime.PyImport_GetModuleDict(); | ||
Runtime.PyDict_SetItemString(dict.DangerousGetAddress(), "CLR", py_clr_module); | ||
Runtime.PyDict_SetItemString(dict.DangerousGetAddress(), "clr", py_clr_module); | ||
SetupNamespaceTracking(); | ||
} | ||
|
||
|
@@ -122,15 +122,15 @@ internal static void RestoreRuntimeData(RuntimeDataStorage storage) | |
/// </summary> | ||
static void SetupNamespaceTracking () | ||
{ | ||
var newset = Runtime.PySet_New(IntPtr.Zero); | ||
var newset = Runtime.PySet_New(new BorrowedReference(IntPtr.Zero)); | ||
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. You Dispose in a finally; should this be a 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. NIT: I'd replace |
||
try | ||
{ | ||
foreach (var ns in AssemblyManager.GetNamespaces()) | ||
{ | ||
var pyNs = Runtime.PyString_FromString(ns); | ||
try | ||
{ | ||
if(Runtime.PySet_Add(newset, pyNs) != 0) | ||
if(Runtime.PySet_Add(newset, new BorrowedReference(pyNs)) != 0) | ||
{ | ||
throw new PythonException(); | ||
} | ||
|
@@ -141,14 +141,14 @@ static void SetupNamespaceTracking () | |
} | ||
} | ||
|
||
if(Runtime.PyDict_SetItemString(root.dict, availableNsKey, newset) != 0) | ||
if(Runtime.PyDict_SetItemString(root.dict, availableNsKey, newset.DangerousGetAddress()) != 0) | ||
{ | ||
throw new PythonException(); | ||
} | ||
} | ||
finally | ||
{ | ||
Runtime.XDecref(newset); | ||
newset.Dispose(); | ||
} | ||
|
||
AssemblyManager.namespaceAdded += OnNamespaceAdded; | ||
|
@@ -163,17 +163,13 @@ static void TeardownNameSpaceTracking() | |
{ | ||
AssemblyManager.namespaceAdded -= OnNamespaceAdded; | ||
// If the C# runtime isn't loaded, then there is no namespaces available | ||
if ((Runtime.PyDict_DelItemString(root.dict, availableNsKey) != 0) && | ||
if ((Runtime.PyDict_DelItemString(new BorrowedReference(root.dict), availableNsKey) != 0) && | ||
(Exceptions.ExceptionMatches(Exceptions.KeyError))) | ||
{ | ||
// Trying to remove a key that's not in the dictionary | ||
// raises an error. We don't care about it. | ||
Runtime.PyErr_Clear(); | ||
} | ||
else if (Exceptions.ErrorOccurred()) | ||
{ | ||
throw new PythonException(); | ||
} | ||
} | ||
|
||
static void OnNamespaceAdded (string name) | ||
|
@@ -183,10 +179,10 @@ static void OnNamespaceAdded (string name) | |
var pyNs = Runtime.PyString_FromString(name); | ||
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. NIT: |
||
try | ||
{ | ||
var nsSet = Runtime.PyDict_GetItemString(root.dict, availableNsKey); | ||
if (nsSet != IntPtr.Zero) | ||
var nsSet = Runtime.PyDict_GetItemString(new BorrowedReference(root.dict), availableNsKey); | ||
if (!nsSet.IsNull) | ||
{ | ||
if(Runtime.PySet_Add(nsSet, pyNs) != 0) | ||
if(Runtime.PySet_Add(nsSet, new BorrowedReference(pyNs)) != 0) | ||
{ | ||
throw new PythonException(); | ||
} | ||
|
@@ -211,16 +207,15 @@ internal static void UpdateCLRModuleDict() | |
// update the module dictionary with the contents of the root dictionary | ||
root.LoadNames(); | ||
BorrowedReference py_mod_dict = Runtime.PyModule_GetDict(ClrModuleReference); | ||
using (var clr_dict = Runtime.PyObject_GenericGetDict(root.ObjectReference)) | ||
{ | ||
Runtime.PyDict_Update(py_mod_dict, clr_dict); | ||
} | ||
using var clr_dict = Runtime.PyObject_GenericGetDict(root.ObjectReference); | ||
|
||
Runtime.PyDict_Update(py_mod_dict, clr_dict); | ||
} | ||
|
||
/// <summary> | ||
/// Return the clr python module (new reference) | ||
/// </summary> | ||
public static unsafe NewReference GetCLRModule(BorrowedReference fromList = default) | ||
public static unsafe NewReference GetCLRModule() | ||
{ | ||
UpdateCLRModuleDict(); | ||
Runtime.XIncref(py_clr_module); | ||
|
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.
I'd add a property
ClrModuleReference
that returns aBorrowedReference
ofpy_clr_module
, and then used it inPyDict_SetItemString
overload, that works withBorrowedReferences
to avoid usingDangerous*
methods.