Skip to content

Got rid of a few deprecation warnings that pollute GitHub code review #1850

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

Merged
merged 1 commit into from
Jul 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
9 changes: 6 additions & 3 deletions src/embed_tests/TestConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ public void PyIntImplicit()
{
var i = new PyInt(1);
var ni = (PyObject)i.As<object>();
Assert.AreEqual(i.rawPtr, ni.rawPtr);
Assert.IsTrue(PythonReferenceComparer.Instance.Equals(i, ni));
}

[Test]
Expand Down Expand Up @@ -178,8 +178,11 @@ public void RawPyObjectProxy()
var clrObject = (CLRObject)ManagedType.GetManagedObject(pyObjectProxy);
Assert.AreSame(pyObject, clrObject.inst);

var proxiedHandle = pyObjectProxy.GetAttr("Handle").As<IntPtr>();
Assert.AreEqual(pyObject.Handle, proxiedHandle);
#pragma warning disable CS0612 // Type or member is obsolete
const string handlePropertyName = nameof(PyObject.Handle);
#pragma warning restore CS0612 // Type or member is obsolete
var proxiedHandle = pyObjectProxy.GetAttr(handlePropertyName).As<IntPtr>();
Assert.AreEqual(pyObject.DangerousGetAddressOrNull(), proxiedHandle);
}

// regression for https://github.com/pythonnet/pythonnet/issues/451
Expand Down
3 changes: 1 addition & 2 deletions src/embed_tests/TestDomainReload.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,7 @@ from Python.EmbeddingTest.Domain import MyClass
{
Debug.Assert(obj.AsManagedObject(type).GetType() == type);
// We only needs its Python handle
PyRuntime.XIncref(obj);
return obj.Handle;
return new NewReference(obj).DangerousMoveToPointer();
}
}
}
Expand Down
7 changes: 5 additions & 2 deletions src/embed_tests/TestFinalizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,9 @@ public void ValidateRefCount()
Assert.AreEqual(ptr, e.Handle);
Assert.AreEqual(2, e.ImpactedObjects.Count);
// Fix for this test, don't do this on general environment
#pragma warning disable CS0618 // Type or member is obsolete
Runtime.Runtime.XIncref(e.Reference);
#pragma warning restore CS0618 // Type or member is obsolete
return false;
};
Finalizer.Instance.IncorrectRefCntResolver += handler;
Expand All @@ -234,8 +236,9 @@ private static IntPtr CreateStringGarbage()
{
PyString s1 = new PyString("test_string");
// s2 steal a reference from s1
PyString s2 = new PyString(StolenReference.DangerousFromPointer(s1.Handle));
return s1.Handle;
IntPtr address = s1.Reference.DangerousGetAddress();
PyString s2 = new (StolenReference.DangerousFromPointer(address));
return address;
}
}
}
3 changes: 2 additions & 1 deletion src/embed_tests/TestNativeTypeOffset.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ public void LoadNativeTypeOffsetClass()
{
PyObject sys = Py.Import("sys");
// We can safely ignore the "m" abi flag
var abiflags = sys.GetAttr("abiflags", "".ToPython()).ToString().Replace("m", "");
var abiflags = sys.HasAttr("abiflags") ? sys.GetAttr("abiflags").ToString() : "";
abiflags = abiflags.Replace("m", "");
if (!string.IsNullOrEmpty(abiflags))
{
string typeName = "Python.Runtime.NativeTypeOffset, Python.Runtime";
Expand Down
2 changes: 1 addition & 1 deletion src/embed_tests/TestPythonException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ def __init__(self, val):
using var tbObj = tbPtr.MoveToPyObject();
// the type returned from PyErr_NormalizeException should not be the same type since a new
// exception was raised by initializing the exception
Assert.AreNotEqual(type.Handle, typeObj.Handle);
Assert.IsFalse(PythonReferenceComparer.Instance.Equals(type, typeObj));
// the message should now be the string from the throw exception during normalization
Assert.AreEqual("invalid literal for int() with base 10: 'dummy string'", strObj.ToString());
}
Expand Down
4 changes: 2 additions & 2 deletions src/runtime/InternString.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public static void Initialize()
Debug.Assert(name == op.As<string>());
SetIntern(name, op);
var field = type.GetField("f" + name, PyIdentifierFieldFlags)!;
field.SetValue(null, op.rawPtr);
field.SetValue(null, op.DangerousGetAddressOrNull());
}
}

Expand Down Expand Up @@ -76,7 +76,7 @@ public static bool TryGetInterned(BorrowedReference op, out string s)
private static void SetIntern(string s, PyString op)
{
_string2interns.Add(s, op);
_intern2strings.Add(op.rawPtr, s);
_intern2strings.Add(op.Reference.DangerousGetAddress(), s);
}
}
}
4 changes: 3 additions & 1 deletion src/runtime/PythonTypes/PyObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public partial class PyObject : DynamicObject, IDisposable, ISerializable
public StackTrace Traceback { get; } = new StackTrace(1);
#endif

protected internal IntPtr rawPtr = IntPtr.Zero;
protected IntPtr rawPtr = IntPtr.Zero;
internal readonly int run = Runtime.GetRun();

internal BorrowedReference obj => new (rawPtr);
Expand Down Expand Up @@ -252,6 +252,8 @@ internal void Leak()
rawPtr = IntPtr.Zero;
}

internal IntPtr DangerousGetAddressOrNull() => rawPtr;

internal void CheckRun()
{
if (run != Runtime.GetRun())
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/Types/ReflectedClrType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,6 @@ static ReflectedClrType AllocateClass(Type clrType)
return new ReflectedClrType(type.Steal());
}

public override bool Equals(PyObject? other) => other != null && rawPtr == other.rawPtr;
public override bool Equals(PyObject? other) => rawPtr == other?.DangerousGetAddressOrNull();
public override int GetHashCode() => rawPtr.GetHashCode();
}
4 changes: 2 additions & 2 deletions src/runtime/Util/PythonReferenceComparer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ public sealed class PythonReferenceComparer : IEqualityComparer<PyObject>
public static PythonReferenceComparer Instance { get; } = new PythonReferenceComparer();
public bool Equals(PyObject? x, PyObject? y)
{
return x?.rawPtr == y?.rawPtr;
return x?.DangerousGetAddressOrNull() == y?.DangerousGetAddressOrNull();
}

public int GetHashCode(PyObject obj) => obj.rawPtr.GetHashCode();
public int GetHashCode(PyObject obj) => obj.DangerousGetAddressOrNull().GetHashCode();

private PythonReferenceComparer() { }
}
Expand Down