- Notifications
You must be signed in to change notification settings - Fork753
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes from5 commits
4a84a1e
88944a2
e1ba92c
2d11f82
dbb3dc5
7304b25
2bc218c
2cf9fc3
6ae63cd
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
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 | ||
{ | ||
public 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() | ||
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. Use a property 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. This is modeled after | ||
=> 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, NewReferencerefA=GetNewReferenceSomehow();NewReferencerefB=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 |
---|---|---|
@@ -15,7 +15,7 @@ | ||
<SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">..\..\</SolutionDir> | ||
<PythonBuildDir Condition=" '$(PythonBuildDir)' == '' ">$(SolutionDir)\bin\</PythonBuildDir> | ||
<AppDesignerFolder>Properties</AppDesignerFolder> | ||
<LangVersion>8.0</LangVersion> | ||
<AllowUnsafeBlocks>true</AllowUnsafeBlocks> | ||
<SignAssembly>false</SignAssembly> | ||
<AssemblyOriginatorKeyFile>..\pythonnet.snk</AssemblyOriginatorKeyFile> | ||
@@ -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).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); | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -139,12 +139,13 @@ public PyObject Values() | ||
/// </remarks> | ||
public PyObject Items() | ||
{ | ||
using varitems = Runtime.PyDict_Items(this.obj); | ||
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. Afterdotnet/roslyn-analyzers#3305 is fixed we can configure build to fail here if | ||
if (items.IsNull) | ||
{ | ||
throw new PythonException(); | ||
} | ||
return items.ToPyObject(); | ||
} | ||