~jbaker/storm/nose_plugin

« back to all changes in this revision

Viewing changes to storm/cextensions.c

Merging fix for reference leak and C89-related changes,
provided by James.

Show diffs side-by-side

added added

removed removed

Lines of Context:
250
250
{
251
251
    static char *kwlist[] = {"owner", NULL};
252
252
    PyObject *owner;
 
253
    int result = -1;
253
254
 
254
255
    if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O", kwlist, &owner))
255
256
        return -1;
256
257
 
257
 
    int result = -1;
258
 
 
259
258
    /* self._owner_ref = weakref.ref(owner) */
260
259
    self->_owner_ref = PyWeakref_NewRef(owner, NULL);
261
260
    if (self->_owner_ref) {
296
295
EventSystem_hook(EventSystemObject *self, PyObject *args)
297
296
{
298
297
    PyObject *result = NULL;
 
298
    PyObject *name, *callback, *data;
299
299
 
300
300
    if (PyTuple_GET_SIZE(args) < 2) {
301
301
        PyErr_SetString(PyExc_TypeError, "Invalid number of arguments");
302
302
        return NULL;
303
303
    }
304
304
 
305
 
    PyObject *name = PyTuple_GET_ITEM(args, 0);
306
 
    PyObject *callback = PyTuple_GET_ITEM(args, 1);
307
 
    PyObject *data = PyTuple_GetSlice(args, 2, PyTuple_GET_SIZE(args));
 
305
    name = PyTuple_GET_ITEM(args, 0);
 
306
    callback = PyTuple_GET_ITEM(args, 1);
 
307
    data = PyTuple_GetSlice(args, 2, PyTuple_GET_SIZE(args));
308
308
    if (data) {
309
 
        /* 
 
309
        /*
310
310
           callbacks = self._hooks.get(name)
311
311
           if callbacks is None:
312
312
               self._hooks.setdefault(name, set()).add((callback, data))
351
351
EventSystem_unhook(EventSystemObject *self, PyObject *args)
352
352
{
353
353
    PyObject *result = NULL;
 
354
    PyObject *name, *callback, *data;
354
355
 
355
356
    if (PyTuple_GET_SIZE(args) < 2) {
356
357
        PyErr_SetString(PyExc_TypeError, "Invalid number of arguments");
357
358
        return NULL;
358
359
    }
359
360
 
360
 
    PyObject *name = PyTuple_GET_ITEM(args, 0);
361
 
    PyObject *callback = PyTuple_GET_ITEM(args, 1);
362
 
    PyObject *data = PyTuple_GetSlice(args, 2, PyTuple_GET_SIZE(args));
 
361
    name = PyTuple_GET_ITEM(args, 0);
 
362
    callback = PyTuple_GET_ITEM(args, 1);
 
363
    data = PyTuple_GetSlice(args, 2, PyTuple_GET_SIZE(args));
363
364
    if (data) {
364
365
        /* 
365
366
           callbacks = self._hooks.get(name)
399
400
    PyObject *tuple = PyTuple_New(PyTuple_GET_SIZE(args) +
400
401
                                  PyTuple_GET_SIZE(data) + 1);
401
402
    if (tuple) {
 
403
        Py_ssize_t i, tuple_i;
 
404
 
402
405
        Py_INCREF(owner);
403
406
        PyTuple_SET_ITEM(tuple, 0, owner);
404
 
        Py_ssize_t i, tuple_i;
405
407
        tuple_i = 1;
406
408
        for (i = 0; i != PyTuple_GET_SIZE(args); i++) {
407
409
            PyObject *item = PyTuple_GET_ITEM(args, i);
423
425
EventSystem_emit(EventSystemObject *self, PyObject *all_args)
424
426
{
425
427
    PyObject *result = NULL;
 
428
    PyObject *name, *args;
426
429
 
427
430
    if (PyTuple_GET_SIZE(all_args) == 0) {
428
431
        PyErr_SetString(PyExc_TypeError, "Invalid number of arguments");
432
435
    /* XXX In the following code we trust on the format inserted by
433
436
     *     the hook() method.  If it's hacked somehow, it may blow up. */
434
437
 
435
 
    PyObject *name = PyTuple_GET_ITEM(all_args, 0);
436
 
    PyObject *args = PyTuple_GetSlice(all_args, 1, PyTuple_GET_SIZE(all_args));
 
438
    name = PyTuple_GET_ITEM(all_args, 0);
 
439
    args = PyTuple_GetSlice(all_args, 1, PyTuple_GET_SIZE(all_args));
437
440
    if (args) {
438
441
        /* owner = self._owner_ref() */
439
442
        PyObject *owner = PyWeakref_GET_OBJECT(self->_owner_ref);
440
443
        /* if owner is not None: */
441
444
        if (owner != Py_None) {
442
 
            Py_INCREF(owner);
443
445
            /* callbacks = self._hooks.get(name) */
444
446
            PyObject *callbacks = PyDict_GetItem(self->_hooks, name);
 
447
            Py_INCREF(owner);
445
448
            /* if callbacks: */
446
449
            if (callbacks && PySet_GET_SIZE(callbacks) != 0) {
447
450
                /* for callback, data in tuple(callbacks): */
1039
1042
Variable_set_state(VariableObject *self, PyObject *args)
1040
1043
{
1041
1044
    /* self._lazy_value, self._value = state */
1042
 
    if (!PyArg_ParseTuple(args, "(OO):set_state",
1043
 
                          &self->_lazy_value, &self->_value))
 
1045
    PyObject *lazy_value, *value;
 
1046
    if (!PyArg_ParseTuple(args, "(OO):set_state", &lazy_value, &value))
1044
1047
        return NULL;
1045
 
    Py_INCREF(self->_lazy_value);
1046
 
    Py_INCREF(self->_value);
 
1048
    Py_INCREF(lazy_value);
 
1049
    REPLACE(self->_lazy_value, lazy_value);
 
1050
    Py_INCREF(value);
 
1051
    REPLACE(self->_value, value);
1047
1052
    Py_RETURN_NONE;
1048
1053
}
1049
1054
 
1509
1514
    */
1510
1515
    PyObject *handler = PyDict_GetItem(self->_dispatch_table, cls);
1511
1516
    if (!handler) {
 
1517
        PyObject *mro;
 
1518
        Py_ssize_t size, i;
 
1519
 
1512
1520
        if (PyErr_Occurred())
1513
1521
            goto error;
1514
1522
 
1515
1523
        /* for mro_cls in cls.__mro__: */
1516
 
        PyObject *mro = expr->ob_type->tp_mro;
1517
 
        Py_ssize_t size = PyTuple_GET_SIZE(mro);
1518
 
        int i;
 
1524
        mro = expr->ob_type->tp_mro;
 
1525
        size = PyTuple_GET_SIZE(mro);
1519
1526
        for (i = 0; i != size; i++) {
1520
1527
            PyObject *mro_cls = PyTuple_GET_ITEM(mro, i);
1521
1528
            /*
1560
1567
 
1561
1568
    /* if inner_precedence < outer_precedence: */
1562
1569
    if (PyObject_Compare(inner_precedence, outer_precedence) == -1) {
 
1570
        PyObject *args, *tmp;
 
1571
 
1563
1572
        if (PyErr_Occurred())
1564
1573
            goto error;
1565
1574
 
1566
1575
        /* return "(%s)" % statement */
1567
 
        PyObject *args, *tmp;
1568
1576
        CATCH(NULL, args = PyTuple_Pack(1, statement));
1569
1577
        tmp = PyUnicode_Format(parenthesis_format, args);
1570
1578
        Py_DECREF(args);
1721
1729
static PyObject *
1722
1730
Compile__call__(CompileObject *self, PyObject *args, PyObject *kwargs)
1723
1731
{
1724
 
    if (!initialize_globals())
1725
 
        return NULL;
1726
 
 
1727
1732
    static char *kwlist[] = {"expr", "state", "join", "raw", "token", NULL};
1728
1733
    PyObject *expr = NULL;
1729
1734
    PyObject *state = Py_None;
1730
 
    PyObject *join = default_compile_join;
 
1735
    PyObject *join;
1731
1736
    char raw = 0;
1732
1737
    char token = 0;
1733
1738
 
1734
1739
    PyObject *result = NULL;
1735
1740
 
 
1741
    if (!initialize_globals())
 
1742
        return NULL;
 
1743
 
 
1744
    join = default_compile_join;
 
1745
 
1736
1746
    if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|OSbb", kwlist,
1737
1747
                                     &expr, &state, &join, &raw, &token)) {
1738
1748
        return NULL;
2022
2032
    Py_CLEAR(self->event);
2023
2033
    Py_CLEAR(self->variables);
2024
2034
    Py_CLEAR(self->primary_vars);
2025
 
    return PyDict_Type.tp_dealloc((PyObject *)self);
 
2035
    PyDict_Type.tp_dealloc((PyObject *)self);
2026
2036
}
2027
2037
 
2028
2038
static PyMethodDef ObjectInfo_methods[] = {