diff options
author | Zbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl> | 2013-03-18 01:12:25 -0400 |
---|---|---|
committer | Zbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl> | 2013-03-18 18:53:58 -0400 |
commit | 85b2850ba94a2a0c15239893c446b3db39b36638 (patch) | |
tree | 61d2bc96d5fa633eb6a4131cc1ab34901130de6c | |
parent | 4c325b2efe529379115ca853854062b29b669d11 (diff) |
systemd-python: allow Reader to be used as a context manager
-rw-r--r-- | src/python-systemd/_reader.c | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/src/python-systemd/_reader.c b/src/python-systemd/_reader.c index 96634a19c3..6759555c12 100644 --- a/src/python-systemd/_reader.c +++ b/src/python-systemd/_reader.c @@ -153,6 +153,35 @@ PyDoc_STRVAR(Reader_close__doc__, "See man:sd_journal_close(3)."); static PyObject* Reader_close(Reader *self, PyObject *args) { + assert(self); + assert(!args); + + sd_journal_close(self->j); + self->j = NULL; + Py_RETURN_NONE; +} + +PyDoc_STRVAR(Reader___enter____doc__, + "__enter__() -> self\n\n" + "Part of the context manager protocol.\n" + "Returns self.\n"); +static PyObject* Reader___enter__(PyObject *self, PyObject *args) +{ + assert(self); + assert(!args); + + Py_INCREF(self); + return self; +} + +PyDoc_STRVAR(Reader___exit____doc__, + "__exit__(type, value, traceback) -> None\n\n" + "Part of the context manager protocol.\n" + "Closes the journal.\n"); +static PyObject* Reader___exit__(Reader *self, PyObject *args) +{ + assert(self); + sd_journal_close(self->j); self->j = NULL; Py_RETURN_NONE; @@ -650,6 +679,9 @@ static PyMethodDef Reader_methods[] = { {"fileno", (PyCFunction) Reader_fileno, METH_NOARGS, Reader_fileno__doc__}, {"reliable_fd", (PyCFunction) Reader_reliable_fd, METH_NOARGS, Reader_reliable_fd__doc__}, {"close", (PyCFunction) Reader_close, METH_NOARGS, Reader_close__doc__}, + {"__enter__", (PyCFunction) Reader___enter__, METH_NOARGS, Reader___enter____doc__}, + {"__exit__", (PyCFunction) Reader___exit__, METH_VARARGS, Reader___exit____doc__}, + {"close", (PyCFunction) Reader_close, METH_NOARGS, Reader_close__doc__}, {"get_next", (PyCFunction) Reader_get_next, METH_VARARGS, Reader_get_next__doc__}, {"get_previous", (PyCFunction) Reader_get_previous, METH_VARARGS, Reader_get_previous__doc__}, {"add_match", (PyCFunction) Reader_add_match, METH_VARARGS|METH_KEYWORDS, Reader_add_match__doc__}, |