From c897b1587cb8017b6c943bf6486286655b0d9e3d Mon Sep 17 00:00:00 2001 From: Herbert Poul Date: Sun, 18 Oct 2020 10:22:12 +0200 Subject: [PATCH] Fixed #32108 -- Made transaction.on_commit() raise TypeError when callback is not a callable. --- django/db/backends/base/base.py | 2 ++ tests/transaction_hooks/tests.py | 5 +++++ 2 files changed, 7 insertions(+) diff --git a/django/db/backends/base/base.py b/django/db/backends/base/base.py index fcaa4b46e9..42b4b178bf 100644 --- a/django/db/backends/base/base.py +++ b/django/db/backends/base/base.py @@ -632,6 +632,8 @@ class BaseDatabaseWrapper: return self.SchemaEditorClass(self, *args, **kwargs) def on_commit(self, func): + if not callable(func): + raise TypeError("on_commit()'s callback must be a callable.") if self.in_atomic_block: # Transaction in progress; save for execution on commit. self.run_on_commit.append((set(self.savepoint_ids), func)) diff --git a/tests/transaction_hooks/tests.py b/tests/transaction_hooks/tests.py index 81ff0066a1..3f3a19e568 100644 --- a/tests/transaction_hooks/tests.py +++ b/tests/transaction_hooks/tests.py @@ -233,3 +233,8 @@ class TestConnectionOnCommit(TransactionTestCase): transaction.on_commit(should_never_be_called) finally: connection.set_autocommit(True) + + def test_raises_exception_non_callable(self): + msg = "on_commit()'s callback must be a callable." + with self.assertRaisesMessage(TypeError, msg): + transaction.on_commit(None)