Skip to content

Commit

Permalink
Treat pathnames as bytes in the Python 3 C modules
Browse files Browse the repository at this point in the history
  • Loading branch information
lelit committed Dec 3, 2015
1 parent 109fe00 commit 4c0ef6e
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 24 deletions.
33 changes: 11 additions & 22 deletions dulwich/_diff_tree.c
Original file line number Diff line number Diff line change
Expand Up @@ -140,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 Down Expand Up @@ -169,37 +174,17 @@ static int entry_path_cmp(PyObject *entry1, PyObject *entry2)
if (!path1)
goto done;

#if PY_MAJOR_VERSION >= 3
/* XXX: Is FSConverter desiderable here? */
if (PyUnicode_Check(path1)) {
PyObject *bytes;
if (!PyUnicode_FSConverter(path1, &bytes))
goto done;
Py_DECREF(path1);
path1 = bytes;
}
#endif
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 PY_MAJOR_VERSION >= 3
/* XXX: Is FSConverter desiderable here? */
if (PyUnicode_Check(path2)) {
PyObject *bytes;
if (!PyUnicode_FSConverter(path2, &bytes))
goto done;
Py_DECREF(path2);
path2 = bytes;
}
#endif
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 @@ -220,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
5 changes: 5 additions & 0 deletions dulwich/_objects.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,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
9 changes: 7 additions & 2 deletions dulwich/_pack.c
Original file line number Diff line number Diff line change
Expand Up @@ -206,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

0 comments on commit 4c0ef6e

Please sign in to comment.