postgres.py
postgres.py copied to clipboard
cursor.get_cursor should return self
That will make it easier to interchange db and cursor objects. Cropped up at https://github.com/gratipay/gratipay.com/issues/2729#issuecomment-54536544. Similar to #39.
It shouldn't return itself, if it does then the transaction will be committed at the end of the inner with
block, instead of the outer one. In gratipay/gratipay.com#2718 I use a context that doesn't do anything when it's entered or exited.
I don't think this is a good idea, because it makes it easy to introduce subtle issues. Requiring explicit changes to support both DB and cursor objects in a function seems less likely to result in bugs.
A method that explicitly supports DB and cursor objects can look like this:
def foo(self, cursor=None):
(self.db or cursor).run("...")
or that:
def foo(self, cursor=None):
with self.db.get_cursor(cursor=cursor) as c:
...
This second pattern currently isn't supported by postgres.py
, it's implemented in Liberapay by subclassing Postgres
.