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
Prev Previous commit
added xmldoc comments to *Reference members
  • Loading branch information
lostmsu committed Feb 23, 2020
commit 6ae63cd9678cd42545cd30b9be336b8ec5240045
14 changes: 5 additions & 9 deletions src/runtime/BorrowedReference.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
namespace Python.Runtime
{
using System;
/// <summary>
/// Represents a reference to a Python object, that is being lent, and
/// can only be safely used until execution returns to the caller.
/// </summary>
readonly ref struct BorrowedReference
{
readonly IntPtr pointer;
public bool IsNull => this.pointer == IntPtr.Zero;

public PyObject ToPyObject()
{
if (this.IsNull) throw new NullReferenceException();

Runtime.XIncref(this.pointer);
return new PyObject(this.pointer);
}

[Obsolete("Use overloads, that take BorrowedReference or NewReference")]
/// <summary>Gets a raw pointer to the Python object</summary>
public IntPtr DangerousGetAddress()
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.

Use a property named Address can be more fit?
Why I have to be warned if I trying use the raw pointer, seems it doesn't make any sense. Or just don't expose the interface and use explicit operator IntPtr instead.

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.

This is modeled after SafeHandle class and its DangerousGetHandle method.
The whole point of introducing these references is to get rid of IntPtrs entirely. It should warn you against converting into IntPtr. Maybe Obsolete part is unnecessary as Dangerous sounds like a warning enough. I will remove Obsolete.

=> this.IsNull ? throw new NullReferenceException() : this.pointer;

Expand Down
11 changes: 10 additions & 1 deletion src/runtime/NewReference.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
namespace Python.Runtime
{
using System;
/// <summary>
/// Represents a reference to a Python object, that is tracked by Python's reference counting.
/// </summary>
[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.

{
Expand All @@ -11,6 +14,10 @@ ref struct NewReference
public IntPtr DangerousGetAddress()
=> this.IsNull ? throw new NullReferenceException() : this.pointer;

/// <summary>
/// Returns <see cref="PyObject"/> wrapper around this reference, which now owns
/// the pointer. Sets the original reference to <c>null</c>, as it no longer owns it.
/// </summary>
public PyObject MoveToPyObject()
{
if (this.IsNull) throw new NullReferenceException();
Expand All @@ -19,7 +26,9 @@ public PyObject MoveToPyObject()
this.pointer = IntPtr.Zero;
return result;
}

/// <summary>
/// Removes this reference to a Python object, and sets it to <c>null</c>.
/// </summary>
public void Dispose()
{
if (!this.IsNull)
Expand Down