File: _json.c

package info (click to toggle)
openvswitch 2.6.2~pre%2Bgit20161223-3
  • links: PTS
  • area: main
  • in suites: stretch
  • size: 42,772 kB
  • ctags: 37,668
  • sloc: sh: 499,654; ansic: 261,360; xml: 21,326; python: 14,843; makefile: 450; perl: 409
file content (268 lines) | stat: -rw-r--r-- 7,435 bytes parent folder | download
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
#include "Python.h"
#include <openvswitch/json.h>
#include "structmember.h"

#if PY_MAJOR_VERSION >= 3
#define IS_PY3K
#endif

typedef struct {
    PyObject_HEAD
    struct json_parser *_parser;
} json_ParserObject;

static void
Parser_dealloc(json_ParserObject * p)
{
    json_parser_abort(p->_parser);
    Py_TYPE(p)->tp_free(p);
}

static PyObject *
Parser_new(PyTypeObject * type, PyObject * args, PyObject * kwargs)
{
    json_ParserObject *self;
    static char *kwlist[] = { "check_trailer", NULL };
    PyObject *check_trailer = NULL;
    int ct_int = 0;

    if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O", kwlist,
                                     &check_trailer)) {
        return NULL;
    }

    if (check_trailer != NULL) {
        ct_int = PyObject_IsTrue(check_trailer);
        if (ct_int < 0) {
            return NULL;
        } else if (ct_int) {
            ct_int = JSPF_TRAILER;
        }
    }

    self = (json_ParserObject *) type->tp_alloc(type, 0);
    if (self != NULL) {
        self->_parser = json_parser_create(ct_int);
    }

    return (PyObject *) self;
}

static PyObject *
Parser_feed(json_ParserObject * self, PyObject * args)
{
    Py_ssize_t input_sz;
    PyObject *input;
    size_t rd;
    char *input_str;

    if (self->_parser == NULL) {
        return NULL;
    }

    if (!PyArg_UnpackTuple(args, "input", 1, 1, &input)) {
        return NULL;
    }
#ifdef IS_PY3K
    if ((input_str = PyUnicode_AsUTF8AndSize(input, &input_sz)) == NULL) {
#else
    if (PyString_AsStringAndSize(input, &input_str, &input_sz) < 0) {
#endif
        return NULL;
    }

    rd = json_parser_feed(self->_parser, input_str, (size_t) input_sz);

#ifdef IS_PY3K
    return PyLong_FromSize_t(rd);
#else
    return PyInt_FromSize_t(rd);
#endif
}

static PyObject *
Parser_is_done(json_ParserObject * self)
{
    if (self->_parser == NULL) {
        return NULL;
    }
    return PyBool_FromLong(json_parser_is_done(self->_parser));
}

static PyObject *
json_to_python(struct json *json)
{
    switch (json->type) {
    case JSON_NULL:
        Py_RETURN_NONE;
    case JSON_FALSE:
        Py_RETURN_FALSE;
    case JSON_TRUE:
        Py_RETURN_TRUE;
    case JSON_OBJECT:{
            struct shash_node *node;
            PyObject *dict = PyDict_New();

            if (dict == NULL) {
                return PyErr_NoMemory();
            }
            SHASH_FOR_EACH(node, json->u.object) {
                PyObject *key = PyUnicode_FromString(node->name);
                PyObject *val = json_to_python(node->data);

                if (!(key && val) || PyDict_SetItem(dict, key, val)) {
                    Py_XDECREF(key);
                    Py_XDECREF(val);
                    Py_XDECREF(dict);
                    return NULL;
                }

                Py_XDECREF(key);
                Py_XDECREF(val);
            }
            return dict;
        }
    case JSON_ARRAY:{
            int i;
            PyObject *arr = PyList_New(json->u.array.n);

            if (arr == NULL) {
                return PyErr_NoMemory();
            }
            for (i = 0; i < json->u.array.n; i++) {
                PyObject *item = json_to_python(json->u.array.elems[i]);

                if (!item || PyList_SetItem(arr, i, item)) {
                    Py_XDECREF(arr);
                    return NULL;
                }
            }
            return arr;
        }
    case JSON_REAL:
        if (json->u.real != 0) {
            return PyFloat_FromDouble(json->u.real);
        } /* fall through to treat 0 as int */
    case JSON_INTEGER:
#ifdef IS_PY3K
        return PyLong_FromLong((long) json->u.integer);
#else
        return PyInt_FromLong((long) json->u.integer);
#endif

    case JSON_STRING:
        return PyUnicode_FromString(json->u.string);
    default:
        return NULL;
    }
}

static PyObject *
Parser_finish(json_ParserObject * self)
{
    struct json *json;
    PyObject *obj;

    if (self->_parser == NULL) {
        return NULL;
    }

    json = json_parser_finish(self->_parser);
    self->_parser = NULL;
    obj = json_to_python(json);
    return obj;
}

static PyMethodDef Parser_methods[] = {
    {"feed", (PyCFunction) Parser_feed, METH_VARARGS,
     "Feed data to the parser and return the index of the last object."},
    {"is_done", (PyCFunction) Parser_is_done, METH_NOARGS,
     "Whether the parser has finished decoding an object."},
    {"finish", (PyCFunction) Parser_finish, METH_NOARGS,
     "Finish parsing and return Python object parsed."},
    {NULL},
};

static PyTypeObject json_ParserType = {
    PyVarObject_HEAD_INIT(NULL, 0)
        "ovs._json.Parser",     /* tp_name */
    sizeof (json_ParserObject), /* tp_basicsize */
    0,                          /* tp_itemsize */
    (destructor) Parser_dealloc,        /* tp_dealloc */
    0,                          /* tp_print */
    0,                          /* tp_getattr */
    0,                          /* tp_setattr */
    0,                          /* tp_compare */
    0,                          /* tp_repr */
    0,                          /* tp_as_number */
    0,                          /* tp_as_sequence */
    0,                          /* tp_as_mapping */
    0,                          /* tp_hash */
    0,                          /* tp_call */
    0,                          /* tp_str */
    0,                          /* tp_getattro */
    0,                          /* tp_setattro */
    0,                          /* tp_as_buffer */
    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,   /* tp_flags */
    "Parser objects",           /* tp_doc */
    0,                          /* tp_traverse */
    0,                          /* tp_clear */
    0,                          /* tp_richcompare */
    0,                          /* tp_weaklistoffset */
    0,                          /* tp_iter */
    0,                          /* tp_iternext */
    Parser_methods,             /* tp_methods */
    0,                          /* tp_members */
    0,                          /* tp_getset */
    0,                          /* tp_base */
    0,                          /* tp_dict */
    0,                          /* tp_descr_get */
    0,                          /* tp_descr_set */
    0,                          /* tp_dictoffset */
    0,                          /* tp_init */
    0,                          /* tp_alloc */
    Parser_new,                 /* tp_new */
};

#ifdef IS_PY3K
static struct PyModuleDef moduledef = {
    PyModuleDef_HEAD_INIT,
    "ovs._json",                /* m_name */
    "OVS JSON Parser module",   /* m_doc */
    0,                          /* m_size */
    0,                          /* m_methods */
    0,                          /* m_slots */
    0,                          /* m_traverse */
    0,                          /* m_clear */
    0,                          /* m_free */
};

#define INITERROR return NULL
#else /* !IS_PY3K */
#define INITERROR return
#endif

PyMODINIT_FUNC
#ifdef IS_PY3K
PyInit__json(void)
#else
init_json(void)
#endif
{
    PyObject *m;

    if (PyType_Ready(&json_ParserType) < 0) {
        INITERROR;
    }
#ifdef IS_PY3K
    m = PyModule_Create(&moduledef);
#else
    m = Py_InitModule3("ovs._json", NULL, "OVS JSON Parser module");
#endif

    Py_INCREF(&json_ParserType);
    PyModule_AddObject(m, "Parser", (PyObject *) & json_ParserType);
#ifdef IS_PY3K
    return m;
#endif
}