diff -r 23d986228c6b Lib/sqlite3/test/types.py --- a/Lib/sqlite3/test/types.py Sat Apr 02 04:48:46 2016 +0300 +++ b/Lib/sqlite3/test/types.py Sat Apr 02 10:56:16 2016 +0300 @@ -284,6 +284,45 @@ self.cur.execute("select * from test where 0 = 1") self.assertEqual(self.cur.description[0][0], "x") + def CheckCursorDescriptionInsert(self): + self.cur.execute("insert into test values (1)") + self.assertIsNone(self.cur.description) + + +@unittest.skipIf(sqlite.sqlite_version_info < (3, 8, 3), 'Common Table Expressions not supported') +class CommonTableExpressionTests(unittest.TestCase): + + def setUp(self): + self.con = sqlite.connect(":memory:") + self.cur = self.con.cursor() + self.cur.execute("create table test(x foo)") + + def tearDown(self): + self.cur.close() + self.con.close() + + def CheckCursorDescriptionCTESimple(self): + self.cur.execute("with one as (select 1) select * from one") + self.assertIsNotNone(self.cur.description) + self.assertEqual(self.cur.description[0][0], "1") + + def CheckCursorDescriptionCTESMultipleColumns(self): + self.cur.execute("insert into test values(1)") + self.cur.execute("insert into test values(2)") + self.cur.execute("with testCTE as (select * from test) select * from testCTE") + self.assertIsNotNone(self.cur.description) + self.assertEqual(self.cur.description[0][0], "x") + + def CheckCursorDescriptionCTE(self): + self.cur.execute("insert into test values (1)") + self.cur.execute("with bar as (select * from test) select * from test where x = 1") + self.assertIsNotNone(self.cur.description) + self.assertEqual(self.cur.description[0][0], "x") + self.cur.execute("with bar as (select * from test) select * from test where x = 2") + self.assertIsNotNone(self.cur.description) + self.assertEqual(self.cur.description[0][0], "x") + + class ObjectAdaptationTests(unittest.TestCase): def cast(obj): return float(obj) @@ -384,7 +423,8 @@ adaptation_suite = unittest.makeSuite(ObjectAdaptationTests, "Check") bin_suite = unittest.makeSuite(BinaryConverterTests, "Check") date_suite = unittest.makeSuite(DateTimeTests, "Check") - return unittest.TestSuite((sqlite_type_suite, decltypes_type_suite, colnames_type_suite, adaptation_suite, bin_suite, date_suite)) + cte_suite = unittest.makeSuite(CommonTableExpressionTests, "Check") + return unittest.TestSuite((sqlite_type_suite, decltypes_type_suite, colnames_type_suite, adaptation_suite, bin_suite, date_suite, cte_suite)) def test(): runner = unittest.TextTestRunner() diff -r 23d986228c6b Modules/_sqlite/cursor.c --- a/Modules/_sqlite/cursor.c Sat Apr 02 04:48:46 2016 +0300 +++ b/Modules/_sqlite/cursor.c Sat Apr 02 10:56:16 2016 +0300 @@ -646,12 +646,11 @@ goto error; } - if (rc == SQLITE_ROW || (rc == SQLITE_DONE && statement_type == STATEMENT_SELECT)) { - if (self->description == Py_None) { - Py_BEGIN_ALLOW_THREADS - numcols = sqlite3_column_count(self->statement->st); - Py_END_ALLOW_THREADS - + if (rc == SQLITE_ROW || rc == SQLITE_DONE) { + Py_BEGIN_ALLOW_THREADS + numcols = sqlite3_column_count(self->statement->st); + Py_END_ALLOW_THREADS + if (self->description == Py_None && numcols > 0) { Py_SETREF(self->description, PyTuple_New(numcols)); if (!self->description) { goto error;