rpm 5.2.1

- Main Page
- Related Pages
- Modules
- Data Structures
- Files

- File List
- Globals

rpmps-py.c
Go to the documentation of this file.
1
4 /*@-modunconnomods -evalorderuncon @*/
5
6 #include "system.h"
7
8 #include <rpmio.h>
9 #include <rpmiotypes.h> /* XXX fnpyKey */
10 #include <rpmtypes.h>
11 #include <rpmtag.h>
12 #define _RPMPS_INTERNAL /* XXX rpmps needs iterator */
13
14 #include "rpmdebug-py.c"
15
16 #include "rpmps-py.h"
17
18 #include "debug.h"
19
20 /*@access FILE @*/
21 /*@access rpmps @*/
22 /*@access rpmProblem @*/
23
24 static PyObject *
25 rpmps_iter(rpmpsObject * s)
26 /*@modifies s @*/
27 {
28 if (_rpmps_debug < 0)
29 fprintf(stderr, "*** rpmps_iter(%p)\n", s);
30 s->psi = rpmpsInitIterator(s->ps);
31 Py_INCREF(s);
32 return (PyObject *)s;
33 }
34
35 /*@null@*/
36 static PyObject *
37 rpmps_iternext(rpmpsObject * s)
38 /*@modifies s @*/
39 {
40 PyObject * result = NULL;
41
42 if (_rpmps_debug < 0)
43 fprintf(stderr, "*** rpmps_iternext(%p) ps %p psi %p\n", s, s->ps, s->psi);
44
45 /* Reset loop indices on 1st entry. */
46 if (s->psi == NULL)
47 s->psi = rpmpsInitIterator(s->ps);
48
49 /* If more to do, return a problem set string. */
50 if (rpmpsNextIterator(s->psi) >= 0)
51 result = Py_BuildValue("s", rpmProblemString(rpmpsProblem(s->psi)));
52 else
53 s->psi = rpmpsFreeIterator(s->psi);
54
55 return result;
56 }
57
64
65 /*@null@*/
66 static PyObject *
67 rpmps_Debug(/*@unused@*/ rpmpsObject * s, PyObject * args,
68 PyObject * kwds)
69 /*@globals _Py_NoneStruct @*/
70 /*@modifies _Py_NoneStruct @*/
71 {
72 char * kwlist[] = {"debugLevel", NULL};
73
74 if (!PyArg_ParseTupleAndKeywords(args, kwds, "i", kwlist, &_rpmps_debug))
75 return NULL;
76
77 Py_INCREF(Py_None);
78 return Py_None;
79 }
80
81 static int
82 rpmps_Append(rpmpsObject * s, PyObject * value)
83 {
84 char *pkgNEVR, *altNEVR, *str1;
85 unsigned long ulong1;
86 int ignoreProblem;
87 rpmProblemType type;
88 fnpyKey key;
89
90 if (!PyArg_ParseTuple(value, "ssOiisN:rpmps value tuple",
91 &pkgNEVR, &altNEVR, &key,
92 &type, &ignoreProblem, &str1,
93 &ulong1))
94 {
95 return -1;
96 }
97 rpmpsAppend(s->ps, type, pkgNEVR, key, str1, NULL, altNEVR, ulong1);
98 return 0;
99 }
100
103 /*@-fullinitblock@*/
104 /*@unchecked@*/ /*@observer@*/
105 static struct PyMethodDef rpmps_methods[] = {
106 {"Debug", (PyCFunction)rpmps_Debug, METH_VARARGS|METH_KEYWORDS,
107 NULL},
108 {"Append", (PyCFunction)rpmps_Append, METH_VARARGS|METH_KEYWORDS,
109 NULL},
110 {NULL, NULL} /* sentinel */
111 };
112 /*@=fullinitblock@*/
113
114 /* ---------- */
115
116 static void
117 rpmps_dealloc(rpmpsObject * s)
118 /*@modifies s @*/
119 {
120 if (_rpmps_debug < 0)
121 fprintf(stderr, "*** rpmps_dealloc(%p)\n", s);
122 if (s) {
123 s->ps = rpmpsFree(s->ps);
124 PyObject_Del(s);
125 }
126 }
127
128 static int
129 rpmps_print(rpmpsObject * s, FILE * fp, /*@unused@*/ int flags)
130 /*@globals fileSystem @*/
131 /*@modifies s, fp, fileSystem @*/
132 {
133 if (_rpmps_debug < 0)
134 fprintf(stderr, "*** rpmps_print(%p,%p,%x)\n", s, (void *)fp, flags);
135 if (s && s->ps)
136 rpmpsPrint(fp, s->ps);
137 return 0;
138 }
139
140 static PyObject * rpmps_getattro(PyObject * o, PyObject * n)
141 /*@*/
142 {
143 if (_rpmps_debug < 0)
144 fprintf(stderr, "*** rpmps_getattro(%p,%p)\n", o, n);
145 return PyObject_GenericGetAttr(o, n);
146 }
147
148 static int rpmps_setattro(PyObject * o, PyObject * n, PyObject * v)
149 /*@*/
150 {
151 if (_rpmps_debug < 0)
152 fprintf(stderr, "*** rpmps_setattro(%p,%p,%p)\n", o, n, v);
153 return PyObject_GenericSetAttr(o, n, v);
154 }
155
156 static int
157 rpmps_length(rpmpsObject * s)
158 /*@*/
159 {
160 int rc;
161 rc = rpmpsNumProblems(s->ps);
162 if (_rpmps_debug < 0)
163 fprintf(stderr, "*** rpmps_length(%p) rc %d\n", s, rc);
164 return rc;
165 }
166
167 /*@null@*/
168 static PyObject *
169 rpmps_subscript(rpmpsObject * s, PyObject * key)
170 /*@*/
171 {
172 PyObject * result = NULL;
173 rpmpsi psi;
174 int ix;
175 int i;
176
177 if (!PyInt_Check(key)) {
178 if (_rpmps_debug < 0)
179 fprintf(stderr, "*** rpmps_subscript(%p[%s],%p[%s])\n", s, lbl(s), key, lbl(key));
180 PyErr_SetString(PyExc_TypeError, "integer expected");
181 return NULL;
182 }
183
184 ix = (int) PyInt_AsLong(key);
185 /* XXX range check */
186
187 psi = rpmpsInitIterator(s->ps);
188 while ((i = rpmpsNextIterator(psi)) >= 0) {
189 if (i != ix)
190 continue;
191 result = Py_BuildValue("s", rpmProblemString(rpmpsProblem(psi)));
192 if (_rpmps_debug < 0)
193 fprintf(stderr, "*** rpmps_subscript(%p,%p) %s\n", s, key, PyString_AsString(result));
194 break;
195 }
196 psi = rpmpsFreeIterator(psi);
197
198 return result;
199 }
200
201 #define PERMIT_RPMPS_SUBSCRIPT /* XXX likely buggy */
202 #if defined(PERMIT_RPMPS_SUBSCRIPT)
203 static int
204 rpmps_ass_sub(rpmpsObject * s, PyObject * key, PyObject * value)
205 /*@modifies s @*/
206 {
207 rpmps ps;
208 int ix;
209
210 if (!PyArg_Parse(key, "i:ass_sub", &ix)) {
211 PyErr_SetString(PyExc_TypeError, "rpmps key type must be integer");
212 return -1;
213 }
214
215 /* XXX get rid of negative indices */
216 if (ix < 0) ix = -ix;
217
218 ps = s->ps;
219
220 if (_rpmps_debug < 0)
221 fprintf(stderr, "*** rpmps_ass_sub(%p[%s],%p[%s],%p[%s]) ps %p[%d:%d:%d]\n", s, lbl(s), key, lbl(key), value, lbl(value), ps, ix, ps->numProblems, ps->numProblemsAlloced);
222
223 if (value == NULL) {
224 if (ix < ps->numProblems) {
225 rpmProblem op = ps->probs + ix;
226
227 op->pkgNEVR = _free(op->pkgNEVR);
228 op->altNEVR = _free(op->altNEVR);
229 op->str1 = _free(op->str1);
230
231 if ((ix+1) == ps->numProblems)
232 memset(op, 0, sizeof(*op));
233 else
234 memmove(op, op+1, (ps->numProblems - ix) * sizeof(*op));
235 if (ps->numProblems > 0)
236 ps->numProblems--;
237 }
238 } else {
239 rpmProblem p = memset(alloca(sizeof(*p)), 0, sizeof(*p));
240 unsigned long ulong1 = p->ulong1;
241
242 if (!PyArg_ParseTuple(value, "ssOiisN:rpmps value tuple",
243 &p->pkgNEVR, &p->altNEVR, &p->key,
244 &p->type, &p->ignoreProblem, &p->str1,
245 &ulong1))
246 {
247 return -1;
248 }
249
250 /*@-branchstate@*/
251 if (ix >= ps->numProblems) {
252 /* XXX force append for indices out of range. */
253 rpmpsAppend(s->ps, p->type, p->pkgNEVR, p->key,
254 p->str1, NULL, p->altNEVR, ulong1);
255 } else {
256 rpmProblem op = ps->probs + ix;
257
258 op->pkgNEVR = _free(op->pkgNEVR);
259 op->altNEVR = _free(op->altNEVR);
260 op->str1 = _free(op->str1);
261
262 p->pkgNEVR = (p->pkgNEVR && *p->pkgNEVR ? xstrdup(p->pkgNEVR) : NULL);
263 p->altNEVR = (p->altNEVR && *p->altNEVR ? xstrdup(p->altNEVR) : NULL);
264 p->str1 = (p->str1 && *p->str1 ? xstrdup(p->str1) : NULL);
265
266 *op = *p; /* structure assignment */
267 }
268 /*@=branchstate@*/
269 }
270
271 return 0;
272 }
273 #endif
274
275 static PyMappingMethods rpmps_as_mapping = {
276 (lenfunc) rpmps_length, /* mp_length */
277 (binaryfunc) rpmps_subscript, /* mp_subscript */
278 #if defined(PERMIT_RPMPS_SUBSCRIPT)
279 (objobjargproc) rpmps_ass_sub, /* mp_ass_subscript */
280 #endif
281 };
282
285 static int rpmps_init(rpmpsObject * s, PyObject *args, PyObject *kwds)
286 /*@modifies s @*/
287 {
288 char * kwlist[] = {NULL};
289
290 if (_rpmps_debug < 0)
291 fprintf(stderr, "*** rpmps_init(%p,%p,%p)\n", s, args, kwds);
292
293 if (!PyArg_ParseTupleAndKeywords(args, kwds, ":rpmps_init", kwlist))
294 return -1;
295
296 s->ps = rpmpsCreate();
297 s->psi = NULL;
298
299 return 0;
300 }
301
304 static void rpmps_free(/*@only@*/ rpmpsObject * s)
305 /*@modifies s @*/
306 {
307 if (_rpmps_debug)
308 fprintf(stderr, "%p -- ps %p\n", s, s->ps);
309 s->ps = rpmpsFree(s->ps);
310
311 PyObject_Del((PyObject *)s);
312 }
313
316 static PyObject * rpmps_alloc(PyTypeObject * subtype, int nitems)
317 /*@*/
318 {
319 PyObject * s = PyType_GenericAlloc(subtype, nitems);
320
321 if (_rpmps_debug < 0)
322 fprintf(stderr, "*** rpmps_alloc(%p,%d) ret %p\n", subtype, nitems, s);
323 return s;
324 }
325
328 /*@null@*/
329 static PyObject * rpmps_new(PyTypeObject * subtype, PyObject *args, PyObject *kwds)
330 /*@*/
331 {
332 rpmpsObject * s = (void *) PyObject_New(rpmpsObject, subtype);
333
334 /* Perform additional initialization. */
335 if (rpmps_init(s, args, kwds) < 0) {
336 rpmps_free(s);
337 return NULL;
338 }
339
340 if (_rpmps_debug)
341 fprintf(stderr, "%p ++ ps %p\n", s, s->ps);
342
343 return (PyObject *)s;
344 }
345
348 /*@unchecked@*/ /*@observer@*/
349 static char rpmps_doc[] =
350 "";
351
352 /*@-fullinitblock@*/
353 PyTypeObject rpmps_Type = {
354 PyObject_HEAD_INIT(&PyType_Type)
355 0, /* ob_size */
356 "rpm.ps", /* tp_name */
357 sizeof(rpmpsObject), /* tp_basicsize */
358 0, /* tp_itemsize */
359 /* methods */
360 (destructor) rpmps_dealloc, /* tp_dealloc */
361 (printfunc) rpmps_print, /* tp_print */
362 (getattrfunc)0, /* tp_getattr */
363 (setattrfunc)0, /* tp_setattr */
364 (cmpfunc)0, /* tp_compare */
365 (reprfunc)0, /* tp_repr */
366 0, /* tp_as_number */
367 0, /* tp_as_sequence */
368 &rpmps_as_mapping, /* tp_as_mapping */
369 (hashfunc)0, /* tp_hash */
370 (ternaryfunc)0, /* tp_call */
371 (reprfunc)0, /* tp_str */
372 (getattrofunc) rpmps_getattro, /* tp_getattro */
373 (setattrofunc) rpmps_setattro, /* tp_setattro */
374 0, /* tp_as_buffer */
375 Py_TPFLAGS_DEFAULT, /* tp_flags */
376 rpmps_doc, /* tp_doc */
377 #if Py_TPFLAGS_HAVE_ITER
378 0, /* tp_traverse */
379 0, /* tp_clear */
380 (richcmpfunc)0, /* tp_richcompare */
381 0, /* tp_weaklistoffset */
382 (getiterfunc) rpmps_iter, /* tp_iter */
383 (iternextfunc) rpmps_iternext, /* tp_iternext */
384 rpmps_methods, /* tp_methods */
385 0, /* tp_members */
386 0, /* tp_getset */
387 0, /* tp_base */
388 0, /* tp_dict */
389 0, /* tp_descr_get */
390 0, /* tp_descr_set */
391 0, /* tp_dictoffset */
392 (initproc) rpmps_init, /* tp_init */
393 (allocfunc) rpmps_alloc, /* tp_alloc */
394 (newfunc) rpmps_new, /* tp_new */
395 (freefunc) rpmps_free, /* tp_free */
396 0, /* tp_is_gc */
397 #endif
398 };
399 /*@=fullinitblock@*/
400
401 /* ---------- */
402
403 rpmps psFromPs(rpmpsObject * s)
404 {
405 return s->ps;
406 }
407
408 rpmpsObject *
409 rpmps_Wrap(rpmps ps)
410 {
411 rpmpsObject * s = PyObject_New(rpmpsObject, &rpmps_Type);
412
413 if (s == NULL)
414 return NULL;
415 s->ps = ps;
416 s->psi = NULL;
417 return s;
418 }
419 /*@=modunconnomods =evalorderuncon @*/