Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 34b7445

Browse files
lostmsuBadSingleton
authored andcommitted
got rid of a few deprecation warnings that pollute GitHub code review
1 parent 1f9d221 commit 34b7445

9 files changed

+23
-15
lines changed

src/embed_tests/TestConverter.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ public void PyIntImplicit()
148148
{
149149
var i = new PyInt(1);
150150
var ni = (PyObject)i.As<object>();
151-
Assert.AreEqual(i.rawPtr, ni.rawPtr);
151+
Assert.IsTrue(PythonReferenceComparer.Instance.Equals(i, ni));
152152
}
153153

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

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

185188
// regression for https://github.com/pythonnet/pythonnet/issues/451

src/embed_tests/TestDomainReload.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,7 @@ from Python.EmbeddingTest.Domain import MyClass
9999
{
100100
Debug.Assert(obj.AsManagedObject(type).GetType() == type);
101101
// We only needs its Python handle
102-
PyRuntime.XIncref(obj);
103-
return obj.Handle;
102+
return new NewReference(obj).DangerousMoveToPointer();
104103
}
105104
}
106105
}

src/embed_tests/TestFinalizer.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,9 @@ public void ValidateRefCount()
212212
Assert.AreEqual(ptr, e.Handle);
213213
Assert.AreEqual(2, e.ImpactedObjects.Count);
214214
// Fix for this test, don't do this on general environment
215+
#pragma warning disable CS0618 // Type or member is obsolete
215216
Runtime.Runtime.XIncref(e.Reference);
217+
#pragma warning restore CS0618 // Type or member is obsolete
216218
return false;
217219
};
218220
Finalizer.Instance.IncorrectRefCntResolver += handler;
@@ -234,8 +236,9 @@ private static IntPtr CreateStringGarbage()
234236
{
235237
PyString s1 = new PyString("test_string");
236238
// s2 steal a reference from s1
237-
PyString s2 = new PyString(StolenReference.DangerousFromPointer(s1.Handle));
238-
return s1.Handle;
239+
IntPtr address = s1.Reference.DangerousGetAddress();
240+
PyString s2 = new (StolenReference.DangerousFromPointer(address));
241+
return address;
239242
}
240243
}
241244
}

src/embed_tests/TestNativeTypeOffset.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ public void LoadNativeTypeOffsetClass()
3333
{
3434
PyObject sys = Py.Import("sys");
3535
// We can safely ignore the "m" abi flag
36-
var abiflags = sys.GetAttr("abiflags", "".ToPython()).ToString().Replace("m", "");
36+
var abiflags = sys.HasAttr("abiflags") ? sys.GetAttr("abiflags").ToString() : "";
37+
abiflags = abiflags.Replace("m", "");
3738
if (!string.IsNullOrEmpty(abiflags))
3839
{
3940
string typeName = "Python.Runtime.NativeTypeOffset, Python.Runtime";

src/embed_tests/TestPythonException.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ def __init__(self, val):
161161
using var tbObj = tbPtr.MoveToPyObject();
162162
// the type returned from PyErr_NormalizeException should not be the same type since a new
163163
// exception was raised by initializing the exception
164-
Assert.AreNotEqual(type.Handle, typeObj.Handle);
164+
Assert.IsFalse(PythonReferenceComparer.Instance.Equals(type, typeObj));
165165
// the message should now be the string from the throw exception during normalization
166166
Assert.AreEqual("invalid literal for int() with base 10: 'dummy string'", strObj.ToString());
167167
}

src/runtime/InternString.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public static void Initialize()
4242
Debug.Assert(name == op.As<string>());
4343
SetIntern(name, op);
4444
var field = type.GetField("f" + name, PyIdentifierFieldFlags)!;
45-
field.SetValue(null, op.rawPtr);
45+
field.SetValue(null, op.DangerousGetAddressOrNull());
4646
}
4747
}
4848

@@ -76,7 +76,7 @@ public static bool TryGetInterned(BorrowedReference op, out string s)
7676
private static void SetIntern(string s, PyString op)
7777
{
7878
_string2interns.Add(s, op);
79-
_intern2strings.Add(op.rawPtr, s);
79+
_intern2strings.Add(op.Reference.DangerousGetAddress(), s);
8080
}
8181
}
8282
}

src/runtime/PythonTypes/PyObject.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public partial class PyObject : DynamicObject, IDisposable, ISerializable
2727
public StackTrace Traceback { get; } = new StackTrace(1);
2828
#endif
2929

30-
protected internal IntPtr rawPtr = IntPtr.Zero;
30+
protected IntPtr rawPtr = IntPtr.Zero;
3131
internal readonly int run = Runtime.GetRun();
3232

3333
internal BorrowedReference obj => new (rawPtr);
@@ -252,6 +252,8 @@ internal void Leak()
252252
rawPtr = IntPtr.Zero;
253253
}
254254

255+
internal IntPtr DangerousGetAddressOrNull() => rawPtr;
256+
255257
internal void CheckRun()
256258
{
257259
if (run != Runtime.GetRun())

src/runtime/Types/ReflectedClrType.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,6 @@ static ReflectedClrType AllocateClass(Type clrType)
116116
return new ReflectedClrType(type.Steal());
117117
}
118118

119-
public override bool Equals(PyObject? other) => other != null && rawPtr == other.rawPtr;
119+
public override bool Equals(PyObject? other) => rawPtr == other?.DangerousGetAddressOrNull();
120120
public override int GetHashCode() => rawPtr.GetHashCode();
121121
}

src/runtime/Util/PythonReferenceComparer.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ public sealed class PythonReferenceComparer : IEqualityComparer<PyObject>
1313
public static PythonReferenceComparer Instance { get; } = new PythonReferenceComparer();
1414
public bool Equals(PyObject? x, PyObject? y)
1515
{
16-
return x?.rawPtr == y?.rawPtr;
16+
return x?.DangerousGetAddressOrNull() == y?.DangerousGetAddressOrNull();
1717
}
1818

19-
public int GetHashCode(PyObject obj) => obj.rawPtr.GetHashCode();
19+
public int GetHashCode(PyObject obj) => obj.DangerousGetAddressOrNull().GetHashCode();
2020

2121
private PythonReferenceComparer() { }
2222
}

0 commit comments

Comments
 (0)