| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -178,6 +178,7 @@ public void TestPythonException_Normalize_ThrowsWhenErrorSet() | |||
| 178 | 178 | var pythonException = PythonException.FetchCurrentRaw(); | |
| 179 | 179 | Exceptions.SetError(Exceptions.TypeError, "Another error"); | |
| 180 | 180 | Assert.Throws<InvalidOperationException>(() => pythonException.Normalize()); | |
| 181 | + Exceptions.Clear(); | ||
| 181 | 182 | } | |
| 182 | 183 | } | |
| 183 | 184 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -108,9 +108,9 @@ internal static void RestoreRuntimeData(RuntimeDataStorage storage) | |||
| 108 | 108 | storage.GetValue("py_clr_module", out py_clr_module); | |
| 109 | 109 | var rootHandle = storage.GetValue<IntPtr>("root"); | |
| 110 | 110 | root = (CLRModule)ManagedType.GetManagedObject(rootHandle); | |
| 111 | - IntPtr dict = Runtime.PyImport_GetModuleDict(); | ||
| 112 | - Runtime.PyDict_SetItemString(dict, "CLR", py_clr_module); | ||
| 113 | - Runtime.PyDict_SetItemString(dict, "clr", py_clr_module); | ||
| 111 | + BorrowedReference dict = Runtime.PyImport_GetModuleDict(); | ||
| 112 | + Runtime.PyDict_SetItemString(dict.DangerousGetAddress(), "CLR", py_clr_module); | ||
| 113 | + Runtime.PyDict_SetItemString(dict.DangerousGetAddress(), "clr", py_clr_module); | ||
| 114 | 114 | SetupNamespaceTracking(); | |
| 115 | 115 | } | |
| 116 | 116 | ||
@@ -122,15 +122,15 @@ internal static void RestoreRuntimeData(RuntimeDataStorage storage) | |||
| 122 | 122 | /// </summary> | |
| 123 | 123 | static void SetupNamespaceTracking () | |
| 124 | 124 | { | |
| 125 | - var newset = Runtime.PySet_New(IntPtr.Zero); | ||
| 125 | + var newset = Runtime.PySet_New(new BorrowedReference(IntPtr.Zero)); | ||
| 126 | 126 | try | |
| 127 | 127 | { | |
| 128 | 128 | foreach (var ns in AssemblyManager.GetNamespaces()) | |
| 129 | 129 | { | |
| 130 | 130 | var pyNs = Runtime.PyString_FromString(ns); | |
| 131 | 131 | try | |
| 132 | 132 | { | |
| 133 | - if(Runtime.PySet_Add(newset, pyNs) != 0) | ||
| 133 | + if(Runtime.PySet_Add(newset, new BorrowedReference(pyNs)) != 0) | ||
| 134 | 134 | { | |
| 135 | 135 | throw new PythonException(); | |
| 136 | 136 | } | |
@@ -141,14 +141,14 @@ static void SetupNamespaceTracking () | |||
| 141 | 141 | } | |
| 142 | 142 | } | |
| 143 | 143 | ||
| 144 | - if(Runtime.PyDict_SetItemString(root.dict, availableNsKey, newset) != 0) | ||
| 144 | + if(Runtime.PyDict_SetItemString(root.dict, availableNsKey, newset.DangerousGetAddress()) != 0) | ||
| 145 | 145 | { | |
| 146 | 146 | throw new PythonException(); | |
| 147 | 147 | } | |
| 148 | 148 | } | |
| 149 | 149 | finally | |
| 150 | 150 | { | |
| 151 | - Runtime.XDecref(newset); | ||
| 151 | + newset.Dispose(); | ||
| 152 | 152 | } | |
| 153 | 153 | ||
| 154 | 154 | AssemblyManager.namespaceAdded += OnNamespaceAdded; | |
@@ -163,17 +163,13 @@ static void TeardownNameSpaceTracking() | |||
| 163 | 163 | { | |
| 164 | 164 | AssemblyManager.namespaceAdded -= OnNamespaceAdded; | |
| 165 | 165 | // If the C# runtime isn't loaded, then there is no namespaces available | |
| 166 | - if ((Runtime.PyDict_DelItemString(root.dict, availableNsKey) != 0) && | ||
| 166 | + if ((Runtime.PyDict_DelItemString(new BorrowedReference(root.dict), availableNsKey) != 0) && | ||
| 167 | 167 | (Exceptions.ExceptionMatches(Exceptions.KeyError))) | |
| 168 | 168 | { | |
| 169 | 169 | // Trying to remove a key that's not in the dictionary | |
| 170 | 170 | // raises an error. We don't care about it. | |
| 171 | 171 | Runtime.PyErr_Clear(); | |
| 172 | 172 | } | |
| 173 | - else if (Exceptions.ErrorOccurred()) | ||
| 174 | - { | ||
| 175 | - throw new PythonException(); | ||
| 176 | - } | ||
| 177 | 173 | } | |
| 178 | 174 | ||
| 179 | 175 | static void OnNamespaceAdded (string name) | |
@@ -183,10 +179,10 @@ static void OnNamespaceAdded (string name) | |||
| 183 | 179 | var pyNs = Runtime.PyString_FromString(name); | |
| 184 | 180 | try | |
| 185 | 181 | { | |
| 186 | - var nsSet = Runtime.PyDict_GetItemString(root.dict, availableNsKey); | ||
| 187 | - if (nsSet != IntPtr.Zero) | ||
| 182 | + var nsSet = Runtime.PyDict_GetItemString(new BorrowedReference(root.dict), availableNsKey); | ||
| 183 | + if (!nsSet.IsNull) | ||
| 188 | 184 | { | |
| 189 | - if(Runtime.PySet_Add(nsSet, pyNs) != 0) | ||
| 185 | + if(Runtime.PySet_Add(nsSet, new BorrowedReference(pyNs)) != 0) | ||
| 190 | 186 | { | |
| 191 | 187 | throw new PythonException(); | |
| 192 | 188 | } | |
@@ -211,16 +207,15 @@ internal static void UpdateCLRModuleDict() | |||
| 211 | 207 | // update the module dictionary with the contents of the root dictionary | |
| 212 | 208 | root.LoadNames(); | |
| 213 | 209 | BorrowedReference py_mod_dict = Runtime.PyModule_GetDict(ClrModuleReference); | |
| 214 | - using (var clr_dict = Runtime.PyObject_GenericGetDict(root.ObjectReference)) | ||
| 215 | - { | ||
| 216 | - Runtime.PyDict_Update(py_mod_dict, clr_dict); | ||
| 217 | - } | ||
| 210 | + using var clr_dict = Runtime.PyObject_GenericGetDict(root.ObjectReference); | ||
| 211 | + | ||
| 212 | + Runtime.PyDict_Update(py_mod_dict, clr_dict); | ||
| 218 | 213 | } | |
| 219 | 214 | ||
| 220 | 215 | /// <summary> | |
| 221 | 216 | /// Return the clr python module (new reference) | |
| 222 | 217 | /// </summary> | |
| 223 | - public static unsafe NewReference GetCLRModule(BorrowedReference fromList = default) | ||
| 218 | + public static unsafe NewReference GetCLRModule() | ||
| 224 | 219 | { | |
| 225 | 220 | UpdateCLRModuleDict(); | |
| 226 | 221 | Runtime.XIncref(py_clr_module); | |
| Back | FazBrowse Home | New Git URL |
0 commit comments