Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(34330)

Side by Side Diff: Modules/_sqlite/row.c

Issue 10203: sqlite3.Row doesn't support sequence protocol
Patch Set: Created 5 years, 3 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « Lib/sqlite3/test/factory.py ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* row.c - an enhanced tuple for database rows 1 /* row.c - an enhanced tuple for database rows
2 * 2 *
3 * Copyright (C) 2005-2010 Gerhard Häring <gh@ghaering.de> 3 * Copyright (C) 2005-2010 Gerhard Häring <gh@ghaering.de>
4 * 4 *
5 * This file is part of pysqlite. 5 * This file is part of pysqlite.
6 * 6 *
7 * This software is provided 'as-is', without any express or implied 7 * This software is provided 'as-is', without any express or implied
8 * warranty. In no event will the authors be held liable for any damages 8 * warranty. In no event will the authors be held liable for any damages
9 * arising from the use of this software. 9 * arising from the use of this software.
10 * 10 *
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
54 return -1; 54 return -1;
55 } 55 }
56 56
57 Py_INCREF(data); 57 Py_INCREF(data);
58 self->data = data; 58 self->data = data;
59 59
60 Py_INCREF(cursor->description); 60 Py_INCREF(cursor->description);
61 self->description = cursor->description; 61 self->description = cursor->description;
62 62
63 return 0; 63 return 0;
64 }
65
66 PyObject* pysqlite_row_item(pysqlite_Row* self, Py_ssize_t idx)
67 {
68 PyObject* item = PyTuple_GetItem(self->data, idx);
69 Py_XINCREF(item);
70 return item;
64 } 71 }
65 72
66 PyObject* pysqlite_row_subscript(pysqlite_Row* self, PyObject* idx) 73 PyObject* pysqlite_row_subscript(pysqlite_Row* self, PyObject* idx)
67 { 74 {
68 long _idx; 75 long _idx;
76 int long_overflow;
69 char* key; 77 char* key;
70 Py_ssize_t nitems, i; 78 Py_ssize_t nitems, i;
71 char* compare_key; 79 char* compare_key;
72 80
73 char* p1; 81 char* p1;
74 char* p2; 82 char* p2;
75 83
76 PyObject* item; 84 PyObject* item;
77 85
78 if (PyLong_Check(idx)) { 86 if (PyLong_Check(idx)) {
79 _idx = PyLong_AsLong(idx); 87 _idx = PyLong_AsLongAndOverflow(idx, &long_overflow);
88 if (long_overflow) {
89 PyErr_Format(PyExc_IndexError,
90 "cannot fit '%.200s' into an index-sized integer",
91 idx->ob_type->tp_name);
92 return NULL;
93 }
94 if (_idx < 0)
95 _idx += PyTuple_GET_SIZE(self->data);
80 item = PyTuple_GetItem(self->data, _idx); 96 item = PyTuple_GetItem(self->data, _idx);
81 Py_XINCREF(item); 97 Py_XINCREF(item);
82 return item; 98 return item;
83 } else if (PyUnicode_Check(idx)) { 99 } else if (PyUnicode_Check(idx)) {
84 key = _PyUnicode_AsString(idx); 100 key = _PyUnicode_AsString(idx);
85 if (key == NULL) 101 if (key == NULL)
86 return NULL; 102 return NULL;
87 103
88 nitems = PyTuple_Size(self->description); 104 nitems = PyTuple_Size(self->description);
89 105
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after
190 } 206 }
191 } 207 }
192 Py_RETURN_NOTIMPLEMENTED; 208 Py_RETURN_NOTIMPLEMENTED;
193 } 209 }
194 210
195 PyMappingMethods pysqlite_row_as_mapping = { 211 PyMappingMethods pysqlite_row_as_mapping = {
196 /* mp_length */ (lenfunc)pysqlite_row_length, 212 /* mp_length */ (lenfunc)pysqlite_row_length,
197 /* mp_subscript */ (binaryfunc)pysqlite_row_subscript, 213 /* mp_subscript */ (binaryfunc)pysqlite_row_subscript,
198 /* mp_ass_subscript */ (objobjargproc)0, 214 /* mp_ass_subscript */ (objobjargproc)0,
199 }; 215 };
216
217 static PySequenceMethods pysqlite_row_as_sequence = {
218 /* sq_length */ (lenfunc)pysqlite_row_length,
219 /* sq_concat */ 0,
220 /* sq_repeat */ 0,
221 /* sq_item */ (ssizeargfunc)pysqlite_row_item,
222 };
223
200 224
201 static PyMethodDef pysqlite_row_methods[] = { 225 static PyMethodDef pysqlite_row_methods[] = {
202 {"keys", (PyCFunction)pysqlite_row_keys, METH_NOARGS, 226 {"keys", (PyCFunction)pysqlite_row_keys, METH_NOARGS,
203 PyDoc_STR("Returns the keys of the row.")}, 227 PyDoc_STR("Returns the keys of the row.")},
204 {NULL, NULL} 228 {NULL, NULL}
205 }; 229 };
206 230
207 231
208 PyTypeObject pysqlite_RowType = { 232 PyTypeObject pysqlite_RowType = {
209 PyVarObject_HEAD_INIT(NULL, 0) 233 PyVarObject_HEAD_INIT(NULL, 0)
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
244 (initproc)pysqlite_row_init, /* tp_init */ 268 (initproc)pysqlite_row_init, /* tp_init */
245 0, /* tp_alloc */ 269 0, /* tp_alloc */
246 0, /* tp_new */ 270 0, /* tp_new */
247 0 /* tp_free */ 271 0 /* tp_free */
248 }; 272 };
249 273
250 extern int pysqlite_row_setup_types(void) 274 extern int pysqlite_row_setup_types(void)
251 { 275 {
252 pysqlite_RowType.tp_new = PyType_GenericNew; 276 pysqlite_RowType.tp_new = PyType_GenericNew;
253 pysqlite_RowType.tp_as_mapping = &pysqlite_row_as_mapping; 277 pysqlite_RowType.tp_as_mapping = &pysqlite_row_as_mapping;
278 pysqlite_RowType.tp_as_sequence = &pysqlite_row_as_sequence;
254 return PyType_Ready(&pysqlite_RowType); 279 return PyType_Ready(&pysqlite_RowType);
255 } 280 }
OLDNEW
« no previous file with comments | « Lib/sqlite3/test/factory.py ('k') | no next file » | no next file with comments »

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+