diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2021-08-08 08:49:44 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-08-08 08:49:44 +0300 |
commit | 0eec6276fdcdde5221370d92b50ea95851760c72 (patch) | |
tree | 35fa0e56f83f404eb2120ec1963c6b5e15813d34 /Lib/sqlite3/test/dbapi.py | |
parent | ebecffdb6d5fffa4249f9a813f1fc1915926feb5 (diff) | |
download | cpython-0eec6276fdcdde5221370d92b50ea95851760c72.tar.gz cpython-0eec6276fdcdde5221370d92b50ea95851760c72.zip |
bpo-44859: Improve error handling in sqlite3 and and raise more accurate exceptions. (GH-27654)
* MemoryError is now raised instead of sqlite3.Warning when
memory is not enough for encoding a statement to UTF-8
in Connection.__call__() and Cursor.execute().
* UnicodEncodeError is now raised instead of sqlite3.Warning when
the statement contains surrogate characters
in Connection.__call__() and Cursor.execute().
* TypeError is now raised instead of ValueError for non-string
script argument in Cursor.executescript().
* ValueError is now raised for script containing the null
character instead of truncating it in Cursor.executescript().
* Correctly handle exceptions raised when getting boolean value
of the result of the progress handler.
* Add many tests covering different corner cases.
Co-authored-by: Erlend Egeberg Aasland <erlend.aasland@innova.no>
Diffstat (limited to 'Lib/sqlite3/test/dbapi.py')
-rw-r--r-- | Lib/sqlite3/test/dbapi.py | 33 |
1 files changed, 30 insertions, 3 deletions
diff --git a/Lib/sqlite3/test/dbapi.py b/Lib/sqlite3/test/dbapi.py index 408f9945f2c..5d7e5bba05b 100644 --- a/Lib/sqlite3/test/dbapi.py +++ b/Lib/sqlite3/test/dbapi.py @@ -26,7 +26,7 @@ import sys import threading import unittest -from test.support import check_disallow_instantiation, threading_helper +from test.support import check_disallow_instantiation, threading_helper, bigmemtest from test.support.os_helper import TESTFN, unlink @@ -758,9 +758,35 @@ class ExtensionTests(unittest.TestCase): def test_cursor_executescript_as_bytes(self): con = sqlite.connect(":memory:") cur = con.cursor() - with self.assertRaises(ValueError) as cm: + with self.assertRaises(TypeError): cur.executescript(b"create table test(foo); insert into test(foo) values (5);") - self.assertEqual(str(cm.exception), 'script argument must be unicode.') + + def test_cursor_executescript_with_null_characters(self): + con = sqlite.connect(":memory:") + cur = con.cursor() + with self.assertRaises(ValueError): + cur.executescript(""" + create table a(i);\0 + insert into a(i) values (5); + """) + + def test_cursor_executescript_with_surrogates(self): + con = sqlite.connect(":memory:") + cur = con.cursor() + with self.assertRaises(UnicodeEncodeError): + cur.executescript(""" + create table a(s); + insert into a(s) values ('\ud8ff'); + """) + + @unittest.skipUnless(sys.maxsize > 2**32, 'requires 64bit platform') + @bigmemtest(size=2**31, memuse=3, dry_run=False) + def test_cursor_executescript_too_large_script(self, maxsize): + con = sqlite.connect(":memory:") + cur = con.cursor() + for size in 2**31-1, 2**31: + with self.assertRaises(sqlite.DataError): + cur.executescript("create table a(s);".ljust(size)) def test_connection_execute(self): con = sqlite.connect(":memory:") @@ -969,6 +995,7 @@ def suite(): CursorTests, ExtensionTests, ModuleTests, + OpenTests, SqliteOnConflictTests, ThreadTests, UninitialisedConnectionTests, |