| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 4271e57 commit 770fc01
9 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,22 @@ | |||
| 1 | + namespace Python.Runtime | ||
| 2 | + { | ||
| 3 | + using System; | ||
| 4 | + /// <summary> | ||
| 5 | + /// Represents a reference to a Python object, that is being lent, and | ||
| 6 | + /// can only be safely used until execution returns to the caller. | ||
| 7 | + /// </summary> | ||
| 8 | + readonly ref struct BorrowedReference | ||
| 9 | + { | ||
| 10 | + readonly IntPtr pointer; | ||
| 11 | + public bool IsNull => this.pointer == IntPtr.Zero; | ||
| 12 | + | ||
| 13 | + /// <summary>Gets a raw pointer to the Python object</summary> | ||
| 14 | + public IntPtr DangerousGetAddress() | ||
| 15 | + => this.IsNull ? throw new NullReferenceException() : this.pointer; | ||
| 16 | + | ||
| 17 | + BorrowedReference(IntPtr pointer) | ||
| 18 | + { | ||
| 19 | + this.pointer = pointer; | ||
| 20 | + } | ||
| 21 | + } | ||
| 22 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,39 @@ | |||
| 1 | + namespace Python.Runtime | ||
| 2 | + { | ||
| 3 | + using System; | ||
| 4 | + /// <summary> | ||
| 5 | + /// Represents a reference to a Python object, that is tracked by Python's reference counting. | ||
| 6 | + /// </summary> | ||
| 7 | + [NonCopyable] | ||
| 8 | + ref struct NewReference | ||
| 9 | + { | ||
| 10 | + IntPtr pointer; | ||
| 11 | + public bool IsNull => this.pointer == IntPtr.Zero; | ||
| 12 | + | ||
| 13 | + /// <summary>Gets a raw pointer to the Python object</summary> | ||
| 14 | + public IntPtr DangerousGetAddress() | ||
| 15 | + => this.IsNull ? throw new NullReferenceException() : this.pointer; | ||
| 16 | + | ||
| 17 | + /// <summary> | ||
| 18 | + /// Returns <see cref="PyObject"/> wrapper around this reference, which now owns | ||
| 19 | + /// the pointer. Sets the original reference to <c>null</c>, as it no longer owns it. | ||
| 20 | + /// </summary> | ||
| 21 | + public PyObject MoveToPyObject() | ||
| 22 | + { | ||
| 23 | + if (this.IsNull) throw new NullReferenceException(); | ||
| 24 | + | ||
| 25 | + var result = new PyObject(this.pointer); | ||
| 26 | + this.pointer = IntPtr.Zero; | ||
| 27 | + return result; | ||
| 28 | + } | ||
| 29 | + /// <summary> | ||
| 30 | + /// Removes this reference to a Python object, and sets it to <c>null</c>. | ||
| 31 | + /// </summary> | ||
| 32 | + public void Dispose() | ||
| 33 | + { | ||
| 34 | + if (!this.IsNull) | ||
| 35 | + Runtime.XDecref(this.pointer); | ||
| 36 | + this.pointer = IntPtr.Zero; | ||
| 37 | + } | ||
| 38 | + } | ||
| 39 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,6 @@ | |||
| 1 | + namespace Python.Runtime | ||
| 2 | + { | ||
| 3 | + using System; | ||
| 4 | + [AttributeUsage(AttributeTargets.Struct)] | ||
| 5 | + class NonCopyableAttribute : Attribute { } | ||
| 6 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -129,6 +129,13 @@ | |||
| 129 | 129 | <PackageReference Include="Microsoft.TargetingPack.NETFramework.v4.5" Version="1.0.1" ExcludeAssets="All" PrivateAssets="All" /> | |
| 130 | 130 | </ItemGroup> | |
| 131 | 131 | ||
| 132 | + <ItemGroup> | ||
| 133 | + <PackageReference Include="NonCopyableAnalyzer" Version="0.5.1"> | ||
| 134 | + <PrivateAssets>all</PrivateAssets> | ||
| 135 | + <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
| 136 | + </PackageReference> | ||
| 137 | + </ItemGroup> | ||
| 138 | + | ||
| 132 | 139 | <Import Project="Sdk.targets" Sdk="Microsoft.NET.Sdk" /> | |
| 133 | 140 | ||
| 134 | 141 | <PropertyGroup> | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -83,6 +83,7 @@ | |||
| 83 | 83 | </Compile> | |
| 84 | 84 | <Compile Include="arrayobject.cs" /> | |
| 85 | 85 | <Compile Include="assemblymanager.cs" /> | |
| 86 | + <Compile Include="BorrowedReference.cs" /> | ||
| 86 | 87 | <Compile Include="classderived.cs" /> | |
| 87 | 88 | <Compile Include="classbase.cs" /> | |
| 88 | 89 | <Compile Include="classmanager.cs" /> | |
@@ -119,6 +120,8 @@ | |||
| 119 | 120 | <Compile Include="moduleobject.cs" /> | |
| 120 | 121 | <Compile Include="modulepropertyobject.cs" /> | |
| 121 | 122 | <Compile Include="nativecall.cs" /> | |
| 123 | + <Compile Include="NewReference.cs" /> | ||
| 124 | + <Compile Include="NonCopyableAttribute.cs" /> | ||
| 122 | 125 | <Compile Include="overload.cs" /> | |
| 123 | 126 | <Compile Include="propertyobject.cs" /> | |
| 124 | 127 | <Compile Include="pyansistring.cs" /> | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -145,7 +145,7 @@ internal static void UpdatePath() | |||
| 145 | 145 | probed.Clear(); | |
| 146 | 146 | for (var i = 0; i < count; i++) | |
| 147 | 147 | { | |
| 148 | - IntPtr item = Runtime.PyList_GetItem(list, i); | ||
| 148 | + BorrowedReference item = Runtime.PyList_GetItem(list, i); | ||
| 149 | 149 | string path = Runtime.GetManagedString(item); | |
| 150 | 150 | if (path != null) | |
| 151 | 151 | { | |
@@ -492,4 +492,4 @@ internal static Type[] GetTypes(Assembly a) | |||
| 492 | 492 | } | |
| 493 | 493 | } | |
| 494 | 494 | } | |
| 495 | - } | ||
| 495 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -292,7 +292,7 @@ internal Binding Bind(IntPtr inst, IntPtr args, IntPtr kw, MethodBase info, Meth | |||
| 292 | 292 | for (int i = 0; i < pynkwargs; ++i) | |
| 293 | 293 | { | |
| 294 | 294 | var keyStr = Runtime.GetManagedString(Runtime.PyList_GetItem(keylist, i)); | |
| 295 | - kwargDict[keyStr] = Runtime.PyList_GetItem(valueList, i); | ||
| 295 | + kwargDict[keyStr] = Runtime.PyList_GetItem(valueList, i).DangerousGetAddress(); | ||
| 296 | 296 | } | |
| 297 | 297 | Runtime.XDecref(keylist); | |
| 298 | 298 | Runtime.XDecref(valueList); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -139,12 +139,20 @@ public PyObject Values() | |||
| 139 | 139 | /// </remarks> | |
| 140 | 140 | public PyObject Items() | |
| 141 | 141 | { | |
| 142 | - IntPtr items = Runtime.PyDict_Items(obj); | ||
| 143 | - if (items == IntPtr.Zero) | ||
| 142 | + var items = Runtime.PyDict_Items(this.obj); | ||
| 143 | + try | ||
| 144 | 144 | { | |
| 145 | - throw new PythonException(); | ||
| 145 | + if (items.IsNull) | ||
| 146 | + { | ||
| 147 | + throw new PythonException(); | ||
| 148 | + } | ||
| 149 | + | ||
| 150 | + return items.MoveToPyObject(); | ||
| 151 | + } | ||
| 152 | + finally | ||
| 153 | + { | ||
| 154 | + items.Dispose(); | ||
| 146 | 155 | } | |
| 147 | - return new PyObject(items); | ||
| 148 | 156 | } | |
| 149 | 157 | ||
| 150 | 158 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1509,6 +1509,8 @@ internal static IntPtr PyUnicode_FromString(string s) | |||
| 1509 | 1509 | return PyUnicode_FromUnicode(s, s.Length); | |
| 1510 | 1510 | } | |
| 1511 | 1511 | ||
| 1512 | + internal static string GetManagedString(in BorrowedReference borrowedReference) | ||
| 1513 | + => GetManagedString(borrowedReference.DangerousGetAddress()); | ||
| 1512 | 1514 | /// <summary> | |
| 1513 | 1515 | /// Function to access the internal PyUnicode/PyString object and | |
| 1514 | 1516 | /// convert it to a managed string with the correct encoding. | |
@@ -1591,7 +1593,7 @@ internal static bool PyDict_Check(IntPtr ob) | |||
| 1591 | 1593 | internal static extern IntPtr PyDict_Values(IntPtr pointer); | |
| 1592 | 1594 | ||
| 1593 | 1595 | [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] | |
| 1594 | - internal static extern IntPtr PyDict_Items(IntPtr pointer); | ||
| 1596 | + internal static extern NewReference PyDict_Items(IntPtr pointer); | ||
| 1595 | 1597 | ||
| 1596 | 1598 | [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] | |
| 1597 | 1599 | internal static extern IntPtr PyDict_Copy(IntPtr pointer); | |
@@ -1631,13 +1633,13 @@ internal static IntPtr PyList_New(long size) | |||
| 1631 | 1633 | [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] | |
| 1632 | 1634 | internal static extern IntPtr PyList_AsTuple(IntPtr pointer); | |
| 1633 | 1635 | ||
| 1634 | - internal static IntPtr PyList_GetItem(IntPtr pointer, long index) | ||
| 1636 | + internal static BorrowedReference PyList_GetItem(IntPtr pointer, long index) | ||
| 1635 | 1637 | { | |
| 1636 | 1638 | return PyList_GetItem(pointer, new IntPtr(index)); | |
| 1637 | 1639 | } | |
| 1638 | 1640 | ||
| 1639 | 1641 | [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] | |
| 1640 | - private static extern IntPtr PyList_GetItem(IntPtr pointer, IntPtr index); | ||
| 1642 | + private static extern BorrowedReference PyList_GetItem(IntPtr pointer, IntPtr index); | ||
| 1641 | 1643 | ||
| 1642 | 1644 | internal static int PyList_SetItem(IntPtr pointer, long index, IntPtr value) | |
| 1643 | 1645 | { | |
| Back | FazBrowse Home | New Git URL |
0 commit comments