From d3ea76a031fd9cb804741ec9955eccbbdb5f16f7 Mon Sep 17 00:00:00 2001 From: Michael Vogt Date: Fri, 3 Feb 2012 10:55:20 +0100 Subject: * tests/test_tagfile.py: - add test for apt_pkg.TagFile() both for compressed/uncompressed files --- debian/changelog | 3 +++ tests/data/tagfile/history.1.log.gz | Bin 0 -> 270 bytes tests/data/tagfile/history.log | 15 +++++++++++++++ tests/test_tagfile.py | 35 +++++++++++++++++++++++++++++++++++ 4 files changed, 53 insertions(+) create mode 100644 tests/data/tagfile/history.1.log.gz create mode 100644 tests/data/tagfile/history.log create mode 100644 tests/test_tagfile.py diff --git a/debian/changelog b/debian/changelog index 17fe379e..89c07119 100644 --- a/debian/changelog +++ b/debian/changelog @@ -7,6 +7,9 @@ python-apt (0.8.4) UNRELEASED; urgency=low - use apt_inst for reading the control_filelist * debian/control: - remove no longer needed dependency on python-debian + * tests/test_tagfile.py: + - add test for apt_pkg.TagFile() both for compressed/uncompressed + files -- Michael Vogt Wed, 04 Jan 2012 12:07:48 +0100 diff --git a/tests/data/tagfile/history.1.log.gz b/tests/data/tagfile/history.1.log.gz new file mode 100644 index 00000000..4174e02b Binary files /dev/null and b/tests/data/tagfile/history.1.log.gz differ diff --git a/tests/data/tagfile/history.log b/tests/data/tagfile/history.log new file mode 100644 index 00000000..f1d72665 --- /dev/null +++ b/tests/data/tagfile/history.log @@ -0,0 +1,15 @@ + +Start-Date: 2012-02-01 13:54:52 +Commandline: apt-get install chromium-browser +Install: chromium-browser:amd64 (16.0.912.77~r118311-0ubuntu1), chromium-browser-l10n:amd64 (16.0.912.77~r118311-0ubuntu1, automatic), chromium-codecs-ffmpeg:amd64 (16.0.912.77~r118311-0ubuntu1, automatic) +End-Date: 2012-02-01 13:55:01 + +Start-Date: 2012-02-02 10:39:04 +Commandline: apt-get install python-geoclue +Install: python-geoclue:amd64 (0.1.0-4build1) +End-Date: 2012-02-02 10:39:08 + +Start-Date: 2012-02-03 10:20:50 +Commandline: apt-get install python-qt4 +Install: python-qt4:amd64 (4.9-3ubuntu1) +End-Date: 2012-02-03 10:20:55 diff --git a/tests/test_tagfile.py b/tests/test_tagfile.py new file mode 100644 index 00000000..b0b5cbdc --- /dev/null +++ b/tests/test_tagfile.py @@ -0,0 +1,35 @@ +#!/usr/bin/python +# +# Copyright (C) 2010 Michael Vogt +# +# Copying and distribution of this file, with or without modification, +# are permitted in any medium without royalty provided the copyright +# notice and this notice are preserved. +"""Unit tests for verifying the correctness of apt_pkg.TagFile""" + +import unittest + +from test_all import get_library_dir +import sys +sys.path.insert(0, get_library_dir()) + +import apt_pkg + +class TestTagFile(unittest.TestCase): + """ test the apt_pkg.TagFile """ + + def test_tag_file(self): + tagfile = apt_pkg.TagFile(open("./data/tagfile/history.log")) + for i, stanza in enumerate(tagfile): + pass + self.assertEqual(i, 2) + + def test_tag_file_compressed(self): + tagfile = apt_pkg.TagFile(open("./data/tagfile/history.1.log.gz")) + for i, stanza in enumerate(tagfile): + #print stanza + pass + self.assertEqual(i, 2) + +if __name__ == "__main__": + unittest.main() -- cgit v1.2.3 From 70e5581fa92082cc47ebadd0ae5e2cbcf928853d Mon Sep 17 00:00:00 2001 From: Michael Vogt Date: Fri, 3 Feb 2012 10:58:04 +0100 Subject: tests/test_tagfile.py: fix data loading when not in tests dir --- tests/test_tagfile.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/tests/test_tagfile.py b/tests/test_tagfile.py index b0b5cbdc..b14dd9b4 100644 --- a/tests/test_tagfile.py +++ b/tests/test_tagfile.py @@ -7,6 +7,7 @@ # notice and this notice are preserved. """Unit tests for verifying the correctness of apt_pkg.TagFile""" +import os import unittest from test_all import get_library_dir @@ -19,13 +20,19 @@ class TestTagFile(unittest.TestCase): """ test the apt_pkg.TagFile """ def test_tag_file(self): - tagfile = apt_pkg.TagFile(open("./data/tagfile/history.log")) + basepath = os.path.dirname(__file__) + tagfilepath = os.path.join( + basepath, "./data/tagfile/history.log") + tagfile = apt_pkg.TagFile(open(tagfilepath)) for i, stanza in enumerate(tagfile): pass self.assertEqual(i, 2) def test_tag_file_compressed(self): - tagfile = apt_pkg.TagFile(open("./data/tagfile/history.1.log.gz")) + basepath = os.path.dirname(__file__) + tagfilepath = os.path.join( + basepath, "./data/tagfile/history.1.log.gz") + tagfile = apt_pkg.TagFile(open(tagfilepath)) for i, stanza in enumerate(tagfile): #print stanza pass -- cgit v1.2.3 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) --- debian/changelog | 3 +++ python/tag.cc | 44 +++++++++++++++++++++++++++++++++++--------- tests/test_tagfile.py | 30 +++++++++++++++--------------- 3 files changed, 53 insertions(+), 24 deletions(-) diff --git a/debian/changelog b/debian/changelog index 89c07119..5813656a 100644 --- a/debian/changelog +++ b/debian/changelog @@ -10,6 +10,9 @@ python-apt (0.8.4) UNRELEASED; urgency=low * tests/test_tagfile.py: - add test for apt_pkg.TagFile() both for compressed/uncompressed files + * python/tag.cc, tests/test_tagfile.py: + - add support a filename argument in apt_pkg.TagFile() (in addition + to the file object currently supported) -- Michael Vogt Wed, 04 Jan 2012 12:07:48 +0100 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); diff --git a/tests/test_tagfile.py b/tests/test_tagfile.py index b14dd9b4..371cc6ee 100644 --- a/tests/test_tagfile.py +++ b/tests/test_tagfile.py @@ -7,6 +7,7 @@ # notice and this notice are preserved. """Unit tests for verifying the correctness of apt_pkg.TagFile""" +import glob import os import unittest @@ -21,22 +22,21 @@ class TestTagFile(unittest.TestCase): def test_tag_file(self): basepath = os.path.dirname(__file__) - tagfilepath = os.path.join( - basepath, "./data/tagfile/history.log") - tagfile = apt_pkg.TagFile(open(tagfilepath)) - for i, stanza in enumerate(tagfile): - pass - self.assertEqual(i, 2) + tagfilepath = os.path.join(basepath, "./data/tagfile/*") + # test once for compressed and uncompressed + for testfile in glob.glob(tagfilepath): + # test once using the open() method and once using the path + for f in [testfile, open(testfile)]: + tagfile = apt_pkg.TagFile(f) + for i, stanza in enumerate(tagfile): + pass + self.assertEqual(i, 2) - def test_tag_file_compressed(self): - basepath = os.path.dirname(__file__) - tagfilepath = os.path.join( - basepath, "./data/tagfile/history.1.log.gz") - tagfile = apt_pkg.TagFile(open(tagfilepath)) - for i, stanza in enumerate(tagfile): - #print stanza - pass - self.assertEqual(i, 2) + def test_errors(self): + # Raises SystemError via lbiapt + self.assertRaises(SystemError, apt_pkg.TagFile, "not-there-no-no") + # Raises Type error + self.assertRaises(TypeError, apt_pkg.TagFile, object()) if __name__ == "__main__": unittest.main() -- 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(-) 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(+) 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