-
Notifications
You must be signed in to change notification settings - Fork 762
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 7 commits
4a84a1e
88944a2
e1ba92c
2d11f82
dbb3dc5
7304b25
2bc218c
2cf9fc3
6ae63cd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
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; | ||
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")] | ||
public IntPtr DangerousGetAddress() | ||
=> this.IsNull ? throw new NullReferenceException() : this.pointer; | ||
|
||
BorrowedReference(IntPtr pointer) | ||
{ | ||
this.pointer = pointer; | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
namespace Python.Runtime | ||
{ | ||
using System; | ||
[NonCopyable] | ||
ref struct NewReference | ||
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. Is it necessary to limit it as a stack-allocated value? If make it to a normal struct and rename it to 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. With Having it as 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. But....sometimes I just want to put them into collections...😂 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. Then you can still do 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. 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 commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more. Also, as mentioned above, NewReference refA = GetNewReferenceSomehow();
NewReference refB = refA; Then you already miappropriated refcounts. 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. Maybe more introduce a type named 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. emmm, but that make 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. @amos402 The idea with |
||
{ | ||
public IntPtr Pointer { get; set; } | ||
public bool IsNull => this.Pointer == IntPtr.Zero; | ||
|
||
public PyObject ToPyObject() | ||
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: 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 |
---|---|---|
|
@@ -83,6 +83,7 @@ | |
</Compile> | ||
<Compile Include="arrayobject.cs" /> | ||
<Compile Include="assemblymanager.cs" /> | ||
<Compile Include="BorrowedReference.cs" /> | ||
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. Is it good for separate these three items to different files? They're small, related, and it make the project files be scattered. 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. The might get more code later on. Generally, I prefer a type per file approach for anything except delegate types. 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. Or create a folder for them? Just just too far if they sorted by name I thought🤣 |
||
<Compile Include="classderived.cs" /> | ||
<Compile Include="classbase.cs" /> | ||
<Compile Include="classmanager.cs" /> | ||
|
@@ -119,6 +120,8 @@ | |
<Compile Include="moduleobject.cs" /> | ||
<Compile Include="modulepropertyobject.cs" /> | ||
<Compile Include="nativecall.cs" /> | ||
<Compile Include="NewReference.cs" /> | ||
<Compile Include="NonCopyableAttribute.cs" /> | ||
<Compile Include="overload.cs" /> | ||
<Compile Include="propertyobject.cs" /> | ||
<Compile Include="pyansistring.cs" /> | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -292,7 +292,7 @@ internal Binding Bind(IntPtr inst, IntPtr args, IntPtr kw, MethodBase info, Meth | |
for (int i = 0; i < pynkwargs; ++i) | ||
{ | ||
var keyStr = Runtime.GetManagedString(Runtime.PyList_GetItem(keylist, i)); | ||
kwargDict[keyStr] = Runtime.PyList_GetItem(valueList, i); | ||
kwargDict[keyStr] = Runtime.PyList_GetItem(valueList, i).DangerousGetAddress(); | ||
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. This generates a compiler warning, that uncovers a potentially dangerous pattern, where |
||
} | ||
Runtime.XDecref(keylist); | ||
Runtime.XDecref(valueList); | ||
|
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.
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.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.
This is modeled after
SafeHandle
class and itsDangerousGetHandle
method.The whole point of introducing these references is to get rid of
IntPtr
s entirely. It should warn you against converting intoIntPtr
. MaybeObsolete
part is unnecessary asDangerous
sounds like a warning enough. I will removeObsolete
.