Fixed #32108 -- Made transaction.on_commit() raise TypeError when callback is not a callable.

This commit is contained in:
Herbert Poul 2020-10-18 10:22:12 +02:00 committed by Mariusz Felisiak
parent 0f18255848
commit c897b1587c
2 changed files with 7 additions and 0 deletions

View File

@ -632,6 +632,8 @@ class BaseDatabaseWrapper:
return self.SchemaEditorClass(self, *args, **kwargs) return self.SchemaEditorClass(self, *args, **kwargs)
def on_commit(self, func): def on_commit(self, func):
if not callable(func):
raise TypeError("on_commit()'s callback must be a callable.")
if self.in_atomic_block: if self.in_atomic_block:
# Transaction in progress; save for execution on commit. # Transaction in progress; save for execution on commit.
self.run_on_commit.append((set(self.savepoint_ids), func)) self.run_on_commit.append((set(self.savepoint_ids), func))

View File

@ -233,3 +233,8 @@ class TestConnectionOnCommit(TransactionTestCase):
transaction.on_commit(should_never_be_called) transaction.on_commit(should_never_be_called)
finally: finally:
connection.set_autocommit(True) 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)