Skip to content

Commit 18744f7

Browse files
committed
1 parent c857356 commit 18744f7

File tree

2 files changed

+49
-3
lines changed

2 files changed

+49
-3
lines changed

src/core/modules/listeners/listeners_manager.cpp

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ void CListenerManager::RegisterListener(PyObject* pCallable)
3939
object oCallable = object(handle<>(borrowed(pCallable)));
4040

4141
// Is the callable already in the vector?
42-
if( !m_vecCallables.HasElement(oCallable) )
42+
if( !IsRegistered(oCallable) )
4343
{
4444
m_vecCallables.AddToTail(oCallable);
4545
}
@@ -57,7 +57,7 @@ void CListenerManager::UnregisterListener(PyObject* pCallable)
5757
// Get the object instance of the callable
5858
object oCallable = object(handle<>(borrowed(pCallable)));
5959

60-
int index = m_vecCallables.Find(oCallable);
60+
int index = FindCallback(oCallable); //m_vecCallables.Find(oCallable);
6161

6262
if (index == -1) {
6363
BOOST_RAISE_EXCEPTION(PyExc_ValueError, "Callback not registered.")
@@ -96,7 +96,19 @@ int CListenerManager::GetCount()
9696
//-----------------------------------------------------------------------------
9797
bool CListenerManager::IsRegistered(object oCallback)
9898
{
99-
return m_vecCallables.HasElement(oCallback);
99+
return FindCallback(oCallback) != -1;
100+
//return m_vecCallables.HasElement(oCallback);
101+
}
102+
103+
int CListenerManager::FindCallback(object oCallback)
104+
{
105+
for (int i=0; i < m_vecCallables.Count(); ++i) {
106+
object oCurrent = m_vecCallables[i];
107+
if (is_same_func(oCallback, oCurrent)) {
108+
return i;
109+
}
110+
}
111+
return -1;
100112
}
101113

102114
object CListenerManager::__getitem__(unsigned int index)
@@ -111,3 +123,29 @@ void CListenerManager::clear()
111123
{
112124
m_vecCallables.RemoveAll();
113125
}
126+
127+
128+
//-----------------------------------------------------------------------------
129+
// Functions
130+
//-----------------------------------------------------------------------------
131+
bool is_same_func(object f1, object f2)
132+
{
133+
object self1, self2;
134+
try {
135+
self1 = f1.attr("__self__");
136+
}
137+
catch (...) {
138+
PyErr_Clear();
139+
return f1 == f2;
140+
}
141+
142+
try {
143+
self2 = f2.attr("__self__");
144+
}
145+
catch (...) {
146+
PyErr_Clear();
147+
return f1 == f2;
148+
}
149+
150+
return self1.ptr() == self2.ptr() && f1.attr("__func__") == f2.attr("__func__");
151+
}

src/core/modules/listeners/listeners_manager.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,17 @@ class CListenerManager
7070
object __getitem__(unsigned int index);
7171
void clear();
7272

73+
int FindCallback(object oCallback);
74+
7375
public:
7476
CUtlVector<object> m_vecCallables;
7577
};
7678

7779

80+
//-----------------------------------------------------------------------------
81+
// Functions
82+
//-----------------------------------------------------------------------------
83+
bool is_same_func(object f1, object f2);
84+
85+
7886
#endif // _LISTENERS_MANAGER_H

0 commit comments

Comments
 (0)