diff options
| author | Julian Andres Klode <jak@debian.org> | 2009-04-13 19:13:25 +0200 |
|---|---|---|
| committer | Julian Andres Klode <jak@debian.org> | 2009-04-13 19:13:25 +0200 |
| commit | 62d7f4b80d56ef1e27039441cd6584cfb71d6502 (patch) | |
| tree | aa2d006fccd4a4f8497eb25dc0bd6c8baf6e6e89 /python | |
| parent | eaefd2f4cb97ed069375f18fb67d8570dc5eaed8 (diff) | |
| download | python-apt-62d7f4b80d56ef1e27039441cd6584cfb71d6502.tar.gz | |
* python/*.cc: Use PyObject_AsFileDescriptor instead of fileno(PyFile_AsFile)
Replace support for file objects with a more generic support for any object
providing a fileno() method and for file descriptors (integers).
This also helps us to port to Python 3, where the previously used PyFile_
functions are not available anymore.
Diffstat (limited to 'python')
| -rw-r--r-- | python/apt_instmodule.cc | 21 | ||||
| -rw-r--r-- | python/apt_pkgmodule.cc | 12 | ||||
| -rw-r--r-- | python/tag.cc | 7 | ||||
| -rw-r--r-- | python/tar.cc | 17 |
4 files changed, 37 insertions, 20 deletions
diff --git a/python/apt_instmodule.cc b/python/apt_instmodule.cc index 48868d86..a9d81be4 100644 --- a/python/apt_instmodule.cc +++ b/python/apt_instmodule.cc @@ -35,14 +35,17 @@ static PyObject *debExtractControl(PyObject *Self,PyObject *Args) { char *Member = "control"; PyObject *File; - if (PyArg_ParseTuple(Args,"O!|s",&PyFile_Type,&File,&Member) == 0) + if (PyArg_ParseTuple(Args,"O|s",&File,&Member) == 0) return 0; // Subscope makes sure any clean up errors are properly handled. PyObject *Res = 0; { // Open the file and associate the .deb - FileFd Fd(fileno(PyFile_AsFile(File)),false); + int fileno = PyObject_AsFileDescriptor(File); + if (fileno == -1) + return 0; + FileFd Fd(fileno,false); debDebFile Deb(Fd); if (_error->PendingError() == true) return HandleErrors(); @@ -76,7 +79,7 @@ static PyObject *debExtractArchive(PyObject *Self,PyObject *Args) char *Rootdir = NULL; char cwd[512]; PyObject *File; - if (PyArg_ParseTuple(Args,"O!|s",&PyFile_Type,&File,&Rootdir) == 0) + if (PyArg_ParseTuple(Args,"O|s",&File,&Rootdir) == 0) return 0; // Subscope makes sure any clean up errors are properly handled. @@ -89,7 +92,10 @@ static PyObject *debExtractArchive(PyObject *Self,PyObject *Args) } // Open the file and associate the .deb - FileFd Fd(fileno(PyFile_AsFile(File)),false); + int fileno = PyObject_AsFileDescriptor(File); + if (fileno == -1) + return 0; + FileFd Fd(fileno,false); debDebFile Deb(Fd); if (_error->PendingError() == true) { if (Rootdir != NULL) @@ -118,11 +124,14 @@ static PyObject *arCheckMember(PyObject *Self,PyObject *Args) char *Member = NULL; bool res = false; PyObject *File; - if (PyArg_ParseTuple(Args,"O!s",&PyFile_Type,&File,&Member) == 0) + if (PyArg_ParseTuple(Args,"Os",&File,&Member) == 0) return 0; // Open the file and associate the .deb - FileFd Fd(fileno(PyFile_AsFile(File)),false); + int fileno = PyObject_AsFileDescriptor(File); + if (fileno == -1) + return 0; + FileFd Fd(fileno,false); ARArchive AR(Fd); if (_error->PendingError() == true) return HandleErrors(Py_BuildValue("b",res)); diff --git a/python/apt_pkgmodule.cc b/python/apt_pkgmodule.cc index 1417e5d0..10d98911 100644 --- a/python/apt_pkgmodule.cc +++ b/python/apt_pkgmodule.cc @@ -184,10 +184,10 @@ static PyObject *md5sum(PyObject *Self,PyObject *Args) } // Digest of a file - if (PyFile_Check(Obj) != 0) + int Fd = PyObject_AsFileDescriptor(Obj); + if (Fd != -1) { MD5Summation Sum; - int Fd = fileno(PyFile_AsFile(Obj)); struct stat St; if (fstat(Fd,&St) != 0 || Sum.AddFD(Fd,St.st_size) == false) @@ -224,10 +224,10 @@ static PyObject *sha1sum(PyObject *Self,PyObject *Args) } // Digest of a file - if (PyFile_Check(Obj) != 0) + int Fd = PyObject_AsFileDescriptor(Obj); + if (Fd != -1) { SHA1Summation Sum; - int Fd = fileno(PyFile_AsFile(Obj)); struct stat St; if (fstat(Fd,&St) != 0 || Sum.AddFD(Fd,St.st_size) == false) @@ -264,10 +264,10 @@ static PyObject *sha256sum(PyObject *Self,PyObject *Args) } // Digest of a file - if (PyFile_Check(Obj) != 0) + int Fd = PyObject_AsFileDescriptor(Obj); + if (Fd != -1) { SHA256Summation Sum; - int Fd = fileno(PyFile_AsFile(Obj)); struct stat St; if (fstat(Fd,&St) != 0 || Sum.AddFD(Fd,St.st_size) == false) diff --git a/python/tag.cc b/python/tag.cc index f3fb2e6c..baf97b59 100644 --- a/python/tag.cc +++ b/python/tag.cc @@ -272,11 +272,14 @@ char *doc_ParseTagFile = "ParseTagFile(File) -> TagFile"; PyObject *ParseTagFile(PyObject *self,PyObject *Args) { PyObject *File; - if (PyArg_ParseTuple(Args,"O!",&PyFile_Type,&File) == 0) + if (PyArg_ParseTuple(Args,"O",&File) == 0) + return 0; + int fileno = PyObject_AsFileDescriptor(File); + if (fileno == -1) return 0; TagFileData *New = PyObject_NEW(TagFileData,&TagFileType); - new (&New->Fd) FileFd(fileno(PyFile_AsFile(File)),false); + new (&New->Fd) FileFd(fileno,false); New->File = File; Py_INCREF(New->File); new (&New->Object) pkgTagFile(&New->Fd); diff --git a/python/tar.cc b/python/tar.cc index e5aaee6f..f0d57823 100644 --- a/python/tar.cc +++ b/python/tar.cc @@ -97,8 +97,7 @@ PyObject *tarExtract(PyObject *Self,PyObject *Args) PyObject *Function; char *Comp; - if (PyArg_ParseTuple(Args,"O!Os",&PyFile_Type,&File, - &Function,&Comp) == 0) + if (PyArg_ParseTuple(Args,"OOs",&File, &Function,&Comp) == 0) return 0; if (PyCallable_Check(Function) == 0) @@ -109,7 +108,11 @@ PyObject *tarExtract(PyObject *Self,PyObject *Args) { // Open the file and associate the tar - FileFd Fd(fileno(PyFile_AsFile(File)),false); + int fileno = PyObject_AsFileDescriptor(File); + if (fileno == -1) + return 0; + + FileFd Fd(fileno,false); ExtractTar Tar(Fd,0xFFFFFFFF,Comp); if (_error->PendingError() == true) return HandleErrors(); @@ -139,8 +142,7 @@ PyObject *debExtract(PyObject *Self,PyObject *Args) char *Chunk; const char *Comp = "gzip"; - if (PyArg_ParseTuple(Args,"O!Os",&PyFile_Type,&File, - &Function,&Chunk) == 0) + if (PyArg_ParseTuple(Args,"OOs",&File,&Function,&Chunk) == 0) return 0; if (PyCallable_Check(Function) == 0) @@ -149,10 +151,13 @@ PyObject *debExtract(PyObject *Self,PyObject *Args) return 0; } + int fileno = PyObject_AsFileDescriptor(File); + if (fileno == -1) + return 0; { // Open the file and associate the tar // Open the file and associate the .deb - FileFd Fd(fileno(PyFile_AsFile(File)),false); + FileFd Fd(fileno,false); debDebFile Deb(Fd); if (_error->PendingError() == true) return HandleErrors(); |
