| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -649,6 +649,9 @@ Row Objects | |||
| 649 | 649 | This method returns a list of column names. Immediately after a query, | |
| 650 | 650 | it is the first member of each tuple in :attr:`Cursor.description`. | |
| 651 | 651 | ||
| 652 | + .. versionchanged:: 3.5 | ||
| 653 | + Added support of slicing. | ||
| 654 | + | ||
| 652 | 655 | Let's assume we initialize a table as in the example given above:: | |
| 653 | 656 | ||
| 654 | 657 | conn = sqlite3.connect(":memory:") | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -111,6 +111,24 @@ def CheckSqliteRowIndex(self): | |||
| 111 | 111 | with self.assertRaises(IndexError): | |
| 112 | 112 | row[2**1000] | |
| 113 | 113 | ||
| 114 | + def CheckSqliteRowSlice(self): | ||
| 115 | + # A sqlite.Row can be sliced like a list. | ||
| 116 | + self.con.row_factory = sqlite.Row | ||
| 117 | + row = self.con.execute("select 1, 2, 3, 4").fetchone() | ||
| 118 | + self.assertEqual(row[0:0], ()) | ||
| 119 | + self.assertEqual(row[0:1], (1,)) | ||
| 120 | + self.assertEqual(row[1:3], (2, 3)) | ||
| 121 | + self.assertEqual(row[3:1], ()) | ||
| 122 | + # Explicit bounds are optional. | ||
| 123 | + self.assertEqual(row[1:], (2, 3, 4)) | ||
| 124 | + self.assertEqual(row[:3], (1, 2, 3)) | ||
| 125 | + # Slices can use negative indices. | ||
| 126 | + self.assertEqual(row[-2:-1], (3,)) | ||
| 127 | + self.assertEqual(row[-2:], (3, 4)) | ||
| 128 | + # Slicing supports steps. | ||
| 129 | + self.assertEqual(row[0:4:2], (1, 3)) | ||
| 130 | + self.assertEqual(row[3:0:-2], (4, 2)) | ||
| 131 | + | ||
| 114 | 132 | def CheckSqliteRowIter(self): | |
| 115 | 133 | """Checks if the row object is iterable""" | |
| 116 | 134 | self.con.row_factory = sqlite.Row | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -13,6 +13,8 @@ Core and Builtins | |||
| 13 | 13 | Library | |
| 14 | 14 | ------- | |
| 15 | 15 | ||
| 16 | + - Issue #13583: sqlite3.Row now supports slice indexing. | ||
| 17 | + | ||
| 16 | 18 | - Issue #18473: Fixed 2to3 and 3to2 compatible pickle mappings. Fixed | |
| 17 | 19 | ambigious reverse mappings. Added many new mappings. Import mapping is no | |
| 18 | 20 | longer applied to modules already mapped with full name mapping. | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -142,8 +142,7 @@ PyObject* pysqlite_row_subscript(pysqlite_Row* self, PyObject* idx) | |||
| 142 | 142 | return NULL; | |
| 143 | 143 | } | |
| 144 | 144 | else if (PySlice_Check(idx)) { | |
| 145 | - PyErr_SetString(PyExc_ValueError, "slices not implemented, yet"); | ||
| 146 | - return NULL; | ||
| 145 | + return PyObject_GetItem(self->data, idx); | ||
| 147 | 146 | } | |
| 148 | 147 | else { | |
| 149 | 148 | PyErr_SetString(PyExc_IndexError, "Index must be int or string"); | |
| Back | FazBrowse Home | New Git URL |
0 commit comments