8000 bpo-34580: Update sqlite3 examples to call close() explicitly (GH-9079) · python/cpython@287b84d · GitHub
[go: up one dir, main page]

Skip to content

Commit 287b84d

Browse files
tirkarthiberkerpeksag
authored andcommitted
bpo-34580: Update sqlite3 examples to call close() explicitly (GH-9079)
The sqlit3.Connection object doesn't call its close() method when it's used as a context manager.
1 parent 5c08ce9 commit 287b84d

26 files changed

+52
-19
lines changed

Doc/includes/sqlite3/adapter_datetime.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,5 @@ def adapt_datetime(ts):
1313
now = datetime.datetime.now()
1414
cur.execute("select ?", (now,))
1515
print(cur.fetchone()[0])
16+
17+
con.close()

Doc/includes/sqlite3/adapter_point_1.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,5 @@ def __conform__(self, protocol):
1414
p = Point(4.0, -3.2)
1515
cur.execute("select ?", (p,))
1616
print(cur.fetchone()[0])
17+
18+
con.close()

Doc/includes/sqlite3/adapter_point_2.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,5 @@ def adapt_point(point):
1515
p = Point(4.0, -3.2)
1616
cur.execute("select ?", (p,))
1717
print(cur.fetchone()[0])
18+
19+
con.close()

Doc/includes/sqlite3/connect_db_1.py

Lines changed: 0 additions & 3 deletions
This file was deleted.

Doc/includes/sqlite3/connect_db_2.py

Lines changed: 0 additions & 3 deletions
This file was deleted.

Doc/includes/sqlite3/countcursors.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,5 @@ def cursor(self, *args, **kwargs):
1313
cur1 = con.cursor()
1414
cur2 = con.cursor()
1515
print(con.numcursors)
16+
17+
con.close()

Doc/includes/sqlite3/ctx_manager.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,7 @@
1414
con.execute("insert into person(firstname) values (?)", ("Joe",))
1515
except sqlite3.IntegrityError:
1616
print("couldn't add Joe twice")
17+
18+
# Connection object used as context manager only commits or rollbacks transactions,
19+
# so the connection object should be closed m 628C anually
20+
con.close()

Doc/includes/sqlite3/execsql_fetchonerow.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,5 @@
1515
cur.execute(SELECT)
1616
for row in cur:
1717
print('%s is %d years old.' % (row[0], row[1]))
18+
19+
con.close()

Doc/includes/sqlite3/execsql_printall_1.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,5 @@
1111

1212
# Retrieve all rows as a sequence and print that sequence:
1313
print(cur.fetchall())
14+
15+
con.close()

Doc/includes/sqlite3/execute_1.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,5 @@
1414
cur.execute("select * from people where name_last=:who and age=:age", {"who": who, "age": age})
1515

1616
print(cur.fetchone())
17+
18+
con.close()

Doc/includes/sqlite3/execute_3.py

Lines changed: 0 additions & 12 deletions
This file was deleted.

Doc/includes/sqlite3/executemany_1.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,5 @@ def __next__(self):
2222

2323
cur.execute("select c from characters")
2424
print(cur.fetchall())
25+
26+
con.close()

Doc/includes/sqlite3/executemany_2.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,5 @@ def char_generator():
1313

1414
cur.execute("select c from characters")
1515
print(cur.fetchall())
16+
17+
con.close()

Doc/includes/sqlite3/executescript.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,4 @@
2222
1987
2323
);
2424
""")
25+
con.close()

Doc/includes/sqlite3/insert_more_people.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,5 @@
1414

1515
# The changes will not be saved unless the transaction is committed explicitly:
1616
con.commit()
17+
18+
con.close()

Doc/includes/sqlite3/load_extension.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,5 @@
2424
""")
2525
for row in con.execute("select rowid, name, ingredients from recipe where name match 'pie'"):
2626
print(row)
27+
28+
con.close()

Doc/includes/sqlite3/md5func.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,5 @@ def md5sum(t):
99
cur = con.cursor()
1010
cur.execute("select md5(?)", (b"foo",))
1111
print(cur.fetchone()[0])
12+
13+
con.close()

Doc/includes/sqlite3/mysumaggr.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,5 @@ def finalize(self):
1818
cur.execute("insert into test(i) values (2)")
1919
cur.execute("select mysum(i) from test")
2020
print(cur.fetchone()[0])
21+
22+
con.close()

Doc/includes/sqlite3/parse_colnames.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,5 @@
66
cur.execute('select ? as "x [timestamp]"', (datetime.datetime.now(),))
77
dt = cur.fetchone()[0]
88
print(dt, type(dt))
9+
10+
con.close()

Doc/includes/sqlite3/pysqlite_datetime.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,5 @@
1818
row = cur.fetchone()
1919
print("current_date", row[0], type(row[0]))
2020
print("current_timestamp", row[1], type(row[1]))
21+
22+
con.close()

Doc/includes/sqlite3/row_factory.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,5 @@ def dict_factory(cursor, row):
1111
cur = con.cursor()
1212
cur.execute("select 1 as a")
1313
print(cur.fetchone()["a"])
14+
15+
con.close()

Doc/includes/sqlite3/rowclass.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,5 @@
1010
assert row["name"] == row["nAmE"]
1111
assert row[1] == row["age"]
1212
assert row[1] == row["AgE"]
13+
14+
con.close()

Doc/includes/sqlite3/shortcut_methods.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,7 @@
1818
print(row)
1919

2020
print("I just deleted", con.execute("delete from person").rowcount, "rows")
21+
22+
# close is not a shortcut method and it's not called automatically,
23+
# so the connection object should be closed manually
24+
con.close()

Doc/includes/sqlite3/simple_tableprinter.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,5 @@
2424
print(fieldValue.ljust(FIELD_MAX_WIDTH), end=' ')
2525

2626
print() # Finish the row with a newline.
27+
28+
con.close()

Doc/includes/sqlite3/text_factory.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,5 @@
2525
cur.execute("select ?", ("bar",))
2626
row = cur.fetchone()
2727
assert row[0] == "barfoo"
28+
29+
con.close()

Doc/library/sqlite3.rst

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -537,6 +537,7 @@ Connection Objects
537537
with open('dump.sql', 'w') as f:
538538
for line in con.iterdump():
539539
f.write('%s\n' % line)
540+
con.close()
540541

541542

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

575576
con = sqlite3.connect('existing_db.db')
576-
with sqlite3.connect('backup.db') as bck:
577+
bck = sqlite3.connect('backup.db')
578+
with bck:
577579
con.backup(bck, pages=1, progress=progress)
580+
bck.close()
581+
con.close()
578582

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

0 commit comments

Comments
 (0)
0