From 7e0a6a8421d0b1de85a69fad80c24b494385a1bf Mon Sep 17 00:00:00 2001 From: Michael Vogt Date: Fri, 3 Feb 2012 13:46:08 +0100 Subject: * python/tag.cc, tests/test_tagfile.py: - add support a filename argument in apt_pkg.TagFile() (in addition to the file object currently supported) --- python/tag.cc | 44 +++++++++++++++++++++++++++++++++++--------- 1 file changed, 35 insertions(+), 9 deletions(-) (limited to 'python/tag.cc') diff --git a/python/tag.cc b/python/tag.cc index 3996af8e..b680dc02 100644 --- a/python/tag.cc +++ b/python/tag.cc @@ -390,24 +390,50 @@ PyObject *ParseSection(PyObject *self,PyObject *Args) static PyObject *TagFileNew(PyTypeObject *type,PyObject *Args,PyObject *kwds) { + TagFileData *New; PyObject *File; + char *kwlist[] = {"file", 0}; if (PyArg_ParseTupleAndKeywords(Args,kwds,"O",kwlist,&File) == 0) return 0; - int fileno = PyObject_AsFileDescriptor(File); - if (fileno == -1) + + // check if we got a filename or a file object + int fileno = -1; + const char *filename = NULL; + if (PyString_Check(File)) + filename = PyObject_AsString(File); + else + fileno = PyObject_AsFileDescriptor(File); + + // handle invalid arguments + if (fileno == -1 && filename == NULL) + { + PyErr_SetString(PyExc_TypeError, + "Argument must be string, fd or have a fileno() method"); return 0; + } - TagFileData *New = (TagFileData*)type->tp_alloc(type, 0); + if (fileno > 0) + { + New = (TagFileData*)type->tp_alloc(type, 0); #ifdef APT_HAS_GZIP - new (&New->Fd) FileFd(); - New->Fd.OpenDescriptor(fileno, FileFd::ReadOnlyGzip, false); + new (&New->Fd) FileFd(); + New->Fd.OpenDescriptor(fileno, FileFd::ReadOnlyGzip, false); #else - new (&New->Fd) FileFd(fileno,false); + new (&New->Fd) FileFd(fileno,false); #endif - New->Owner = File; - Py_INCREF(New->Owner); - new (&New->Object) pkgTagFile(&New->Fd); + New->Owner = File; + Py_INCREF(New->Owner); + new (&New->Object) pkgTagFile(&New->Fd); + } + else + { + New = (TagFileData*)type->tp_alloc(type, 0); + new (&New->Fd) FileFd(filename, FileFd::ReadOnly, FileFd::Extension, false); + New->Owner = File; + Py_INCREF(New->Owner); + new (&New->Object) pkgTagFile(&New->Fd); + } // Create the section New->Section = (TagSecData*)(&PyTagSection_Type)->tp_alloc(&PyTagSection_Type, 0); -- cgit v1.2.3 From e93ad4643202b6227ea9e4ab765cbb272c9d2413 Mon Sep 17 00:00:00 2001 From: Michael Vogt Date: Mon, 6 Feb 2012 14:48:51 +0100 Subject: python/tag.cc: cleanup --- python/tag.cc | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) (limited to 'python/tag.cc') diff --git a/python/tag.cc b/python/tag.cc index b680dc02..88a48df9 100644 --- a/python/tag.cc +++ b/python/tag.cc @@ -413,27 +413,23 @@ static PyObject *TagFileNew(PyTypeObject *type,PyObject *Args,PyObject *kwds) return 0; } + New = (TagFileData*)type->tp_alloc(type, 0); if (fileno > 0) { - New = (TagFileData*)type->tp_alloc(type, 0); #ifdef APT_HAS_GZIP new (&New->Fd) FileFd(); New->Fd.OpenDescriptor(fileno, FileFd::ReadOnlyGzip, false); #else new (&New->Fd) FileFd(fileno,false); #endif - New->Owner = File; - Py_INCREF(New->Owner); - new (&New->Object) pkgTagFile(&New->Fd); } else { - New = (TagFileData*)type->tp_alloc(type, 0); new (&New->Fd) FileFd(filename, FileFd::ReadOnly, FileFd::Extension, false); - New->Owner = File; - Py_INCREF(New->Owner); - new (&New->Object) pkgTagFile(&New->Fd); } + New->Owner = File; + Py_INCREF(New->Owner); + new (&New->Object) pkgTagFile(&New->Fd); // Create the section New->Section = (TagSecData*)(&PyTagSection_Type)->tp_alloc(&PyTagSection_Type, 0); -- cgit v1.2.3 From 3514672ed2a333eb6791d425ebe59b883cf8d349 Mon Sep 17 00:00:00 2001 From: Michael Vogt Date: Mon, 6 Feb 2012 14:55:25 +0100 Subject: python/tag.cc: make it build with older apt versions too --- python/tag.cc | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'python/tag.cc') diff --git a/python/tag.cc b/python/tag.cc index 88a48df9..49f53c31 100644 --- a/python/tag.cc +++ b/python/tag.cc @@ -425,7 +425,12 @@ static PyObject *TagFileNew(PyTypeObject *type,PyObject *Args,PyObject *kwds) } else { + // FileFd::Extension got added in this revision +#if (APT_PKG_MAJOR >= 4 && APT_PKG_MINOR >= 12) new (&New->Fd) FileFd(filename, FileFd::ReadOnly, FileFd::Extension, false); +#else + new (&New->Fd) FileFd(filename, FileFd::ReadOnly, false); +#endif } New->Owner = File; Py_INCREF(New->Owner); -- cgit v1.2.3