On Sunday 19 January 2014 11:52:40 Łukasz Rekucki wrote:
> On 19 January 2014 09:12, Shai Berger <
sh...@platonix.com> wrote:
> > On Friday 17 January 2014 01:19:29 Michael Manfre wrote:
> >> In an effort to make Django a bit more explicit with regards to closing
> >> cursors when they are no longer needed, I have created ticket #21751 [1]
> >> with a pull request[2].
> >>
> >> Most of the changes are straight forward and change the usage of a
> >> cursor so that it uses a context manager or closes the cursor in
> >> 'finally'.
> >>
> >> Example:
> >>
> >> # old pattern
> >> connection.cursor().execute(...)
> >>
> >> # changes to
> >>
> >> with connection.cursor() as cursor:
> >> cursor.execute(...)
> >
> > I think this is suboptimal, API-wise. Python destroys temporaries soon
> > enough.
>
> You mean CPython, right? Considering that a DB cursor can also
> allocate resources in the DB itself, I think it's not a good idea to
> rely on GC here. Unfortunately, the pull request in question won't
> help in many cases due to the way how try: finally: works in
> generators[1].
>
Yep, I had CPython's behavior in mind. Didn't realize how different the others
Still, spreading with-blocks all over the code for this looks very ugly to me.
cursors instead. Perhaps even only as private API, as that usage is mostly