8000 gh-80254: Disallow recursive usage of cursors in `sqlite3` converters by erlend-aasland · Pull Request #29054 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-80254: Disallow recursive usage of cursors in sqlite3 converters #29054

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 15 commits into from
May 3, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Use subTest
  • Loading branch information
Erlend E. Aasland committed Oct 19, 2021
commit bb0a729e53e8200810aea2ace3d62b588554b04f
37 changes: 14 additions & 23 deletions Lib/sqlite3/test/test_regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -489,36 +489,27 @@ def test_executescript_step_through_select(self):

class ConverterProgrammingErrorTestCase(unittest.TestCase):
def setUp(self):
self.con = sqlite.connect(':memory:', detect_types=sqlite.PARSE_COLNAMES)
self.con = sqlite.connect(":memory:", detect_types=sqlite.PARSE_COLNAMES)
self.cur = self.con.cursor()
self.cur.execute('create table test(x foo)')
self.cur.execute("create table test(x foo)")
self.cur.executemany("insert into test(x) values (?)", [("foo",), ("bar",)])

sqlite.converters['CURSOR_INIT'] = lambda x: self.cur.__init__(self.con)
sqlite.converters['CURSOR_CLOSE'] = lambda x: self.cur.close()
sqlite.converters['CURSOR_ITER'] = lambda x, l=[]: self.cur.fetchone() if l else l.append(None)
sqlite.converters["CURSOR_INIT"] = lambda x: self.cur.__init__(self.con)
sqlite.converters["CURSOR_CLOSE"] = lambda x: self.cur.close()
sqlite.converters["CURSOR_ITER"] = lambda x, l=[]: self.cur.fetchone() if l else l.append(None)

def tearDown(self):
del sqlite.converters['CURSOR_INIT']
del sqlite.converters['CURSOR_CLOSE']
del sqlite.converters['CURSOR_ITER']
del sqlite.converters["CURSOR_INIT"]
del sqlite.converters["CURSOR_CLOSE"]
del sqlite.converters["CURSOR_ITER"]
self.cur.close()
self.con.close()

def test_cursor_init(self):
self.cur.execute('insert into test(x) values (?)', ('foo',))
with self.assertRaises(sqlite.ProgrammingError):
self.cur.execute('select x as "x [CURSOR_INIT]", x from test')

def test_cursor_close(self):
self.cur.execute('insert into test(x) values (?)', ('foo',))
with self.assertRaises(sqlite.ProgrammingError):
self.cur.execute('select x as "x [CURSOR_CLOSE]", x from test')

def test_cursor_iter(self):
self.cur.executemany('insert into test(x) values (?)', (('foo',),) * 2)
self.cur.execute('select x as "x [CURSOR_ITER]", x from test')
with self.assertRaises(sqlite.ProgrammingError):
self.cur.fetchone()
def test_recursive_cursor_usage(self):
for converter in "CURSOR_INIT", "CURSOR_CLOSE", "CURSOR_ITER":
with self.subTest(converter=converter):
self.cur.execute(f'select x as "x [{converter}]", x from test')
self.assertRaises(sqlite.ProgrammingError, self.cur.fetchall)


if __name__ == "__main__":
Expand Down
0