diff options
| author | Michael Vogt <michael.vogt@ubuntu.com> | 2012-02-06 14:56:13 +0100 |
|---|---|---|
| committer | Michael Vogt <michael.vogt@ubuntu.com> | 2012-02-06 14:56:13 +0100 |
| commit | 89df8999cddf71b1f5cbbc621d64260fd19f1bb2 (patch) | |
| tree | c1d8472ff8116cf518a77de3afbe2e930a803757 /python | |
| parent | 64c1ffa6310efdff6353346fe621aea10e36f2c9 (diff) | |
| parent | 3514672ed2a333eb6791d425ebe59b883cf8d349 (diff) | |
| download | python-apt-89df8999cddf71b1f5cbbc621d64260fd19f1bb2.tar.gz | |
* 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)
Diffstat (limited to 'python')
| -rw-r--r-- | python/tag.cc | 39 |
1 files changed, 33 insertions, 6 deletions
diff --git a/python/tag.cc b/python/tag.cc index 3996af8e..49f53c31 100644 --- a/python/tag.cc +++ b/python/tag.cc @@ -390,21 +390,48 @@ 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); + New = (TagFileData*)type->tp_alloc(type, 0); + if (fileno > 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); +#endif + } + 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(fileno,false); + new (&New->Fd) FileFd(filename, FileFd::ReadOnly, false); #endif + } New->Owner = File; Py_INCREF(New->Owner); new (&New->Object) pkgTagFile(&New->Fd); |
