Skip to content

bpo-34580: Clarify calling close when connection is used in a context manager#9079

Merged
berkerpeksag merged 9 commits into
python:masterfrom
tirkarthi:bpo34580
May 19, 2019
Merged

bpo-34580: Clarify calling close when connection is used in a context manager#9079
berkerpeksag merged 9 commits into
python:masterfrom
tirkarthi:bpo34580

Conversation

@tirkarthi

@tirkarthi tirkarthi commented Sep 6, 2018

Copy link
Copy Markdown
Member

Add an extra note that the connection object doesn't call close in a context manager.

Thanks

https://bugs.python.org/issue34580

@bedevere-bot bedevere-bot added the docs Documentation in the Doc dir label Sep 6, 2018
@tirkarthi tirkarthi changed the title bpo34580: Clarify calling close when connection is used in a context manager bpo-34580: Clarify calling close when connection is used in a context manager Sep 6, 2018
Comment thread Doc/library/sqlite3.rst Outdated
Comment thread Doc/includes/sqlite3/connect_db_1.py Outdated
Comment thread Doc/includes/sqlite3/connect_db_2.py Outdated
Comment thread Doc/includes/sqlite3/ctx_manager.py
Comment thread Doc/includes/sqlite3/execute_3.py Outdated
Comment thread Doc/includes/sqlite3/row_factory.py
Comment thread Doc/includes/sqlite3/shortcut_methods.py
@bedevere-bot

Copy link
Copy Markdown

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

@tirkarthi

Copy link
Copy Markdown
Member Author

I have made the requested changes; please review again

@bedevere-bot

Copy link
Copy Markdown

Thanks for making the requested changes!

@berkerpeksag: please review the changes made to this pull request.

@tirkarthi

tirkarthi commented Sep 28, 2018

Copy link
Copy Markdown
Member Author

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!

@tirkarthi

Copy link
Copy Markdown
Member Author

Friendly ping @berkerpeksag :)

@csabella csabella requested a review from berkerpeksag May 17, 2019 23:31
@berkerpeksag berkerpeksag merged commit 287b84d into python:master May 19, 2019
@miss-islington

Copy link
Copy Markdown
Contributor

Thanks @tirkarthi for the PR, and @berkerpeksag for merging it 🌮🎉.. I'm working now to backport this PR to: 3.7.
🐍🍒⛏🤖

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request May 19, 2019
…GH-9079)

The sqlit3.Connection object doesn't call its close() method when it's used
as a context manager.
(cherry picked from commit 287b84d)

Co-authored-by: Xtreak <tir.karthi@gmail.com>
@bedevere-bot

Copy link
Copy Markdown

GH-13429 is a backport of this pull request to the 3.7 branch.

berkerpeksag pushed a commit that referenced this pull request May 19, 2019
The sqlite3.Connection object doesn't call its close() method when it's used
as a context manager.

(cherry picked from commit 287b84d)

Co-authored-by: Xtreak <tir.karthi@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

docs Documentation in the Doc dir skip news

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants