Commit d77425ca authored by Julian Andres Klode's avatar Julian Andres Klode

Introduce apt_pkg.Error class replacing SystemError

The use of SystemError has always been a hack, and with the
new dedicated class, we will actually know whether an
exception was raised by apt or not.
parent 051a7880
......@@ -12,6 +12,14 @@ enhanced with APT-specific methods. Because APT only provides a stream based
view on a tar archive, this module's :class:`TarFile` class only provides a
very small subset of those functions.
Exceptions
----------
.. class:: Error
This is the same class as :class:`apt_pkg.Error`, provided here for
convenience.
AR Archives
-----------
.. class:: ArArchive(file)
......
......@@ -28,6 +28,10 @@ be called without having run init*(), but will not return the expected value.
the commandline afterwards and finally call :func:`init_system`.
Exceptions
----------
.. autoclass:: Error
Working with the cache
----------------------
.. class:: Cache([progress: apt.progress.base.OpProgress])
......
......@@ -2,6 +2,11 @@ What's New In python-apt 1.1
==============================
This release is built against APT 1.1
Highlights
----------
* Code that previously raised :class:`SystemError` now raises
:class:`apt_pkg.Error`.
Removed
-------
* The class :class:`apt_pkg.IndexRecords` has been removed, as it was removed
......@@ -11,6 +16,8 @@ Removed
Added
------
* The class :class:`apt_pkg.HashStringList` has been added.
* The class :class:`apt_pkg.Error` and an alias :class:`apt_inst.Error` has
been added.
Deprecated
......
......@@ -23,6 +23,7 @@
#include <Python.h>
/*}}}*/
PyObject *PyAptError;
static PyMethodDef *methods = 0;
......@@ -49,8 +50,10 @@ static struct PyModuleDef moduledef = {
0
};
#define RETURN(x) return x
#define INIT_ERROR return 0
extern "C" PyObject * PyInit_apt_inst()
#else
#define INIT_ERROR return
extern "C" void initapt_inst()
#define RETURN(x) return
#endif
......@@ -61,6 +64,14 @@ extern "C" void initapt_inst()
PyObject *module = Py_InitModule3("apt_inst",methods, apt_inst_doc);
#endif
PyObject *apt_pkg = PyImport_ImportModule("apt_pkg");
if (apt_pkg == NULL)
INIT_ERROR;
PyAptError = PyObject_GetAttrString(apt_pkg, "Error");
if (PyAptError == NULL)
INIT_ERROR;
PyModule_AddObject(module,"Error",PyAptError);
ADDTYPE(module,"ArMember",&PyArMember_Type);
ADDTYPE(module,"ArArchive",&PyArArchive_Type);
ADDTYPE(module,"DebFile",&PyDebFile_Type);
......
......@@ -35,6 +35,16 @@
#include <Python.h>
/*}}}*/
static char PyAptError_Doc[] =
"Exception class for most python-apt exceptions.\n"
"\n"
"This class replaces the use of :class:`SystemError` in previous versions\n"
"of python-apt. It inherits from :class:`SystemError`, so make sure to\n"
"catch this class first.\n\n"
".. versionadded:: 1.1";
PyObject *PyAptError;
/**
* A Python->C->Python gettext() function.
*
......@@ -256,7 +266,7 @@ static PyObject *md5sum(PyObject *Self,PyObject *Args)
if (fstat(Fd,&St) != 0 ||
Sum.AddFD(Fd,St.st_size) == false)
{
PyErr_SetFromErrno(PyExc_SystemError);
PyErr_SetFromErrno(PyAptError);
return 0;
}
......@@ -301,7 +311,7 @@ static PyObject *sha1sum(PyObject *Self,PyObject *Args)
if (fstat(Fd,&St) != 0 ||
Sum.AddFD(Fd,St.st_size) == false)
{
PyErr_SetFromErrno(PyExc_SystemError);
PyErr_SetFromErrno(PyAptError);
return 0;
}
......@@ -346,7 +356,7 @@ static PyObject *sha256sum(PyObject *Self,PyObject *Args)
if (fstat(Fd,&St) != 0 ||
Sum.AddFD(Fd,St.st_size) == false)
{
PyErr_SetFromErrno(PyExc_SystemError);
PyErr_SetFromErrno(PyAptError);
return 0;
}
......@@ -391,7 +401,7 @@ static PyObject *sha512sum(PyObject *Self,PyObject *Args)
if (fstat(Fd,&St) != 0 ||
Sum.AddFD(Fd,St.st_size) == false)
{
PyErr_SetFromErrno(PyExc_SystemError);
PyErr_SetFromErrno(PyAptError);
return 0;
}
......@@ -785,6 +795,9 @@ extern "C" void initapt_pkg()
// Finalize our types to add slots, etc.
if (PyType_Ready(&PyConfiguration_Type) == -1) INIT_ERROR;
if (PyType_Ready(&PyCacheFile_Type) == -1) INIT_ERROR;
PyAptError = PyErr_NewExceptionWithDoc("apt_pkg.Error", PyAptError_Doc, PyExc_SystemError, NULL);
if (PyAptError == NULL)
INIT_ERROR;
// Initialize the module
#if PY_MAJOR_VERSION >= 3
......@@ -799,7 +812,7 @@ extern "C" void initapt_pkg()
// Global configuration, should never be deleted.
Config->NoDelete = true;
PyModule_AddObject(Module,"config",Config);
PyModule_AddObject(Module,"Error",PyAptError);
......@@ -981,7 +994,6 @@ extern "C" void initapt_pkg()
PyModule_AddIntConstant(Module,"INSTSTATE_HOLD",pkgCache::State::Hold);
PyModule_AddIntConstant(Module,"INSTSTATE_HOLD_REINSTREQ",pkgCache::State::HoldReInstReq);
#if PY_MAJOR_VERSION >= 3
return Module;
#endif
......
......@@ -594,7 +594,7 @@ static PyObject *debfile_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
const ARArchive::Member *member = self->Object->FindMember("debian-binary");
if (!member)
return PyErr_Format(PyExc_SystemError, "No debian archive, missing %s",
return PyErr_Format(PyAptError, "No debian archive, missing %s",
"debian-binary");
if (!self->Fd.Seek(member->Start))
......
......@@ -45,7 +45,7 @@ PyObject *HandleErrors(PyObject *Res)
}
if (errcnt == 0)
Err = "Internal Error";
PyErr_SetString(PyExc_SystemError,Err.c_str());
PyErr_SetString(PyAptError,Err.c_str());
return 0;
}
......
......@@ -32,6 +32,11 @@
#include <iostream>
#include <new>
/**
* Exception class for almost all Python errors
*/
extern PyObject *PyAptError;
#if PYTHON_API_VERSION < 1013
typedef int Py_ssize_t;
#endif
......
......@@ -50,7 +50,7 @@ static int hashes_init(PyObject *self, PyObject *args, PyObject *kwds)
else if ((Fd = PyObject_AsFileDescriptor(object)) != -1) {
struct stat St;
if (fstat(Fd, &St) != 0 || hashes.AddFD(Fd, St.st_size) == false) {
PyErr_SetFromErrno(PyExc_SystemError);
PyErr_SetFromErrno(PyAptError);
return -1;
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment