Skip to content

Commit c0eaa69

Browse files
committed
Fix patch-after-blob-free
1 parent 91dfaf2 commit c0eaa69

File tree

5 files changed

+16
-6
lines changed

5 files changed

+16
-6
lines changed

src/blob.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ Blob_diff(Blob *self, PyObject *args, PyObject *kwds)
8181
if (err < 0)
8282
return Error_set(err);
8383

84-
return wrap_patch(patch);
84+
return wrap_patch(patch, self, py_blob);
8585
}
8686

8787

@@ -130,7 +130,7 @@ Blob_diff_to_buffer(Blob *self, PyObject *args, PyObject *kwds)
130130
if (err < 0)
131131
return Error_set(err);
132132

133-
return wrap_patch(patch);
133+
return wrap_patch(patch, self, NULL);
134134
}
135135

136136
static PyMethodDef Blob_methods[] = {

src/diff.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,7 @@ diff_get_patch_byindex(git_diff *diff, size_t idx)
422422
if (err < 0)
423423
return Error_set(err);
424424

425-
return (PyObject*) wrap_patch(patch);
425+
return (PyObject*) wrap_patch(patch, NULL, NULL);
426426
}
427427

428428
PyObject *

src/patch.c

+10-2
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ PyTypeObject PatchType;
4040

4141

4242
PyObject *
43-
wrap_patch(git_patch *patch)
43+
wrap_patch(git_patch *patch, Blob *oldblob, Blob *newblob)
4444
{
4545
Patch *py_patch;
4646
PyObject *py_hunk;
@@ -59,6 +59,12 @@ wrap_patch(git_patch *patch)
5959
if (py_hunk)
6060
PyList_SetItem((PyObject*) py_patch->hunks, i, py_hunk);
6161
}
62+
63+
Py_XINCREF(oldblob);
64+
py_patch->oldblob = oldblob;
65+
66+
Py_XINCREF(newblob);
67+
py_patch->newblob = newblob;
6268
}
6369

6470
return (PyObject*) py_patch;
@@ -67,6 +73,8 @@ wrap_patch(git_patch *patch)
6773
static void
6874
Patch_dealloc(Patch *self)
6975
{
76+
Py_CLEAR(self->oldblob);
77+
Py_CLEAR(self->newblob);
7078
Py_CLEAR(self->hunks);
7179
git_patch_free(self->patch);
7280
PyObject_Del(self);
@@ -169,7 +177,7 @@ Patch_create_from(PyObject *self, PyObject *args, PyObject *kwds)
169177
if (err < 0)
170178
return Error_set(err);
171179

172-
return wrap_patch(patch);
180+
return wrap_patch(patch, oldblob, newblob);
173181
}
174182

175183

src/patch.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,6 @@
3232
#include <Python.h>
3333
#include <git2.h>
3434

35-
PyObject* wrap_patch(git_patch *patch);
35+
PyObject* wrap_patch(git_patch *patch, Blob *oldblob, Blob *newblob);
3636

3737
#endif

src/types.h

+2
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@ typedef struct {
9595
PyObject_HEAD
9696
git_patch *patch;
9797
PyObject* hunks;
98+
Blob* oldblob;
99+
Blob* newblob;
98100
} Patch;
99101

100102
/* git_diff */

0 commit comments

Comments
 (0)