Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 08d230a

Browse files
Issue #24257: Fixed incorrect uses of PyObject_IsInstance().
Fixed segmentation fault in sqlite3.Row constructor with faked cursor type. Fixed system error in the comparison of faked types.SimpleNamespace.
1 parent df9ba36 commit 08d230a

6 files changed

Lines changed: 36 additions & 8 deletions

File tree

Lib/sqlite3/test/factory.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,14 @@ def CheckSqliteRowAsSequence(self):
162162
self.assertEqual(list(reversed(row)), list(reversed(as_tuple)))
163163
self.assertIsInstance(row, Sequence)
164164

165+
def CheckFakeCursorClass(self):
166+
# Issue #24257: Incorrect use of PyObject_IsInstance() caused
167+
# segmentation fault.
168+
class FakeCursor(str):
169+
__class__ = sqlite.Cursor
170+
cur = self.con.cursor(factory=FakeCursor)
171+
self.assertRaises(TypeError, sqlite.Row, cur, ())
172+
165173
def tearDown(self):
166174
self.con.close()
167175

Lib/test/test_types.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1169,6 +1169,22 @@ def test_pickle(self):
11691169

11701170
self.assertEqual(ns, ns_roundtrip, pname)
11711171

1172+
def test_fake_namespace_compare(self):
1173+
# Issue #24257: Incorrect use of PyObject_IsInstance() caused
1174+
# SystemError.
1175+
class FakeSimpleNamespace(str):
1176+
__class__ = types.SimpleNamespace
1177+
self.assertFalse(types.SimpleNamespace() == FakeSimpleNamespace())
1178+
self.assertTrue(types.SimpleNamespace() != FakeSimpleNamespace())
1179+
with self.assertRaises(TypeError):
1180+
types.SimpleNamespace() < FakeSimpleNamespace()
1181+
with self.assertRaises(TypeError):
1182+
types.SimpleNamespace() <= FakeSimpleNamespace()
1183+
with self.assertRaises(TypeError):
1184+
types.SimpleNamespace() > FakeSimpleNamespace()
1185+
with self.assertRaises(TypeError):
1186+
types.SimpleNamespace() >= FakeSimpleNamespace()
1187+
11721188

11731189
def test_main():
11741190
run_unittest(TypesTests, MappingProxyTests, ClassCreationTests,

Misc/NEWS

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ Release date: tba
1010
Core and Builtins
1111
-----------------
1212

13+
- Issue #24257: Fixed system error in the comparison of faked
14+
types.SimpleNamespace.
15+
1316
- Issue #22939: Fixed integer overflow in iterator object. Patch by
1417
Clement Rouault.
1518

@@ -56,6 +59,9 @@ Core and Builtins
5659
Library
5760
-------
5861

62+
- Issue #24257: Fixed segmentation fault in sqlite3.Row constructor with faked
63+
cursor type.
64+
5965
- Issue #22107: tempfile.gettempdir() and tempfile.mkdtemp() now try again
6066
when a directory with the chosen name already exists on Windows as well as
6167
on Unix. tempfile.mkstemp() now fails early if parent directory is not

Modules/_sqlite/row.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ pysqlite_row_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
4646
if (!PyArg_ParseTuple(args, "OO", &cursor, &data))
4747
return NULL;
4848

49-
if (!PyObject_IsInstance((PyObject*)cursor, (PyObject*)&pysqlite_CursorType)) {
49+
if (!PyObject_TypeCheck((PyObject*)cursor, &pysqlite_CursorType)) {
5050
PyErr_SetString(PyExc_TypeError, "instance of cursor required for first argument");
5151
return NULL;
5252
}

Objects/genobject.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -398,8 +398,7 @@ _PyGen_FetchStopIterationValue(PyObject **pvalue) {
398398
PyErr_Fetch(&et, &ev, &tb);
399399
if (ev) {
400400
/* exception will usually be normalised already */
401-
if (Py_TYPE(ev) == (PyTypeObject *) et
402-
|| PyObject_IsInstance(ev, PyExc_StopIteration)) {
401+
if (PyObject_TypeCheck(ev, (PyTypeObject *) et)) {
403402
value = ((PyStopIterationObject *)ev)->value;
404403
Py_INCREF(value);
405404
Py_DECREF(ev);
@@ -409,7 +408,7 @@ _PyGen_FetchStopIterationValue(PyObject **pvalue) {
409408
} else {
410409
/* normalisation required */
411410
PyErr_NormalizeException(&et, &ev, &tb);
412-
if (!PyObject_IsInstance(ev, PyExc_StopIteration)) {
411+
if (!PyObject_TypeCheck(ev, (PyTypeObject *)PyExc_StopIteration)) {
413412
PyErr_Restore(et, ev, tb);
414413
return -1;
415414
}

Objects/namespaceobject.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -164,12 +164,11 @@ namespace_clear(_PyNamespaceObject *ns)
164164
static PyObject *
165165
namespace_richcompare(PyObject *self, PyObject *other, int op)
166166
{
167-
if (PyObject_IsInstance(self, (PyObject *)&_PyNamespace_Type) &&
168-
PyObject_IsInstance(other, (PyObject *)&_PyNamespace_Type))
167+
if (PyObject_TypeCheck(self, &_PyNamespace_Type) &&
168+
PyObject_TypeCheck(other, &_PyNamespace_Type))
169169
return PyObject_RichCompare(((_PyNamespaceObject *)self)->ns_dict,
170170
((_PyNamespaceObject *)other)->ns_dict, op);
171-
Py_INCREF(Py_NotImplemented);
172-
return Py_NotImplemented;
171+
Py_RETURN_NOTIMPLEMENTED;
173172
}
174173

175174

0 commit comments

Comments
 (0)