72e60fd4eabcfbcdbfe01e8c38b94052bc6c2067Jakub Hrozek/*
72e60fd4eabcfbcdbfe01e8c38b94052bc6c2067Jakub Hrozek Authors:
72e60fd4eabcfbcdbfe01e8c38b94052bc6c2067Jakub Hrozek Jakub Hrozek <jhrozek@redhat.com>
72e60fd4eabcfbcdbfe01e8c38b94052bc6c2067Jakub Hrozek
72e60fd4eabcfbcdbfe01e8c38b94052bc6c2067Jakub Hrozek Copyright (C) 2011 Red Hat
72e60fd4eabcfbcdbfe01e8c38b94052bc6c2067Jakub Hrozek
72e60fd4eabcfbcdbfe01e8c38b94052bc6c2067Jakub Hrozek This program is free software; you can redistribute it and/or modify
72e60fd4eabcfbcdbfe01e8c38b94052bc6c2067Jakub Hrozek it under the terms of the GNU General Public License as published by
72e60fd4eabcfbcdbfe01e8c38b94052bc6c2067Jakub Hrozek the Free Software Foundation; either version 3 of the License, or
72e60fd4eabcfbcdbfe01e8c38b94052bc6c2067Jakub Hrozek (at your option) any later version.
72e60fd4eabcfbcdbfe01e8c38b94052bc6c2067Jakub Hrozek
72e60fd4eabcfbcdbfe01e8c38b94052bc6c2067Jakub Hrozek This program is distributed in the hope that it will be useful,
72e60fd4eabcfbcdbfe01e8c38b94052bc6c2067Jakub Hrozek but WITHOUT ANY WARRANTY; without even the implied warranty of
72e60fd4eabcfbcdbfe01e8c38b94052bc6c2067Jakub Hrozek MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
72e60fd4eabcfbcdbfe01e8c38b94052bc6c2067Jakub Hrozek GNU General Public License for more details.
72e60fd4eabcfbcdbfe01e8c38b94052bc6c2067Jakub Hrozek
72e60fd4eabcfbcdbfe01e8c38b94052bc6c2067Jakub Hrozek You should have received a copy of the GNU General Public License
72e60fd4eabcfbcdbfe01e8c38b94052bc6c2067Jakub Hrozek along with this program. If not, see <http://www.gnu.org/licenses/>.
72e60fd4eabcfbcdbfe01e8c38b94052bc6c2067Jakub Hrozek*/
72e60fd4eabcfbcdbfe01e8c38b94052bc6c2067Jakub Hrozek
72e60fd4eabcfbcdbfe01e8c38b94052bc6c2067Jakub Hrozek#include "src/util/sss_python.h"
72e60fd4eabcfbcdbfe01e8c38b94052bc6c2067Jakub Hrozek#include "config.h"
72e60fd4eabcfbcdbfe01e8c38b94052bc6c2067Jakub Hrozek
72e60fd4eabcfbcdbfe01e8c38b94052bc6c2067Jakub HrozekPyObject *
72e60fd4eabcfbcdbfe01e8c38b94052bc6c2067Jakub Hrozeksss_exception_with_doc(char *name, char *doc, PyObject *base, PyObject *dict)
72e60fd4eabcfbcdbfe01e8c38b94052bc6c2067Jakub Hrozek{
72e60fd4eabcfbcdbfe01e8c38b94052bc6c2067Jakub Hrozek#ifdef HAVE_PYERR_NEWEXCEPTIONWITHDOC
72e60fd4eabcfbcdbfe01e8c38b94052bc6c2067Jakub Hrozek return PyErr_NewExceptionWithDoc(name, doc, base, dict);
72e60fd4eabcfbcdbfe01e8c38b94052bc6c2067Jakub Hrozek#else
72e60fd4eabcfbcdbfe01e8c38b94052bc6c2067Jakub Hrozek int result;
72e60fd4eabcfbcdbfe01e8c38b94052bc6c2067Jakub Hrozek PyObject *ret = NULL;
72e60fd4eabcfbcdbfe01e8c38b94052bc6c2067Jakub Hrozek PyObject *mydict = NULL; /* points to the dict only if we create it */
72e60fd4eabcfbcdbfe01e8c38b94052bc6c2067Jakub Hrozek PyObject *docobj;
72e60fd4eabcfbcdbfe01e8c38b94052bc6c2067Jakub Hrozek
72e60fd4eabcfbcdbfe01e8c38b94052bc6c2067Jakub Hrozek if (dict == NULL) {
72e60fd4eabcfbcdbfe01e8c38b94052bc6c2067Jakub Hrozek dict = mydict = PyDict_New();
72e60fd4eabcfbcdbfe01e8c38b94052bc6c2067Jakub Hrozek if (dict == NULL) {
72e60fd4eabcfbcdbfe01e8c38b94052bc6c2067Jakub Hrozek return NULL;
72e60fd4eabcfbcdbfe01e8c38b94052bc6c2067Jakub Hrozek }
72e60fd4eabcfbcdbfe01e8c38b94052bc6c2067Jakub Hrozek }
72e60fd4eabcfbcdbfe01e8c38b94052bc6c2067Jakub Hrozek
72e60fd4eabcfbcdbfe01e8c38b94052bc6c2067Jakub Hrozek if (doc != NULL) {
72e60fd4eabcfbcdbfe01e8c38b94052bc6c2067Jakub Hrozek docobj = PyString_FromString(doc);
72e60fd4eabcfbcdbfe01e8c38b94052bc6c2067Jakub Hrozek if (docobj == NULL)
72e60fd4eabcfbcdbfe01e8c38b94052bc6c2067Jakub Hrozek goto failure;
72e60fd4eabcfbcdbfe01e8c38b94052bc6c2067Jakub Hrozek result = PyDict_SetItemString(dict, "__doc__", docobj);
72e60fd4eabcfbcdbfe01e8c38b94052bc6c2067Jakub Hrozek Py_DECREF(docobj);
72e60fd4eabcfbcdbfe01e8c38b94052bc6c2067Jakub Hrozek if (result < 0)
72e60fd4eabcfbcdbfe01e8c38b94052bc6c2067Jakub Hrozek goto failure;
72e60fd4eabcfbcdbfe01e8c38b94052bc6c2067Jakub Hrozek }
72e60fd4eabcfbcdbfe01e8c38b94052bc6c2067Jakub Hrozek
72e60fd4eabcfbcdbfe01e8c38b94052bc6c2067Jakub Hrozek ret = PyErr_NewException(name, base, dict);
72e60fd4eabcfbcdbfe01e8c38b94052bc6c2067Jakub Hrozek failure:
72e60fd4eabcfbcdbfe01e8c38b94052bc6c2067Jakub Hrozek Py_XDECREF(mydict);
72e60fd4eabcfbcdbfe01e8c38b94052bc6c2067Jakub Hrozek return ret;
72e60fd4eabcfbcdbfe01e8c38b94052bc6c2067Jakub Hrozek#endif
72e60fd4eabcfbcdbfe01e8c38b94052bc6c2067Jakub Hrozek}