root/releases/pkgcore/0.2.10/src/depset.c @ ferringb%2540gmail.com-20070318211948-xn1jcpy4pfdiz1f3

Revision ferringb%2540gmail.com-20070318211948-xn1jcpy4pfdiz1f3, 17.2 KB (checked in by Brian Harring <ferringb@…>, 22 months ago)

another depset optimization; collapse and restrictions into the parent if it's an and restriction, addresses another issue with provides that will be tweaked further.

Line 
1/*
2 * Copyright: 2006-2007 Brian Harring <ferringb@gmail.com>
3 * License: GPL2
4 *
5 * C version of some of pkgcore (for extra speed).
6 */
7
8/* This does not really do anything since we do not use the "#"
9 * specifier in a PyArg_Parse or similar call, but hey, not using it
10 * means we are Py_ssize_t-clean too!
11 */
12
13#define PY_SSIZE_T_CLEAN
14
15#include <Python.h>
16#include "py24-compatibility.h"
17
18// exceptions, loaded during initialization.
19static PyObject *pkgcore_depset_ParseErrorExc = NULL;
20static PyObject *pkgcore_depset_ValContains = NULL;
21static PyObject *pkgcore_depset_PkgCond = NULL;
22static PyObject *pkgcore_depset_PkgAnd = NULL;
23static PyObject *pkgcore_depset_PkgOr = NULL;
24
25#define ISDIGIT(c) ('0' <= (c) && '9' >= (c))
26#define ISALPHA(c) (('a' <= (c) && 'z' >= (c)) || ('A' <= (c) && 'Z' >= (c)))
27#define ISLOWER(c) ('a' <= (c) && 'z' >= (c))
28#define ISALNUM(c) (ISALPHA(c) || ISDIGIT(c))
29
30static void
31_Err_SetParse(PyObject *dep_str, PyObject *msg, char *tok_start, char *tok_end)
32{
33    PyObject *ret;
34    PyObject *args = Py_BuildValue("(S)", dep_str);
35    if(!args)
36        return;
37    PyObject *kwds = Py_BuildValue("{sSss#}", "msg", msg,
38        "token", tok_start, tok_end - tok_start);
39    if(kwds) {
40        ret = PyObject_Call(pkgcore_depset_ParseErrorExc, args, kwds);
41        if(ret) {
42            PyErr_SetObject(pkgcore_depset_ParseErrorExc, ret);
43            Py_DECREF(ret);
44        }
45        Py_DECREF(kwds);
46    }
47    Py_DECREF(args);
48}
49
50static void
51Err_WrapException(PyObject *dep_str, char *tok_start,
52    char *tok_end)
53{
54    PyObject *type, *val, *tb;
55    PyErr_Fetch(&type, &val, &tb);
56    if(val) {
57        _Err_SetParse(dep_str, val, tok_start, tok_end);
58    }
59    Py_XDECREF(type);
60    Py_XDECREF(val);
61    Py_XDECREF(tb);
62}
63
64static void
65Err_SetParse(PyObject *dep_str, char *msg, char *tok_start, char *tok_end)
66{
67    PyObject *s = PyString_FromString(msg);
68    if(!s)
69        return;
70    _Err_SetParse(dep_str, s, tok_start, tok_end);
71    Py_DECREF(s);
72}
73
74static inline PyObject *
75make_use_conditional(char *use_start, char *use_end, PyObject *payload)
76{
77    PyObject *val;
78    if('!' == *use_start) {
79        PyObject *kwds = Py_BuildValue("{sO}", "negate", Py_True);
80        if(!kwds)
81            return NULL;
82        PyObject *args = Py_BuildValue("(s#)", use_start + 1,
83            use_end - use_start -1);
84        if(!args) {
85            Py_DECREF(kwds);
86            return NULL;
87        }
88        val = PyObject_Call(pkgcore_depset_ValContains, args, kwds);
89        Py_DECREF(args);
90        Py_DECREF(kwds);
91    } else {
92        val = PyObject_CallFunction(pkgcore_depset_ValContains, "s#",
93            use_start, use_end - use_start);
94    }
95    if(!val)
96        return NULL;
97
98    PyObject *restriction = PyObject_CallFunction(pkgcore_depset_PkgCond,
99        "sOO", "use", val, payload);
100    Py_DECREF(val);
101    return restriction;
102}
103
104#define SKIP_SPACES(ptr)     \
105while ('\t' == *(ptr) || ' ' == *(ptr) || '\n' == *(ptr)) (ptr)++;
106
107#define SKIP_NONSPACES(ptr)                                                  \
108while('\t' != *(ptr) && ' ' != *(ptr) && '\n' != *(ptr) && '\0' != *(ptr))  \
109    (ptr)++;
110
111#define ISSPACE(ptr) ('\t' == *(ptr) || ' ' == *(ptr) || '\n' == *(ptr))
112
113static PyObject *
114internal_parse_depset(PyObject *dep_str, char **ptr, int *has_conditionals,
115    PyObject *element_func,
116    PyObject *and_func, PyObject *or_func,
117    PyObject *parent_func,
118    char initial_frame)
119{
120    char *start = *ptr;
121    char *p = NULL;
122    PyObject *restrictions = NULL;
123    PyObject *item = NULL;
124    PyObject *tmp = NULL;
125    PyObject *kwds = NULL;
126
127    // should just use alloca here.
128
129    #define PARSE_DEPSET_STACK_STORAGE 16
130    PyObject *stack_restricts[PARSE_DEPSET_STACK_STORAGE];
131    Py_ssize_t item_count = 0, tup_size = PARSE_DEPSET_STACK_STORAGE;
132    Py_ssize_t item_size = 1;
133
134    SKIP_SPACES(start);
135    p = start;
136    while('\0' != *start) {
137        start = p;
138        SKIP_NONSPACES(p);
139        if('(' == *start) {
140            // new and frame.
141            if(!and_func) {
142                Err_SetParse(dep_str, "this depset doesn't support and blocks",
143                start, p);
144                goto internal_parse_depset_error;
145            }
146            if(p - start != 1) {
147                Err_SetParse(dep_str,
148                    "either a space or end of string is required after (",
149                    start, p);
150                goto internal_parse_depset_error;
151            }
152            if(!(tmp = internal_parse_depset(dep_str, &p, has_conditionals,
153                element_func, and_func, or_func, and_func, 0)))
154                goto internal_parse_depset_error;
155
156            if(tmp == Py_None) {
157                Py_DECREF(tmp);
158                Err_SetParse(dep_str, "empty payload", start, p);
159                goto internal_parse_depset_error;
160            } else if(!PyTuple_CheckExact(tmp)) {
161                item = tmp;
162            } else if (parent_func && and_func == parent_func) {
163                item = tmp;
164                item_size = PyTuple_GET_SIZE(item);
165            } else {
166                if(!(kwds = Py_BuildValue("{sO}", "finalize", Py_True))) {
167                    Py_DECREF(tmp);
168                    goto internal_parse_depset_error;
169                }
170
171                item = PyObject_Call(and_func, tmp, kwds);
172                Py_DECREF(kwds);
173                Py_DECREF(tmp);
174                if(!item)
175                    goto internal_parse_depset_error;
176            }
177
178        } else if(')' == *start) {
179            // end of a frame
180            if(initial_frame) {
181                Err_SetParse(dep_str, ") found without matching (",
182                    NULL, NULL);
183                goto internal_parse_depset_error;
184            }
185            if(p - start != 1) {
186                Err_SetParse(dep_str,
187                    "either a space or end of string is required after )",
188                    start, p);
189                goto internal_parse_depset_error;
190            }
191
192            if(!*p)
193                p--;
194            break;
195
196        } else if('?' == p[-1]) {
197            // use conditional
198            if (p - start == 1 || ('!' == *start && p - start == 2)) {
199                Err_SetParse(dep_str, "empty use conditional", start, p);
200                goto internal_parse_depset_error;
201            }
202            char *conditional_end = p - 1;
203            SKIP_SPACES(p);
204            if ('(' != *p) {
205                Err_SetParse(dep_str,
206                    "( has to be the next token for a conditional",
207                    start, p);
208                goto internal_parse_depset_error;
209            } else if(!ISSPACE(p + 1) || '\0' == p[1]) {
210                Err_SetParse(dep_str,
211                    "( has to be followed by whitespace",
212                    start, p);
213                goto internal_parse_depset_error;
214            }
215            p++;
216            if(!(tmp = internal_parse_depset(dep_str, &p, has_conditionals,
217                element_func, and_func, or_func, NULL, 0)))
218                goto internal_parse_depset_error;
219
220            if(tmp == Py_None) {
221                Py_DECREF(tmp);
222                Err_SetParse(dep_str, "empty payload", start, p);
223                goto internal_parse_depset_error;
224
225            } else if(!PyTuple_CheckExact(tmp)) {
226                item = PyTuple_New(1);
227                if(!tmp) {
228                    Py_DECREF(item);
229                    goto internal_parse_depset_error;
230                }
231                PyTuple_SET_ITEM(item, 0, tmp);
232                tmp = item;
233            }
234            item = make_use_conditional(start, conditional_end, tmp);
235            Py_DECREF(tmp);
236            if(!item)
237                goto internal_parse_depset_error;
238            *has_conditionals = 1;
239
240        } else if ('|' == *start) {
241            if('|' != start[1] || !or_func) {
242                Err_SetParse(dep_str,
243                    "stray |, or this depset doesn't support or blocks",
244                    NULL, NULL);
245                goto internal_parse_depset_error;
246            }
247
248            if(p - start != 2) {
249                Err_SetParse(dep_str, "|| must have space followed by a (",
250                    start, p);
251                goto internal_parse_depset_error;
252            }
253            SKIP_SPACES(p);
254            if ('(' != *p || (!ISSPACE(p + 1) && '\0' != p[1])) {
255                Err_SetParse(dep_str,
256                    "( has to be the next token for a conditional",
257                    start, p);
258                goto internal_parse_depset_error;
259            }
260            p++;
261            if(!(tmp = internal_parse_depset(dep_str, &p, has_conditionals,
262                element_func, and_func, or_func, NULL, 0)))
263                goto internal_parse_depset_error;
264
265            if(tmp == Py_None) {
266                Py_DECREF(tmp);
267                Err_SetParse(dep_str, "empty payload", start, p);
268                goto internal_parse_depset_error;
269            } else if (!PyTuple_CheckExact(tmp)) {
270                item = tmp;
271            } else {
272                if(!(kwds = Py_BuildValue("{sO}", "finalize", Py_True))) {
273                    Py_DECREF(tmp);
274                    goto internal_parse_depset_error;
275                }
276                item = PyObject_Call(or_func, tmp, kwds);
277                Py_DECREF(kwds);
278                Py_DECREF(tmp);
279                if(!item)
280                    goto internal_parse_depset_error;
281            }
282        } else {
283            char *ptr_s = start;
284            while (ptr_s < p) {
285                if('|' == *ptr_s || ')' == *ptr_s || '(' == *ptr_s) {
286                    Err_SetParse(dep_str,
287                        "stray character detected in item", start ,p);
288                    goto internal_parse_depset_error;
289                }
290                ptr_s++;
291            }
292            item = PyObject_CallFunction(element_func, "s#", start, p - start);
293            if(!item) {
294                Err_WrapException(dep_str, start, p);
295                goto internal_parse_depset_error;
296            }
297            assert(!PyErr_Occurred());
298        }
299
300        // append it.
301        if(item_count + item_size > tup_size) {
302            while(tup_size < item_count + item_size)
303                tup_size <<= 1;
304            if(!restrictions) {
305                // switch over.
306                if(!(restrictions = PyTuple_New(tup_size))) {
307                    Py_DECREF(item);
308                    goto internal_parse_depset_error;
309                }
310                Py_ssize_t x = 0;
311                for(; x < item_count; x++) {
312                    PyTuple_SET_ITEM(restrictions, x,
313                        stack_restricts[x]);
314                }
315            } else if(_PyTuple_Resize(&restrictions, tup_size)) {
316                Py_DECREF(item);
317                goto internal_parse_depset_error;
318            }
319            // now we're using restrictions.
320        }
321        if(restrictions) {
322            if(item_size == 1) {
323                PyTuple_SET_ITEM(restrictions, item_count++, item);
324            } else {
325                Py_ssize_t x = 0;
326                for(; x < item_size; x++) {
327                    Py_INCREF(PyTuple_GET_ITEM(item, x));
328                    PyTuple_SET_ITEM(restrictions, item_count + x,
329                        PyTuple_GET_ITEM(item, x));
330                }
331                item_count += x;
332                item_size = 1;
333                // we're done with the tuple, already stole the items from it.
334                Py_DECREF(item);
335            }
336        } else {
337            if(item_size == 1) {
338                stack_restricts[item_count++] = item;
339            } else {
340                Py_ssize_t x = 0;
341                for(;x < item_size; x++) {
342                    Py_INCREF(PyTuple_GET_ITEM(item, x));
343                    stack_restricts[item_count + x] = PyTuple_GET_ITEM(item, x);
344                }
345                item_count += item_size;
346                item_size = 1;
347                // we're done with the tuple, already stole the items from it.
348                Py_DECREF(item);
349            }
350        }
351        SKIP_SPACES(p);
352        start = p;
353    }
354
355    if(initial_frame) {
356        if(*p) {
357            Err_SetParse(dep_str, "stray ')' encountered", start, p);
358            goto internal_parse_depset_error;
359        }
360    } else {
361        if('\0' == *p) {
362            Err_SetParse(dep_str, "depset lacks closure", *ptr, p);
363            goto internal_parse_depset_error;
364        }
365        p++;
366    }
367
368    if(!restrictions) {
369        if(item_count == 0) {
370            restrictions = Py_None;
371            Py_INCREF(restrictions);
372        } else if(item_count == 1) {
373            restrictions = stack_restricts[0];
374        } else {
375            restrictions = PyTuple_New(item_count);
376            if(!restrictions)
377                goto internal_parse_depset_error;
378            Py_ssize_t x =0;
379            for(;x < item_count; x++) {
380                PyTuple_SET_ITEM(restrictions, x,
381                    stack_restricts[x]);
382            }
383        }
384    } else if(item_count < tup_size) {
385        if(_PyTuple_Resize(&restrictions, item_count))
386            goto internal_parse_depset_error;
387    }
388    *ptr = p;
389    return restrictions;
390
391    internal_parse_depset_error:
392    if(item_count) {
393        if(!restrictions) {
394            item_count--;
395            while(item_count >= 0) {
396                Py_DECREF(stack_restricts[item_count]);
397                item_count--;
398            }
399        } else
400            Py_DECREF(restrictions);
401    }
402    // dealloc.
403    return NULL;
404}
405
406static PyObject *
407pkgcore_parse_depset(PyObject *self, PyObject *args)
408{
409    PyObject *dep_str, *element_func;
410    PyObject *and_func = NULL, *or_func = NULL;
411    if(!PyArg_ParseTuple(args, "SO|OO", &dep_str, &element_func, &and_func,
412        &or_func))
413        return NULL;
414
415    int has_conditionals = 0;
416
417    if(and_func == Py_None)
418        and_func = NULL;
419    if(or_func == Py_None)
420        or_func = NULL;
421
422    char *p = PyString_AsString(dep_str);
423    if(!p)
424        return NULL;
425    PyObject *ret = internal_parse_depset(dep_str, &p, &has_conditionals,
426        element_func, and_func, or_func, and_func, 1);
427    if(!ret)
428        return NULL;
429    if(!PyTuple_Check(ret)) {
430        PyObject *tmp;
431        if(ret == Py_None) {
432            tmp = PyTuple_New(0);
433        } else {
434            tmp = PyTuple_New(1);
435            PyTuple_SET_ITEM(tmp, 0, ret);
436        }
437        if(!tmp) {
438            Py_DECREF(ret);
439            return NULL;
440        }
441        ret = tmp;
442    }
443    PyObject *conditionals_bool = has_conditionals ? Py_True : Py_False;
444    Py_INCREF(conditionals_bool);
445
446    PyObject *final = PyTuple_New(2);
447    if(!final) {
448        Py_DECREF(ret);
449        Py_DECREF(conditionals_bool);
450        return NULL;
451    }
452    PyTuple_SET_ITEM(final, 0, conditionals_bool);
453    PyTuple_SET_ITEM(final, 1, ret);
454    return final;
455}
456
457static PyMethodDef pkgcore_depset_methods[] = {
458    {"parse_depset", (PyCFunction)pkgcore_parse_depset, METH_VARARGS,
459        "initialize a depset instance"},
460    {NULL}
461};
462
463
464PyDoc_STRVAR(
465    pkgcore_depset_documentation,
466    "cpython depset parsing functionality");
467
468
469static int
470load_external_objects()
471{
472    PyObject *s, *m = NULL;
473    #define LOAD_MODULE(module)         \
474    s = PyString_FromString(module);    \
475    if(!s)                              \
476        return 1;                       \
477    m = PyImport_Import(s);             \
478    Py_DECREF(s);                       \
479    if(!m)                              \
480        return 1;
481
482    if(!pkgcore_depset_ParseErrorExc) {
483        LOAD_MODULE("pkgcore.ebuild.errors");
484        pkgcore_depset_ParseErrorExc = PyObject_GetAttrString(m,
485            "ParseError");
486        Py_DECREF(m);
487        if(!pkgcore_depset_ParseErrorExc) {
488            return 1;
489        }
490    }
491    if(!pkgcore_depset_ValContains) {
492        LOAD_MODULE("pkgcore.restrictions.values");
493        pkgcore_depset_ValContains = PyObject_GetAttrString(m,
494            "ContainmentMatch");
495        Py_DECREF(m);
496        if(!pkgcore_depset_ValContains)
497            return 1;
498    }
499    if(!pkgcore_depset_PkgCond) {
500        LOAD_MODULE("pkgcore.restrictions.packages");
501        pkgcore_depset_PkgCond = PyObject_GetAttrString(m,
502            "Conditional");
503        Py_DECREF(m);
504        if(!pkgcore_depset_PkgCond)
505            return 1;
506    }
507
508    if(!pkgcore_depset_PkgAnd || !pkgcore_depset_PkgOr) {
509        LOAD_MODULE("pkgcore.restrictions.boolean");
510    } else
511        m = NULL;
512
513    #undef LOAD_MODULE
514
515    #define LOAD_ATTR(ptr, attr)                            \
516    if(!(ptr)) {                                            \
517        if(!((ptr) = PyObject_GetAttrString(m, (attr)))) {  \
518            Py_DECREF(m);                                   \
519            return 1;                                       \
520        }                                                   \
521    }
522    LOAD_ATTR(pkgcore_depset_PkgAnd, "AndRestriction");
523    LOAD_ATTR(pkgcore_depset_PkgOr, "OrRestriction");
524    #undef LOAD_ATTR
525
526    Py_CLEAR(m);
527    return 0;
528}
529
530
531PyMODINIT_FUNC
532init_depset()
533{
534    // first get the exceptions we use.
535    if(load_external_objects())
536        /* XXX this returns *before* we called Py_InitModule3, so it
537         * triggers a SystemError. But if we initialize the module
538         * first python code can get at uninitialized pointers through
539         * our exported functions, which would be worse.
540         */
541         return;
542
543    if (!Py_InitModule3("_depset", pkgcore_depset_methods,
544                        pkgcore_depset_documentation))
545        return;
546
547    /* Success! */
548}
Note: See TracBrowser for help on using the browser.