8000 [WIP] OdbBackend: Python subclassing by ddevault · Pull Request #948 · libgit2/pygit2 · GitHub
[go: up one dir, main page]

Skip to content

[WIP] OdbBackend: Python subclassing #948

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 9 commits into from
Dec 21, 2019
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Implement OdbBackend as iterable
  • Loading branch information
ddevault committed Dec 4, 2019
commit 6bd09cbf66a8b41ad225659239f4df00decd9610
30 changes: 28 additions & 2 deletions src/odb_backend.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ struct pygit2_odb_backend
*exists,
*exists_prefix,
*refresh,
*foreach, /* __iter__ */
*writepack,
*freshen;
};
Expand Down Expand Up @@ -252,6 +251,30 @@ pygit2_odb_backend_refresh(git_odb_backend *_be)
return 0;
}

static int
pygit2_odb_backend_foreach(git_odb_backend *_be,
git_odb_foreach_cb cb, void *payload)
{
int err;
PyObject *item;
git_oid oid;
struct pygit2_odb_backend *be = (struct pygit2_odb_backend *)_be;
PyObject *iterator = PyObject_GetIter((PyObject *)be->OdbBackend);
assert(iterator);

while ((item = PyIter_Next(iterator))) {
py_oid_to_git_oid(item, &oid);
cb(&oid, payload);
Py_DECREF(item);
}

if ((err = git_error_for_exc()) != 0) {
return err;
}

return 0;
}

static void
pygit2_odb_backend_free(git_odb_backend *_be)
{
Expand Down Expand Up @@ -342,6 +365,10 @@ OdbBackend_init(OdbBackend *self, PyObject *args, PyObject *kwds)
Py_INCREF(be->refresh);
}

if (PyIter_Check((PyObject *)self)) {
be->backend.foreach = pygit2_odb_backend_foreach;
}

/*
be->writepack = PyObject_GetAttrString(
(PyObject *)self, "writepack");
Expand Down Expand Up @@ -379,7 +406,6 @@ OdbBackend_dealloc(OdbBackend *self)
Py_CLEAR(be->exists);
Py_CLEAR(be->exists_prefix);
Py_CLEAR(be->refresh);
Py_CLEAR(be->foreach);
Py_CLEAR(be->writepack);
Py_CLEAR(be->freshen);
free(be);
Expand Down
0