diff --git a/README.rst b/README.rst index afdeeb8..081972f 100644 --- a/README.rst +++ b/README.rst @@ -148,9 +148,8 @@ Creating commits Commits can be created by calling the ``create_commit`` method of the repository with the following parameters:: - >>> from time import time - >>> author = Signature('Alice Author', 'alice@authors.tld', time(), 0) - >>> committer = Signature('Cecil Committer', 'cecil@committers.tld', time(), 0) + >>> author = Signature('Alice Author', 'alice@authors.tld') + >>> committer = Signature('Cecil Committer', 'cecil@committers.tld') >>> tree = repo.TreeBuilder().write() >>> repo.create_commit( ... 'refs/heads/master', # the name of the reference to update diff --git a/src/pygit2/signature.c b/src/pygit2/signature.c index 430f881..19a28cb 100644 --- a/src/pygit2/signature.c +++ b/src/pygit2/signature.c @@ -36,32 +36,26 @@ int Signature_init(Signature *self, PyObject *args, PyObject *kwds) { + const char *keywords[] = { + "name", "email", "time", "offset", "encoding", NULL}; PyObject *py_name; - char *name, *email, *encoding = NULL; - long long time; - int offset; + char *name, *email, *encoding = "ascii"; + long long time = -1; + int offset = 0; int err; git_signature *signature; - if (kwds) { - PyErr_SetString(PyExc_TypeError, - "Signature takes no keyword arguments"); - return -1; - } - - if (!PyArg_ParseTuple(args, "Os|Lis", - &py_name, &email, &time, &offset, &encoding)) + if (!PyArg_ParseTupleAndKeywords( + args, kwds, "Os|Lis", keywords, + &py_name, &email, &time, &offset, &encoding)) return -1; name = py_str_to_c_str(py_name, encoding); if (name == NULL) return -1; - if (PySequence_Length(args) == 2) { + if (time == -1) { err = git_signature_now(&signature, name, email); - } else if (PySequence_Length(args) == 3) { - PyErr_SetString(PyExc_TypeError, "offset must be specified with time"); - return -1; } else { err = git_signature_new(&signature, name, email, time, offset); } diff --git a/test/test_commit.py b/test/test_commit.py index 0884364..35ec65f 100644 --- a/test/test_commit.py +++ b/test/test_commit.py @@ -68,7 +68,9 @@ class CommitTest(utils.BareRepoTestCase): repo = self.repo message = 'New commit.\n\nMessage with non-ascii chars: ééé.\n' committer = Signature('John Doe', 'jdoe@example.com', 12346, 0) - author = Signature('J. David Ibáñez', 'jdavid@example.com', 12345, 0) + author = Signature( + 'J. David Ibáñez', 'jdavid@example.com', 12345, 0, + encoding='utf-8') tree = '967fce8df97cc71722d3c2a5930ef3e6f1d27b12' tree_prefix = tree[:5] too_short_prefix = tree[:3] diff --git a/test/test_signature.py b/test/test_signature.py index 2b59c29..2917a49 100644 --- a/test/test_signature.py +++ b/test/test_signature.py @@ -37,26 +37,29 @@ from .utils import NoRepoTestCase class SignatureTest(NoRepoTestCase): def test_default(self): - signature = Signature('Foo Ibáñez', 'foo@example.com', 1322174594, 60) + signature = Signature( + 'Foo', 'foo@example.com', 1322174594, 60) encoding = signature._encoding - self.assertEqual(encoding, 'utf-8') + self.assertEqual(encoding, 'ascii') self.assertEqual(signature.name, signature._name.decode(encoding)) self.assertEqual(signature.name.encode(encoding), signature._name) + def test_ascii(self): + self.assertRaises( + UnicodeEncodeError, Signature, 'Foo Ibáñez', 'foo@example.com') + def test_latin1(self): encoding = 'iso-8859-1' - signature = Signature('Foo Ibáñez', 'foo@example.com', 1322174594, 60, - encoding) + signature = Signature( + 'Foo Ibáñez', 'foo@example.com', encoding=encoding) self.assertEqual(encoding, signature._encoding) self.assertEqual(signature.name, signature._name.decode(encoding)) self.assertEqual(signature.name.encode(encoding), signature._name) def test_now(self): - self.assertRaises(TypeError, Signature, 'Foo Ibáñez', - 'foo@example.com', 1322174594) - signature = Signature('Foo Ibáñez', 'foo@example.com') - encoding = signature._encoding - self.assertEqual(encoding, 'utf-8') + encoding = 'utf-8' + signature = Signature( + 'Foo Ibáñez', 'foo@example.com', encoding=encoding) self.assertEqual(encoding, signature._encoding) self.assertEqual(signature.name, signature._name.decode(encoding)) self.assertEqual(signature.name.encode(encoding), signature._name)