Skip to content

Add str() and repr() support to Signature type #1135

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@

- CI: various fixes after migration to libgit2 v1.4

- New ``Signature`` supports ``str()`` and ``repr()``
`#1135 <https://github.com/libgit2/pygit2/pull/1135>`_


1.9.0 (2022-02-22)
-------------------------
Expand Down
2 changes: 1 addition & 1 deletion docs/objects.rst
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ The author and committer attributes of commit objects are ``Signature``
objects::

>>> commit.author
<pygit2.Signature object at 0x7f75e9b1f5f8>
pygit2.Signature('Foo Ibáñez', '[email protected]', 1322174594, 60, 'utf-8')

Signatures can be compared for (in)equality.

Expand Down
36 changes: 34 additions & 2 deletions src/signature.c
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,38 @@ Signature_richcompare(PyObject *a, PyObject *b, int op)

}

PyObject *
Signature__str__(Signature *self)
{
PyObject *name, *email, *str;
name = to_unicode(self->signature->name, self->encoding, NULL);
email = to_unicode(self->signature->email, self->encoding, NULL);
str = PyUnicode_FromFormat("%U <%U>", name, email);
Py_DECREF(name);
Py_DECREF(email);
return str;
}

PyObject *
Signature__repr__(Signature *self)
{
PyObject *name, *email, *encoding, *str;
name = to_unicode(self->signature->name, self->encoding, NULL);
email = to_unicode(self->signature->email, self->encoding, NULL);
encoding = to_unicode(self->encoding, self->encoding, NULL);
str = PyUnicode_FromFormat(
"pygit2.Signature(%R, %R, %lld, %ld, %R)",
name,
email,
self->signature->when.time,
self->signature->when.offset,
encoding);
Py_DECREF(name);
Py_DECREF(email);
Py_DECREF(encoding);
return str;
}


PyDoc_STRVAR(Signature__doc__, "Signature.");

Expand All @@ -234,13 +266,13 @@ PyTypeObject SignatureType = {
0, /* tp_getattr */
0, /* tp_setattr */
0, /* tp_compare */
0, /* tp_repr */
Signature__repr__, /* tp_repr */
0, /* tp_as_number */
0, /* tp_as_sequence */
0, /* tp_as_mapping */
0, /* tp_hash */
0, /* tp_call */
0, /* tp_str */
Signature__str__, /* tp_str */
0, /* tp_getattro */
0, /* tp_setattro */
0, /* tp_as_buffer */
Expand Down
11 changes: 11 additions & 0 deletions test/test_signature.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,14 @@ def test_now():
assert signature.email == signature.raw_email.decode(encoding)
assert signature.email.encode(encoding) == signature.raw_email
assert abs(signature.time - time.time()) < 5

def test_str():
signature = Signature('Foo Ibáñez', '[email protected]', encoding='utf-8')
assert str(signature) == 'Foo Ibáñez <[email protected]>'

def test_repr():
signature = Signature(
'Foo Ibáñez', '[email protected]', 1322174594, 60, encoding='utf-8')
expected_signature = \
"pygit2.Signature('Foo Ibáñez', '[email protected]', 1322174594, 60, 'utf-8')"
assert repr(signature) == expected_signature