summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJulian Andres Klode <jak@debian.org>2015-06-10 12:50:39 +0200
committerJulian Andres Klode <jak@debian.org>2015-06-10 12:52:28 +0200
commit09a64ad2b63e5a8af386327ff741fddfb65b7f92 (patch)
tree8ab07495a0213f0c8f7bdabc06f447adde1b52a4
parentab39c5fd9906b72e86b1b3dd82b7b5a476e3c4f5 (diff)
downloadpython-apt-09a64ad2b63e5a8af386327ff741fddfb65b7f92.tar.gz
TagFile: Provide close() and context manager
This is mostly meant to be used with tag files that are opened with a path, rather than a file object. Closes: #748922
-rw-r--r--python/tag.cc58
1 files changed, 58 insertions, 0 deletions
diff --git a/python/tag.cc b/python/tag.cc
index 8fb0a78e..967eaa19 100644
--- a/python/tag.cc
+++ b/python/tag.cc
@@ -384,6 +384,61 @@ static PyObject *TagFileJump(PyObject *Self,PyObject *Args)
return HandleErrors(PyBool_FromLong(1));
}
+
+static char *doc_Close =
+ "close()\n\n"
+ "Close the file.";
+static PyObject *TagFileClose(PyObject *self, PyObject *args)
+{
+ if (args != NULL && !PyArg_ParseTuple(args, ""))
+ return NULL;
+
+ TagFileData &Obj = *(TagFileData *) self;
+
+ Obj.Fd.Close();
+
+ Py_INCREF(Py_None);
+ return HandleErrors(Py_None);
+}
+
+static PyObject *TagFileExit(PyObject *self, PyObject *args)
+{
+
+ PyObject *exc_type = 0;
+ PyObject *exc_value = 0;
+ PyObject *traceback = 0;
+ if (!PyArg_UnpackTuple(args, "__exit__", 3, 3, &exc_type, &exc_value,
+ &traceback)) {
+ return 0;
+ }
+
+ PyObject *res = TagFileClose(self, NULL);
+
+ if (res == NULL) {
+ // The close failed. If no exception happened within the suite, we
+ // will raise an error here. Otherwise, we just display the error, so
+ // Python can handle the original exception instead.
+ if (exc_type == Py_None)
+ return NULL;
+
+ PyErr_WriteUnraisable(self);
+ } else {
+ Py_DECREF(res);
+ }
+ // Return False, as required by the context manager protocol.
+ Py_RETURN_FALSE;
+}
+
+static PyObject *TagFileEnter(PyObject *self, PyObject *args)
+{
+ if (!PyArg_ParseTuple(args, ""))
+ return NULL;
+
+ Py_INCREF(self);
+
+ return self;
+}
+
/*}}}*/
// ParseSection - Parse a single section from a tag file /*{{{*/
// ---------------------------------------------------------------------
@@ -656,6 +711,9 @@ static PyMethodDef TagFileMethods[] =
{"step",TagFileStep,METH_VARARGS,doc_Step},
{"offset",TagFileOffset,METH_VARARGS,doc_Offset},
{"jump",TagFileJump,METH_VARARGS,doc_Jump},
+ {"close",TagFileClose,METH_VARARGS,doc_Close},
+ {"__enter__",TagFileEnter,METH_VARARGS,"Context manager entry, return self."},
+ {"__exit__",TagFileExit,METH_VARARGS,"Context manager exit, calls close."},
{}
};