| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 7f8a247 commit 11bebe4
5 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -93,6 +93,12 @@ internal PyObject(in StolenReference reference) | |||
| 93 | 93 | Finalizer.Instance.ThrottledCollect(); | |
| 94 | 94 | } | |
| 95 | 95 | ||
| 96 | + /// <summary> | ||
| 97 | + /// Create a new PyObject instance of this object, bumping the reference | ||
| 98 | + /// count. | ||
| 99 | + /// </summary> | ||
| 100 | + public PyObject NewReference() => new(this); | ||
| 101 | + | ||
| 96 | 102 | // Ensure that encapsulated Python object is decref'ed appropriately | |
| 97 | 103 | // when the managed wrapper is garbage-collected. | |
| 98 | 104 | ~PyObject() | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -35,6 +35,12 @@ internal PyType(in StolenReference reference, bool prevalidated = false) : base( | |||
| 35 | 35 | throw new ArgumentException("object is not a type"); | |
| 36 | 36 | } | |
| 37 | 37 | ||
| 38 | + /// <summary> | ||
| 39 | + /// Create a new PyType instance of this object, bumping the reference | ||
| 40 | + /// count. | ||
| 41 | + /// </summary> | ||
| 42 | + public new PyType NewReference() => new(this); | ||
| 43 | + | ||
| 38 | 44 | protected PyType(SerializationInfo info, StreamingContext context) : base(info, context) { } | |
| 39 | 45 | ||
| 40 | 46 | internal new static PyType? FromNullableReference(BorrowedReference reference) | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -18,14 +18,12 @@ internal class MethodBinding : ExtensionType | |||
| 18 | 18 | internal MaybeMethodInfo info; | |
| 19 | 19 | internal MethodObject m; | |
| 20 | 20 | internal PyObject? target; | |
| 21 | - internal PyType? targetType; | ||
| 21 | + internal PyType targetType; | ||
| 22 | 22 | ||
| 23 | - public MethodBinding(MethodObject m, PyObject? target, PyType? targetType = null) | ||
| 23 | + public MethodBinding(MethodObject m, PyObject? target, PyType targetType) | ||
| 24 | 24 | { | |
| 25 | 25 | this.target = target; | |
| 26 | - | ||
| 27 | - this.targetType = targetType ?? target?.GetPythonType(); | ||
| 28 | - | ||
| 26 | + this.targetType = targetType; | ||
| 29 | 27 | this.info = null; | |
| 30 | 28 | this.m = m; | |
| 31 | 29 | } | |
@@ -54,10 +52,7 @@ public static NewReference mp_subscript(BorrowedReference tp, BorrowedReference | |||
| 54 | 52 | } | |
| 55 | 53 | ||
| 56 | 54 | MethodObject overloaded = self.m.WithOverloads(overloads); | |
| 57 | - var mb = new MethodBinding( | ||
| 58 | - overloaded, | ||
| 59 | - self.target is null ? null : new PyObject(self.target.Reference), | ||
| 60 | - self.targetType is null ? null : new PyType(self.targetType.Reference)); | ||
| 55 | + var mb = new MethodBinding(overloaded, self.target?.NewReference(), self.targetType.NewReference()); | ||
| 61 | 56 | return mb.Alloc(); | |
| 62 | 57 | } | |
| 63 | 58 | ||
@@ -144,8 +139,7 @@ public static NewReference tp_getattro(BorrowedReference ob, BorrowedReference k | |||
| 144 | 139 | // FIXME: deprecate __overloads__ soon... | |
| 145 | 140 | case "__overloads__": | |
| 146 | 141 | case "Overloads": | |
| 147 | - var om = new OverloadMapper(self.m, | ||
| 148 | - self.target is null ? null : new PyObject(self.target.Reference)); | ||
| 142 | + var om = new OverloadMapper(self.m, self.target?.NewReference(), self.targetType.NewReference()); | ||
| 149 | 143 | return om.Alloc(); | |
| 150 | 144 | case "__signature__" when Runtime.InspectModule is not null: | |
| 151 | 145 | var sig = self.Signature; | |
@@ -253,7 +247,6 @@ public static NewReference tp_call(BorrowedReference ob, BorrowedReference args, | |||
| 253 | 247 | } | |
| 254 | 248 | } | |
| 255 | 249 | ||
| 256 | - | ||
| 257 | 250 | /// <summary> | |
| 258 | 251 | /// MethodBinding __hash__ implementation. | |
| 259 | 252 | /// </summary> | |
@@ -289,6 +282,7 @@ public static NewReference tp_repr(BorrowedReference ob) | |||
| 289 | 282 | protected override void OnClear() | |
| 290 | 283 | { | |
| 291 | 284 | target?.Dispose(); | |
| 285 | + targetType.Dispose(); | ||
| 292 | 286 | target = null; | |
| 293 | 287 | } | |
| 294 | 288 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -197,7 +197,7 @@ public static NewReference tp_descr_get(BorrowedReference ds, BorrowedReference | |||
| 197 | 197 | && self.type.Value.IsInstanceOfType(obj.inst)) | |
| 198 | 198 | { | |
| 199 | 199 | var basecls = ReflectedClrType.GetOrCreate(self.type.Value); | |
| 200 | - return new MethodBinding(self, new PyObject(ob), basecls).Alloc(); | ||
| 200 | + return new MethodBinding(self, new PyObject(ob), basecls.NewReference()).Alloc(); | ||
| 201 | 201 | } | |
| 202 | 202 | ||
| 203 | 203 | return new MethodBinding(self, target: new PyObject(ob), targetType: new PyType(tp)).Alloc(); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -11,10 +11,12 @@ internal class OverloadMapper : ExtensionType | |||
| 11 | 11 | { | |
| 12 | 12 | private readonly MethodObject m; | |
| 13 | 13 | private PyObject? target; | |
| 14 | + readonly PyType targetType; | ||
| 14 | 15 | ||
| 15 | - public OverloadMapper(MethodObject m, PyObject? target) | ||
| 16 | + public OverloadMapper(MethodObject m, PyObject? target, PyType targetType) | ||
| 16 | 17 | { | |
| 17 | 18 | this.target = target; | |
| 19 | + this.targetType = targetType; | ||
| 18 | 20 | this.m = m; | |
| 19 | 21 | } | |
| 20 | 22 | ||
@@ -42,10 +44,7 @@ public static NewReference mp_subscript(BorrowedReference tp, BorrowedReference | |||
| 42 | 44 | return Exceptions.RaiseTypeError(e); | |
| 43 | 45 | } | |
| 44 | 46 | ||
| 45 | - var mb = new MethodBinding( | ||
| 46 | - self.m, | ||
| 47 | - self.target is null ? null : new PyObject(self.target.Reference)) | ||
| 48 | - { info = mi }; | ||
| 47 | + var mb = new MethodBinding(self.m, self.target?.NewReference(), self.targetType.NewReference()) { info = mi }; | ||
| 49 | 48 | return mb.Alloc(); | |
| 50 | 49 | } | |
| 51 | 50 | ||
@@ -61,6 +60,7 @@ public static NewReference tp_repr(BorrowedReference op) | |||
| 61 | 60 | protected override void OnClear() | |
| 62 | 61 | { | |
| 63 | 62 | target?.Dispose(); | |
| 63 | + targetType.Dispose(); | ||
| 64 | 64 | target = null; | |
| 65 | 65 | } | |
| 66 | 66 | } | |
| Back | FazBrowse Home | New Git URL |
0 commit comments