Skip to content

Commit d02b1fb

Browse files
committed
Implement repr()
1 parent 66a1145 commit d02b1fb

File tree

3 files changed

+12
-1
lines changed

3 files changed

+12
-1
lines changed

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ Alternate string representation to the built-in `str` type.
1313
* Write docs (see `str` type docs)
1414
* Write docstrings
1515
* Fill out setup.py
16-
* Implement repr
1716
* Implement iter (iterate over Unicode code points, "runes")
1817
* Implement str methods
1918
* Implement buffer interface?

src/cstring.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,13 @@ static PyObject *cstring_str(PyObject *self) {
4848
return PyUnicode_FromString(CSTRING_VALUE(self));
4949
}
5050

51+
static PyObject *cstring_repr(PyObject *self) {
52+
PyObject *tmp = cstring_str(self);
53+
PyObject *repr = PyObject_Repr(tmp);
54+
Py_DECREF(tmp);
55+
return repr;
56+
}
57+
5158
static Py_hash_t cstring_hash(PyObject *self) {
5259
if(CSTRING_HASH(self) == -1)
5360
CSTRING_HASH(self) = _Py_HashBytes(CSTRING_VALUE(self), Py_SIZE(self));
@@ -196,6 +203,7 @@ static PyTypeObject cstring_type = {
196203
.tp_dealloc = cstring_dealloc,
197204
.tp_richcompare = cstring_richcompare,
198205
.tp_str = cstring_str,
206+
.tp_repr = cstring_repr,
199207
.tp_hash = cstring_hash,
200208
.tp_as_sequence = &cstring_as_sequence,
201209
.tp_as_mapping = &cstring_as_mapping,

test/test_cstring.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ def test_str():
66
assert str(result) == 'hello, world'
77

88

9+
def test_repr():
10+
assert repr(cstring('"hello" "world" 🙂')) == repr('"hello" "world" 🙂')
11+
12+
913
def test_hash():
1014
a = cstring('hello')
1115
b = cstring('hello')

0 commit comments

Comments
 (0)