| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -148,7 +148,7 @@ public void PyIntImplicit() | |||
| 148 | 148 | { | |
| 149 | 149 | var i = new PyInt(1); | |
| 150 | 150 | var ni = (PyObject)i.As<object>(); | |
| 151 | - Assert.AreEqual(i.rawPtr, ni.rawPtr); | ||
| 151 | + Assert.IsTrue(PythonReferenceComparer.Instance.Equals(i, ni)); | ||
| 152 | 152 | } | |
| 153 | 153 | ||
| 154 | 154 | [Test] | |
@@ -178,8 +178,11 @@ public void RawPyObjectProxy() | |||
| 178 | 178 | var clrObject = (CLRObject)ManagedType.GetManagedObject(pyObjectProxy); | |
| 179 | 179 | Assert.AreSame(pyObject, clrObject.inst); | |
| 180 | 180 | ||
| 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); | ||
| 183 | 186 | } | |
| 184 | 187 | ||
| 185 | 188 | // regression for https://github.com/pythonnet/pythonnet/issues/451 | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -99,8 +99,7 @@ from Python.EmbeddingTest.Domain import MyClass | |||
| 99 | 99 | { | |
| 100 | 100 | Debug.Assert(obj.AsManagedObject(type).GetType() == type); | |
| 101 | 101 | // We only needs its Python handle | |
| 102 | - PyRuntime.XIncref(obj); | ||
| 103 | - return obj.Handle; | ||
| 102 | + return new NewReference(obj).DangerousMoveToPointer(); | ||
| 104 | 103 | } | |
| 105 | 104 | } | |
| 106 | 105 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -212,7 +212,9 @@ public void ValidateRefCount() | |||
| 212 | 212 | Assert.AreEqual(ptr, e.Handle); | |
| 213 | 213 | Assert.AreEqual(2, e.ImpactedObjects.Count); | |
| 214 | 214 | // Fix for this test, don't do this on general environment | |
| 215 | + #pragma warning disable CS0618 // Type or member is obsolete | ||
| 215 | 216 | Runtime.Runtime.XIncref(e.Reference); | |
| 217 | + #pragma warning restore CS0618 // Type or member is obsolete | ||
| 216 | 218 | return false; | |
| 217 | 219 | }; | |
| 218 | 220 | Finalizer.Instance.IncorrectRefCntResolver += handler; | |
@@ -234,8 +236,9 @@ private static IntPtr CreateStringGarbage() | |||
| 234 | 236 | { | |
| 235 | 237 | PyString s1 = new PyString("test_string"); | |
| 236 | 238 | // 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; | ||
| 239 | 242 | } | |
| 240 | 243 | } | |
| 241 | 244 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -33,7 +33,8 @@ public void LoadNativeTypeOffsetClass() | |||
| 33 | 33 | { | |
| 34 | 34 | PyObject sys = Py.Import("sys"); | |
| 35 | 35 | // 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", ""); | ||
| 37 | 38 | if (!string.IsNullOrEmpty(abiflags)) | |
| 38 | 39 | { | |
| 39 | 40 | string typeName = "Python.Runtime.NativeTypeOffset, Python.Runtime"; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -161,7 +161,7 @@ def __init__(self, val): | |||
| 161 | 161 | using var tbObj = tbPtr.MoveToPyObject(); | |
| 162 | 162 | // the type returned from PyErr_NormalizeException should not be the same type since a new | |
| 163 | 163 | // exception was raised by initializing the exception | |
| 164 | - Assert.AreNotEqual(type.Handle, typeObj.Handle); | ||
| 164 | + Assert.IsFalse(PythonReferenceComparer.Instance.Equals(type, typeObj)); | ||
| 165 | 165 | // the message should now be the string from the throw exception during normalization | |
| 166 | 166 | Assert.AreEqual("invalid literal for int() with base 10: 'dummy string'", strObj.ToString()); | |
| 167 | 167 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -42,7 +42,7 @@ public static void Initialize() | |||
| 42 | 42 | Debug.Assert(name == op.As<string>()); | |
| 43 | 43 | SetIntern(name, op); | |
| 44 | 44 | var field = type.GetField("f" + name, PyIdentifierFieldFlags)!; | |
| 45 | - field.SetValue(null, op.rawPtr); | ||
| 45 | + field.SetValue(null, op.DangerousGetAddressOrNull()); | ||
| 46 | 46 | } | |
| 47 | 47 | } | |
| 48 | 48 | ||
@@ -76,7 +76,7 @@ public static bool TryGetInterned(BorrowedReference op, out string s) | |||
| 76 | 76 | private static void SetIntern(string s, PyString op) | |
| 77 | 77 | { | |
| 78 | 78 | _string2interns.Add(s, op); | |
| 79 | - _intern2strings.Add(op.rawPtr, s); | ||
| 79 | + _intern2strings.Add(op.Reference.DangerousGetAddress(), s); | ||
| 80 | 80 | } | |
| 81 | 81 | } | |
| 82 | 82 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -27,7 +27,7 @@ public partial class PyObject : DynamicObject, IDisposable, ISerializable | |||
| 27 | 27 | public StackTrace Traceback { get; } = new StackTrace(1); | |
| 28 | 28 | #endif | |
| 29 | 29 | ||
| 30 | - protected internal IntPtr rawPtr = IntPtr.Zero; | ||
| 30 | + protected IntPtr rawPtr = IntPtr.Zero; | ||
| 31 | 31 | internal readonly int run = Runtime.GetRun(); | |
| 32 | 32 | ||
| 33 | 33 | internal BorrowedReference obj => new (rawPtr); | |
@@ -252,6 +252,8 @@ internal void Leak() | |||
| 252 | 252 | rawPtr = IntPtr.Zero; | |
| 253 | 253 | } | |
| 254 | 254 | ||
| 255 | + internal IntPtr DangerousGetAddressOrNull() => rawPtr; | ||
| 256 | + | ||
| 255 | 257 | internal void CheckRun() | |
| 256 | 258 | { | |
| 257 | 259 | if (run != Runtime.GetRun()) | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -117,6 +117,6 @@ static ReflectedClrType AllocateClass(Type clrType) | |||
| 117 | 117 | return new ReflectedClrType(type.Steal()); | |
| 118 | 118 | } | |
| 119 | 119 | ||
| 120 | - public override bool Equals(PyObject? other) => other != null && rawPtr == other.rawPtr; | ||
| 120 | + public override bool Equals(PyObject? other) => rawPtr == other?.DangerousGetAddressOrNull(); | ||
| 121 | 121 | public override int GetHashCode() => rawPtr.GetHashCode(); | |
| 122 | 122 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -13,10 +13,10 @@ public sealed class PythonReferenceComparer : IEqualityComparer<PyObject> | |||
| 13 | 13 | public static PythonReferenceComparer Instance { get; } = new PythonReferenceComparer(); | |
| 14 | 14 | public bool Equals(PyObject? x, PyObject? y) | |
| 15 | 15 | { | |
| 16 | - return x?.rawPtr == y?.rawPtr; | ||
| 16 | + return x?.DangerousGetAddressOrNull() == y?.DangerousGetAddressOrNull(); | ||
| 17 | 17 | } | |
| 18 | 18 | ||
| 19 | - public int GetHashCode(PyObject obj) => obj.rawPtr.GetHashCode(); | ||
| 19 | + public int GetHashCode(PyObject obj) => obj.DangerousGetAddressOrNull().GetHashCode(); | ||
| 20 | 20 | ||
| 21 | 21 | private PythonReferenceComparer() { } | |
| 22 | 22 | } | |
| Back | FazBrowse Home | New Git URL |
0 commit comments