File tree Expand file tree Collapse file tree 5 files changed +45
-5
lines changed Expand file tree Collapse file tree 5 files changed +45
-5
lines changed Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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 30
30
<SolutionDir Condition =" $(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'" >..\..\</SolutionDir >
31
31
<PythonBuildDir Condition =" '$(PythonBuildDir)' == ''" >$(SolutionDir)\bin\</PythonBuildDir >
32
32
<PublishDir Condition =" '$(TargetFramework)'!='net40'" >$(PythonBuildDir)\$(TargetFramework)\</PublishDir >
33
- <LangVersion >7.3 </LangVersion >
33
+ <LangVersion >8.0 </LangVersion >
34
34
<AllowUnsafeBlocks >True</AllowUnsafeBlocks >
35
35
<AssemblyOriginatorKeyFile >..\pythonnet.snk</AssemblyOriginatorKeyFile >
36
36
<CustomDefineConstants Condition =" '$(CustomDefineConstants)' == ''" >$(PYTHONNET_DEFINE_CONSTANTS)</CustomDefineConstants >
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 @@ -139,12 +139,13 @@ 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
+ using var items = Runtime . PyDict_Items ( this . obj ) ;
143
+ if ( items . IsNull )
144
144
{
145
145
throw new PythonException ( ) ;
146
146
}
147
- return new PyObject ( items ) ;
147
+
148
+ return items . ToPyObject ( ) ;
148
149
}
149
150
150
151
Original file line number Diff line number Diff line change @@ -1591,7 +1591,7 @@ internal static bool PyDict_Check(IntPtr ob)
1591
1591
internal static extern IntPtr PyDict_Values( IntPtr pointer) ;
1592
1592
1593
1593
[ DllImport( _PythonDll, CallingConvention = CallingConvention. Cdecl) ]
1594
- internal static extern IntPtr PyDict_Items( IntPtr pointer) ;
1594
+ internal static extern NewReference PyDict_Items( IntPtr pointer) ;
1595
1595
1596
1596
[ DllImport( _PythonDll, CallingConvention = CallingConvention. Cdecl) ]
1597
1597
internal static extern IntPtr PyDict_Copy( IntPtr pointer) ;
You can’t perform that action at this time.
0 commit comments