Пример #1
0
static PyObject *pyAssertFact(PyObject *self, PyObject *args) {
    void *handle;
    char *fact;
    if (!PyArg_ParseTuple(args, "ls", &handle, &fact)) {
        PyErr_SetString(RulesError, "pyAssertFact Invalid argument");
        return NULL;
    }

    unsigned int result = assertFact(handle, fact);
    if (result == RULES_OK) {
        return Py_BuildValue("i", 1);    
    } else if (result == ERR_EVENT_NOT_HANDLED) {
        return Py_BuildValue("i", 0);    
    } else {
        if (result == ERR_OUT_OF_MEMORY) {
            PyErr_NoMemory();
        } else { 
            char *message;
            if (asprintf(&message, "Could not assert fact, error code: %d", result)) {
                PyErr_NoMemory();
            } else {
                PyErr_SetString(RulesError, message);
                free(message);
            }
        }
        return NULL;
    }
}
Пример #2
0
static VALUE rbAssertFact(VALUE self, VALUE handle, VALUE fact) {
    Check_Type(handle, T_FIXNUM);
    Check_Type(fact, T_STRING);

    unsigned int result = assertFact((void *)FIX2LONG(handle), RSTRING_PTR(fact));
    if (result == RULES_OK) {
        return INT2FIX(1);    
    } else if (result == ERR_EVENT_NOT_HANDLED) {
        return INT2FIX(0);    
    } else {
        if (result == ERR_OUT_OF_MEMORY) {
            rb_raise(rb_eNoMemError, "Out of memory");
        } else { 
            rb_raise(rb_eException, "Could not assert fact, error code: %d", result);
        }
    }

    return Qnil;
}