Skip to content

Commit 7943561

Browse files
committed
Cosmetic fixes
1 parent 32285b1 commit 7943561

File tree

4 files changed

+32
-28
lines changed

4 files changed

+32
-28
lines changed

Doc/library/sqlite3.rst

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -535,7 +535,7 @@ Connection Objects
535535
.. method:: backup(target, *, pages=0, progress=None, name="main", sleep=0.250)
536536

537537
This method makes a backup of a SQLite database even while it's being accessed
538-
by other clients, or concurrently by the same connection. The copy will be
538+
by other clients, or concurrently by the same connection. The copy will be
539539
written into the mandatory argument *target*, that must be another
540540
:class:`Connection` instance.
541541

@@ -551,7 +551,8 @@ Connection Objects
551551
The *name* argument specifies the database name that will be copied: it must be
552552
a string containing either ``"main"``, the default, to indicate the main
553553
database, ``"temp"`` to indicate the temporary database or the name specified
554-
after the ``AS`` keyword in an ``ATTACH`` statement for an attached database.
554+
after the ``AS`` keyword in an ``ATTACH DATABASE`` statement for an attached
555+
database.
555556

556557
The *sleep* argument specifies the number of seconds to sleep by between
557558
successive attempts to backup remaining pages, can be specified either as an
@@ -562,7 +563,7 @@ Connection Objects
562563
import sqlite3
563564

564565
def progress(status, remaining, total):
565-
print(f"Copied {total-remaining} of {total} pages...")
566+
print(f'Copied {total-remaining} of {total} pages...')
566567

567568
con = sqlite3.connect('existing_db.db')
568569
with sqlite3.connect('backup.db') as bck:
@@ -576,10 +577,9 @@ Connection Objects
576577
dest = sqlite3.connect(':memory:')
577578
source.backup(dest)
578579

579-
.. note:: This is available only when the underlying SQLite library is at
580-
version 3.6.11 or higher.
580+
Availability: SQLite 3.6.11 or higher
581581

582-
.. versionadded:: 3.7
582+
.. versionadded:: 3.8
583583

584584

585585
.. _sqlite3-cursor-objects:

Lib/sqlite3/test/backup.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import sqlite3 as sqlite
22
import unittest
33

4-
from test import support
5-
64

75
@unittest.skipIf(sqlite.sqlite_version_info < (3, 6, 11), "Backup API not supported")
86
class BackupTests(unittest.TestCase):
@@ -42,8 +40,10 @@ def test_bad_target_in_transaction(self):
4240
bck = sqlite.connect(':memory:')
4341
bck.execute('CREATE TABLE bar (key INTEGER)')
4442
bck.executemany('INSERT INTO bar (key) VALUES (?)', [(3,), (4,)])
45-
with self.assertRaises(sqlite.OperationalError):
43+
with self.assertRaises(sqlite.OperationalError) as cm:
4644
self.cx.backup(bck)
45+
if sqlite.sqlite_version_info < (3, 8, 7):
46+
self.assertEqual(str(cm.exception), 'target is in transaction')
4747

4848
def test_keyword_only_args(self):
4949
with self.assertRaises(TypeError):
@@ -96,10 +96,10 @@ def progress(status, remaining, total):
9696
self.assertEqual(journal[0], 0)
9797

9898
def test_non_callable_progress(self):
99-
with self.assertRaises(TypeError) as err:
99+
with self.assertRaises(TypeError) as cm:
100100
with sqlite.connect(':memory:') as bck:
101101
self.cx.backup(bck, pages=1, progress='bar')
102-
self.assertEqual(str(err.exception), 'progress argument must be a callable')
102+
self.assertEqual(str(cm.exception), 'progress argument must be a callable')
103103

104104
def test_modifying_progress(self):
105105
journal = []
@@ -138,9 +138,10 @@ def test_database_source_name(self):
138138
self.cx.backup(bck, name='main')
139139
with sqlite.connect(':memory:') as bck:
140140
self.cx.backup(bck, name='temp')
141-
with self.assertRaises(sqlite.OperationalError):
141+
with self.assertRaises(sqlite.OperationalError) as cm:
142142
with sqlite.connect(':memory:') as bck:
143143
self.cx.backup(bck, name='non-existing')
144+
self.assertEqual(str(cm.exception), 'SQL logic error or missing database')
144145

145146
self.cx.execute("ATTACH DATABASE ':memory:' AS attached_db")
146147
self.cx.execute('CREATE TABLE attached_db.foo (key INTEGER)')
@@ -150,7 +151,7 @@ def test_database_source_name(self):
150151
self.cx.backup(bck, name='attached_db')
151152
self.verify_backup(bck)
152153

153-
# Used by the Lib/test/test_sqlite.py
154+
154155
def suite():
155156
return unittest.makeSuite(BackupTests)
156157

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1-
sqlite3.Connection now exposes a backup() method, if the underlying SQLite
2-
library is at version 3.6.11 or higher. Patch by Lele Gaifax.
1+
:class:`sqlite3.Connection` now exposes a :class:`~sqlite3.Connection.backup`
2+
method, if the underlying SQLite library is at version 3.6.11
3+
or higher. Patch by Lele Gaifax.

Modules/_sqlite/connection.c

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1554,19 +1554,21 @@ pysqlite_connection_backup(pysqlite_Connection *self, PyObject *args, PyObject *
15541554
PyErr_SetString(pysqlite_OperationalError, sqlite3_errstr(rc));
15551555
#else
15561556
switch (rc) {
1557-
case SQLITE_READONLY:
1558-
PyErr_SetString(pysqlite_OperationalError,
1559-
"attempt to write a readonly database");
1560-
break;
1561-
case SQLITE_BUSY:
1562-
PyErr_SetString(pysqlite_OperationalError, "database is locked");
1563-
break;
1564-
case SQLITE_LOCKED:
1565-
PyErr_SetString(pysqlite_OperationalError, "database table is locked");
1566-
break;
1567-
default:
1568-
PyErr_Format(pysqlite_OperationalError, "unrecognized error code: %d", rc);
1569-
break;
1557+
case SQLITE_READONLY:
1558+
PyErr_SetString(pysqlite_OperationalError,
1559+
"attempt to write a readonly database");
1560+
break;
1561+
case SQLITE_BUSY:
1562+
PyErr_SetString(pysqlite_OperationalError, "database is locked");
1563+
break;
1564+
case SQLITE_LOCKED:
1565+
PyErr_SetString(pysqlite_OperationalError,
1566+
"database table is locked");
1567+
break;
1568+
default:
1569+
PyErr_Format(pysqlite_OperationalError,
1570+
"unrecognized error code: %d", rc);
1571+
break;
15701572
}
15711573
#endif
15721574
}

0 commit comments

Comments
 (0)