summaryrefslogtreecommitdiff
path: root/python/tag.cc
diff options
context:
space:
mode:
Diffstat (limited to 'python/tag.cc')
-rw-r--r--python/tag.cc95
1 files changed, 69 insertions, 26 deletions
diff --git a/python/tag.cc b/python/tag.cc
index 46fb8f34..61b919aa 100644
--- a/python/tag.cc
+++ b/python/tag.cc
@@ -338,11 +338,14 @@ static PyObject *TagFileNext(PyObject *Self)
const char *Start;
const char *Stop;
Obj.Section->Object.GetSection(Start,Stop);
- // Duplicate the data
- Obj.Section->Data = new char[Stop-Start];
- strncpy(Obj.Section->Data, Start, Stop-Start);
+ // Duplicate the data and
+ // append a \n because GetSection() will only give us a single \n
+ // but Scan() needs \n\n to work
+ Obj.Section->Data = new char[Stop-Start+2];
+ snprintf(Obj.Section->Data, Stop-Start+2, "%s\n", Start);
// Rescan it
- Obj.Section->Object.Scan(Obj.Section->Data, Stop-Start);
+ if(Obj.Section->Object.Scan(Obj.Section->Data, Stop-Start+2) == false)
+ return HandleErrors(NULL);
Py_INCREF(Obj.Section);
return HandleErrors(Obj.Section);
@@ -381,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 /*{{{*/
// ---------------------------------------------------------------------
@@ -418,16 +476,6 @@ static PyObject *TagSecNew(PyTypeObject *type,PyObject *Args,PyObject *kwds) {
return New;
}
-#ifdef COMPAT_0_7
-char *doc_ParseSection = "ParseSection(Text) -> TagSection()\n\nDeprecated.";
-PyObject *ParseSection(PyObject *self,PyObject *Args)
-{
- PyErr_WarnEx(PyExc_DeprecationWarning, "apt_pkg.ParseSection() is "
- "deprecated. Please see apt_pkg.TagSection() for the "
- "replacement.", 1);
- return TagSecNew(&PyTagSection_Type,Args,0);
-}
-#endif
/*}}}*/
// ParseTagFile - Parse a tagd file /*{{{*/
// ---------------------------------------------------------------------
@@ -445,9 +493,8 @@ static PyObject *TagFileNew(PyTypeObject *type,PyObject *Args,PyObject *kwds)
// check if we got a filename or a file object
int fileno = -1;
- const char *filename = NULL;
- filename = PyObject_AsString(File);
- if (filename == NULL) {
+ PyApt_Filename filename;
+ if (!filename.init(File)) {
PyErr_Clear();
fileno = PyObject_AsFileDescriptor(File);
}
@@ -485,6 +532,8 @@ static PyObject *TagFileNew(PyTypeObject *type,PyObject *Args,PyObject *kwds)
#if PY_MAJOR_VERSION >= 3
if (fileno != -1) {
New->Encoding = PyObject_GetAttr(File, PyUnicode_FromString("encoding"));
+ if (!New->Encoding)
+ PyErr_Clear();
if (New->Encoding && !PyUnicode_Check(New->Encoding))
New->Encoding = 0;
} else
@@ -507,15 +556,6 @@ static PyObject *TagFileNew(PyTypeObject *type,PyObject *Args,PyObject *kwds)
return HandleErrors(New);
}
-#ifdef COMPAT_0_7
-char *doc_ParseTagFile = "ParseTagFile(file) -> TagFile()\n\nDeprecated.";
-PyObject *ParseTagFile(PyObject *self,PyObject *Args) {
- PyErr_WarnEx(PyExc_DeprecationWarning, "apt_pkg.ParseTagFile() is "
- "deprecated. Please see apt_pkg.TagFile() for the "
- "replacement.", 1);
- return TagFileNew(&PyTagFile_Type,Args,0);
-}
-#endif
/*}}}*/
#ifdef HAVE_OPEN_MEMSTREAM
// RewriteSection - Rewrite a section.. /*{{{*/
@@ -673,6 +713,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."},
{}
};