bpo-34580: Clarify calling close when connection is used in a context manager by tirkarthi · Pull Request #9079 · python/cpython (original) (raw)
I have deleted the unused files and added comments about usage of close
in the PR comments. In addition to that I noticed examples in executescript and iterdump where also missing close. I have added them.
One thing I noticed with the backup example was that connection object was initialized in the context manager itself thus the commit would be called but the connection object will be de-allocated without closing the connection. I hope I am correct here given my understanding of the PR and context managers. So I have changed the current example
Current example :
con = sqlite3.connect('existing_db.db') with sqlite3.connect('backup.db') as bck: con.backup(bck, pages=1, progress=progress)
Example in the PR :
con = sqlite3.connect('existing_db.db') bck = sqlite3.connect('backup.db') with bck: con.backup(bck, pages=1, progress=progress) bck.close() con.close()
Apologies for multiple commits that might have spammed you after notifying with the bot. I added con.backup(bck, pages=1, progress=progress)
and then realized it needs to be wrapped in a with statement so that the changes are committed in the transaction.
Thanks for the review!