Skip to content

Commit 3d13b29

Browse files
committed
record: Implement __contains__ (see issue #11)
1 parent 0c913fa commit 3d13b29

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

asyncpg/protocol/record/recordobj.c

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,18 @@ record_items(PyObject *o, PyObject *args)
469469
}
470470

471471

472+
static int
473+
record_contains(ApgRecordObject *o, PyObject *arg)
474+
{
475+
if (!ApgRecord_CheckExact(o)) {
476+
PyErr_BadInternalCall();
477+
return -1;
478+
}
479+
480+
return PySequence_Contains(o->mapping, arg);
481+
}
482+
483+
472484
static PySequenceMethods record_as_sequence = {
473485
(lenfunc)record_length, /* sq_length */
474486
0, /* sq_concat */
@@ -477,7 +489,7 @@ static PySequenceMethods record_as_sequence = {
477489
0, /* sq_slice */
478490
0, /* sq_ass_item */
479491
0, /* sq_ass_slice */
480-
0, /* sq_contains */
492+
(objobjproc)record_contains, /* sq_contains */
481493
};
482494

483495

tests/test_record.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,19 @@ def test_record_hash(self):
212212
self.assertNotIn(r3, d)
213213
self.assertIn(r4, d)
214214

215+
def test_record_contains(self):
216+
r = Record(R_AB, (42, 43))
217+
self.assertIn('a', r)
218+
self.assertIn('b', r)
219+
self.assertNotIn('z', r)
220+
221+
r = Record(None, (42, 43))
222+
with self.assertRaises(TypeError):
223+
self.assertIn('a', r)
224+
225+
with self.assertRaises(TypeError):
226+
type(r).__contains__(None, 'a')
227+
215228
def test_record_cmp(self):
216229
AB = collections.namedtuple('AB', ('a', 'b'))
217230

0 commit comments

Comments
 (0)