Skip to content
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions Doc/library/sqlite3.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1417,6 +1417,10 @@ Connection objects

See :ref:`sqlite3-howto-row-factory` for more details.

.. versionchanged:: next
Deleting the ``row_factory`` attribute is no longer allowed and raises
:exc:`AttributeError`.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure that it's worth it to document this minor behavior. I'm not sure that it's useful to mention the exact exception (AttributeError).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the feedback.
I've simplified the versionchanged note and removed the mention of the specific exception. I kept the note itself since this is a user-visible behavior change, but I'm open to removing it if you think it's not necessary.


.. attribute:: text_factory

A :term:`callable` that accepts a :class:`bytes` parameter
Expand All @@ -1426,6 +1430,10 @@ Connection objects

See :ref:`sqlite3-howto-encoding` for more details.

.. versionchanged:: next
Deleting the ``text_factory`` attribute is no longer allowed and raises
:exc:`AttributeError`.

.. attribute:: total_changes

Return the total number of database rows that have been modified, inserted, or
Expand Down Expand Up @@ -1709,6 +1717,10 @@ Cursor objects

See :ref:`sqlite3-howto-row-factory` for more details.

.. versionchanged:: next
Deleting the ``row_factory`` attribute is no longer allowed and raises
:exc:`AttributeError`.


.. The sqlite3.Row example used to be a how-to. It has now been incorporated
into the Row reference. We keep the anchor here in order not to break
Expand Down
10 changes: 10 additions & 0 deletions Lib/test/test_sqlite3/test_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,16 @@ def test_sqlite_row_index(self):
with self.assertRaises(IndexError):
row[complex()] # index must be int or string

def test_delete_connection_row_factory(self):
# gh-149738: deleting row_factory should raise an exception
with self.assertRaises(AttributeError):
del self.con.row_factory
Comment thread
sepehr-rs marked this conversation as resolved.

def test_delete_connection_text_factory(self):
# gh-149738: deleting text_factory should raise an exception
with self.assertRaises(AttributeError):
del self.con.text_factory

def test_sqlite_row_index_unicode(self):
row = self.con.execute("select 1 as \xff").fetchone()
self.assertEqual(row["\xff"], 1)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
:mod:`sqlite3`: Disallow removing ``row_factory`` and ``text_factory`` attributes
Comment thread
sepehr-rs marked this conversation as resolved.
of a connection to prevent a crash on a query.
47 changes: 45 additions & 2 deletions Modules/_sqlite/connection.c
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,47 @@ pysqlite_connection_cursor_impl(pysqlite_Connection *self, PyObject *factory)
return cursor;
}

static PyObject *
connection_get_row_factory(PyObject *op, void *closure)
{
pysqlite_Connection *self = (pysqlite_Connection *)op;
return Py_NewRef(self->row_factory);
}

static int
connection_set_row_factory(PyObject *op, PyObject *value, void *closure)
{
pysqlite_Connection *self = (pysqlite_Connection *)op;
if (value == NULL) {
PyErr_SetString(PyExc_AttributeError,
"cannot delete row_factory attribute");
return -1;
}
Py_XSETREF(self->row_factory, Py_NewRef(value));
return 0;
}

static PyObject *
connection_get_text_factory(PyObject *op, void *closure)
{
pysqlite_Connection *self = (pysqlite_Connection *)op;
return Py_NewRef(self->text_factory);
}

static int
connection_set_text_factory(PyObject *op, PyObject *value, void *closure)
{
pysqlite_Connection *self = (pysqlite_Connection *)op;
if (value == NULL) {
PyErr_SetString(PyExc_AttributeError,
"cannot delete text_factory attribute");
return -1;
}
Py_XSETREF(self->text_factory, Py_NewRef(value));
return 0;
}


/*[clinic input]
_sqlite3.Connection.blobopen as blobopen

Expand Down Expand Up @@ -2620,6 +2661,10 @@ static PyGetSetDef connection_getset[] = {
{"in_transaction", pysqlite_connection_get_in_transaction, NULL},
{"autocommit", get_autocommit, set_autocommit},
{"__text_signature__", get_sig, NULL},
{"row_factory", connection_get_row_factory,
connection_set_row_factory},
{"text_factory", connection_get_text_factory,
connection_set_text_factory},
{NULL}
};

Expand Down Expand Up @@ -2667,8 +2712,6 @@ static struct PyMemberDef connection_members[] =
{"InternalError", _Py_T_OBJECT, offsetof(pysqlite_Connection, InternalError), Py_READONLY},
{"ProgrammingError", _Py_T_OBJECT, offsetof(pysqlite_Connection, ProgrammingError), Py_READONLY},
{"NotSupportedError", _Py_T_OBJECT, offsetof(pysqlite_Connection, NotSupportedError), Py_READONLY},
{"row_factory", _Py_T_OBJECT, offsetof(pysqlite_Connection, row_factory)},
{"text_factory", _Py_T_OBJECT, offsetof(pysqlite_Connection, text_factory)},
{NULL}
};

Expand Down
Loading