Skip to content

Commit 5ddc78c

Browse files
committed
feat: bind snake case name events along with original method .net to python
1 parent c04c79f commit 5ddc78c

File tree

2 files changed

+81
-2
lines changed

2 files changed

+81
-2
lines changed

src/embed_tests/ClassManagerTests.cs

Lines changed: 80 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,18 @@ public class SnakeCaseNamesTesClass
4444
public string PublicStringProperty { get; set; } = "public_string_property";
4545
public static string PublicStaticStringProperty { get; set; } = "public_static_string_property";
4646

47+
public event EventHandler<string> PublicStringEvent;
48+
public static event EventHandler<string> PublicStaticStringEvent;
49+
50+
public void InvokePublicStringEvent(string value)
51+
{
52+
PublicStringEvent?.Invoke(this, value);
53+
}
54+
55+
public static void InvokePublicStaticStringEvent(string value)
56+
{
57+
PublicStaticStringEvent?.Invoke(null, value);
58+
}
4759

4860
public int AddNumbersAndGetHalf(int a, int b)
4961
{
@@ -124,7 +136,6 @@ public void CanSetStaticFieldUsingSnakeCaseName()
124136
var module = PyModule.FromString("module", $@"
125137
from clr import AddReference
126138
AddReference(""Python.EmbeddingTest"")
127-
AddReference(""System"")
128139
129140
from Python.EmbeddingTest import *
130141
@@ -195,7 +206,6 @@ public void CanSetStaticPropertyUsingSnakeCaseName()
195206
var module = PyModule.FromString("module", $@"
196207
from clr import AddReference
197208
AddReference(""Python.EmbeddingTest"")
198-
AddReference(""System"")
199209
200210
from Python.EmbeddingTest import *
201211
@@ -220,6 +230,74 @@ def SetSnakeCaseStaticProperty(value):
220230
}
221231
}
222232

233+
[TestCase("PublicStringEvent")]
234+
[TestCase("public_string_event")]
235+
public void BindsSnakeCaseEvents(string eventName)
236+
{
237+
var obj = new SnakeCaseNamesTesClass();
238+
using var pyObj = obj.ToPython();
239+
240+
var value = "";
241+
var eventHandler = new EventHandler<string>((sender, arg) => { value = arg; });
242+
243+
// Try with the original event name
244+
using (Py.GIL())
245+
{
246+
var module = PyModule.FromString("module", $@"
247+
def AddEventHandler(obj, handler):
248+
obj.{eventName} += handler
249+
250+
def RemoveEventHandler(obj, handler):
251+
obj.{eventName} -= handler
252+
");
253+
254+
using var pyEventHandler = eventHandler.ToPython();
255+
256+
module.InvokeMethod("AddEventHandler", pyObj, pyEventHandler);
257+
obj.InvokePublicStringEvent("new value 1");
258+
Assert.AreEqual("new value 1", value);
259+
260+
module.InvokeMethod("RemoveEventHandler", pyObj, pyEventHandler);
261+
obj.InvokePublicStringEvent("new value 2");
262+
Assert.AreEqual("new value 1", value); // Should not have changed
263+
}
264+
}
265+
266+
[TestCase("PublicStaticStringEvent")]
267+
[TestCase("public_static_string_event")]
268+
public void BindsSnakeCaseStaticEvents(string eventName)
269+
{
270+
var value = "";
271+
var eventHandler = new EventHandler<string>((sender, arg) => { value = arg; });
272+
273+
// Try with the original event name
274+
using (Py.GIL())
275+
{
276+
var module = PyModule.FromString("module", $@"
277+
from clr import AddReference
278+
AddReference(""Python.EmbeddingTest"")
279+
280+
from Python.EmbeddingTest import *
281+
282+
def AddEventHandler(handler):
283+
ClassManagerTests.SnakeCaseNamesTesClass.{eventName} += handler
284+
285+
def RemoveEventHandler(handler):
286+
ClassManagerTests.SnakeCaseNamesTesClass.{eventName} -= handler
287+
");
288+
289+
using var pyEventHandler = eventHandler.ToPython();
290+
291+
module.InvokeMethod("AddEventHandler", pyEventHandler);
292+
SnakeCaseNamesTesClass.InvokePublicStaticStringEvent("new value 1");
293+
Assert.AreEqual("new value 1", value);
294+
295+
module.InvokeMethod("RemoveEventHandler", pyEventHandler);
296+
SnakeCaseNamesTesClass.InvokePublicStaticStringEvent("new value 2");
297+
Assert.AreEqual("new value 1", value); // Should not have changed
298+
}
299+
}
300+
223301
#endregion
224302
}
225303

src/runtime/ClassManager.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -529,6 +529,7 @@ private static ClassInfo GetClassInfo(Type type, ClassBase impl)
529529
? new EventBinding(ei)
530530
: new EventObject(ei);
531531
ci.members[ei.Name] = ob.AllocObject();
532+
ci.members[ei.Name.ToSnakeCase()] = ob.AllocObject();
532533
continue;
533534

534535
case MemberTypes.NestedType:

0 commit comments

Comments
 (0)