Skip to content

Commit ced3598

Browse files
miss-islingtonsobolevnerlend-aasland
authored
[3.12] gh-111726: Explicitly close database connections in sqlite3 doctests (GH-111730) (#117630)
(cherry picked from commit a770266) Co-authored-by: Nikita Sobolev <mail@sobolevn.me> Co-authored-by: Erlend E. Aasland <erlend@python.org>
1 parent 3734bee commit ced3598

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

Doc/library/sqlite3.rst

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
src = sqlite3.connect(":memory:", isolation_level=None)
1717
dst = sqlite3.connect("tutorial.db", isolation_level=None)
1818
src.backup(dst)
19+
src.close()
20+
dst.close()
1921
del src, dst
2022

2123
.. _sqlite3-intro:
@@ -220,6 +222,7 @@ creating a new cursor, then querying the database:
220222
>>> title, year = res.fetchone()
221223
>>> print(f'The highest scoring Monty Python movie is {title!r}, released in {year}')
222224
The highest scoring Monty Python movie is 'Monty Python and the Holy Grail', released in 1975
225+
>>> new_con.close()
223226

224227
You've now created an SQLite database using the :mod:`!sqlite3` module,
225228
inserted data and retrieved values from it in multiple ways.
@@ -735,6 +738,7 @@ Connection objects
735738
>>> for row in con.execute("SELECT md5(?)", (b"foo",)):
736739
... print(row)
737740
('acbd18db4cc2f85cedef654fccc4a4d8',)
741+
>>> con.close()
738742

739743

740744
.. method:: create_aggregate(name, n_arg, aggregate_class)
@@ -871,6 +875,7 @@ Connection objects
871875
FROM test ORDER BY x
872876
""")
873877
print(cur.fetchall())
878+
con.close()
874879

875880
.. testoutput::
876881
:hide:
@@ -1161,6 +1166,8 @@ Connection objects
11611166
src = sqlite3.connect('example.db')
11621167
dst = sqlite3.connect(':memory:')
11631168
src.backup(dst)
1169+
dst.close()
1170+
src.close()
11641171

11651172
.. versionadded:: 3.7
11661173

@@ -1227,6 +1234,10 @@ Connection objects
12271234
>>> con.getlimit(sqlite3.SQLITE_LIMIT_ATTACHED)
12281235
1
12291236

1237+
.. testcleanup:: sqlite3.limits
1238+
1239+
con.close()
1240+
12301241
.. versionadded:: 3.11
12311242

12321243
.. _SQLite limit category: https://www.sqlite.org/c3ref/c_limit_attached.html
@@ -1508,6 +1519,10 @@ Cursor objects
15081519
# cur is an sqlite3.Cursor object
15091520
cur.executemany("INSERT INTO data VALUES(?)", rows)
15101521

1522+
.. testcleanup:: sqlite3.cursor
1523+
1524+
con.close()
1525+
15111526
.. note::
15121527

15131528
Any resulting rows are discarded,
@@ -1613,6 +1628,7 @@ Cursor objects
16131628
>>> cur = con.cursor()
16141629
>>> cur.connection == con
16151630
True
1631+
>>> con.close()
16161632

16171633
.. attribute:: description
16181634

@@ -1733,6 +1749,7 @@ Blob objects
17331749
greeting = blob.read()
17341750

17351751
print(greeting) # outputs "b'Hello, world!'"
1752+
con.close()
17361753

17371754
.. testoutput::
17381755
:hide:
@@ -2045,6 +2062,7 @@ Here's an example of both styles:
20452062
params = (1972,)
20462063
cur.execute("SELECT * FROM lang WHERE first_appeared = ?", params)
20472064
print(cur.fetchall())
2065+
con.close()
20482066

20492067
.. testoutput::
20502068
:hide:
@@ -2103,6 +2121,7 @@ The object passed to *protocol* will be of type :class:`PrepareProtocol`.
21032121

21042122
cur.execute("SELECT ?", (Point(4.0, -3.2),))
21052123
print(cur.fetchone()[0])
2124+
con.close()
21062125

21072126
.. testoutput::
21082127
:hide:
@@ -2133,6 +2152,7 @@ This function can then be registered using :func:`register_adapter`.
21332152

21342153
cur.execute("SELECT ?", (Point(1.0, 2.5),))
21352154
print(cur.fetchone()[0])
2155+
con.close()
21362156

21372157
.. testoutput::
21382158
:hide:
@@ -2217,6 +2237,8 @@ The following example illustrates the implicit and explicit approaches:
22172237
cur.execute("INSERT INTO test(p) VALUES(?)", (p,))
22182238
cur.execute('SELECT p AS "p [point]" FROM test')
22192239
print("with column names:", cur.fetchone()[0])
2240+
cur.close()
2241+
con.close()
22202242

22212243
.. testoutput::
22222244
:hide:
@@ -2423,6 +2445,8 @@ Some useful URI tricks include:
24232445
res = con2.execute("SELECT data FROM shared")
24242446
assert res.fetchone() == (28,)
24252447

2448+
con1.close()
2449+
con2.close()
24262450

24272451
More information about this feature, including a list of parameters,
24282452
can be found in the `SQLite URI documentation`_.
@@ -2469,6 +2493,7 @@ Queries now return :class:`!Row` objects:
24692493
'Earth'
24702494
>>> row["RADIUS"] # Column names are case-insensitive.
24712495
6378
2496+
>>> con.close()
24722497

24732498
.. note::
24742499

@@ -2495,6 +2520,7 @@ Using it, queries now return a :class:`!dict` instead of a :class:`!tuple`:
24952520
>>> for row in con.execute("SELECT 1 AS a, 2 AS b"):
24962521
... print(row)
24972522
{'a': 1, 'b': 2}
2523+
>>> con.close()
24982524

24992525
The following row factory returns a :term:`named tuple`:
25002526

@@ -2521,6 +2547,7 @@ The following row factory returns a :term:`named tuple`:
25212547
1
25222548
>>> row.b # Attribute access.
25232549
2
2550+
>>> con.close()
25242551

25252552
With some adjustments, the above recipe can be adapted to use a
25262553
:class:`~dataclasses.dataclass`, or any other custom class,

0 commit comments

Comments
 (0)