X Tutup
Skip to content

Commit 5d6e5d5

Browse files
committed
use reference types instead of IntPtr where possible in runtime.cs
there are only a few errors left in runtime.cs, but plenty outside of it
1 parent bc3265d commit 5d6e5d5

15 files changed

+597
-1036
lines changed

Directory.Build.props

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
<Project>
22
<PropertyGroup>
33
<VersionPrefix>3.0.0</VersionPrefix>
4-
<AssemblyCopyright>Copyright (c) 2006-2020 The Contributors of the Python.NET Project</AssemblyCopyright>
4+
<AssemblyCopyright>Copyright (c) 2006-2021 The Contributors of the Python.NET Project</AssemblyCopyright>
55
<AssemblyCompany>pythonnet</AssemblyCompany>
66
<AssemblyProduct>Python.NET</AssemblyProduct>
7-
<LangVersion>9.0</LangVersion>
7+
<LangVersion>10.0</LangVersion>
88
<IsPackable>false</IsPackable>
99
</PropertyGroup>
1010
<ItemGroup>
1111
<PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
12-
<PackageReference Include="Microsoft.Net.Compilers.Toolset" Version="3.9.0-3.final">
12+
<PackageReference Include="Microsoft.Net.Compilers.Toolset" Version="4.0.0-5.final">
1313
<PrivateAssets>all</PrivateAssets>
1414
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
1515
</PackageReference>

src/runtime/BorrowedReference.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ public override bool Equals(object obj) {
4646
return false;
4747
}
4848

49+
public static implicit operator BorrowedReference(PyObject pyObject) => pyObject.Reference;
50+
4951
public override int GetHashCode() => pointer.GetHashCode();
5052
}
5153
}

src/runtime/NewReference.cs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,12 @@ public NewReference(BorrowedReference reference, bool canBeNull = false)
1818
var address = canBeNull
1919
? reference.DangerousGetAddressOrNull()
2020
: reference.DangerousGetAddress();
21-
Runtime.XIncref(address);
21+
#pragma warning disable CS0618 // Type or member is obsolete
22+
Runtime.XIncref(reference);
23+
#pragma warning restore CS0618 // Type or member is obsolete
2224
this.pointer = address;
2325
}
2426

25-
[Pure]
26-
public static implicit operator BorrowedReference(in NewReference reference)
27-
=> new BorrowedReference(reference.pointer);
28-
2927
/// <summary>
3028
/// Returns <see cref="PyObject"/> wrapper around this reference, which now owns
3129
/// the pointer. Sets the original reference to <c>null</c>, as it no longer owns it.
@@ -62,6 +60,12 @@ public IntPtr DangerousMoveToPointerOrNull()
6260
/// the pointer. Sets the original reference to <c>null</c>, as it no longer owns it.
6361
/// </summary>
6462
public PyObject MoveToPyObjectOrNull() => this.IsNull() ? null : this.MoveToPyObject();
63+
64+
[Pure]
65+
public BorrowedReference BorrowNullable() => new(pointer);
66+
[Pure]
67+
public BorrowedReference Borrow() => this.IsNull() ? throw new NullReferenceException() : this.BorrowNullable();
68+
6569
/// <summary>
6670
/// Call this method to move ownership of this reference to a Python C API function,
6771
/// that steals reference passed to it.
@@ -88,14 +92,14 @@ public StolenReference Steal()
8892
/// <summary>
8993
/// Removes this reference to a Python object, and sets it to <c>null</c>.
9094
/// </summary>
95+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
9196
public void Dispose()
9297
{
9398
if (this.IsNull())
9499
{
95100
return;
96101
}
97-
Runtime.XDecref(pointer);
98-
pointer = IntPtr.Zero;
102+
Runtime.XDecref(this.Steal());
99103
}
100104

101105
/// <summary>

src/runtime/Python.Runtime.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<PropertyGroup>
33
<TargetFrameworks>netstandard2.0</TargetFrameworks>
44
<Platforms>AnyCPU</Platforms>
5-
<LangVersion>9.0</LangVersion>
5+
<LangVersion>10.0</LangVersion>
66
<RootNamespace>Python.Runtime</RootNamespace>
77
<AssemblyName>Python.Runtime</AssemblyName>
88

src/runtime/Util.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
#nullable enable
22
using System;
33
using System.Collections.Generic;
4+
using System.Diagnostics;
5+
using System.Diagnostics.Contracts;
46
using System.IO;
7+
using System.Runtime.CompilerServices;
58
using System.Runtime.InteropServices;
69

710
namespace Python.Runtime
@@ -18,6 +21,21 @@ internal static class Util
1821
internal const string UseOverloadWithReferenceTypes =
1922
"This API is unsafe, and will be removed in the future. Use overloads working with *Reference types";
2023

24+
25+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
26+
internal unsafe static T* ReadPtr<T>(BorrowedReference @ref, int offset)
27+
where T: unmanaged
28+
{
29+
IntPtr ptr = Marshal.ReadIntPtr(@ref.DangerousGetAddress(), offset);
30+
return (T*)ptr;
31+
}
32+
33+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
34+
internal unsafe static IntPtr ReadIntPtr(BorrowedReference @ref, int offset)
35+
{
36+
return Marshal.ReadIntPtr(@ref.DangerousGetAddress(), offset);
37+
}
38+
2139
internal static Int64 ReadCLong(IntPtr tp, int offset)
2240
{
2341
// On Windows, a C long is always 32 bits.

src/runtime/converter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ static Converter()
4747
/// <summary>
4848
/// Given a builtin Python type, return the corresponding CLR type.
4949
/// </summary>
50-
internal static Type? GetTypeByAlias(IntPtr op)
50+
internal static Type? GetTypeByAlias(BorrowedReference op)
5151
{
5252
if (op == Runtime.PyStringType)
5353
return stringType;

src/runtime/metatype.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ internal class MetaType : ManagedType
2323
/// <summary>
2424
/// Metatype initialization. This bootstraps the CLR metatype to life.
2525
/// </summary>
26-
public static IntPtr Initialize()
26+
public static PyObject Initialize()
2727
{
2828
PyCLRMetaType = TypeManager.CreateMetaType(typeof(MetaType), out _metaSlotsHodler);
2929
return PyCLRMetaType;

src/runtime/methodwrapper.cs

Lines changed: 0 additions & 56 deletions
This file was deleted.

src/runtime/native/NativeFunc.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
namespace Python.Runtime.Native;
2+
3+
/// <summary>Catch-all type for native function objects (to be pointed to)</summary>
4+
struct NativeFunc
5+
{
6+
}

src/runtime/native/PyGILState.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using System;
2+
using System.Runtime.InteropServices;
3+
4+
namespace Python.Runtime.Native;
5+
6+
/// <remarks><c>PyGILState_STATE</c></remarks>
7+
[StructLayout(LayoutKind.Sequential)]
8+
struct PyGILState
9+
{
10+
IntPtr handle;
11+
}

0 commit comments

Comments
 (0)
X Tutup