Skip to content

Commit

Permalink
Merge branch 'py3-c-modules' of git://github.com/lelit/dulwich
Browse files Browse the repository at this point in the history
  • Loading branch information
jelmer committed Dec 3, 2015
2 parents e02f902 + b1e3eaf commit 0b78314
Show file tree
Hide file tree
Showing 4 changed files with 178 additions and 48 deletions.
78 changes: 64 additions & 14 deletions dulwich/_diff_tree.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,22 @@ typedef int Py_ssize_t;
#define Py_SIZE(ob) (((PyVarObject*)(ob))->ob_size)
#endif

#if PY_MAJOR_VERSION >= 3
#define PyInt_FromLong PyLong_FromLong
#define PyInt_AsLong PyLong_AsLong
#define PyInt_AS_LONG PyLong_AS_LONG
#define PyString_AS_STRING PyBytes_AS_STRING
#define PyString_AsString PyBytes_AsString
#define PyString_AsStringAndSize PyBytes_AsStringAndSize
#define PyString_Check PyBytes_Check
#define PyString_CheckExact PyBytes_CheckExact
#define PyString_FromStringAndSize PyBytes_FromStringAndSize
#define PyString_FromString PyBytes_FromString
#define PyString_GET_SIZE PyBytes_GET_SIZE
#define PyString_Size PyBytes_Size
#define _PyString_Join _PyBytes_Join
#endif

static PyObject *tree_entry_cls = NULL, *null_entry = NULL,
*defaultdict_cls = NULL, *int_cls = NULL;
static int block_size;
Expand Down Expand Up @@ -124,8 +140,13 @@ static PyObject **tree_entries(char *path, Py_ssize_t path_len, PyObject *tree,
memcpy(new_path, PyString_AS_STRING(name), name_len);
}

#if PY_MAJOR_VERSION >= 3
result[i] = PyObject_CallFunction(tree_entry_cls, "y#OO", new_path,
new_path_len, PyTuple_GET_ITEM(old_entry, 1), sha);
#else
result[i] = PyObject_CallFunction(tree_entry_cls, "s#OO", new_path,
new_path_len, PyTuple_GET_ITEM(old_entry, 1), sha);
#endif
PyMem_Free(new_path);
if (!result[i]) {
goto error;
Expand All @@ -152,16 +173,18 @@ static int entry_path_cmp(PyObject *entry1, PyObject *entry2)
path1 = PyObject_GetAttrString(entry1, "path");
if (!path1)
goto done;

if (!PyString_Check(path1)) {
PyErr_SetString(PyExc_TypeError, "path is not a string");
PyErr_SetString(PyExc_TypeError, "path is not a (byte)string");
goto done;
}

path2 = PyObject_GetAttrString(entry2, "path");
if (!path2)
goto done;

if (!PyString_Check(path2)) {
PyErr_SetString(PyExc_TypeError, "path is not a string");
PyErr_SetString(PyExc_TypeError, "path is not a (byte)string");
goto done;
}

Expand All @@ -182,7 +205,11 @@ static PyObject *py_merge_entries(PyObject *self, PyObject *args)
char *path_str;
int cmp;

#if PY_MAJOR_VERSION >= 3
if (!PyArg_ParseTuple(args, "y#OO", &path_str, &path_len, &tree1, &tree2))
#else
if (!PyArg_ParseTuple(args, "s#OO", &path_str, &path_len, &tree1, &tree2))
#endif
return NULL;

entries1 = tree_entries(path_str, path_len, tree1, &n1);
Expand Down Expand Up @@ -390,12 +417,28 @@ static PyMethodDef py_diff_tree_methods[] = {
{ NULL, NULL, 0, NULL }
};

PyMODINIT_FUNC
init_diff_tree(void)
static PyObject *
moduleinit(void)
{
PyObject *m, *objects_mod = NULL, *diff_tree_mod = NULL;
PyObject *block_size_obj = NULL;
PyObject *block_size_obj = NULL;

#if PY_MAJOR_VERSION >= 3
static struct PyModuleDef moduledef = {
PyModuleDef_HEAD_INIT,
"_diff_tree", /* m_name */
NULL, /* m_doc */
-1, /* m_size */
py_diff_tree_methods, /* m_methods */
NULL, /* m_reload */
NULL, /* m_traverse */
NULL, /* m_clear*/
NULL, /* m_free */
};
m = PyModule_Create(&moduledef);
#else
m = Py_InitModule("_diff_tree", py_diff_tree_methods);
#endif
if (!m)
goto error;

Expand Down Expand Up @@ -437,11 +480,8 @@ init_diff_tree(void)
}

Py_DECREF(diff_tree_mod);
#if PY_MAJOR_VERSION < 3
return;
#else
return NULL;
#endif

return m;

error:
Py_XDECREF(objects_mod);
Expand All @@ -450,9 +490,19 @@ init_diff_tree(void)
Py_XDECREF(block_size_obj);
Py_XDECREF(defaultdict_cls);
Py_XDECREF(int_cls);
#if PY_MAJOR_VERSION < 3
return;
#else
return NULL;
#endif
}

#if PY_MAJOR_VERSION >= 3
PyMODINIT_FUNC
PyInit__diff_tree(void)
{
return moduleinit();
}
#else
PyMODINIT_FUNC
init_diff_tree(void)
{
moduleinit();
}
#endif
79 changes: 52 additions & 27 deletions dulwich/_objects.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,15 @@
typedef int Py_ssize_t;
#endif

#if PY_MAJOR_VERSION >= 3
#define PyInt_Check(obj) 0
#define PyInt_CheckExact(obj) 0
#define PyInt_AsLong PyLong_AsLong
#define PyString_AS_STRING PyBytes_AS_STRING
#define PyString_Check PyBytes_Check
#define PyString_FromStringAndSize PyBytes_FromStringAndSize
#endif

#if defined(__MINGW32_VERSION) || defined(__APPLE__)
size_t rep_strnlen(char *text, size_t maxlen);
size_t rep_strnlen(char *text, size_t maxlen)
Expand Down Expand Up @@ -59,8 +68,13 @@ static PyObject *py_parse_tree(PyObject *self, PyObject *args, PyObject *kw)
PyObject *ret, *item, *name, *sha, *py_strict = NULL;
static char *kwlist[] = {"text", "strict", NULL};

#if PY_MAJOR_VERSION >= 3
if (!PyArg_ParseTupleAndKeywords(args, kw, "y#|O", kwlist,
&text, &len, &py_strict))
#else
if (!PyArg_ParseTupleAndKeywords(args, kw, "s#|O", kwlist,
&text, &len, &py_strict))
#endif
return NULL;
strict = py_strict ? PyObject_IsTrue(py_strict) : 0;
/* TODO: currently this returns a list; if memory usage is a concern,
Expand Down Expand Up @@ -248,58 +262,69 @@ static PyMethodDef py_objects_methods[] = {
{ NULL, NULL, 0, NULL }
};

PyMODINIT_FUNC
init_objects(void)
static PyObject *
moduleinit(void)
{
PyObject *m, *objects_mod, *errors_mod;

m = Py_InitModule3("_objects", py_objects_methods, NULL);
if (m == NULL) {
#if PY_MAJOR_VERSION < 3
return;
#if PY_MAJOR_VERSION >= 3
static struct PyModuleDef moduledef = {
PyModuleDef_HEAD_INIT,
"_objects", /* m_name */
NULL, /* m_doc */
-1, /* m_size */
py_objects_methods, /* m_methods */
NULL, /* m_reload */
NULL, /* m_traverse */
NULL, /* m_clear*/
NULL, /* m_free */
};
m = PyModule_Create(&moduledef);
#else
return NULL;
m = Py_InitModule3("_objects", py_objects_methods, NULL);
#endif
if (m == NULL) {
return NULL;
}

errors_mod = PyImport_ImportModule("dulwich.errors");
if (errors_mod == NULL) {
#if PY_MAJOR_VERSION < 3
return;
#else
return NULL;
#endif
return NULL;
}

object_format_exception_cls = PyObject_GetAttrString(
errors_mod, "ObjectFormatException");
Py_DECREF(errors_mod);
if (object_format_exception_cls == NULL) {
#if PY_MAJOR_VERSION < 3
return;
#else
return NULL;
#endif
return NULL;
}

/* This is a circular import but should be safe since this module is
* imported at at the very bottom of objects.py. */
objects_mod = PyImport_ImportModule("dulwich.objects");
if (objects_mod == NULL) {
#if PY_MAJOR_VERSION < 3
return;
#else
return NULL;
#endif
return NULL;
}

tree_entry_cls = PyObject_GetAttrString(objects_mod, "TreeEntry");
Py_DECREF(objects_mod);
if (tree_entry_cls == NULL) {
#if PY_MAJOR_VERSION < 3
return;
#else
return NULL;
#endif
return NULL;
}

return m;
}

#if PY_MAJOR_VERSION >= 3
PyMODINIT_FUNC
PyInit__objects(void)
{
return moduleinit();
}
#else
PyMODINIT_FUNC
init_objects(void)
{
moduleinit();
}
#endif
62 changes: 56 additions & 6 deletions dulwich/_pack.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,19 @@
#include <Python.h>
#include <stdint.h>

#if PY_MAJOR_VERSION >= 3
#define PyInt_FromLong PyLong_FromLong
#define PyString_AS_STRING PyBytes_AS_STRING
#define PyString_AsString PyBytes_AsString
#define PyString_Check PyBytes_Check
#define PyString_CheckExact PyBytes_CheckExact
#define PyString_FromStringAndSize PyBytes_FromStringAndSize
#define PyString_FromString PyBytes_FromString
#define PyString_GET_SIZE PyBytes_GET_SIZE
#define PyString_Size PyBytes_Size
#define _PyString_Join _PyBytes_Join
#endif

static PyObject *PyExc_ApplyDeltaError = NULL;

static int py_is_sha(PyObject *sha)
Expand Down Expand Up @@ -193,8 +206,13 @@ static PyObject *py_bisect_find_sha(PyObject *self, PyObject *args)
char *sha;
int sha_len;
int start, end;
if (!PyArg_ParseTuple(args, "iis#O", &start, &end,
&sha, &sha_len, &unpack_name))
#if PY_MAJOR_VERSION >= 3
if (!PyArg_ParseTuple(args, "iiy#O", &start, &end,
&sha, &sha_len, &unpack_name))
#else
if (!PyArg_ParseTuple(args, "iis#O", &start, &end,
&sha, &sha_len, &unpack_name))
#endif
return NULL;

if (sha_len != 20) {
Expand Down Expand Up @@ -239,21 +257,53 @@ static PyMethodDef py_pack_methods[] = {
{ NULL, NULL, 0, NULL }
};

void init_pack(void)
static PyObject *
moduleinit(void)
{
PyObject *m;
PyObject *errors_module;

errors_module = PyImport_ImportModule("dulwich.errors");
if (errors_module == NULL)
return;
return NULL;

PyExc_ApplyDeltaError = PyObject_GetAttrString(errors_module, "ApplyDeltaError");
Py_DECREF(errors_module);
if (PyExc_ApplyDeltaError == NULL)
return;
return NULL;

#if PY_MAJOR_VERSION >= 3
static struct PyModuleDef moduledef = {
PyModuleDef_HEAD_INIT,
"_pack", /* m_name */
NULL, /* m_doc */
-1, /* m_size */
py_pack_methods, /* m_methods */
NULL, /* m_reload */
NULL, /* m_traverse */
NULL, /* m_clear*/
NULL, /* m_free */
};
m = PyModule_Create(&moduledef);
#else
m = Py_InitModule3("_pack", py_pack_methods, NULL);
#endif
if (m == NULL)
return;
return NULL;

return m;
}

#if PY_MAJOR_VERSION >= 3
PyMODINIT_FUNC
PyInit__pack(void)
{
return moduleinit();
}
#else
PyMODINIT_FUNC
init_pack(void)
{
moduleinit();
}
#endif
7 changes: 6 additions & 1 deletion dulwich/tests/test_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,13 @@ def test_getitem_unicode(self):
for k, contained in test_keys:
self.assertEqual(k in r, contained)

# Avoid deprecation warning under Py3.2+
if hasattr(self, 'assertRaisesRegex'):
assertRaisesRegexp = self.assertRaisesRegex
else:
assertRaisesRegexp = self.assertRaisesRegexp
for k, _ in test_keys:
self.assertRaisesRegexp(
assertRaisesRegexp(
TypeError, "'name' must be bytestring, not int",
r.__getitem__, 12
)
Expand Down

0 comments on commit 0b78314

Please sign in to comment.