8000 Add len to mapping protocol · atpalmer-python/python-cstring@c658f3c · GitHub
[go: up one dir, main page]

Skip to content

Commit c658f3c

Browse files
committed
Add len to mapping protocol
1 parent bea520f commit c658f3c

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

src/cstring.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,10 @@ static PySequenceMethods cstring_as_sequence = {
141141
.sq_contains = cstring_contains,
142142
};
143143

144+
static PyMappingMethods cstring_as_mapping = {
145+
.mp_length = cstring_len,
146+
};
147+
144148
static PyTypeObject cstring_type = {
145149
PyVarObject_HEAD_INIT(NULL, 0)
146150
.tp_name = "cstring",
@@ -154,6 +158,7 @@ static PyTypeObject cstring_type = {
154158
.tp_str = cstring_str,
155159
.tp_hash = cstring_hash,
156160
.tp_as_sequence = &cstring_as_sequence,
161+
.tp_as_mapping = &cstring_as_mapping,
157162
};
158163

159164
static struct PyModuleDef module = {

test/test_mapping.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from cstring import cstring
2+
3+
4+
def test_len():
5+
result = cstring('hello, world')
6+
assert len(result) == 12
7+

0 commit comments

Comments
 (0)
0