| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 2343f89 commit cc6b8e4
4 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -10,6 +10,8 @@ readonly ref struct BorrowedReference | |||
| 10 | 10 | readonly IntPtr pointer; | |
| 11 | 11 | public bool IsNull => this.pointer == IntPtr.Zero; | |
| 12 | 12 | ||
| 13 | + public static implicit operator IntPtr(in BorrowedReference self) => self.DangerousGetAddress(); | ||
| 14 | + | ||
| 13 | 15 | /// <summary>Gets a raw pointer to the Python object</summary> | |
| 14 | 16 | public IntPtr DangerousGetAddress() | |
| 15 | 17 | => this.IsNull ? throw new NullReferenceException() : this.pointer; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -15,6 +15,10 @@ ref struct NewReference | |||
| 15 | 15 | public static implicit operator BorrowedReference(in NewReference reference) | |
| 16 | 16 | => new BorrowedReference(reference.pointer); | |
| 17 | 17 | ||
| 18 | + [Pure] | ||
| 19 | + public static implicit operator IntPtr(in NewReference reference) | ||
| 20 | + => DangerousGetAddress(reference); | ||
| 21 | + | ||
| 18 | 22 | /// <summary> | |
| 19 | 23 | /// Returns <see cref="PyObject"/> wrapper around this reference, which now owns | |
| 20 | 24 | /// the pointer. Sets the original reference to <c>null</c>, as it no longer owns it. | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -748,6 +748,22 @@ internal static IntPtr SelfIncRef(IntPtr op) | |||
| 748 | 748 | return op; | |
| 749 | 749 | } | |
| 750 | 750 | ||
| 751 | + /// <remark> | ||
| 752 | + /// We need this method because BorrowedReference can be implicitly casted to IntPtr. | ||
| 753 | + /// </remark> | ||
| 754 | + internal static void XDecref(BorrowedReference op) | ||
| 755 | + { | ||
| 756 | + throw new InvalidOperationException("Cannot DecRef a borrowed reference."); | ||
| 757 | + } | ||
| 758 | + | ||
| 759 | + /// <remark> | ||
| 760 | + /// We need this method because NewReference can be implicitly casted to IntPtr. | ||
| 761 | + /// </remark> | ||
| 762 | + internal static void XDecref(NewReference op) | ||
| 763 | + { | ||
| 764 | + op.Dispose(); | ||
| 765 | + } | ||
| 766 | + | ||
| 751 | 767 | internal static unsafe void XDecref(IntPtr op) | |
| 752 | 768 | { | |
| 753 | 769 | #if PYTHON_WITH_PYDEBUG || NETSTANDARD | |
@@ -2129,7 +2145,7 @@ internal static void Py_CLEAR(ref IntPtr ob) | |||
| 2129 | 2145 | //==================================================================== | |
| 2130 | 2146 | ||
| 2131 | 2147 | [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] | |
| 2132 | - internal static extern IntPtr PyCapsule_New(IntPtr pointer, string name, IntPtr destructor); | ||
| 2148 | + internal static extern NewReference PyCapsule_New(IntPtr pointer, string name, IntPtr destructor); | ||
| 2133 | 2149 | ||
| 2134 | 2150 | [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] | |
| 2135 | 2151 | internal static extern IntPtr PyCapsule_GetPointer(IntPtr capsule, string name); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -36,8 +36,8 @@ public static Type FormatterType | |||
| 36 | 36 | /// </summary> | |
| 37 | 37 | static void ClearCLRData () | |
| 38 | 38 | { | |
| 39 | - IntPtr capsule = PySys_GetObject("clr_data").DangerousGetAddressUnchecked(); | ||
| 40 | - if (capsule != IntPtr.Zero) | ||
| 39 | + BorrowedReference capsule = PySys_GetObject("clr_data"); | ||
| 40 | + if (!capsule.IsNull) | ||
| 41 | 41 | { | |
| 42 | 42 | IntPtr oldData = PyCapsule_GetPointer(capsule, null); | |
| 43 | 43 | PyMem_Free(oldData); | |
@@ -85,10 +85,10 @@ internal static void Stash() | |||
| 85 | 85 | Marshal.Copy(data, 0, mem + IntPtr.Size, (int)ms.Length); | |
| 86 | 86 | ||
| 87 | 87 | ClearCLRData(); | |
| 88 | - IntPtr capsule = PyCapsule_New(mem, null, IntPtr.Zero); | ||
| 88 | + NewReference capsule = PyCapsule_New(mem, null, IntPtr.Zero); | ||
| 89 | 89 | PySys_SetObject("clr_data", capsule); | |
| 90 | 90 | // Let the dictionary own the reference | |
| 91 | - XDecref(capsule); | ||
| 91 | + capsule.Dispose(); | ||
| 92 | 92 | } | |
| 93 | 93 | ||
| 94 | 94 | internal static void RestoreRuntimeData() | |
@@ -105,8 +105,8 @@ internal static void RestoreRuntimeData() | |||
| 105 | 105 | ||
| 106 | 106 | private static void RestoreRuntimeDataImpl() | |
| 107 | 107 | { | |
| 108 | - IntPtr capsule = PySys_GetObject("clr_data").DangerousGetAddressUnchecked(); | ||
| 109 | - if (capsule == IntPtr.Zero) | ||
| 108 | + BorrowedReference capsule = PySys_GetObject("clr_data"); | ||
| 109 | + if (capsule.IsNull) | ||
| 110 | 110 | { | |
| 111 | 111 | return; | |
| 112 | 112 | } | |
| Back | FazBrowse Home | New Git URL |
0 commit comments