Skip to content

Commit 6f60713

Browse files
sobolevnerlend-aasland
authored andcommitted
pythongh-111726: Explicitly close database connections in sqlite3 doctests (python#111730)
Co-authored-by: Erlend E. Aasland <[email protected]>
1 parent d3185bf commit 6f60713

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.
@@ -744,6 +747,7 @@ Connection objects
744747
>>> for row in con.execute("SELECT md5(?)", (b"foo",)):
745748
... print(row)
746749
('acbd18db4cc2f85cedef654fccc4a4d8',)
750+
>>> con.close()
747751

748752
.. versionchanged:: 3.13
749753

@@ -890,6 +894,7 @@ Connection objects
890894
FROM test ORDER BY x
891895
""")
892896
print(cur.fetchall())
897+
con.close()
893898

894899
.. testoutput::
895900
:hide:
@@ -1201,6 +1206,8 @@ Connection objects
12011206
src = sqlite3.connect('example.db')
12021207
dst = sqlite3.connect(':memory:')
12031208
src.backup(dst)
1209+
dst.close()
1210+
src.close()
12041211

12051212
.. versionadded:: 3.7
12061213

@@ -1267,6 +1274,10 @@ Connection objects
12671274
>>> con.getlimit(sqlite3.SQLITE_LIMIT_ATTACHED)
12681275
1
12691276

1277+
.. testcleanup:: sqlite3.limits
1278+
1279+
con.close()
1280+
12701281
.. versionadded:: 3.11
12711282

12721283
.. _SQLite limit category: https://www.sqlite.org/c3ref/c_limit_attached.html
@@ -1548,6 +1559,10 @@ Cursor objects
15481559
# cur is an sqlite3.Cursor object
15491560
cur.executemany("INSERT INTO data VALUES(?)", rows)
15501561

1562+
.. testcleanup:: sqlite3.cursor
1563+
1564+
con.close()
1565+
15511566
.. note::
15521567

15531568
Any resulting rows are discarded,
@@ -1653,6 +1668,7 @@ Cursor objects
16531668
>>> cur = con.cursor()
16541669
>>> cur.connection == con
16551670
True
1671+
>>> con.close()
16561672

16571673
.. attribute:: description
16581674

@@ -1773,6 +1789,7 @@ Blob objects
17731789
greeting = blob.read()
17741790

17751791
print(greeting) # outputs "b'Hello, world!'"
1792+
con.close()
17761793

17771794
.. testoutput::
17781795
:hide:
@@ -2085,6 +2102,7 @@ Here's an example of both styles:
20852102
params = (1972,)
20862103
cur.execute("SELECT * FROM lang WHERE first_appeared = ?", params)
20872104
print(cur.fetchall())
2105+
con.close()
20882106

20892107
.. testoutput::
20902108
:hide:
@@ -2143,6 +2161,7 @@ The object passed to *protocol* will be of type :class:`PrepareProtocol`.
21432161

21442162
cur.execute("SELECT ?", (Point(4.0, -3.2),))
21452163
print(cur.fetchone()[0])
2164+
con.close()
21462165

21472166
.. testoutput::
21482167
:hide:
@@ -2173,6 +2192,7 @@ This function can then be registered using :func:`register_adapter`.
21732192

21742193
cur.execute("SELECT ?", (Point(1.0, 2.5),))
21752194
print(cur.fetchone()[0])
2195+
con.close()
21762196

21772197
.. testoutput::
21782198
:hide:
@@ -2257,6 +2277,8 @@ The following example illustrates the implicit and explicit approaches:
22572277
cur.execute("INSERT INTO test(p) VALUES(?)", (p,))
22582278
cur.execute('SELECT p AS "p [point]" FROM test')
22592279
print("with column names:", cur.fetchone()[0])
2280+
cur.close()
2281+
con.close()
22602282

22612283
.. testoutput::
22622284
:hide:
@@ -2463,6 +2485,8 @@ Some useful URI tricks include:
24632485
res = con2.execute("SELECT data FROM shared")
24642486
assert res.fetchone() == (28,)
24652487

2488+
con1.close()
2489+
con2.close()
24662490

24672491
More information about this feature, including a list of parameters,
24682492
can be found in the `SQLite URI documentation`_.
@@ -2509,6 +2533,7 @@ Queries now return :class:`!Row` objects:
25092533
'Earth'
25102534
>>> row["RADIUS"] # Column names are case-insensitive.
25112535
6378
2536+
>>> con.close()
25122537

25132538
.. note::
25142539

@@ -2535,6 +2560,7 @@ Using it, queries now return a :class:`!dict` instead of a :class:`!tuple`:
25352560
>>> for row in con.execute("SELECT 1 AS a, 2 AS b"):
25362561
... print(row)
25372562
{'a': 1, 'b': 2}
2563+
>>> con.close()
25382564

25392565
The following row factory returns a :term:`named tuple`:
25402566

@@ -2561,6 +2587,7 @@ The following row factory returns a :term:`named tuple`:
25612587
1
25622588
>>> row.b # Attribute access.
25632589
2
2590+
>>> con.close()
25642591

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

0 commit comments

Comments
 (0)