File: exception.c

package info (click to toggle)
zbar 0.10%2Bdoc-10
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 3,908 kB
  • ctags: 2,903
  • sloc: ansic: 21,641; sh: 10,745; cpp: 1,997; python: 774; xml: 473; perl: 289; makefile: 188
file content (145 lines) | stat: -rw-r--r-- 4,237 bytes parent folder | download | duplicates (5)
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
/*------------------------------------------------------------------------
 *  Copyright 2009 (c) Jeff Brown <spadix@users.sourceforge.net>
 *
 *  This file is part of the ZBar Bar Code Reader.
 *
 *  The ZBar Bar Code Reader is free software; you can redistribute it
 *  and/or modify it under the terms of the GNU Lesser Public License as
 *  published by the Free Software Foundation; either version 2.1 of
 *  the License, or (at your option) any later version.
 *
 *  The ZBar Bar Code Reader 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 Lesser Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser Public License
 *  along with the ZBar Bar Code Reader; if not, write to the Free
 *  Software Foundation, Inc., 51 Franklin St, Fifth Floor,
 *  Boston, MA  02110-1301  USA
 *
 *  http://sourceforge.net/projects/zbar
 *------------------------------------------------------------------------*/

#include "zbarmodule.h"

static inline PyObject*
exc_get_message (zbarException *self,
                 void *closure)
{
    PyBaseExceptionObject *super = (PyBaseExceptionObject*)self;
    if(!PyString_Size(super->message)) {
        Py_CLEAR(super->message);
        if(!self->obj || !zbarProcessor_Check(self->obj))
            super->message = PyString_FromString("unknown zbar error");
        else {
            const void *zobj = ((zbarProcessor*)self->obj)->zproc;
            super->message = PyString_FromString(_zbar_error_string(zobj, 1));
        }
    }
    Py_INCREF(super->message);
    return(super->message);
}

static int
exc_init (zbarException *self,
          PyObject *args,
          PyObject *kwds)
{
    if(!_PyArg_NoKeywords(self->base.ob_type->tp_name, kwds))
        return(-1);
    PyBaseExceptionObject *super = (PyBaseExceptionObject*)self;
    Py_CLEAR(super->args);
    Py_INCREF(args);
    super->args = args;

    if(PyTuple_GET_SIZE(args) == 1) {
        Py_CLEAR(self->obj);
        self->obj = PyTuple_GET_ITEM(args, 0);
        Py_INCREF(self->obj);
    }
    return(0);
}

static int
exc_traverse (zbarException *self,
              visitproc visit,
              void *arg)
{
    Py_VISIT(self->obj);
    PyTypeObject *base = (PyTypeObject*)PyExc_Exception;
    return(base->tp_traverse((PyObject*)self, visit, arg));
}

static int
exc_clear (zbarException *self)
{
    Py_CLEAR(self->obj);
    ((PyTypeObject*)PyExc_Exception)->tp_clear((PyObject*)self);
    return(0);
}

static void
exc_dealloc (zbarException *self)
{
    exc_clear(self);
    ((PyTypeObject*)PyExc_Exception)->tp_dealloc((PyObject*)self);
}

static PyObject*
exc_str (zbarException *self)
{
    return(exc_get_message(self, NULL));
}

static int
exc_set_message (zbarException *self,
                 PyObject *value,
                 void *closure)
{
    PyBaseExceptionObject *super = (PyBaseExceptionObject*)self;
    Py_CLEAR(super->message);
    if(!value)
        value = PyString_FromString("");
    else
        Py_INCREF(value);
    super->message = value;
    return(0);
}

static PyGetSetDef exc_getset[] = {
    { "message", (getter)exc_get_message, (setter)exc_set_message, },
    { NULL, },
};

PyTypeObject zbarException_Type = {
    PyObject_HEAD_INIT(NULL)
    .tp_name        = "zbar.Exception",
    .tp_basicsize   = sizeof(zbarException),
    .tp_flags       = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE |
                      Py_TPFLAGS_HAVE_GC,
    .tp_init        = (initproc)exc_init,
    .tp_traverse    = (traverseproc)exc_traverse,
    .tp_clear       = (inquiry)exc_clear,
    .tp_dealloc     = (destructor)exc_dealloc,
    .tp_str         = (reprfunc)exc_str,
    .tp_getset      = exc_getset,
};

PyObject*
zbarErr_Set (PyObject *self)
{
    const void *zobj = ((zbarProcessor*)self)->zproc;
    zbar_error_t err = _zbar_get_error_code(zobj);

    if(err == ZBAR_ERR_NOMEM)
        PyErr_NoMemory();
    else if(err < ZBAR_ERR_NUM) {
        PyObject *type = zbar_exc[err];
        assert(type);
        PyErr_SetObject(type, self);
    }
    else
        PyErr_SetObject(zbar_exc[0], self);
    return(NULL);
}