aboutsummaryrefslogtreecommitdiff
path: root/python/exception.hh
blob: b4a173f6dbfcaac536b77cc3f4e2e17cc3d4aa2b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/* vim: set sw=4 sts=4 et foldmethod=syntax : */

/*
 * Copyright (c) 2007 Piotr JaroszyƄski
 *
 * This file is part of the Paludis package manager. Paludis is free software;
 * you can redistribute it and/or modify it under the terms of the GNU General
 * Public License version 2, as published by the Free Software Foundation.
 *
 * Paludis is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
 * Place, Suite 330, Boston, MA  02111-1307  USA
 */

#ifndef PALUDIS_GUARD_PYTHON_EXCEPTION_HH
#define PALUDIS_GUARD_PYTHON_EXCEPTION_HH 1

#include <paludis/util/tr1_memory.hh>
#include <paludis/util/tr1_functional.hh>
#include <paludis/util/private_implementation_pattern.hh>
#include <paludis/util/exception.hh>

#include <boost/python.hpp>

namespace paludis
{
    namespace python
    {
        class RegisteredExceptionBase
        {
            public:
                virtual ~RegisteredExceptionBase() = 0;

                virtual PyObject * get_py_exception() const = 0;
        };

        template <typename Ex_>
        class RegisteredException :
            public RegisteredExceptionBase
        {
            private:
                const std::string _name;
                const std::string _longname;
                PyObject * _e;

            public:
                RegisteredException(const std::string & name, const std::string & doc, PyObject * base);

                void translator(const Ex_ & x) const;

                PyObject *
                get_py_exception() const
                {
                    return _e;
                }
        };

        template <class Ex_>
        RegisteredException<Ex_>::RegisteredException(const std::string & name,
                const std::string & doc, PyObject * base) :
            _name(name),
            _longname("paludis." + name),
            _e(PyErr_NewException(const_cast<char*>(_longname.c_str()), base, NULL))
        {
            using namespace tr1::placeholders;
            PyModule_AddObject(boost::python::detail::current_scope, const_cast<char*>(_name.c_str()), _e);
            PyObject * doc_string = PyString_FromString(doc.c_str());
            PyObject_SetAttrString(_e, "__doc__", doc_string);
            boost::python::register_exception_translator<Ex_>(
                    tr1::bind(tr1::mem_fn(&RegisteredException<Ex_>::translator),
                        this, _1)
                    );
        }

        template <class Ex_>
        void
        RegisteredException<Ex_>::translator(const Ex_ & x) const
        {
            PyObject * backtrace = PyString_FromString(x.backtrace("\n").c_str());
            PyObject * message = PyString_FromString(x.message().c_str());
            PyObject * what = PyString_FromString(x.what());
            PyObject_SetAttrString(_e, "backtrace", backtrace);
            PyObject_SetAttrString(_e, "message", message);
            PyObject_SetAttrString(_e, "what", what);

            PyErr_SetString(_e, x.message().c_str());
        }

        class PALUDIS_VISIBLE ExceptionRegister :
            public InstantiationPolicy<ExceptionRegister, instantiation_method::SingletonTag>,
            private PrivateImplementationPattern<ExceptionRegister>
        {
            friend class InstantiationPolicy<ExceptionRegister, instantiation_method::SingletonTag>;

            private:
                ExceptionRegister();

                void add_map_item(const std::string & name, tr1::shared_ptr<RegisteredExceptionBase>);
                PyObject * get_py_exception(const std::string & name);

            public:
                ~ExceptionRegister();

                template <typename Ex_>
                void add_exception(const std::string & name, const std::string & doc)
                {
                    add_map_item(name, tr1::shared_ptr<RegisteredExceptionBase>(
                                new RegisteredException<Ex_>(name, doc, 0)));
                }

                template <typename Ex_>
                void add_exception(const std::string & name, const std::string & base, const std::string & doc)
                {
                    add_map_item(name, tr1::shared_ptr<RegisteredExceptionBase>(
                                new RegisteredException<Ex_>(name, doc, get_py_exception(base))));
                }
        };

        class PALUDIS_VISIBLE PythonError :
            public Exception
        {
            public:
                PythonError(const std::string & message) throw ();
        };

        class PALUDIS_VISIBLE PythonMethodNotImplemented :
            public PythonError
        {
            public:
                PythonMethodNotImplemented(const std::string & class_name,
                        const std::string & function_name) throw ();
        };

        class PALUDIS_VISIBLE PythonContainerConversionError :
            public PythonError
        {
            public:
                PythonContainerConversionError(const std::string & class_name,
                        const std::string & container_name, const std::string & o_type) throw ();
        };
    }
}

#endif