@@ -47,7 +47,6 @@ Controller_Trampoline::~Controller_Trampoline()
4747 {
4848 PythonEnvironment::gil acquire {" ~Controller_Trampoline" };
4949 m_methodCache.clear ();
50- m_onEventMethod = py::object ();
5150 m_pySelf = py::object ();
5251 }
5352}
@@ -60,16 +59,8 @@ void Controller_Trampoline::initializePythonCache()
6059 // Must be called with GIL held
6160 m_pySelf = py::cast (this );
6261
63- // Cache the fallback "onEvent" method if it exists
64- if (py::hasattr (m_pySelf, " onEvent" ))
65- {
66- py::object fct = m_pySelf.attr (" onEvent" );
67- if (PyCallable_Check (fct.ptr ()))
68- {
69- m_hasOnEvent = true ;
70- m_onEventMethod = fct;
71- }
72- }
62+ // Pre-cache the fallback "onEvent" method via the standard cache path
63+ getCachedMethod (" onEvent" );
7364
7465 m_cacheInitialized = true ;
7566}
@@ -122,15 +113,6 @@ void Controller_Trampoline::invalidateMethodCache(const std::string& methodName)
122113 if (!m_cacheInitialized)
123114 return ;
124115
125- if (methodName == " onEvent" )
126- {
127- // Clear the dedicated fallback cache; handleEvent will re-resolve it lazily
128- m_hasOnEvent = false ;
129- m_onEventMethod = py::object ();
130- m_onEventDirty = true ;
131- return ;
132- }
133-
134116 // Remove the entry so getCachedMethod will re-resolve it on next call
135117 m_methodCache.erase (methodName);
136118}
@@ -208,39 +190,16 @@ void Controller_Trampoline::handleEvent(Event* event)
208190 // Build the event-specific method name (e.g., "onAnimateBeginEvent")
209191 std::string methodName = std::string (" on" ) + event->getClassName ();
210192
211- // Try to get the cached method for this specific event type
193+ // Try the event-specific method first, then fall back to generic "onEvent"
212194 py::object method = getCachedMethod (methodName);
195+ if (!method)
196+ method = getCachedMethod (" onEvent" );
213197
214198 if (method)
215199 {
216- // Found a specific handler for this event type
217200 bool isHandled = callCachedMethod (method, event);
218201 if (isHandled)
219202 event->setHandled ();
220- return ;
221- }
222-
223- // Re-resolve "onEvent" if it was invalidated by a __setattr__ call
224- if (m_onEventDirty)
225- {
226- m_onEventDirty = false ;
227- if (py::hasattr (m_pySelf, " onEvent" ))
228- {
229- py::object fct = m_pySelf.attr (" onEvent" );
230- if (PyCallable_Check (fct.ptr ()))
231- {
232- m_hasOnEvent = true ;
233- m_onEventMethod = fct;
234- }
235- }
236- }
237-
238- // Fall back to the generic "onEvent" method if available
239- if (m_hasOnEvent)
240- {
241- bool isHandled = callCachedMethod (m_onEventMethod, event);
242- if (isHandled)
243- event->setHandled ();
244203 }
245204 });
246205}
0 commit comments