-
Notifications
You must be signed in to change notification settings - Fork 773
Proposal: Safe pointers #1043
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Proposal: Safe pointers #1043
Changes from 1 commit
4a84a1e
88944a2
e1ba92c
2d11f82
dbb3dc5
7304b25
2bc218c
2cf9fc3
6ae63cd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| namespace Python.Runtime | ||
| { | ||
| using System; | ||
| [NonCopyable] | ||
| ref struct NewReference | ||
| { | ||
| public IntPtr Pointer { get; set; } | ||
| public bool IsNull => this.Pointer == IntPtr.Zero; | ||
|
|
||
| public PyObject ToPyObject() | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I prefer two methods:
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Constructing a new |
||
| { | ||
| 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; | ||
| } | ||
| } | ||
| } | ||
| 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 { } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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); | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
| if (items.IsNull) | ||
| { | ||
| throw new PythonException(); | ||
| } | ||
| return new PyObject(items); | ||
|
|
||
| return items.ToPyObject(); | ||
| } | ||
|
|
||
|
|
||
|
|
||
There was a problem hiding this comment.
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 forusing ()usage.For better usage, a
BorrowedReferencemay be able to promote to aPyReferencefor using the implict operate overloading.There was a problem hiding this comment.
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 structsaves you from addingrefeverywhere you take it.There was a problem hiding this comment.
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...😂
There was a problem hiding this comment.
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
DangerousGetHandleor create aPyObjectfrom them.There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 ifrefwere removed. And[NoCopyable]is basically the sole purpose of this change, as if you doThen you already miappropriated refcounts.
There was a problem hiding this comment.
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
PyHandlefor that?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
emmm, but that make
BorrowedReferenceembarrassed.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@amos402 The idea with
PyHandlecan be reviewed separately. This PR is only for tracking new vs borrowed references as used in Python documentation for C API.