upgrade rpm version to 4.14.1
[platform/upstream/rpm.git] / python / rpmstrpool-py.c
1 #include "rpmsystem-py.h"
2 #include <rpm/rpmstrpool.h>
3 #include "rpmstrpool-py.h"
4
5 struct rpmstrPoolObject_s {
6     PyObject_HEAD
7     PyObject *md_dict;
8     rpmstrPool pool;
9 };
10
11 static char strpool_doc[] = "";
12
13 static void strpool_dealloc(rpmstrPoolObject *s)
14 {
15     s->pool = rpmstrPoolFree(s->pool);
16     Py_TYPE(s)->tp_free((PyObject *)s);
17 }
18
19 static PyObject *strpool_new(PyTypeObject *subtype,
20                              PyObject *args, PyObject *kwds)
21 {
22     return rpmstrPool_Wrap(subtype, NULL);
23 }
24
25 static PyObject *strpool_str2id(rpmstrPoolObject *s,
26                                 PyObject *args, PyObject *kwds)
27 {
28     char * kwlist[] = { "str", "create", NULL };
29     const char *str = NULL;
30     int create = 1;
31
32     if (!PyArg_ParseTupleAndKeywords(args, kwds, "s|i", kwlist, &str, &create))
33         return NULL;
34
35     return PyLong_FromLong(rpmstrPoolId(s->pool, str, create));
36 }
37
38 static PyObject *strpool_id2str(rpmstrPoolObject *s, PyObject *item)
39 {
40     PyObject *ret = NULL;
41     rpmsid id = 0;
42
43     if (PyArg_Parse(item, "I", &id)) {
44         const char *str = rpmstrPoolStr(s->pool, id);
45
46         if (str)
47             ret = PyBytes_FromString(str);
48         else 
49             PyErr_SetObject(PyExc_KeyError, item);
50     }
51     return ret;
52 }
53
54 static PyObject *strpool_freeze(rpmstrPoolObject *s,
55                                 PyObject *args, PyObject *kwds)
56 {
57     char * kwlist[] = { "keephash", NULL };
58     int keephash = 0;
59
60     if (!PyArg_ParseTupleAndKeywords(args, kwds, "|i", kwlist, &keephash))
61         return NULL;
62
63     rpmstrPoolFreeze(s->pool, keephash);
64     Py_RETURN_NONE;
65 }
66
67 static PyObject *strpool_unfreeze(rpmstrPoolObject *s)
68 {
69     rpmstrPoolUnfreeze(s->pool);
70     Py_RETURN_NONE;
71 }
72
73 static Py_ssize_t strpool_length(rpmstrPoolObject *s)
74 {
75     return rpmstrPoolNumStr(s->pool);
76 }
77
78 static struct PyMethodDef strpool_methods[] = {
79     { "str2id", (PyCFunction)strpool_str2id,    METH_VARARGS|METH_KEYWORDS,
80         NULL },
81     { "id2str", (PyCFunction)strpool_id2str,    METH_O,
82         NULL },
83     { "freeze", (PyCFunction)strpool_freeze,    METH_VARARGS|METH_KEYWORDS,
84         NULL },
85     { "unfreeze", (PyCFunction)strpool_unfreeze, METH_NOARGS,
86         NULL },
87     { NULL,     NULL }
88 };
89
90 static PyMappingMethods strpool_as_mapping = {
91     (lenfunc) strpool_length,           /* mp_length */
92     (binaryfunc) strpool_id2str,        /* mp_subscript */
93     (objobjargproc) 0,                  /* mp_ass_subscript */
94 };
95
96 PyTypeObject rpmstrPool_Type = {
97         PyVarObject_HEAD_INIT(&PyType_Type, 0)
98         "rpm.strpool",                  /* tp_name */
99         sizeof(rpmstrPoolObject),       /* tp_size */
100         0,                              /* tp_itemsize */
101         (destructor) strpool_dealloc,   /* tp_dealloc */
102         0,                              /* tp_print */
103         (getattrfunc)0,                 /* tp_getattr */
104         0,                              /* tp_setattr */
105         0,                              /* tp_compare */
106         0,                              /* tp_repr */
107         0,                              /* tp_as_number */
108         0,                              /* tp_as_sequence */
109         &strpool_as_mapping,            /* tp_as_mapping */
110         0,                              /* tp_hash */
111         0,                              /* tp_call */
112         0,                              /* tp_str */
113         PyObject_GenericGetAttr,        /* tp_getattro */
114         PyObject_GenericSetAttr,        /* tp_setattro */
115         0,                              /* tp_as_buffer */
116         Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, /* tp_flags */
117         strpool_doc,                    /* tp_doc */
118         0,                              /* tp_traverse */
119         0,                              /* tp_clear */
120         0,                              /* tp_richcompare */
121         0,                              /* tp_weaklistoffset */
122         0,                              /* tp_iter */
123         0,                              /* tp_iternext */
124         strpool_methods,                /* tp_methods */
125         0,                              /* tp_members */
126         0,                              /* tp_getset */
127         0,                              /* tp_base */
128         0,                              /* tp_dict */
129         0,                              /* tp_descr_get */
130         0,                              /* tp_descr_set */
131         0,                              /* tp_dictoffset */
132         0,                              /* tp_init */
133         0,                              /* tp_alloc */
134         strpool_new,                    /* tp_new */
135         0,                              /* tp_free */
136         0,                              /* tp_is_gc */
137 };
138
139 PyObject * rpmstrPool_Wrap(PyTypeObject *subtype, rpmstrPool pool)
140 {
141     rpmstrPoolObject *s = (rpmstrPoolObject *)subtype->tp_alloc(subtype, 0);
142     if (s == NULL) return NULL;
143
144     /* permit referencing a pre-existing pool as well */
145     s->pool = (pool != NULL) ? rpmstrPoolLink(pool) : rpmstrPoolCreate();
146
147     return (PyObject *) s;
148 }
149
150 int poolFromPyObject(PyObject *item, rpmstrPool *pool)
151 {
152     rpmstrPoolObject *p = NULL;
153     if (PyArg_Parse(item, "O!", &rpmstrPool_Type, &p))
154         *pool = p->pool;
155     return (p != NULL);
156 }