Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
May 19, 2019
2 changes: 2 additions & 0 deletions Doc/includes/sqlite3/adapter_datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@ def adapt_datetime(ts):
now = datetime.datetime.now()
cur.execute("select ?", (now,))
print(cur.fetchone()[0])

con.close()
2 changes: 2 additions & 0 deletions Doc/includes/sqlite3/adapter_point_1.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,5 @@ def __conform__(self, protocol):
p = Point(4.0, -3.2)
cur.execute("select ?", (p,))
print(cur.fetchone()[0])

con.close()
2 changes: 2 additions & 0 deletions Doc/includes/sqlite3/adapter_point_2.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,5 @@ def adapt_point(point):
p = Point(4.0, -3.2)
cur.execute("select ?", (p,))
print(cur.fetchone()[0])

con.close()
3 changes: 0 additions & 3 deletions Doc/includes/sqlite3/connect_db_1.py

This file was deleted.

3 changes: 0 additions & 3 deletions Doc/includes/sqlite3/connect_db_2.py

This file was deleted.

2 changes: 2 additions & 0 deletions Doc/includes/sqlite3/countcursors.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@ def cursor(self, *args, **kwargs):
cur1 = con.cursor()
cur2 = con.cursor()
print(con.numcursors)

con.close()
4 changes: 4 additions & 0 deletions Doc/includes/sqlite3/ctx_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,7 @@
con.execute("insert into person(firstname) values (?)", ("Joe",))
except sqlite3.IntegrityError:
print("couldn't add Joe twice")

# Connection object used as context manager only commits or rollbacks transactions,
# so the connection object should be closed manually
con.close()
2 changes: 2 additions & 0 deletions Doc/includes/sqlite3/execsql_fetchonerow.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,5 @@
cur.execute(SELECT)
for row in cur:
print('%s is %d years old.' % (row[0], row[1]))

con.close()
2 changes: 2 additions & 0 deletions Doc/includes/sqlite3/execsql_printall_1.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@

# Retrieve all rows as a sequence and print that sequence:
print(cur.fetchall())

con.close()
2 changes: 2 additions & 0 deletions Doc/includes/sqlite3/execute_1.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,5 @@
cur.execute("select * from people where name_last=:who and age=:age", {"who": who, "age": age})

print(cur.fetchone())

con.close()
12 changes: 0 additions & 12 deletions Doc/includes/sqlite3/execute_3.py

This file was deleted.

2 changes: 2 additions & 0 deletions Doc/includes/sqlite3/executemany_1.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,5 @@ def __next__(self):

cur.execute("select c from characters")
print(cur.fetchall())

con.close()
2 changes: 2 additions & 0 deletions Doc/includes/sqlite3/executemany_2.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@ def char_generator():

cur.execute("select c from characters")
print(cur.fetchall())

con.close()
1 change: 1 addition & 0 deletions Doc/includes/sqlite3/executescript.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@
1987
);
""")
con.close()
2 changes: 2 additions & 0 deletions Doc/includes/sqlite3/insert_more_people.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,5 @@

# The changes will not be saved unless the transaction is committed explicitly:
con.commit()

con.close()
2 changes: 2 additions & 0 deletions Doc/includes/sqlite3/load_extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,5 @@
""")
for row in con.execute("select rowid, name, ingredients from recipe where name match 'pie'"):
print(row)

con.close()
2 changes: 2 additions & 0 deletions Doc/includes/sqlite3/md5func.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ def md5sum(t):
cur = con.cursor()
cur.execute("select md5(?)", (b"foo",))
print(cur.fetchone()[0])

con.close()
2 changes: 2 additions & 0 deletions Doc/includes/sqlite3/mysumaggr.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,5 @@ def finalize(self):
cur.execute("insert into test(i) values (2)")
cur.execute("select mysum(i) from test")
print(cur.fetchone()[0])

con.close()
2 changes: 2 additions & 0 deletions Doc/includes/sqlite3/parse_colnames.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@
cur.execute('select ? as "x [timestamp]"', (datetime.datetime.now(),))
dt = cur.fetchone()[0]
print(dt, type(dt))

con.close()
2 changes: 2 additions & 0 deletions Doc/includes/sqlite3/pysqlite_datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,5 @@
row = cur.fetchone()
print("current_date", row[0], type(row[0]))
print("current_timestamp", row[1], type(row[1]))

con.close()
2 changes: 2 additions & 0 deletions Doc/includes/sqlite3/row_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@ def dict_factory(cursor, row):
cur = con.cursor()
cur.execute("select 1 as a")
print(cur.fetchone()["a"])

con.close()
2 changes: 2 additions & 0 deletions Doc/includes/sqlite3/rowclass.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@
assert row["name"] == row["nAmE"]
assert row[1] == row["age"]
assert row[1] == row["AgE"]

con.close()
4 changes: 4 additions & 0 deletions Doc/includes/sqlite3/shortcut_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,7 @@
print(row)

print("I just deleted", con.execute("delete from person").rowcount, "rows")

# close is not a shortcut method and it's not called automatically,
# so the connection object should be closed manually
con.close()
2 changes: 2 additions & 0 deletions Doc/includes/sqlite3/simple_tableprinter.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,5 @@
print(fieldValue.ljust(FIELD_MAX_WIDTH), end=' ')

print() # Finish the row with a newline.

con.close()
2 changes: 2 additions & 0 deletions Doc/includes/sqlite3/text_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,5 @@
cur.execute("select ?", ("bar",))
row = cur.fetchone()
assert row[0] == "barfoo"

con.close()
6 changes: 5 additions & 1 deletion Doc/library/sqlite3.rst
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,7 @@ Connection Objects
with open('dump.sql', 'w') as f:
for line in con.iterdump():
f.write('%s\n' % line)
con.close()


.. method:: backup(target, *, pages=0, progress=None, name="main", sleep=0.250)
Expand Down Expand Up @@ -573,8 +574,11 @@ Connection Objects
print(f'Copied {total-remaining} of {total} pages...')

con = sqlite3.connect('existing_db.db')
with sqlite3.connect('backup.db') as bck:
bck = sqlite3.connect('backup.db')
with bck:
con.backup(bck, pages=1, progress=progress)
bck.close()
con.close()

Example 2, copy an existing database into a transient copy::

Expand Down