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
Next Next commit
allow comparing BorrowedReference to null
  • Loading branch information
lostmsu committed Feb 24, 2021
commit c3fc7f08a0137c560840a0ebe2f2b401a83b3739
8 changes: 8 additions & 0 deletions src/runtime/BorrowedReference.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ public BorrowedReference(IntPtr pointer)
=> a.pointer == b.pointer;
public static bool operator !=(BorrowedReference a, BorrowedReference b)
=> a.pointer != b.pointer;
public static bool operator ==(BorrowedReference reference, NullOnly @null)
=> reference.IsNull;
public static bool operator !=(BorrowedReference reference, NullOnly @null)
=> !reference.IsNull;
public static bool operator ==(NullOnly @null, BorrowedReference reference)
=> reference.IsNull;
public static bool operator !=(NullOnly @null, BorrowedReference reference)
=> !reference.IsNull;

public override bool Equals(object obj) {
if (obj is IntPtr ptr)
Expand Down
6 changes: 3 additions & 3 deletions src/runtime/importhook.cs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ public static unsafe NewReference GetCLRModule(BorrowedReference fromList = defa

// find any items from the from list and get them from the root if they're not
// already in the module dictionary
if (fromList != null && fromList != default)
if (fromList != null)
{
if (Runtime.PyTuple_Check(fromList))
{
Expand Down Expand Up @@ -224,7 +224,7 @@ public static IntPtr __import__(IntPtr self, IntPtr argsRaw, IntPtr kw)
if (num_args >= 4)
{
fromList = Runtime.PyTuple_GetItem(args, 3);
if (fromList != default &&
if (fromList != null &&
Runtime.PyObject_IsTrue(fromList) == 1)
{
fromlist = true;
Expand Down Expand Up @@ -297,7 +297,7 @@ public static IntPtr __import__(IntPtr self, IntPtr argsRaw, IntPtr kw)
BorrowedReference modules = Runtime.PyImport_GetModuleDict();
BorrowedReference module = Runtime.PyDict_GetItem(modules, py_mod_name);

if (module != default)
if (module != null)
{
if (fromlist)
{
Expand Down
12 changes: 12 additions & 0 deletions src/runtime/tricks/NullOnly.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace Python.Runtime
{
/// <summary>
/// An utility class, that can only have one value: <c>null</c>.
/// <para>Useful for overloading operators on structs,
/// that have meaningful concept of <c>null</c> value (e.g. pointers and references).</para>
/// </summary>
class NullOnly
Comment thread
lostmsu marked this conversation as resolved.
{
private NullOnly() { }
}
}