Commit 62d7f4b8 authored by Julian Andres Klode's avatar Julian Andres Klode
Browse files

* 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.
parent eaefd2f4
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -2,6 +2,8 @@ python-apt (0.7.11) UNRELEASED; urgency=low

  * python/tag.cc:
    - Support 'key in mapping' for TagSections
  * Replace support for file objects with a more generic support for any object
    providing a fileno() method and for file descriptors (integers).

 -- Julian Andres Klode <jak@debian.org>  Mon, 13 Apr 2009 18:08:10 +0200

+15 −6
Original line number Diff line number Diff line
@@ -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));
+6 −6
Original line number Diff line number Diff line
@@ -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)
+5 −2
Original line number Diff line number Diff line
@@ -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);
+11 −6
Original line number Diff line number Diff line
@@ -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();