Skip to content

Commit 0241b38

Browse files
committed
switched eventobject.cs and eventbiding.cs to the new style references
1 parent 1d80162 commit 0241b38

File tree

3 files changed

+47
-52
lines changed

3 files changed

+47
-52
lines changed

src/runtime/eventbinding.cs

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,10 @@ namespace Python.Runtime
99
internal class EventBinding : ExtensionType
1010
{
1111
private EventObject e;
12-
private IntPtr target;
12+
private PyObject? target;
1313

14-
public EventBinding(EventObject e, IntPtr target)
14+
public EventBinding(EventObject e, PyObject? target)
1515
{
16-
Runtime.XIncref(target);
1716
this.target = target;
1817
this.e = e;
1918
}
@@ -22,58 +21,56 @@ public EventBinding(EventObject e, IntPtr target)
2221
/// <summary>
2322
/// EventBinding += operator implementation.
2423
/// </summary>
25-
public static IntPtr nb_inplace_add(IntPtr ob, IntPtr arg)
24+
public static NewReference nb_inplace_add(BorrowedReference ob, BorrowedReference arg)
2625
{
27-
var self = (EventBinding)GetManagedObject(ob);
26+
var self = (EventBinding)GetManagedObject(ob)!;
2827

2928
if (Runtime.PyCallable_Check(arg) < 1)
3029
{
3130
Exceptions.SetError(Exceptions.TypeError, "event handlers must be callable");
32-
return IntPtr.Zero;
31+
return default;
3332
}
3433

35-
if (!self.e.AddEventHandler(self.target, arg))
34+
if (!self.e.AddEventHandler(self.target.BorrowNullable(), new PyObject(arg)))
3635
{
37-
return IntPtr.Zero;
36+
return default;
3837
}
3938

40-
Runtime.XIncref(self.pyHandle);
41-
return self.pyHandle;
39+
return new NewReference(self.pyHandle);
4240
}
4341

4442

4543
/// <summary>
4644
/// EventBinding -= operator implementation.
4745
/// </summary>
48-
public static IntPtr nb_inplace_subtract(IntPtr ob, IntPtr arg)
46+
public static NewReference nb_inplace_subtract(BorrowedReference ob, BorrowedReference arg)
4947
{
50-
var self = (EventBinding)GetManagedObject(ob);
48+
var self = (EventBinding)GetManagedObject(ob)!;
5149

5250
if (Runtime.PyCallable_Check(arg) < 1)
5351
{
5452
Exceptions.SetError(Exceptions.TypeError, "invalid event handler");
55-
return IntPtr.Zero;
53+
return default;
5654
}
5755

58-
if (!self.e.RemoveEventHandler(self.target, arg))
56+
if (!self.e.RemoveEventHandler(self.target.BorrowNullable(), arg))
5957
{
60-
return IntPtr.Zero;
58+
return default;
6159
}
6260

63-
Runtime.XIncref(self.pyHandle);
64-
return self.pyHandle;
61+
return new NewReference(self.pyHandle);
6562
}
6663

6764

6865
/// <summary>
6966
/// EventBinding __hash__ implementation.
7067
/// </summary>
71-
public static nint tp_hash(IntPtr ob)
68+
public static nint tp_hash(BorrowedReference ob)
7269
{
73-
var self = (EventBinding)GetManagedObject(ob);
70+
var self = (EventBinding)GetManagedObject(ob)!;
7471
nint x = 0;
7572

76-
if (self.target != IntPtr.Zero)
73+
if (self.target != null)
7774
{
7875
x = Runtime.PyObject_Hash(self.target);
7976
if (x == -1)
@@ -95,10 +92,10 @@ public static nint tp_hash(IntPtr ob)
9592
/// <summary>
9693
/// EventBinding __repr__ implementation.
9794
/// </summary>
98-
public static IntPtr tp_repr(IntPtr ob)
95+
public static NewReference tp_repr(BorrowedReference ob)
9996
{
100-
var self = (EventBinding)GetManagedObject(ob);
101-
string type = self.target == IntPtr.Zero ? "unbound" : "bound";
97+
var self = (EventBinding)GetManagedObject(ob)!;
98+
string type = self.target == null ? "unbound" : "bound";
10299
string s = string.Format("<{0} event '{1}'>", type, self.e.name);
103100
return Runtime.PyString_FromString(s);
104101
}

src/runtime/eventobject.cs

Lines changed: 24 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ namespace Python.Runtime
1111
internal class EventObject : ExtensionType
1212
{
1313
internal string name;
14-
internal EventBinding unbound;
14+
internal EventBinding? unbound;
1515
internal EventInfo info;
16-
internal Hashtable reg;
16+
internal Hashtable? reg;
1717

1818
public EventObject(EventInfo info)
1919
{
@@ -25,12 +25,12 @@ public EventObject(EventInfo info)
2525
/// <summary>
2626
/// Register a new Python object event handler with the event.
2727
/// </summary>
28-
internal bool AddEventHandler(IntPtr target, IntPtr handler)
28+
internal bool AddEventHandler(BorrowedReference target, PyObject handler)
2929
{
30-
object obj = null;
31-
if (target != IntPtr.Zero)
30+
object? obj = null;
31+
if (target != null)
3232
{
33-
var co = (CLRObject)GetManagedObject(target);
33+
var co = (CLRObject)GetManagedObject(target)!;
3434
obj = co.inst;
3535
}
3636

@@ -70,18 +70,18 @@ internal bool AddEventHandler(IntPtr target, IntPtr handler)
7070
/// <summary>
7171
/// Remove the given Python object event handler.
7272
/// </summary>
73-
internal bool RemoveEventHandler(IntPtr target, IntPtr handler)
73+
internal bool RemoveEventHandler(BorrowedReference target, BorrowedReference handler)
7474
{
7575
if (reg == null)
7676
{
7777
Exceptions.SetError(Exceptions.ValueError, "unknown event handler");
7878
return false;
7979
}
8080

81-
object obj = null;
82-
if (target != IntPtr.Zero)
81+
object? obj = null;
82+
if (target != null)
8383
{
84-
var co = (CLRObject)GetManagedObject(target);
84+
var co = (CLRObject)GetManagedObject(target)!;
8585
obj = co.inst;
8686
}
8787

@@ -100,7 +100,7 @@ internal bool RemoveEventHandler(IntPtr target, IntPtr handler)
100100
return false;
101101
}
102102

103-
object[] args = { null };
103+
object?[] args = { null };
104104
MethodInfo mi = info.GetRemoveMethod(true);
105105

106106
for (var i = 0; i < list.Count; i++)
@@ -132,7 +132,7 @@ internal bool RemoveEventHandler(IntPtr target, IntPtr handler)
132132
/// Descriptor __get__ implementation. A getattr on an event returns
133133
/// a "bound" event that keeps a reference to the object instance.
134134
/// </summary>
135-
public static IntPtr tp_descr_get(IntPtr ds, IntPtr ob, IntPtr tp)
135+
public static NewReference tp_descr_get(BorrowedReference ds, BorrowedReference ob, BorrowedReference tp)
136136
{
137137
var self = GetManagedObject(ds) as EventObject;
138138
EventBinding binding;
@@ -146,24 +146,23 @@ public static IntPtr tp_descr_get(IntPtr ds, IntPtr ob, IntPtr tp)
146146
// an instance) we return an 'unbound' EventBinding that will
147147
// be cached for future accesses through the type.
148148

149-
if (ob == IntPtr.Zero)
149+
if (ob == null)
150150
{
151151
if (self.unbound == null)
152152
{
153-
self.unbound = new EventBinding(self, IntPtr.Zero);
153+
self.unbound = new EventBinding(self, target: null);
154154
}
155155
binding = self.unbound;
156-
Runtime.XIncref(binding.pyHandle);
157-
return binding.pyHandle;
156+
return new NewReference(binding.pyHandle);
158157
}
159158

160159
if (Runtime.PyObject_IsInstance(ob, tp) < 1)
161160
{
162161
return Exceptions.RaiseTypeError("invalid argument");
163162
}
164163

165-
binding = new EventBinding(self, ob);
166-
return binding.pyHandle;
164+
binding = new EventBinding(self, new PyObject(ob));
165+
return new NewReference(binding.pyHandle);
167166
}
168167

169168

@@ -174,7 +173,7 @@ public static IntPtr tp_descr_get(IntPtr ds, IntPtr ob, IntPtr tp)
174173
/// 'ob.SomeEvent += method', Python will attempt to set the attribute
175174
/// SomeEvent on ob to the result of the '+=' operation.
176175
/// </summary>
177-
public new static int tp_descr_set(IntPtr ds, IntPtr ob, IntPtr val)
176+
public static int tp_descr_set(BorrowedReference ds, BorrowedReference ob, BorrowedReference val)
178177
{
179178
var e = GetManagedObject(val) as EventBinding;
180179

@@ -191,31 +190,27 @@ public static IntPtr tp_descr_get(IntPtr ds, IntPtr ob, IntPtr tp)
191190
/// <summary>
192191
/// Descriptor __repr__ implementation.
193192
/// </summary>
194-
public static IntPtr tp_repr(IntPtr ob)
193+
public static NewReference tp_repr(BorrowedReference ob)
195194
{
196-
var self = (EventObject)GetManagedObject(ob);
195+
var self = (EventObject)GetManagedObject(ob)!;
197196
return Runtime.PyString_FromString($"<event '{self.name}'>");
198197
}
199198

200199

201200
protected override void Clear()
202201
{
203-
if (this.unbound is not null)
204-
{
205-
Runtime.XDecref(this.unbound.pyHandle);
206-
this.unbound = null;
207-
}
202+
this.unbound = null!;
208203
base.Clear();
209204
}
210205
}
211206

212207

213208
internal class Handler
214209
{
215-
public IntPtr hash;
216-
public Delegate del;
210+
public readonly nint hash;
211+
public readonly Delegate del;
217212

218-
public Handler(IntPtr hash, Delegate d)
213+
public Handler(nint hash, Delegate d)
219214
{
220215
this.hash = hash;
221216
this.del = d;

src/runtime/pyobject.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1429,5 +1429,8 @@ internal static class PyObjectExtensions
14291429
{
14301430
internal static NewReference NewReferenceOrNull(this PyObject? self)
14311431
=> self is null || self.IsDisposed ? default : new NewReference(self);
1432+
1433+
internal static BorrowedReference BorrowNullable(this PyObject? self)
1434+
=> self is null ? default : self.Reference;
14321435
}
14331436
}

0 commit comments

Comments
 (0)