rpm  4.5
rpmrc-py.c
Go to the documentation of this file.
1 
5 #include "system.h"
6 
7 #include "structmember.h"
8 
9 #include "rpmdebug-py.c"
10 
11 #include <rpmcli.h>
12 
13 #include "rpmrc-py.h"
14 
15 #include "debug.h"
16 
17 /*@unchecked@*/
18 static int _rc_debug = 0;
19 
29 
32 PyObject *
33 rpmrc_AddMacro(/*@unused@*/ PyObject * self, PyObject * args, PyObject * kwds)
34 {
35  char * name, * val;
36  char * kwlist[] = {"name", "value", NULL};
37 
38  if (!PyArg_ParseTupleAndKeywords(args, kwds, "ss:AddMacro", kwlist,
39  &name, &val))
40  return NULL;
41 
42  addMacro(NULL, name, NULL, val, -1);
43 
44  Py_INCREF(Py_None);
45  return Py_None;
46 }
47 
50 PyObject *
51 rpmrc_DelMacro(/*@unused@*/ PyObject * self, PyObject * args, PyObject * kwds)
52 {
53  char * name;
54  char * kwlist[] = {"name", NULL};
55 
56  if (!PyArg_ParseTupleAndKeywords(args, kwds, "s:DelMacro", kwlist, &name))
57  return NULL;
58 
59  delMacro(NULL, name);
60 
61  Py_INCREF(Py_None);
62  return Py_None;
63 }
64 
67 #if Py_TPFLAGS_HAVE_ITER /* XXX backport to python-1.5.2 */
68 
70 static void rpmrc_dealloc(PyObject * s)
71  /*@*/
72 {
73 if (_rc_debug)
74 fprintf(stderr, "*** rpmrc_dealloc(%p[%s])\n", s, lbl(s));
75  PyDict_Type.tp_dealloc(s);
76 }
77 
80 static int rpmrc_print(PyObject * s, FILE *fp, int flags)
81  /*@*/
82 {
83 /*@-formattype@*/
84 if (_rc_debug)
85 fprintf(stderr, "*** rpmrc_print(%p[%s],%p,%x)\n", s, lbl(s), fp, flags);
86 /*@=formattype@*/
87  return PyDict_Type.tp_print(s, fp, flags);
88 }
89 
92 static int rpmrc_compare(PyObject * a, PyObject * b)
93  /*@*/
94 {
95 if (_rc_debug)
96 fprintf(stderr, "*** rpmrc_compare(%p[%s],%p[%s])\n", a, lbl(a), b, lbl(b));
97  return PyDict_Type.tp_compare(a, b);
98 }
99 
102 static PyObject * rpmrc_repr(PyObject * s)
103  /*@*/
104 {
105 if (_rc_debug)
106 fprintf(stderr, "*** rpmrc_repr(%p[%s])\n", s, lbl(s));
107  return PyDict_Type.tp_repr(s);
108 }
109 
112 static long rpmrc_hash(PyObject * s)
113  /*@*/
114 {
115  /* XXX dict objects are unhashable */
116 if (_rc_debug)
117 fprintf(stderr, "*** rpmrc_hash(%p[%s])\n", s, lbl(s));
118  return PyDict_Type.tp_hash(s);
119 }
120 
123 static int
124 rpmrc_length(PyObject * s)
125  /*@*/
126 {
127 if (_rc_debug)
128 fprintf(stderr, "*** rpmrc_length(%p[%s])\n", s, lbl(s));
129  return PyDict_Type.tp_as_mapping->mp_length(s);
130 }
131 
134 static PyObject *
135 rpmrc_subscript(PyObject * s, PyObject * key)
136  /*@*/
137 {
138 if (_rc_debug)
139 fprintf(stderr, "*** rpmrc_subscript(%p[%s], %p[%s])\n", s, lbl(s), key, lbl(key));
140  return PyDict_Type.tp_as_mapping->mp_subscript(s, key);
141 }
142 
145 static int
146 rpmrc_ass_subscript(PyObject * s, PyObject * key, PyObject * value)
147  /*@*/
148 {
149 if (_rc_debug)
150 fprintf(stderr, "*** rpmrc_ass_subscript(%p[%s], %p[%s], %p[%s])\n", s, lbl(s), key, lbl(key), value, lbl(value));
151  return PyDict_Type.tp_as_mapping->mp_ass_subscript(s, key, value);
152 }
153 
154 /*@unchecked@*/ /*@observer@*/
155 static PyMappingMethods rpmrc_as_mapping = {
156  rpmrc_length, /* mp_length */
157  rpmrc_subscript, /* mp_subscript */
158  rpmrc_ass_subscript, /* mp_ass_subscript */
159 };
160 
163 static PyObject * rpmrc_getattro (PyObject *s, PyObject *name)
164  /*@*/
165 {
166 if (_rc_debug)
167 fprintf(stderr, "*** rpmrc_getattro(%p[%s], \"%s\")\n", s, lbl(s), PyString_AS_STRING(name));
168  return PyObject_GenericGetAttr(s, name);
169 }
170 
173 static int rpmrc_setattro (PyObject *s, PyObject *name, PyObject * value)
174  /*@*/
175 {
176 if (_rc_debug)
177 fprintf(stderr, "*** rpmrc_setattro(%p[%s], \"%s \", \"%s\")\n", s, lbl(s), PyString_AS_STRING(name), PyString_AS_STRING(value));
178  return PyDict_Type.tp_setattro(s, name, value);
179 }
180 
183 /*@unchecked@*/ /*@observer@*/
184 static char rpmrc_doc[] =
185 "";
186 
189 static int rpmrc_traverse(PyObject * s, visitproc visit, void *arg)
190  /*@*/
191 {
192 if (_rc_debug)
193 fprintf(stderr, "*** rpmrc_traverse(%p[%s],%p,%p)\n", s, lbl(s), visit, arg);
194  return PyDict_Type.tp_traverse(s, visit, arg);
195 }
196 
199 static int rpmrc_clear(PyObject * s)
200  /*@*/
201 {
202 if (_rc_debug)
203 fprintf(stderr, "*** rpmrc_clear(%p[%s])\n", s, lbl(s));
204  return PyDict_Type.tp_clear(s);
205 }
206 
209 static PyObject * rpmrc_richcompare(PyObject * v, PyObject * w, int op)
210  /*@*/
211 {
212 if (_rc_debug)
213 fprintf(stderr, "*** rpmrc_richcompare(%p[%s],%p[%s],%x)\n", v, lbl(v), w, lbl(w), op);
214  return PyDict_Type.tp_richcompare(v, w, op);
215 }
216 
217 #if PY_MAJOR_VERSION == 2 && PY_MINOR_VERSION < 4
218 
220 static PyObject * rpmrc_iter(PyObject * s)
221  /*@*/
222 {
223 if (_rc_debug)
224 fprintf(stderr, "*** rpmrc_iter(%p[%s])\n", s, lbl(s));
225  if (s->ob_type == &PyDictIter_Type)
226  return PyDictIter_Type.tp_iter(s);
227  return PyDict_Type.tp_iter(s);
228 }
229 
232 /*@null@*/
233 static PyObject * rpmrc_iternext(PyObject * s)
234  /*@*/
235 {
236 if (_rc_debug)
237 fprintf(stderr, "*** rpmrc_iternext(%p[%s])\n", s, lbl(s));
238  if (s->ob_type == &PyDictIter_Type)
239  return PyDictIter_Type.tp_iternext(s);
240  return NULL;
241 }
242 
245 /*@null@*/
246 /* XXX: does this _actually_ take any arguments? I don't think it does... */
247 static PyObject * rpmrc_next(PyObject * s, PyObject *args)
248  /*@*/
249 {
250 if (_rc_debug)
251 fprintf(stderr, "*** rpmrc_next(%p[%s],%p)\n", s, lbl(s), args);
252  if (s->ob_type == &PyDictIter_Type)
253  return PyDictIter_Type.tp_methods[0].ml_meth(s, args);
254  return NULL;
255 }
256 #else
257 #define rpmrc_iter 0
258 #define rpmrc_iternext 0
259 #endif
260 
263 static int rpmrc_init(PyObject * s, PyObject *args, PyObject *kwds)
264  /*@*/
265 {
266 if (_rc_debug)
267 fprintf(stderr, "*** rpmrc_init(%p[%s],%p,%p)\n", s, lbl(s), args, kwds);
268  if (PyDict_Type.tp_init(s, args, kwds) < 0)
269  return -1;
270  return 0;
271 }
272 
275 static void rpmrc_free(PyObject * s)
276  /*@*/
277 {
278 if (_rc_debug)
279 fprintf(stderr, "*** rpmrc_free(%p[%s])\n", s, lbl(s));
280  _PyObject_GC_Del(s);
281 }
282 
285 static PyObject * rpmrc_alloc(PyTypeObject * subtype, int nitems)
286  /*@*/
287 {
288  PyObject * ns = PyType_GenericAlloc(subtype, nitems);
289 
290 if (_rc_debug)
291 fprintf(stderr, "*** rpmrc_alloc(%p[%s},%d) ret %p[%s]\n", subtype, lbl(subtype), nitems, ns, lbl(ns));
292  return (PyObject *) ns;
293 }
294 
297 /*@null@*/
298 static PyObject * rpmrc_new(PyTypeObject * subtype, PyObject *args, PyObject *kwds)
299  /*@*/
300 {
301  PyObject * ns;
302 
303  /* Derive an initialized dictionary of the appropriate size. */
304  ns = PyDict_Type.tp_new(&rpmrc_Type, args, kwds);
305 
306  /* Perform additional initialization. */
307  if (rpmrc_init(ns, args, kwds) < 0) {
308  rpmrc_free(ns);
309  return NULL;
310  }
311 
312 if (_rc_debug)
313 fprintf(stderr, "*** rpmrc_new(%p[%s],%p,%p) ret %p[%s]\n", subtype, lbl(subtype), args, kwds, ns, lbl(ns));
314  return ns;
315 }
316 #endif
317 
320 /*@-fullinitblock@*/
321 /*@unchecked@*/ /*@observer@*/
322 static struct PyMethodDef rpmrc_methods[] = {
323  { "addMacro", (PyCFunction) rpmrc_AddMacro, METH_VARARGS|METH_KEYWORDS,
324  NULL },
325  { "delMacro", (PyCFunction) rpmrc_DelMacro, METH_VARARGS|METH_KEYWORDS,
326  NULL },
327 #if Py_TPFLAGS_HAVE_ITER && PY_MAJOR_VERSION == 2 && PY_MINOR_VERSION < 4
328  { "next", (PyCFunction) rpmrc_next, METH_VARARGS,
329  "next() -- get the next value, or raise StopIteration"},
330 #endif
331  {NULL, NULL} /* sentinel */
332 };
333 /*@=fullinitblock@*/
334 
337 /*@-fullinitblock@*/
338 #if Py_TPFLAGS_HAVE_ITER
339 PyTypeObject rpmrc_Type = {
340  PyObject_HEAD_INIT(&PyType_Type)
341  0, /* ob_size */
342  "rpm.rc", /* tp_name */
343  sizeof(rpmrcObject), /* tp_size */
344  0, /* tp_itemsize */
345  (destructor) rpmrc_dealloc, /* tp_dealloc */
346  rpmrc_print, /* tp_print */
347  0, /* tp_getattr */
348  0, /* tp_setattr */
349  rpmrc_compare, /* tp_compare */
350  rpmrc_repr, /* tp_repr */
351  0, /* tp_as_number */
352  0, /* tp_as_sequence */
353  &rpmrc_as_mapping, /* tp_as_mapping */
354  rpmrc_hash, /* tp_hash */
355  0, /* tp_call */
356  0, /* tp_str */
357  (getattrofunc) rpmrc_getattro, /* tp_getattro */
358  (setattrofunc) rpmrc_setattro, /* tp_setattro */
359  0, /* tp_as_buffer */
360  Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_BASETYPE, /* tp_flags */
361  rpmrc_doc, /* tp_doc */
362  rpmrc_traverse, /* tp_traverse */
363  rpmrc_clear, /* tp_clear */
364  rpmrc_richcompare, /* tp_richcompare */
365  0, /* tp_weaklistoffset */
366  rpmrc_iter, /* tp_iter */
367  rpmrc_iternext, /* tp_iternext */
368  rpmrc_methods, /* tp_methods */
369  0, /* tp_members */
370  0, /* tp_getset */
371  &PyDict_Type, /* tp_base */
372  0, /* tp_dict */
373  0, /* tp_descr_get */
374  0, /* tp_descr_set */
375  0, /* tp_dictoffset */
376  rpmrc_init, /* tp_init */
377  rpmrc_alloc, /* tp_alloc */
378  rpmrc_new, /* tp_new */
379  rpmrc_free, /* tp_free */
380  0, /* tp_is_gc */
381 };
382 #else
383 PyTypeObject rpmrc_Type = {
384  PyObject_HEAD_INIT(&PyType_Type)
385  0, /* ob_size */
386  "rpm.rc", /* tp_name */
387  sizeof(rpmrcObject), /* tp_size */
388  0, /* tp_itemsize */
389  0, /* tp_dealloc */
390  0, /* tp_print */
391  0, /* tp_getattr */
392  0, /* tp_setattr */
393  0, /* tp_compare */
394  0, /* tp_repr */
395  0, /* tp_as_number */
396  0, /* tp_as_sequence */
397  0, /* tp_as_mapping */
398  0, /* tp_hash */
399  0, /* tp_call */
400  0, /* tp_str */
401  0, /* tp_getattro */
402  0, /* tp_setattro */
403  0, /* tp_as_buffer */
404  0, /* tp_flags */
405  0 /* tp_doc */
406 };
407 #endif
408 /*@=fullinitblock@*/
409 
410 #if Py_TPFLAGS_HAVE_ITER
411 PyObject * rpmrc_Create(/*@unused@*/ PyObject * self, PyObject *args, PyObject *kwds)
412 {
413  return rpmrc_new(&rpmrc_Type, args, kwds);
414 }
415 #endif