From 30084e00c4a2f941780ea38fe28c2a268098140a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn=20Nieto?= Date: Sun, 26 Jan 2014 09:47:42 +0100 Subject: [PATCH] Add support for pypy Fortunately pypy provides support for a lot of the CPython API, so the changes are minimal. The most important changes are: - constructors always get a keyword argument dictionary, even if no keyword arguments are passed - trying to assign to a read-only attribute raises TypeError instead of AttributeError Apart from that, pypy does not provide MAXPATHLEN. There is a hack in place currently, but there is only place that's using that macro, and there shouldn't be a need for it much longer. This fixes #209. --- src/config.c | 2 +- src/index.c | 2 +- src/pygit2.c | 12 +++++++++++- src/repository.c | 2 +- test/test_commit.py | 19 +++++++++++++------ test/test_tag.py | 15 +++++++++++---- 6 files changed, 38 insertions(+), 14 deletions(-) diff --git a/src/config.c b/src/config.c index f767558..fdb2593 100644 --- a/src/config.c +++ b/src/config.c @@ -59,7 +59,7 @@ Config_init(Config *self, PyObject *args, PyObject *kwds) char *path = NULL; int err; - if (kwds) { + if (kwds && PyDict_Size(kwds) > 0) { PyErr_SetString(PyExc_TypeError, "Config takes no keyword arguments"); return -1; diff --git a/src/index.c b/src/index.c index ce47f13..34daf1d 100644 --- a/src/index.c +++ b/src/index.c @@ -46,7 +46,7 @@ Index_init(Index *self, PyObject *args, PyObject *kwds) char *path; int err; - if (kwds) { + if (kwds && PyDict_Size(kwds) > 0) { PyErr_SetString(PyExc_TypeError, "Index takes no keyword arguments"); return -1; } diff --git a/src/pygit2.c b/src/pygit2.c index 7c72e50..4665277 100644 --- a/src/pygit2.c +++ b/src/pygit2.c @@ -27,7 +27,12 @@ #define PY_SSIZE_T_CLEAN #include -#include + +/* Pypy does not provide this header */ +#ifndef PYPY_VERSION +# include +#endif + #include #include "error.h" #include "types.h" @@ -35,6 +40,11 @@ #include "repository.h" #include "oid.h" +/* FIXME: This is for pypy */ +#ifndef MAXPATHLEN +# define MAXPATHLEN 1024 +#endif + extern PyObject *GitError; extern PyTypeObject RepositoryType; diff --git a/src/repository.c b/src/repository.c index fa0dd7e..b0464d4 100644 --- a/src/repository.c +++ b/src/repository.c @@ -77,7 +77,7 @@ Repository_init(Repository *self, PyObject *args, PyObject *kwds) char *path; int err; - if (kwds) { + if (kwds && PyDict_Size(kwds) > 0) { PyErr_SetString(PyExc_TypeError, "Repository takes no keyword arguments"); return -1; diff --git a/test/test_commit.py b/test/test_commit.py index f462f78..e7d8c5c 100644 --- a/test/test_commit.py +++ b/test/test_commit.py @@ -34,6 +34,12 @@ import unittest from pygit2 import GIT_OBJ_COMMIT, Signature, Oid from . import utils +# pypy raises TypeError on writing to read-only, so we need to check +# and change the test accordingly +try: + import __pypy__ +except ImportError: + __pypy__ = None COMMIT_SHA = '5fe808e8953c12735680c257f56600cb0de44b10' @@ -131,12 +137,13 @@ class CommitTest(utils.BareRepoTestCase): author = ('Jane Doe', 'jdoe2@example.com', 12345) commit = self.repo[COMMIT_SHA] - self.assertRaises(AttributeError, setattr, commit, 'message', message) - self.assertRaises(AttributeError, setattr, commit, 'committer', - committer) - self.assertRaises(AttributeError, setattr, commit, 'author', author) - self.assertRaises(AttributeError, setattr, commit, 'tree', None) - self.assertRaises(AttributeError, setattr, commit, 'parents', None) + + error_type = AttributeError if not __pypy__ else TypeError + self.assertRaises(error_type, setattr, commit, 'message', message) + self.assertRaises(error_type, setattr, commit, 'committer', committer) + self.assertRaises(error_type, setattr, commit, 'author', author) + self.assertRaises(error_type, setattr, commit, 'tree', None) + self.assertRaises(error_type, setattr, commit, 'parents', None) if __name__ == '__main__': diff --git a/test/test_tag.py b/test/test_tag.py index 7acfd30..e31f7b0 100644 --- a/test/test_tag.py +++ b/test/test_tag.py @@ -34,6 +34,12 @@ import unittest import pygit2 from . import utils +# pypy raises TypeError on writing to read-only, so we need to check +# and change the test accordingly +try: + import __pypy__ +except ImportError: + __pypy__ = None TAG_SHA = '3d2962987c695a29f1f80b6c3aa4ec046ef44369' @@ -84,10 +90,11 @@ class TagTest(utils.BareRepoTestCase): tagger = ('John Doe', 'jdoe@example.com', 12347) tag = self.repo[TAG_SHA] - self.assertRaises(AttributeError, setattr, tag, 'name', name) - self.assertRaises(AttributeError, setattr, tag, 'target', target) - self.assertRaises(AttributeError, setattr, tag, 'tagger', tagger) - self.assertRaises(AttributeError, setattr, tag, 'message', message) + error_type = AttributeError if not __pypy__ else TypeError + self.assertRaises(error_type, setattr, tag, 'name', name) + self.assertRaises(error_type, setattr, tag, 'target', target) + self.assertRaises(error_type, setattr, tag, 'tagger', tagger) + self.assertRaises(error_type, setattr, tag, 'message', message) def test_get_object(self): repo = self.repo