Skip to content

Commit e79ec70

Browse files
Issue python#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.
2 parents 5cbd833 + 08d230a commit e79ec70

File tree

6 files changed

+36
-8
lines changed

6 files changed

+36
-8
lines changed

Lib/sqlite3/test/factory.py

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

183+
def CheckFakeCursorClass(self):
184+
# Issue #24257: Incorrect use of PyObject_IsInstance() caused
185+
# segmentation fault.
186+
class FakeCursor(str):
187+
__class__ = sqlite.Cursor
188+
cur = self.con.cursor(factory=FakeCursor)
189+
self.assertRaises(TypeError, sqlite.Row, cur, ())
190+
183191
def tearDown(self):
184192
self.con.close()
185193

Lib/test/test_types.py

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

11731173
self.assertEqual(ns, ns_roundtrip, pname)
11741174

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

11761192
class CoroutineTests(unittest.TestCase):
11771193
def test_wrong_args(self):

Misc/NEWS

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ Release date: 2015-05-24
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

@@ -55,6 +58,9 @@ Core and Builtins
5558
Library
5659
-------
5760

61+
- Issue #24257: Fixed segmentation fault in sqlite3.Row constructor with faked
62+
cursor type.
63+
5864
- Issue #15836: assertRaises(), assertRaisesRegex(), assertWarns() and
5965
assertWarnsRegex() assertments now check the type of the first argument
6066
to prevent possible user error. Based on patch by Daniel Wagner-Hall.

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
@@ -442,8 +442,7 @@ _PyGen_FetchStopIterationValue(PyObject **pvalue) {
442442
PyErr_Fetch(&et, &ev, &tb);
443443
if (ev) {
444444
/* exception will usually be normalised already */
445-
if (Py_TYPE(ev) == (PyTypeObject *) et
446-
|| PyObject_IsInstance(ev, PyExc_StopIteration)) {
445+
if (PyObject_TypeCheck(ev, (PyTypeObject *) et)) {
447446
value = ((PyStopIterationObject *)ev)->value;
448447
Py_INCREF(value);
449448
Py_DECREF(ev);
@@ -453,7 +452,7 @@ _PyGen_FetchStopIterationValue(PyObject **pvalue) {
453452
} else {
454453
/* normalisation required */
455454
PyErr_NormalizeException(&et, &ev, &tb);
456-
if (!PyObject_IsInstance(ev, PyExc_StopIteration)) {
455+
if (!PyObject_TypeCheck(ev, (PyTypeObject *)PyExc_StopIteration)) {
457456
PyErr_Restore(et, ev, tb);
458457
return -1;
459458
}

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)