| 1 | /* |
|---|
| 2 | * Copyright: 2006 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. |
|---|
| 19 | static PyObject *pkgcore_depset_ParseErrorExc = NULL; |
|---|
| 20 | static PyObject *pkgcore_depset_ValContains = NULL; |
|---|
| 21 | static PyObject *pkgcore_depset_PkgCond = NULL; |
|---|
| 22 | static PyObject *pkgcore_depset_PkgAnd = NULL; |
|---|
| 23 | static 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 | |
|---|
| 30 | static 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 | |
|---|
| 50 | static void |
|---|
| 51 | Err_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 | |
|---|
| 64 | static void |
|---|
| 65 | Err_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 | |
|---|
| 74 | static inline PyObject * |
|---|
| 75 | make_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 (PyObject *)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 (PyObject *)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 (PyObject *)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) \ |
|---|
| 105 | while ('\t' == *(ptr) || ' ' == *(ptr) || '\n' == *(ptr)) (ptr)++; |
|---|
| 106 | |
|---|
| 107 | #define SKIP_NONSPACES(ptr) \ |
|---|
| 108 | while('\t' != *(ptr) && ' ' != *(ptr) && '\n' != *(ptr) && '\0' != *(ptr)) \ |
|---|
| 109 | (ptr)++; |
|---|
| 110 | |
|---|
| 111 | #define ISSPACE(ptr) ('\t' == *(ptr) || ' ' == *(ptr) || '\n' == *(ptr)) |
|---|
| 112 | |
|---|
| 113 | static PyObject * |
|---|
| 114 | internal_parse_depset(PyObject *dep_str, char **ptr, int *has_conditionals, |
|---|
| 115 | PyObject *element_func, |
|---|
| 116 | PyObject *and_func, PyObject *or_func, |
|---|
| 117 | char initial_frame) |
|---|
| 118 | { |
|---|
| 119 | char *start = *ptr; |
|---|
| 120 | char *p = NULL; |
|---|
| 121 | PyObject *restrictions = NULL; |
|---|
| 122 | PyObject *item = NULL; |
|---|
| 123 | PyObject *tmp = NULL; |
|---|
| 124 | PyObject *kwds = NULL; |
|---|
| 125 | #define PARSE_DEPSET_STACK_STORAGE 16 |
|---|
| 126 | PyObject *stack_restricts[PARSE_DEPSET_STACK_STORAGE]; |
|---|
| 127 | Py_ssize_t item_count = 0, tup_size = PARSE_DEPSET_STACK_STORAGE; |
|---|
| 128 | |
|---|
| 129 | SKIP_SPACES(start); |
|---|
| 130 | p = start; |
|---|
| 131 | while('\0' != *start) { |
|---|
| 132 | start = p; |
|---|
| 133 | SKIP_NONSPACES(p); |
|---|
| 134 | if('(' == *start) { |
|---|
| 135 | // new and frame. |
|---|
| 136 | if(!and_func) { |
|---|
| 137 | Err_SetParse(dep_str, "this depset doesn't support and blocks", |
|---|
| 138 | start, p); |
|---|
| 139 | goto internal_parse_depset_error; |
|---|
| 140 | } |
|---|
| 141 | if(p - start != 1) { |
|---|
| 142 | Err_SetParse(dep_str, |
|---|
| 143 | "either a space or end of string is required after (", |
|---|
| 144 | start, p); |
|---|
| 145 | goto internal_parse_depset_error; |
|---|
| 146 | } |
|---|
| 147 | if(!(tmp = internal_parse_depset(dep_str, &p, has_conditionals, |
|---|
| 148 | element_func, and_func, or_func, 0))) |
|---|
| 149 | goto internal_parse_depset_error; |
|---|
| 150 | |
|---|
| 151 | if(tmp == Py_None) { |
|---|
| 152 | Py_DECREF(tmp); |
|---|
| 153 | Err_SetParse(dep_str, "empty payload", start, p); |
|---|
| 154 | goto internal_parse_depset_error; |
|---|
| 155 | } else if(!PyTuple_CheckExact(tmp)) { |
|---|
| 156 | item = tmp; |
|---|
| 157 | } else { |
|---|
| 158 | if(!(kwds = Py_BuildValue("{sO}", "finalize", Py_True))) { |
|---|
| 159 | Py_DECREF(tmp); |
|---|
| 160 | goto internal_parse_depset_error; |
|---|
| 161 | } |
|---|
| 162 | |
|---|
| 163 | item = PyObject_Call(and_func, tmp, kwds); |
|---|
| 164 | Py_DECREF(kwds); |
|---|
| 165 | Py_DECREF(tmp); |
|---|
| 166 | if(!item) |
|---|
| 167 | goto internal_parse_depset_error; |
|---|
| 168 | } |
|---|
| 169 | |
|---|
| 170 | } else if(')' == *start) { |
|---|
| 171 | // end of a frame |
|---|
| 172 | if(initial_frame) { |
|---|
| 173 | Err_SetParse(dep_str, ") found without matching (", |
|---|
| 174 | NULL, NULL); |
|---|
| 175 | goto internal_parse_depset_error; |
|---|
| 176 | } |
|---|
| 177 | if(p - start != 1) { |
|---|
| 178 | Err_SetParse(dep_str, |
|---|
| 179 | "either a space or end of string is required after )", |
|---|
| 180 | start, p); |
|---|
| 181 | goto internal_parse_depset_error; |
|---|
| 182 | } |
|---|
| 183 | |
|---|
| 184 | if(!*p) |
|---|
| 185 | p--; |
|---|
| 186 | break; |
|---|
| 187 | |
|---|
| 188 | } else if('?' == p[-1]) { |
|---|
| 189 | // use conditional |
|---|
| 190 | if (p - start == 1 || ('!' == *start && p - start == 2)) { |
|---|
| 191 | Err_SetParse(dep_str, "empty use conditional", start, p); |
|---|
| 192 | goto internal_parse_depset_error; |
|---|
| 193 | } |
|---|
| 194 | char *conditional_end = p - 1; |
|---|
| 195 | SKIP_SPACES(p); |
|---|
| 196 | if ('(' != *p) { |
|---|
| 197 | Err_SetParse(dep_str, |
|---|
| 198 | "( has to be the next token for a conditional", |
|---|
| 199 | start, p); |
|---|
| 200 | goto internal_parse_depset_error; |
|---|
| 201 | } else if(!ISSPACE(p + 1) || '\0' == p[1]) { |
|---|
| 202 | Err_SetParse(dep_str, |
|---|
| 203 | "( has to be followed by whitespace", |
|---|
| 204 | start, p); |
|---|
| 205 | goto internal_parse_depset_error; |
|---|
| 206 | } |
|---|
| 207 | p++; |
|---|
| 208 | if(!(tmp = internal_parse_depset(dep_str, &p, has_conditionals, |
|---|
| 209 | element_func, and_func, or_func, 0))) |
|---|
| 210 | goto internal_parse_depset_error; |
|---|
| 211 | |
|---|
| 212 | if(tmp == Py_None) { |
|---|
| 213 | Py_DECREF(tmp); |
|---|
| 214 | Err_SetParse(dep_str, "empty payload", start, p); |
|---|
| 215 | goto internal_parse_depset_error; |
|---|
| 216 | |
|---|
| 217 | } else if(!PyTuple_CheckExact(tmp)) { |
|---|
| 218 | item = PyTuple_New(1); |
|---|
| 219 | if(!tmp) { |
|---|
| 220 | Py_DECREF(item); |
|---|
| 221 | goto internal_parse_depset_error; |
|---|
| 222 | } |
|---|
| 223 | PyTuple_SET_ITEM(item, 0, tmp); |
|---|
| 224 | tmp = item; |
|---|
| 225 | } |
|---|
| 226 | item = make_use_conditional(start, conditional_end, tmp); |
|---|
| 227 | Py_DECREF(tmp); |
|---|
| 228 | if(!item) |
|---|
| 229 | goto internal_parse_depset_error; |
|---|
| 230 | *has_conditionals = 1; |
|---|
| 231 | |
|---|
| 232 | } else if ('|' == *start) { |
|---|
| 233 | if('|' != start[1] || !or_func) { |
|---|
| 234 | Err_SetParse(dep_str, |
|---|
| 235 | "stray |, or this depset doesn't support or blocks", |
|---|
| 236 | NULL, NULL); |
|---|
| 237 | goto internal_parse_depset_error; |
|---|
| 238 | } |
|---|
| 239 | |
|---|
| 240 | if(p - start != 2) { |
|---|
| 241 | Err_SetParse(dep_str, "|| must have space followed by a (", |
|---|
| 242 | start, p); |
|---|
| 243 | goto internal_parse_depset_error; |
|---|
| 244 | } |
|---|
| 245 | SKIP_SPACES(p); |
|---|
| 246 | if ('(' != *p || (!ISSPACE(p + 1) && '\0' != p[1])) { |
|---|
| 247 | Err_SetParse(dep_str, |
|---|
| 248 | "( has to be the next token for a conditional", |
|---|
| 249 | start, p); |
|---|
| 250 | goto internal_parse_depset_error; |
|---|
| 251 | } |
|---|
| 252 | p++; |
|---|
| 253 | if(!(tmp = internal_parse_depset(dep_str, &p, has_conditionals, |
|---|
| 254 | element_func, and_func, or_func, 0))) |
|---|
| 255 | goto internal_parse_depset_error; |
|---|
| 256 | |
|---|
| 257 | if(tmp == Py_None) { |
|---|
| 258 | Py_DECREF(tmp); |
|---|
| 259 | Err_SetParse(dep_str, "empty payload", start, p); |
|---|
| 260 | goto internal_parse_depset_error; |
|---|
| 261 | } else if (!PyTuple_CheckExact(tmp)) { |
|---|
| 262 | item = tmp; |
|---|
| 263 | } else { |
|---|
| 264 | if(!(kwds = Py_BuildValue("{sO}", "finalize", Py_True))) { |
|---|
| 265 | Py_DECREF(tmp); |
|---|
| 266 | goto internal_parse_depset_error; |
|---|
| 267 | } |
|---|
| 268 | item = PyObject_Call(or_func, tmp, kwds); |
|---|
| 269 | Py_DECREF(kwds); |
|---|
| 270 | Py_DECREF(tmp); |
|---|
| 271 | if(!item) |
|---|
| 272 | goto internal_parse_depset_error; |
|---|
| 273 | } |
|---|
| 274 | } else { |
|---|
| 275 | char *ptr_s = start; |
|---|
| 276 | while (ptr_s < p) { |
|---|
| 277 | if('|' == *ptr_s || ')' == *ptr_s || '(' == *ptr_s) { |
|---|
| 278 | Err_SetParse(dep_str, |
|---|
| 279 | "stray character detected in item", start ,p); |
|---|
| 280 | goto internal_parse_depset_error; |
|---|
| 281 | } |
|---|
| 282 | ptr_s++; |
|---|
| 283 | } |
|---|
| 284 | item = PyObject_CallFunction(element_func, "s#", start, p - start); |
|---|
| 285 | if(!item) { |
|---|
| 286 | Err_WrapException(dep_str, start, p); |
|---|
| 287 | goto internal_parse_depset_error; |
|---|
| 288 | } |
|---|
| 289 | assert(!PyErr_Occurred()); |
|---|
| 290 | } |
|---|
| 291 | |
|---|
| 292 | // append it. |
|---|
| 293 | if(item_count == tup_size) { |
|---|
| 294 | if(!restrictions) { |
|---|
| 295 | // switch over. |
|---|
| 296 | restrictions = PyTuple_New(PARSE_DEPSET_STACK_STORAGE << 1); |
|---|
| 297 | if(!restrictions) { |
|---|
| 298 | Py_DECREF(item); |
|---|
| 299 | goto internal_parse_depset_error; |
|---|
| 300 | } |
|---|
| 301 | for(item_count=0; item_count < PARSE_DEPSET_STACK_STORAGE; |
|---|
| 302 | item_count++) { |
|---|
| 303 | PyTuple_SET_ITEM(restrictions, item_count, |
|---|
| 304 | stack_restricts[item_count]); |
|---|
| 305 | } |
|---|
| 306 | } else if(_PyTuple_Resize(&restrictions, tup_size * 2)) { |
|---|
| 307 | Py_DECREF(item); |
|---|
| 308 | goto internal_parse_depset_error; |
|---|
| 309 | } |
|---|
| 310 | tup_size *= 2; |
|---|
| 311 | // now we're using restrictions. |
|---|
| 312 | } |
|---|
| 313 | if(restrictions) { |
|---|
| 314 | PyTuple_SET_ITEM(restrictions, item_count, item); |
|---|
| 315 | } else { |
|---|
| 316 | stack_restricts[item_count] = item; |
|---|
| 317 | } |
|---|
| 318 | item_count++; |
|---|
| 319 | SKIP_SPACES(p); |
|---|
| 320 | start = p; |
|---|
| 321 | } |
|---|
| 322 | |
|---|
| 323 | if(initial_frame) { |
|---|
| 324 | if(*p) { |
|---|
| 325 | Err_SetParse(dep_str, "stray ')' encountered", start, p); |
|---|
| 326 | goto internal_parse_depset_error; |
|---|
| 327 | } |
|---|
| 328 | } else { |
|---|
| 329 | if('\0' == *p) { |
|---|
| 330 | Err_SetParse(dep_str, "depset lacks closure", *ptr, p); |
|---|
| 331 | goto internal_parse_depset_error; |
|---|
| 332 | } |
|---|
| 333 | p++; |
|---|
| 334 | } |
|---|
| 335 | |
|---|
| 336 | if(!restrictions) { |
|---|
| 337 | if(item_count == 0) { |
|---|
| 338 | restrictions = Py_None; |
|---|
| 339 | Py_INCREF(restrictions); |
|---|
| 340 | } else if(item_count == 1) { |
|---|
| 341 | restrictions = stack_restricts[0]; |
|---|
| 342 | } else { |
|---|
| 343 | restrictions = PyTuple_New(item_count); |
|---|
| 344 | if(!restrictions) |
|---|
| 345 | goto internal_parse_depset_error; |
|---|
| 346 | item_count--; |
|---|
| 347 | while(item_count >= 0) { |
|---|
| 348 | PyTuple_SET_ITEM(restrictions, item_count, |
|---|
| 349 | stack_restricts[item_count]); |
|---|
| 350 | item_count--; |
|---|
| 351 | } |
|---|
| 352 | } |
|---|
| 353 | } else if(item_count < tup_size) { |
|---|
| 354 | if(_PyTuple_Resize(&restrictions, item_count)) |
|---|
| 355 | goto internal_parse_depset_error; |
|---|
| 356 | } |
|---|
| 357 | *ptr = p; |
|---|
| 358 | return restrictions; |
|---|
| 359 | |
|---|
| 360 | internal_parse_depset_error: |
|---|
| 361 | if(item_count) { |
|---|
| 362 | if(!restrictions) { |
|---|
| 363 | item_count--; |
|---|
| 364 | while(item_count >= 0) { |
|---|
| 365 | Py_DECREF(stack_restricts[item_count]); |
|---|
| 366 | item_count--; |
|---|
| 367 | } |
|---|
| 368 | } else |
|---|
| 369 | Py_DECREF(restrictions); |
|---|
| 370 | } |
|---|
| 371 | // dealloc. |
|---|
| 372 | return (PyObject *)NULL; |
|---|
| 373 | } |
|---|
| 374 | |
|---|
| 375 | static PyObject * |
|---|
| 376 | pkgcore_parse_depset(PyObject *self, PyObject *args) |
|---|
| 377 | { |
|---|
| 378 | PyObject *dep_str, *element_func; |
|---|
| 379 | PyObject *and_func = NULL, *or_func = NULL; |
|---|
| 380 | if(!PyArg_ParseTuple(args, "SO|OO", &dep_str, &element_func, &and_func, |
|---|
| 381 | &or_func)) |
|---|
| 382 | return (PyObject *)NULL; |
|---|
| 383 | |
|---|
| 384 | int has_conditionals = 0; |
|---|
| 385 | |
|---|
| 386 | if(and_func == Py_None) |
|---|
| 387 | and_func = NULL; |
|---|
| 388 | if(or_func == Py_None) |
|---|
| 389 | or_func = NULL; |
|---|
| 390 | |
|---|
| 391 | char *p = PyString_AsString(dep_str); |
|---|
| 392 | if(!p) |
|---|
| 393 | return (PyObject *)NULL; |
|---|
| 394 | PyObject *ret = internal_parse_depset(dep_str, &p, &has_conditionals, |
|---|
| 395 | element_func, and_func, or_func, 1); |
|---|
| 396 | if(!ret) |
|---|
| 397 | return (PyObject *)NULL; |
|---|
| 398 | if(!PyTuple_Check(ret)) { |
|---|
| 399 | PyObject *tmp; |
|---|
| 400 | if(ret == Py_None) { |
|---|
| 401 | tmp = PyTuple_New(0); |
|---|
| 402 | } else { |
|---|
| 403 | tmp = PyTuple_New(1); |
|---|
| 404 | PyTuple_SET_ITEM(tmp, 0, ret); |
|---|
| 405 | } |
|---|
| 406 | if(!tmp) { |
|---|
| 407 | Py_DECREF(ret); |
|---|
| 408 | return NULL; |
|---|
| 409 | } |
|---|
| 410 | ret = tmp; |
|---|
| 411 | } |
|---|
| 412 | PyObject *conditionals_bool = has_conditionals ? Py_True : Py_False; |
|---|
| 413 | Py_INCREF(conditionals_bool); |
|---|
| 414 | |
|---|
| 415 | PyObject *final = PyTuple_New(2); |
|---|
| 416 | if(!final) { |
|---|
| 417 | Py_DECREF(ret); |
|---|
| 418 | Py_DECREF(conditionals_bool); |
|---|
| 419 | return (PyObject *)NULL; |
|---|
| 420 | } |
|---|
| 421 | PyTuple_SET_ITEM(final, 0, conditionals_bool); |
|---|
| 422 | PyTuple_SET_ITEM(final, 1, ret); |
|---|
| 423 | return final; |
|---|
| 424 | } |
|---|
| 425 | |
|---|
| 426 | static PyMethodDef pkgcore_depset_methods[] = { |
|---|
| 427 | {"parse_depset", (PyCFunction)pkgcore_parse_depset, METH_VARARGS, |
|---|
| 428 | "initialize a depset instance"}, |
|---|
| 429 | {NULL} |
|---|
| 430 | }; |
|---|
| 431 | |
|---|
| 432 | |
|---|
| 433 | PyDoc_STRVAR( |
|---|
| 434 | pkgcore_depset_documentation, |
|---|
| 435 | "cpython depset parsing functionality"); |
|---|
| 436 | |
|---|
| 437 | |
|---|
| 438 | static int |
|---|
| 439 | load_external_objects() |
|---|
| 440 | { |
|---|
| 441 | PyObject *s, *m = NULL; |
|---|
| 442 | #define LOAD_MODULE(module) \ |
|---|
| 443 | s = PyString_FromString(module); \ |
|---|
| 444 | if(!s) \ |
|---|
| 445 | return 1; \ |
|---|
| 446 | m = PyImport_Import(s); \ |
|---|
| 447 | Py_DECREF(s); \ |
|---|
| 448 | if(!m) \ |
|---|
| 449 | return 1; |
|---|
| 450 | |
|---|
| 451 | if(!pkgcore_depset_ParseErrorExc) { |
|---|
| 452 | LOAD_MODULE("pkgcore.ebuild.errors"); |
|---|
| 453 | pkgcore_depset_ParseErrorExc = PyObject_GetAttrString(m, |
|---|
| 454 | "ParseError"); |
|---|
| 455 | Py_DECREF(m); |
|---|
| 456 | if(!pkgcore_depset_ParseErrorExc) { |
|---|
| 457 | return 1; |
|---|
| 458 | } |
|---|
| 459 | } |
|---|
| 460 | if(!pkgcore_depset_ValContains) { |
|---|
| 461 | LOAD_MODULE("pkgcore.restrictions.values"); |
|---|
| 462 | pkgcore_depset_ValContains = PyObject_GetAttrString(m, |
|---|
| 463 | "ContainmentMatch"); |
|---|
| 464 | Py_DECREF(m); |
|---|
| 465 | if(!pkgcore_depset_ValContains) |
|---|
| 466 | return 1; |
|---|
| 467 | } |
|---|
| 468 | if(!pkgcore_depset_PkgCond) { |
|---|
| 469 | LOAD_MODULE("pkgcore.restrictions.packages"); |
|---|
| 470 | pkgcore_depset_PkgCond = PyObject_GetAttrString(m, |
|---|
| 471 | "Conditional"); |
|---|
| 472 | Py_DECREF(m); |
|---|
| 473 | if(!pkgcore_depset_PkgCond) |
|---|
| 474 | return 1; |
|---|
| 475 | } |
|---|
| 476 | |
|---|
| 477 | if(!pkgcore_depset_PkgAnd || !pkgcore_depset_PkgOr) { |
|---|
| 478 | LOAD_MODULE("pkgcore.restrictions.boolean"); |
|---|
| 479 | } else |
|---|
| 480 | m = NULL; |
|---|
| 481 | |
|---|
| 482 | #undef LOAD_MODULE |
|---|
| 483 | |
|---|
| 484 | #define LOAD_ATTR(ptr, attr) \ |
|---|
| 485 | if(!(ptr)) { \ |
|---|
| 486 | if(!((ptr) = PyObject_GetAttrString(m, (attr)))) { \ |
|---|
| 487 | Py_DECREF(m); \ |
|---|
| 488 | return 1; \ |
|---|
| 489 | } \ |
|---|
| 490 | } |
|---|
| 491 | LOAD_ATTR(pkgcore_depset_PkgAnd, "AndRestriction"); |
|---|
| 492 | LOAD_ATTR(pkgcore_depset_PkgOr, "OrRestriction"); |
|---|
| 493 | #undef LOAD_ATTR |
|---|
| 494 | |
|---|
| 495 | Py_CLEAR(m); |
|---|
| 496 | return 0; |
|---|
| 497 | } |
|---|
| 498 | |
|---|
| 499 | |
|---|
| 500 | PyMODINIT_FUNC |
|---|
| 501 | init_depset() |
|---|
| 502 | { |
|---|
| 503 | // first get the exceptions we use. |
|---|
| 504 | if(load_external_objects()) |
|---|
| 505 | /* XXX this returns *before* we called Py_InitModule3, so it |
|---|
| 506 | * triggers a SystemError. But if we initialize the module |
|---|
| 507 | * first python code can get at uninitialized pointers through |
|---|
| 508 | * our exported functions, which would be worse. |
|---|
| 509 | */ |
|---|
| 510 | return; |
|---|
| 511 | |
|---|
| 512 | if (!Py_InitModule3("_depset", pkgcore_depset_methods, |
|---|
| 513 | pkgcore_depset_documentation)) |
|---|
| 514 | return; |
|---|
| 515 | |
|---|
| 516 | /* Success! */ |
|---|
| 517 | } |
|---|