Annotation of gpl/axl/py-axl/py_axl_error.c, revision 1.1.1.1
1.1 misho 1: /**
2: * PyAxl: Axl Library python bindings
3: * Copyright (C) 2009 Advanced Software Production Line, S.L.
4: *
5: * This program is free software; you can redistribute it and/or
6: * modify it under the terms of the GNU Lesser General Public License
7: * as published by the Free Software Foundation; either version 2.1
8: * of the License, or (at your option) any later version.
9: *
10: * This program is distributed in the hope that it will be useful,
11: * but WITHOUT ANY WARRANTY; without even the implied warranty of
12: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13: * Lesser General Public License for more details.
14: *
15: * You should have received a copy of the GNU Lesser General Public
16: * License along with this program; if not, write to the Free
17: * Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18: * 02111-1307 USA
19: *
20: * You may find a copy of the license under this software is released
21: * at COPYING file. This is LGPL software: you are welcome to develop
22: * proprietary applications using this library without any royalty or
23: * fee but returning back any change, improvement or addition in the
24: * form of source code, project image, documentation patches, etc.
25: *
26: * For commercial support for XML enabled solutions contact us:
27: *
28: * Postal address:
29: * Advanced Software Production Line, S.L.
30: * C/ Antonio Suarez Nº 10,
31: * Edificio Alius A, Despacho 102
32: * Alcalá de Henares 28802 (Madrid)
33: * Spain
34: *
35: * Email address:
36: * info@aspl.es - http://www.aspl.es/axl
37: */
38: #include <py_axl_error.h>
39:
40: #define LOG_DOMAIN "py-axl-error"
41:
42: struct _PyAxlError {
43: /* header required to initialize python required bits for
44: every python object */
45: PyObject_HEAD
46:
47: /* pointer to the axl error */
48: axlError * error;
49: };
50:
51: static int py_axl_error_init_type (PyAxlError *self, PyObject *args, PyObject *kwds)
52: {
53: return 0;
54: }
55:
56: /**
57: * @brief Function used to allocate memory required by the object
58: * axl.Error
59: */
60: static PyObject * py_axl_error_new (PyTypeObject *type, PyObject *args, PyObject *kwds)
61: {
62: PyAxlError *self;
63:
64: /* create the object */
65: self = (PyAxlError *)type->tp_alloc(type, 0);
66:
67: return (PyObject *)self;
68: }
69:
70: /**
71: * @brief Function used to finish and dealloc memory used by the
72: * object axl.Error
73: */
74: static void py_axl_error_dealloc (PyAxlError* self)
75: {
76: axl_error_free (self->error);
77: self->error = NULL;
78:
79: /* free the node it self */
80: self->ob_type->tp_free ((PyObject*)self);
81:
82: return;
83: }
84:
85: /**
86: * @brief This function implements the generic attribute getting that
87: * allows to perform complex member resolution (not merely direct
88: * member access).
89: */
90: PyObject * py_axl_error_get_attr (PyObject *o, PyObject *attr_name) {
91: const char * attr = NULL;
92: PyObject * result;
93: PyAxlError * self = (PyAxlError *) o;
94:
95: /* now implement other attributes */
96: if (! PyArg_Parse (attr_name, "s", &attr))
97: return NULL;
98:
99: __axl_log (LOG_DOMAIN, AXL_LEVEL_DEBUG, "received request to report axl.Error attr name %s (self: %p)",
100: attr, o);
101:
102: if (axl_cmp (attr, "code")) {
103: return Py_BuildValue ("i", axl_error_get_code (self->error));
104: } else if (axl_cmp (attr, "msg")) {
105: return Py_BuildValue ("s", axl_error_get (self->error));
106: }
107:
108: /* first implement generic attr already defined */
109: result = PyObject_GenericGetAttr (o, attr_name);
110: if (result)
111: return result;
112:
113: return NULL;
114: }
115:
116: /**
117: * @brief Implements attribute set operation.
118: */
119: int py_axl_error_set_attr (PyObject *o, PyObject *attr_name, PyObject *v)
120: {
121: const char * attr = NULL;
122: /* PyAxlError * self = (PyAxlError *) o; */
123: /* axl_bool boolean_value = axl_false; */
124:
125: /* now implement other attributes */
126: if (! PyArg_Parse (attr_name, "s", &attr))
127: return -1;
128:
129: /* now implement generic setter */
130: return PyObject_GenericSetAttr (o, attr_name, v);
131: }
132:
133: static PyMethodDef py_axl_error_methods[] = {
134: {NULL}
135: };
136:
137: static PyTypeObject PyAxlErrorType = {
138: PyObject_HEAD_INIT(NULL)
139: 0, /* ob_size*/
140: "axl.Error", /* tp_name*/
141: sizeof(PyAxlError), /* tp_basicsize*/
142: 0, /* tp_itemsize*/
143: (destructor)py_axl_error_dealloc, /* tp_dealloc*/
144: 0, /* tp_print*/
145: 0, /* tp_getattr*/
146: 0, /* tp_setattr*/
147: 0, /* tp_compare*/
148: 0, /* tp_repr*/
149: 0, /* tp_as_number*/
150: 0, /* tp_as_sequence*/
151: 0, /* tp_as_mapping*/
152: 0, /* tp_hash */
153: 0, /* tp_call*/
154: 0, /* tp_str*/
155: py_axl_error_get_attr, /* tp_getattro*/
156: py_axl_error_set_attr, /* tp_setattro*/
157: 0, /* tp_as_buffer*/
158: Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags*/
159: "Error object; wrapper of axlError API type", /* tp_doc */
160: 0, /* tp_traverse */
161: 0, /* tp_clear */
162: 0, /* tp_richcompare */
163: 0, /* tp_weaklistoffset */
164: 0, /* tp_iter */
165: 0, /* tp_iternext */
166: py_axl_error_methods, /* tp_methods */
167: 0, /* py_axl_error_members, */ /* tp_members */
168: 0, /* tp_getset */
169: 0, /* tp_base */
170: 0, /* tp_dict */
171: 0, /* tp_descr_get */
172: 0, /* tp_descr_set */
173: 0, /* tp_dictoffset */
174: (initproc)py_axl_error_init_type, /* tp_init */
175: 0, /* tp_alloc */
176: py_axl_error_new, /* tp_new */
177:
178: };
179:
180:
181: /**
182: * @brief Allows to check if the PyObject received represents a
183: * PyAxlError reference.
184: */
185: axl_bool py_axl_error_check (PyObject * obj)
186: {
187: /* check null references */
188: if (obj == NULL)
189: return axl_false;
190:
191: /* return check result */
192: return PyObject_TypeCheck (obj, &PyAxlErrorType);
193: }
194:
195: PyObject * py_axl_error_create (axlError * error)
196: {
197: /* return a new instance */
198: PyAxlError * obj = (PyAxlError *) PyObject_CallObject ((PyObject *) &PyAxlErrorType, NULL);
199:
200: /* check ref created */
201: if (obj == NULL) {
202: __axl_log (LOG_DOMAIN, AXL_LEVEL_CRITICAL, "Failed to create PyAxlError object, returning NULL");
203: return NULL;
204: } /* end if */
205:
206: /* set error if defined */
207: if (error)
208: obj->error = error;
209:
210: return __PY_OBJECT (obj);
211: }
212:
213: void init_axl_error (PyObject * module)
214: {
215: /* register type */
216: if (PyType_Ready(&PyAxlErrorType) < 0)
217: return;
218:
219: Py_INCREF (&PyAxlErrorType);
220: PyModule_AddObject(module, "Error", (PyObject *)&PyAxlErrorType);
221:
222: return;
223: }
224:
225:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>