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
Next Next commit
renamed NewReference.ToPyObject to MoveToPyObject
removed public property Pointer from NewReference and replaced with DangerousGetAddress
  • Loading branch information
lostmsu committed Feb 23, 2020
commit 2cf9fc36872789bbbb863a173addb4b35b7c5e95
18 changes: 11 additions & 7 deletions src/runtime/NewReference.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,27 @@ namespace Python.Runtime
[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;
IntPtr pointer;
public bool IsNull => this.pointer == IntPtr.Zero;

public PyObject ToPyObject()
/// <summary>Gets a raw pointer to the Python object</summary>
public IntPtr DangerousGetAddress()
=> this.IsNull ? throw new NullReferenceException() : this.pointer;

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

var result = new PyObject(this.Pointer);
this.Pointer = IntPtr.Zero;
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;
Runtime.XDecref(this.pointer);
this.pointer = IntPtr.Zero;
}
}
}
2 changes: 1 addition & 1 deletion src/runtime/pydict.cs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ public PyObject Items()
throw new PythonException();
}

return items.ToPyObject();
return items.MoveToPyObject();
}
finally
{
Expand Down