Skip to content

Commit 86e4f33

Browse files
spethischoegl
authored andcommitted
[Python/Reactor] Check for attempts to assign unknown delegates
1 parent 2907cde commit 86e4f33

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

interfaces/cython/cantera/delegator.pyx

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,9 @@ cdef int assign_delegates(obj, CxxDelegator* delegator) except -1:
274274
cdef string cxx_name
275275
cdef string cxx_when
276276
obj._delegates = []
277+
valid_names = set()
277278
for name, options in obj.delegatable_methods.items():
279+
valid_names.add(name)
278280
if len(options) == 3:
279281
# Delegate with pre-selected mode, without using prefix on method name
280282
when = options[2]
@@ -375,6 +377,14 @@ cdef int assign_delegates(obj, CxxDelegator* delegator) except -1:
375377
# collector
376378
obj._delegates.append(method)
377379

380+
# Check for methods on the base object that have no matching delegate
381+
for name in dir(obj):
382+
if (name.startswith("before_") or name.startswith("after_") or
383+
name.startswith("replace_")):
384+
base_name = name.split("_", 1)[1]
385+
if base_name not in valid_names:
386+
raise ValueError(f"'{base_name}' is not a delegatable method")
387+
378388
return 0
379389

380390

test/python/test_reactor.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3115,6 +3115,13 @@ def replace_component_index(self, name):
31153115
r2.component_index("H2")
31163116
assert r2.component_index("succeed") == 0
31173117

3118+
class DummyReactor3(ct.ExtensibleReactor):
3119+
def before_foobar(self, t):
3120+
pass
3121+
3122+
with pytest.raises(ValueError, match="'foobar' is not a delegatable method"):
3123+
DummyReactor3(self.gas)
3124+
31183125
def test_delegate_throws(self):
31193126
class TestException(Exception):
31203127
pass

0 commit comments

Comments
 (0)