diff --git a/devito/types/basic.py b/devito/types/basic.py index 348547d2b7..db455e8924 100644 --- a/devito/types/basic.py +++ b/devito/types/basic.py @@ -497,7 +497,7 @@ def _subs(self, old, new, **hints): devito subclasses of sympy types are quite strict. """ try: - if old.name == self.name: + if old.is_Symbol and old.name == self.name: return new except AttributeError: pass diff --git a/tests/test_symbolics.py b/tests/test_symbolics.py index 39e4bdfa68..e77adb0c26 100644 --- a/tests/test_symbolics.py +++ b/tests/test_symbolics.py @@ -375,6 +375,16 @@ def test_safeinv(): assert 'SAFEINV' in str(op1) assert 'SAFEINV' in str(op2) + # Ensure .subs leaves the caller unchanged if no substitutions occur. + # We used to have a bug where passing SafeInv to .subs would result + # in the construction of weird, nonsensical objects due to the _subs + # stub in types.AbstractSymbol + f = Function(name='f', grid=grid) + ui = u1.indexify() + safeinv = SafeInv(ui, ui) + v = ui._subs(safeinv, f.indexify()) + assert str(v) == 'u[x, y]' + def test_def_function(): foo0 = DefFunction('foo', arguments=['a', 'b'], template=['int'])