]>
Commit | Line | Data |
---|---|---|
fadebc89 MT |
1 | /* |
2 | libloc - A library to determine the location of someone on the Internet | |
3 | ||
4 | Copyright (C) 2017 IPFire Development Team <info@ipfire.org> | |
5 | ||
6 | This library is free software; you can redistribute it and/or | |
7 | modify it under the terms of the GNU Lesser General Public | |
8 | License as published by the Free Software Foundation; either | |
9 | version 2.1 of the License, or (at your option) any later version. | |
10 | ||
11 | This library is distributed in the hope that it will be useful, | |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 | Lesser General Public License for more details. | |
15 | */ | |
16 | ||
17 | #include <Python.h> | |
18 | ||
19 | #include <loc/libloc.h> | |
20 | #include <loc/as.h> | |
21 | #include <loc/stringpool.h> | |
22 | ||
23 | #include "as.h" | |
24 | ||
25 | static PyObject* AS_new(PyTypeObject* type, PyObject* args, PyObject* kwds) { | |
26 | // Create libloc context | |
27 | struct loc_ctx* ctx; | |
28 | int r = loc_new(&ctx); | |
29 | if (r) | |
30 | return NULL; | |
31 | ||
32 | // Create stringpool | |
33 | struct loc_stringpool* pool; | |
34 | r = loc_stringpool_new(ctx, &pool); | |
35 | if (r) { | |
36 | loc_unref(ctx); | |
37 | return NULL; | |
38 | } | |
39 | ||
40 | ASObject* self = (ASObject*)type->tp_alloc(type, 0); | |
41 | if (self) { | |
42 | self->ctx = ctx; | |
43 | self->pool = pool; | |
44 | } | |
45 | ||
46 | return (PyObject*)self; | |
47 | } | |
48 | ||
49 | static void AS_dealloc(ASObject* self) { | |
50 | if (self->as) | |
51 | loc_as_unref(self->as); | |
52 | ||
53 | if (self->pool) | |
54 | loc_stringpool_unref(self->pool); | |
55 | ||
56 | if (self->ctx) | |
57 | loc_unref(self->ctx); | |
58 | ||
59 | Py_TYPE(self)->tp_free((PyObject* )self); | |
60 | } | |
61 | ||
62 | static int AS_init(ASObject* self, PyObject* args, PyObject* kwargs) { | |
63 | uint32_t number = 0; | |
64 | ||
65 | if (!PyArg_ParseTuple(args, "i", &number)) | |
66 | return -1; | |
67 | ||
68 | // Create the AS object | |
69 | int r = loc_as_new(self->ctx, self->pool, &self->as, number); | |
70 | if (r) | |
71 | return -1; | |
72 | ||
73 | return 0; | |
74 | } | |
75 | ||
76 | static PyObject* AS_get_number(ASObject* self) { | |
77 | uint32_t number = loc_as_get_number(self->as); | |
78 | ||
79 | return PyLong_FromLong(number); | |
80 | } | |
81 | ||
82 | static PyObject* AS_get_name(ASObject* self) { | |
83 | const char* name = loc_as_get_name(self->as); | |
84 | ||
85 | return PyUnicode_FromString(name); | |
86 | } | |
87 | ||
88 | static struct PyGetSetDef AS_getsetters[] = { | |
89 | { | |
90 | "name", | |
91 | (getter)AS_get_name, | |
92 | NULL, | |
93 | NULL, | |
94 | NULL, | |
95 | }, | |
96 | { | |
97 | "number", | |
98 | (getter)AS_get_number, | |
99 | NULL, | |
100 | NULL, | |
101 | NULL, | |
102 | }, | |
103 | { NULL }, | |
104 | }; | |
105 | ||
106 | PyTypeObject ASType = { | |
107 | PyVarObject_HEAD_INIT(NULL, 0) | |
108 | tp_name: "location.AS", | |
109 | tp_basicsize: sizeof(ASObject), | |
110 | tp_flags: Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, | |
111 | tp_new: AS_new, | |
112 | tp_dealloc: (destructor)AS_dealloc, | |
113 | tp_init: (initproc)AS_init, | |
114 | tp_doc: "AS object", | |
115 | tp_getset: AS_getsetters, | |
116 | }; |