Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
NewReference type and an example usage
  • Loading branch information
lostmsu committed Feb 13, 2020
commit 4a84a1e35f072fe506e1f9d0d85b45ecdf0bbcdf
26 changes: 26 additions & 0 deletions src/runtime/NewReference.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
namespace Python.Runtime
{
using System;
[NonCopyable]
ref struct NewReference
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it necessary to limit it as a stack-allocated value? If make it to a normal struct and rename it to PyReference, it can be assigned to a collection and implement the IDisposable for using () usage.
For better usage, a BorrowedReference may be able to promote to a PyReference for using the implict operate overloading.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With [NoCopyable] you won't be able to put it into a collection anyway. And without [NoCopyable] it does not provide any safety guarantees.

Having it as ref struct saves you from adding ref everywhere you take it.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But....sometimes I just want to put them into collections...😂

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then you can still do DangerousGetHandle or create a PyObject from them.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But this type also has a purpose for reminding others that it's a reference don't forget to decref it, isn't it?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because it must be clear from code, that care is necessary around this scenario.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, as mentioned above, [NoCopyable] would already prevent you from putting an instance into a collection, even if ref were removed. And [NoCopyable] is basically the sole purpose of this change, as if you do

NewReference refA = GetNewReferenceSomehow();
NewReference refB = refA;

Then you already miappropriated refcounts.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe more introduce a type named PyHandle for that?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

emmm, but that make BorrowedReference embarrassed.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@amos402 The idea with PyHandle can be reviewed separately. This PR is only for tracking new vs borrowed references as used in Python documentation for C API.

{
public IntPtr Pointer { get; set; }
public bool IsNull => this.Pointer == IntPtr.Zero;

public PyObject ToPyObject()
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer two methods:
ToPyObject: inref and create a PyObject, not assign the Pointer to nullptr
AsPyObject: use this implementation

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Constructing a new PyObject from references should be done using PyObject constructors (which can be added later). I agree though, that ToPyObject is not descriptive enough. Maybe MoveToPyObject or IntoPyObject?

{
if (this.IsNull) throw new NullReferenceException();

var result = new PyObject(this.Pointer);
this.Pointer = IntPtr.Zero;
return result;
}

public void Dispose()
{
if (!this.IsNull)
Runtime.XDecref(this.Pointer);
this.Pointer = IntPtr.Zero;
}
}
}
6 changes: 6 additions & 0 deletions src/runtime/NonCopyableAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Python.Runtime
{
using System;
[AttributeUsage(AttributeTargets.Struct)]
class NonCopyableAttribute : Attribute { }
}
9 changes: 8 additions & 1 deletion src/runtime/Python.Runtime.15.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
<SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">..\..\</SolutionDir>
<PythonBuildDir Condition="'$(PythonBuildDir)' == ''">$(SolutionDir)\bin\</PythonBuildDir>
<PublishDir Condition="'$(TargetFramework)'!='net40'">$(PythonBuildDir)\$(TargetFramework)\</PublishDir>
<LangVersion>7.3</LangVersion>
<LangVersion>8.0</LangVersion>
<AllowUnsafeBlocks>True</AllowUnsafeBlocks>
<AssemblyOriginatorKeyFile>..\pythonnet.snk</AssemblyOriginatorKeyFile>
<CustomDefineConstants Condition="'$(CustomDefineConstants)' == ''">$(PYTHONNET_DEFINE_CONSTANTS)</CustomDefineConstants>
Expand Down Expand Up @@ -129,6 +129,13 @@
<PackageReference Include="Microsoft.TargetingPack.NETFramework.v4.5" Version="1.0.1" ExcludeAssets="All" PrivateAssets="All" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="NonCopyableAnalyzer" Version="0.5.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>

<Import Project="Sdk.targets" Sdk="Microsoft.NET.Sdk" />

<PropertyGroup>
Expand Down
7 changes: 4 additions & 3 deletions src/runtime/pydict.cs
Original file line number Diff line number Diff line change
Expand Up @@ -139,12 +139,13 @@ public PyObject Values()
/// </remarks>
public PyObject Items()
{
IntPtr items = Runtime.PyDict_Items(obj);
if (items == IntPtr.Zero)
using var items = Runtime.PyDict_Items(this.obj);
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After dotnet/roslyn-analyzers#3305 is fixed we can configure build to fail here if using would be omitted

if (items.IsNull)
{
throw new PythonException();
}
return new PyObject(items);

return items.ToPyObject();
}


Expand Down
2 changes: 1 addition & 1 deletion src/runtime/runtime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1591,7 +1591,7 @@ internal static bool PyDict_Check(IntPtr ob)
internal static extern IntPtr PyDict_Values(IntPtr pointer);

[DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)]
internal static extern IntPtr PyDict_Items(IntPtr pointer);
internal static extern NewReference PyDict_Items(IntPtr pointer);

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