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

Skip to content

Commit a4fb014

Browse files
committed
Support initialization from bytearray
1 parent 08cbb98 commit a4fb014

File tree

2 files changed

+9
-0
lines changed

2 files changed

+9
-0
lines changed

src/cstring.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@ static const char *_obj_as_string_and_size(PyObject *o, Py_ssize_t *s) {
5050
return buffer;
5151
}
5252

53+
if(PyByteArray_Check(o)) {
54+
*s = PyByteArray_Size(o);
55+
return PyByteArray_AsString(o);
56+
}
57+
5358
PyErr_Format(
5459
PyExc_TypeError,
5560
"Invalid initialization type: %s.",

test/test_slots.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ def test_new_from_bytes():
55
assert cstring(b'hello, world') == cstring('hello, world')
66

77

8+
def test_new_from_bytearray():
9+
assert cstring(bytearray('hello, world', 'utf8')) == cstring('hello, world')
10+
11+
812
def test_new_from_cstring():
913
assert cstring(cstring('hello, world')) == cstring('hello, world')
1014

0 commit comments

Comments
 (0)