diff options
author | Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> | 2020-03-21 14:32:25 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-03-21 14:32:25 (GMT) |
commit | 687f5921a46cf95c2a648d8031f9e99cdcc3e6b7 (patch) | |
tree | 3c1336581057da00284ddee7d8d5863a7ec4e287 /Modules/_sqlite | |
parent | ba26bf30940f4347fedcf8ebc374c6e2dc375afa (diff) | |
download | cpython-687f5921a46cf95c2a648d8031f9e99cdcc3e6b7.zip cpython-687f5921a46cf95c2a648d8031f9e99cdcc3e6b7.tar.gz cpython-687f5921a46cf95c2a648d8031f9e99cdcc3e6b7.tar.bz2 |
bpo-39652: Truncate the column name after '[' only if PARSE_COLNAMES is set. (GH-18942)
(cherry picked from commit b146568dfcbcd7409c724f8917e4f77433dd56e4)
Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
Diffstat (limited to 'Modules/_sqlite')
-rw-r--r-- | Modules/_sqlite/cursor.c | 29 |
1 files changed, 22 insertions, 7 deletions
diff --git a/Modules/_sqlite/cursor.c b/Modules/_sqlite/cursor.c index 01b9dc4..8cfa6e5 100644 --- a/Modules/_sqlite/cursor.c +++ b/Modules/_sqlite/cursor.c @@ -193,22 +193,30 @@ pysqlite_build_row_cast_map(pysqlite_Cursor* self) } static PyObject * -_pysqlite_build_column_name(const char* colname) +_pysqlite_build_column_name(pysqlite_Cursor *self, const char *colname) { const char* pos; + Py_ssize_t len; if (!colname) { Py_RETURN_NONE; } - for (pos = colname;; pos++) { - if (*pos == 0 || *pos == '[') { - if ((*pos == '[') && (pos > colname) && (*(pos-1) == ' ')) { - pos--; + if (self->connection->detect_types & PARSE_COLNAMES) { + for (pos = colname; *pos; pos++) { + if (*pos == '[') { + if ((pos != colname) && (*(pos-1) == ' ')) { + pos--; + } + break; } - return PyUnicode_FromStringAndSize(colname, pos - colname); } + len = pos - colname; + } + else { + len = strlen(colname); } + return PyUnicode_FromStringAndSize(colname, len); } /* @@ -370,6 +378,7 @@ _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject* args) PyObject* result; int numcols; PyObject* descriptor; + PyObject* column_name; PyObject* second_argument = NULL; sqlite_int64 lastrowid; @@ -546,7 +555,13 @@ _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject* args) if (!descriptor) { goto error; } - PyTuple_SetItem(descriptor, 0, _pysqlite_build_column_name(sqlite3_column_name(self->statement->st, i))); + column_name = _pysqlite_build_column_name(self, + sqlite3_column_name(self->statement->st, i)); + if (!column_name) { + Py_DECREF(descriptor); + goto error; + } + PyTuple_SetItem(descriptor, 0, column_name); Py_INCREF(Py_None); PyTuple_SetItem(descriptor, 1, Py_None); Py_INCREF(Py_None); PyTuple_SetItem(descriptor, 2, Py_None); Py_INCREF(Py_None); PyTuple_SetItem(descriptor, 3, Py_None); |