Re: [pysqlite] sqlite3 - implicit close() when cursor object reclaimed?

Gerhard Häring <[email protected]> Sat, 06 Sep 2008 23:55:15 +0200
Newsgroups gmane.comp.python.db.pysqlite.user
Message-ID <[email protected]>
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

[email protected] wrote:
>     >> Shouldn't pysqlite_cursor_close be called from
>     >> pysqlite_cursor_dealloc?
> 
>     Gerhard> Which is the difference you see between pysqlite_cursor_close and
>     Gerhard> pysqlite_cursor_dealloc that I don't see?
> 
>     Gerhard> Both call pysqlite_statement_reset(). pysqlite_cursor_close
>     Gerhard> additionally has (helpful to not shoot yourself in the foot
>     Gerhard> with older SQLite versions) a check that it was called from the
>     Gerhard> same thread that the cursor was created from. But that's it
>     Gerhard> from what I saw in the Python 3.0 sources.
> 
> That's what I was referring to.
> 
> So is this code safe/correct even though I don't explicitly call c.close()?

Yes. FWIW I've never called close() using pysqlite (or any other
adapter) except in the pysqlite test suite.

> If I call close() here the genexp only yields a single value.
> 
>     def iterkeys(self):
>         c = self._conn.cursor()
>         c.execute("select key from dict order by key")
>         return (e[0] for e in c)

Is it unclear why it behaves like this?

Unlike other databases, SQLite really supports "cursors". So I do
something like get_next_row_of_resultset() on the SQLite side whenever
the pysqlite user requests a new row. For some really stupid reason I
don't remember right now (*), however, I always read one row ahead of
time. That's why you still get one row when you call .close() after
c.execute().

I hope that explains the behaviour. Otherwise I can try to explain more
of the internals.

- -- Gerhard

(*) I think it was the requirement to have a cursor.description right
after the execute() call, not after fetching the first row.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD4DBQFIwvxDdIO4ozGCH14RAkmTAJdYdZB0AOKc3iFsaZE7U3AYQnn+AKCJS/q4
IPM3zmndEUL8drz+BLRPiQ==
=a8lJ
-----END PGP SIGNATURE-----