Radek Krejci | 0f3499a | 2017-10-13 13:39:36 +0200 | [diff] [blame^] | 1 | /** |
| 2 | * @file err.c |
| 3 | * @author Radek Krejci <rkrejci@cesnet.cz> |
| 4 | * @brief NETCONF reply errors |
| 5 | * |
| 6 | * Copyright (c) 2017 CESNET, z.s.p.o. |
| 7 | * |
| 8 | * This source code is licensed under BSD 3-Clause License (the "License"). |
| 9 | * You may not use this file except in compliance with the License. |
| 10 | * You may obtain a copy of the License at |
| 11 | * |
| 12 | * https://opensource.org/licenses/BSD-3-Clause |
| 13 | */ |
| 14 | |
| 15 | /* Python API header */ |
| 16 | #include <Python.h> |
| 17 | #include <structmember.h> |
| 18 | |
| 19 | /* standard headers */ |
| 20 | #include <string.h> |
| 21 | |
| 22 | #include "netconf.h" |
| 23 | #include "messages_p.h" |
| 24 | |
| 25 | static void |
| 26 | ncErrFree(ncErrObject *self) |
| 27 | { |
| 28 | nc_client_err_clean(self->err, self->ctx); |
| 29 | |
| 30 | Py_TYPE(self)->tp_free((PyObject*)self); |
| 31 | } |
| 32 | |
| 33 | static PyObject * |
| 34 | ncErrStr(ncErrObject *self) |
| 35 | { |
| 36 | return PyUnicode_FromFormat("NETCONF error-reply: %s", self->err->message); |
| 37 | } |
| 38 | |
| 39 | /* |
| 40 | * tp_getset callbacs held by ncErrGetSetters[] |
| 41 | */ |
| 42 | |
| 43 | static PyObject * |
| 44 | ncErrGetType(ncErrObject *self, void *closure) |
| 45 | { |
| 46 | if (!self->err->type) { |
| 47 | Py_RETURN_NONE; |
| 48 | } |
| 49 | return PyUnicode_FromString(self->err->type); |
| 50 | } |
| 51 | |
| 52 | static PyObject * |
| 53 | ncErrGetTag(ncErrObject *self, void *closure) |
| 54 | { |
| 55 | if (!self->err->tag) { |
| 56 | Py_RETURN_NONE; |
| 57 | } |
| 58 | return PyUnicode_FromString(self->err->tag); |
| 59 | } |
| 60 | |
| 61 | static PyObject * |
| 62 | ncErrGetMessage(ncErrObject *self, void *closure) |
| 63 | { |
| 64 | if (!self->err->message) { |
| 65 | Py_RETURN_NONE; |
| 66 | } |
| 67 | return PyUnicode_FromString(self->err->message); |
| 68 | } |
| 69 | |
| 70 | /* |
| 71 | * Callback structures |
| 72 | */ |
| 73 | |
| 74 | static PyGetSetDef ncErrGetSetters[] = { |
| 75 | {"type", (getter)ncErrGetType, NULL, "<error-type>", NULL}, |
| 76 | {"tag", (getter)ncErrGetTag, NULL, "<error-tag>", NULL}, |
| 77 | {"message", (getter)ncErrGetMessage, NULL, "<error-message>", NULL}, |
| 78 | {NULL} /* Sentinel */ |
| 79 | }; |
| 80 | |
| 81 | PyDoc_STRVAR(ncErrDoc, |
| 82 | "NETCONF Error Reply information.\n\n"); |
| 83 | |
| 84 | PyTypeObject ncErrType = { |
| 85 | PyVarObject_HEAD_INIT(NULL, 0) |
| 86 | "netconf2.Err", /* tp_name */ |
| 87 | sizeof(ncErrObject), /* tp_basicsize */ |
| 88 | 0, /* tp_itemsize */ |
| 89 | (destructor)ncErrFree, /* tp_dealloc */ |
| 90 | 0, /* tp_print */ |
| 91 | 0, /* tp_getattr */ |
| 92 | 0, /* tp_setattr */ |
| 93 | 0, /* tp_reserved */ |
| 94 | (reprfunc)ncErrStr, /* tp_repr */ |
| 95 | 0, /* tp_as_number */ |
| 96 | 0, /* tp_as_sequence */ |
| 97 | 0, /* tp_as_mapping */ |
| 98 | 0, /* tp_hash */ |
| 99 | 0, /* tp_call */ |
| 100 | (reprfunc)ncErrStr, /* tp_str */ |
| 101 | 0, /* tp_getattro */ |
| 102 | 0, /* tp_setattro */ |
| 103 | 0, /* tp_as_buffer */ |
| 104 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
| 105 | ncErrDoc, /* tp_doc */ |
| 106 | 0, /* tp_traverse */ |
| 107 | 0, /* tp_clear */ |
| 108 | 0, /* tp_richcompare */ |
| 109 | 0, /* tp_weaklistoffset */ |
| 110 | 0, /* tp_iter */ |
| 111 | 0, /* tp_iternext */ |
| 112 | 0, /* tp_methods */ |
| 113 | 0, /* tp_members */ |
| 114 | ncErrGetSetters, /* tp_getset */ |
| 115 | 0, /* tp_base */ |
| 116 | 0, /* tp_dict */ |
| 117 | 0, /* tp_descr_get */ |
| 118 | 0, /* tp_descr_set */ |
| 119 | 0, /* tp_dictoffset */ |
| 120 | 0, /* tp_init */ |
| 121 | 0, /* tp_alloc */ |
| 122 | 0, /* tp_new */ |
| 123 | }; |
| 124 | |