Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4a84a1e

Browse files
committed
NewReference type and an example usage
1 parent f5548e3 commit 4a84a1e

File tree

5 files changed

+45
-5
lines changed

5 files changed

+45
-5
lines changed

src/runtime/NewReference.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
namespace Python.Runtime
2+
{
3+
using System;
4+
[NonCopyable]
5+
ref struct NewReference
6+
{
7+
public IntPtr Pointer { get; set; }
8+
public bool IsNull => this.Pointer == IntPtr.Zero;
9+
10+
public PyObject ToPyObject()
11+
{
12+
if (this.IsNull) throw new NullReferenceException();
13+
14+
var result = new PyObject(this.Pointer);
15+
this.Pointer = IntPtr.Zero;
16+
return result;
17+
}
18+
19+
public void Dispose()
20+
{
21+
if (!this.IsNull)
22+
Runtime.XDecref(this.Pointer);
23+
this.Pointer = IntPtr.Zero;
24+
}
25+
}
26+
}

src/runtime/NonCopyableAttribute.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
namespace Python.Runtime
2+
{
3+
using System;
4+
[AttributeUsage(AttributeTargets.Struct)]
5+
class NonCopyableAttribute : Attribute { }
6+
}

src/runtime/Python.Runtime.15.csproj

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
<SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">..\..\</SolutionDir>
3131
<PythonBuildDir Condition="'$(PythonBuildDir)' == ''">$(SolutionDir)\bin\</PythonBuildDir>
3232
<PublishDir Condition="'$(TargetFramework)'!='net40'">$(PythonBuildDir)\$(TargetFramework)\</PublishDir>
33-
<LangVersion>7.3</LangVersion>
33+
<LangVersion>8.0</LangVersion>
3434
<AllowUnsafeBlocks>True</AllowUnsafeBlocks>
3535
<AssemblyOriginatorKeyFile>..\pythonnet.snk</AssemblyOriginatorKeyFile>
3636
<CustomDefineConstants Condition="'$(CustomDefineConstants)' == ''">$(PYTHONNET_DEFINE_CONSTANTS)</CustomDefineConstants>
@@ -129,6 +129,13 @@
129129
<PackageReference Include="Microsoft.TargetingPack.NETFramework.v4.5" Version="1.0.1" ExcludeAssets="All" PrivateAssets="All" />
130130
</ItemGroup>
131131

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+
132139
<Import Project="Sdk.targets" Sdk="Microsoft.NET.Sdk" />
133140

134141
<PropertyGroup>

src/runtime/pydict.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,12 +139,13 @@ public PyObject Values()
139139
/// </remarks>
140140
public PyObject Items()
141141
{
142-
IntPtr items = Runtime.PyDict_Items(obj);
143-
if (items == IntPtr.Zero)
142+
using var items = Runtime.PyDict_Items(this.obj);
143+
if (items.IsNull)
144144
{
145145
throw new PythonException();
146146
}
147-
return new PyObject(items);
147+
148+
return items.ToPyObject();
148149
}
149150

150151

src/runtime/runtime.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1591,7 +1591,7 @@ internal static bool PyDict_Check(IntPtr ob)
15911591
internal static extern IntPtr PyDict_Values(IntPtr pointer);
15921592

15931593
[DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)]
1594-
internal static extern IntPtr PyDict_Items(IntPtr pointer);
1594+
internal static extern NewReference PyDict_Items(IntPtr pointer);
15951595

15961596
[DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)]
15971597
internal static extern IntPtr PyDict_Copy(IntPtr pointer);

0 commit comments

Comments
 (0)