1 #include "rpmsystem-py.h"
2 #include <rpm/rpmstrpool.h>
3 #include "rpmstrpool-py.h"
5 struct rpmstrPoolObject_s {
11 static char strpool_doc[] = "";
13 static void strpool_dealloc(rpmstrPoolObject *s)
15 s->pool = rpmstrPoolFree(s->pool);
16 Py_TYPE(s)->tp_free((PyObject *)s);
19 static PyObject *strpool_new(PyTypeObject *subtype,
20 PyObject *args, PyObject *kwds)
22 return rpmstrPool_Wrap(subtype, NULL);
25 static PyObject *strpool_str2id(rpmstrPoolObject *s,
26 PyObject *args, PyObject *kwds)
28 char * kwlist[] = { "str", "create", NULL };
29 const char *str = NULL;
32 if (!PyArg_ParseTupleAndKeywords(args, kwds, "s|i", kwlist, &str, &create))
35 return PyLong_FromLong(rpmstrPoolId(s->pool, str, create));
38 static PyObject *strpool_id2str(rpmstrPoolObject *s, PyObject *item)
43 if (PyArg_Parse(item, "I", &id)) {
44 const char *str = rpmstrPoolStr(s->pool, id);
47 ret = PyBytes_FromString(str);
49 PyErr_SetObject(PyExc_KeyError, item);
54 static PyObject *strpool_freeze(rpmstrPoolObject *s,
55 PyObject *args, PyObject *kwds)
57 char * kwlist[] = { "keephash", NULL };
60 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|i", kwlist, &keephash))
63 rpmstrPoolFreeze(s->pool, keephash);
67 static PyObject *strpool_unfreeze(rpmstrPoolObject *s)
69 rpmstrPoolUnfreeze(s->pool);
73 static Py_ssize_t strpool_length(rpmstrPoolObject *s)
75 return rpmstrPoolNumStr(s->pool);
78 static struct PyMethodDef strpool_methods[] = {
79 { "str2id", (PyCFunction)strpool_str2id, METH_VARARGS|METH_KEYWORDS,
81 { "id2str", (PyCFunction)strpool_id2str, METH_O,
83 { "freeze", (PyCFunction)strpool_freeze, METH_VARARGS|METH_KEYWORDS,
85 { "unfreeze", (PyCFunction)strpool_unfreeze, METH_NOARGS,
90 static PyMappingMethods strpool_as_mapping = {
91 (lenfunc) strpool_length, /* mp_length */
92 (binaryfunc) strpool_id2str, /* mp_subscript */
93 (objobjargproc) 0, /* mp_ass_subscript */
96 PyTypeObject rpmstrPool_Type = {
97 PyVarObject_HEAD_INIT(&PyType_Type, 0)
98 "rpm.strpool", /* tp_name */
99 sizeof(rpmstrPoolObject), /* tp_size */
101 (destructor) strpool_dealloc, /* tp_dealloc */
103 (getattrfunc)0, /* tp_getattr */
107 0, /* tp_as_number */
108 0, /* tp_as_sequence */
109 &strpool_as_mapping, /* tp_as_mapping */
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 */
120 0, /* tp_richcompare */
121 0, /* tp_weaklistoffset */
124 strpool_methods, /* tp_methods */
129 0, /* tp_descr_get */
130 0, /* tp_descr_set */
131 0, /* tp_dictoffset */
134 strpool_new, /* tp_new */
139 PyObject * rpmstrPool_Wrap(PyTypeObject *subtype, rpmstrPool pool)
141 rpmstrPoolObject *s = (rpmstrPoolObject *)subtype->tp_alloc(subtype, 0);
142 if (s == NULL) return NULL;
144 /* permit referencing a pre-existing pool as well */
145 s->pool = (pool != NULL) ? rpmstrPoolLink(pool) : rpmstrPoolCreate();
147 return (PyObject *) s;
150 int poolFromPyObject(PyObject *item, rpmstrPool *pool)
152 rpmstrPoolObject *p = NULL;
153 if (PyArg_Parse(item, "O!", &rpmstrPool_Type, &p))