@@ -193,22 +193,30 @@ pysqlite_build_row_cast_map(pysqlite_Cursor* self)
193193}
194194
195195static PyObject *
196- _pysqlite_build_column_name (const char * colname )
196+ _pysqlite_build_column_name (pysqlite_Cursor * self , const char * colname )
197197{
198198 const char * pos ;
199+ Py_ssize_t len ;
199200
200201 if (!colname ) {
201202 Py_RETURN_NONE ;
202203 }
203204
204- for (pos = colname ;; pos ++ ) {
205- if (* pos == 0 || * pos == '[' ) {
206- if ((* pos == '[' ) && (pos > colname ) && (* (pos - 1 ) == ' ' )) {
207- pos -- ;
205+ if (self -> connection -> detect_types & PARSE_COLNAMES ) {
206+ for (pos = colname ; * pos ; pos ++ ) {
207+ if (* pos == '[' ) {
208+ if ((pos != colname ) && (* (pos - 1 ) == ' ' )) {
209+ pos -- ;
210+ }
211+ break ;
208212 }
209- return PyUnicode_FromStringAndSize (colname , pos - colname );
210213 }
214+ len = pos - colname ;
215+ }
216+ else {
217+ len = strlen (colname );
211218 }
219+ return PyUnicode_FromStringAndSize (colname , len );
212220}
213221
214222/*
@@ -370,6 +378,7 @@ _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject* args)
370378 PyObject * result ;
371379 int numcols ;
372380 PyObject * descriptor ;
381+ PyObject * column_name ;
373382 PyObject * second_argument = NULL ;
374383 sqlite_int64 lastrowid ;
375384
@@ -536,7 +545,13 @@ _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject* args)
536545 if (!descriptor ) {
537546 goto error ;
538547 }
539- PyTuple_SetItem (descriptor , 0 , _pysqlite_build_column_name (sqlite3_column_name (self -> statement -> st , i )));
548+ column_name = _pysqlite_build_column_name (self ,
549+ sqlite3_column_name (self -> statement -> st , i ));
550+ if (!column_name ) {
551+ Py_DECREF (descriptor );
552+ goto error ;
553+ }
554+ PyTuple_SetItem (descriptor , 0 , column_name );
540555 Py_INCREF (Py_None ); PyTuple_SetItem (descriptor , 1 , Py_None );
541556 Py_INCREF (Py_None ); PyTuple_SetItem (descriptor , 2 , Py_None );
542557 Py_INCREF (Py_None ); PyTuple_SetItem (descriptor , 3 , Py_None );
0 commit comments