Beispiel #1
0
static int
lobject_setup(lobjectObject *self, connectionObject *conn,
              Oid oid, const char *smode, Oid new_oid, const char *new_file)
{
    Dprintf("lobject_setup: init lobject object at %p", self);

    if (conn->autocommit) {
        psyco_set_error(ProgrammingError, NULL,
            "can't use a lobject outside of transactions", NULL, NULL);
        return -1;
    }

    self->conn = conn;
    self->mark = conn->mark;

    Py_INCREF((PyObject*)self->conn);

    self->fd = -1;
    self->oid = InvalidOid;

    if (0 != lobject_open(self, conn, oid, smode, new_oid, new_file))
        return -1;

   Dprintf("lobject_setup: good lobject object at %p, refcnt = "
           FORMAT_CODE_PY_SSIZE_T, self, Py_REFCNT(self));
   Dprintf("lobject_setup:    oid = %d, fd = %d", self->oid, self->fd);
   return 0;
}
Beispiel #2
0
/* Performs very basic validation on an incoming SQL string.
 * Returns a new reference to a str instance on success; NULL on failure,
 * after having set an exception.
 */
PyObject *
curs_validate_sql_basic(cursorObject *self, PyObject *sql)
{
    PyObject *rv = NULL;
    PyObject *comp = NULL;
    int iscomp;

    if (!sql || !PyObject_IsTrue(sql)) {
        psyco_set_error(ProgrammingError, self,
                         "can't execute an empty query");
        goto exit;
    }

    if (Bytes_Check(sql)) {
        /* Necessary for ref-count symmetry with the unicode case: */
        Py_INCREF(sql);
        rv = sql;
    }
    else if (PyUnicode_Check(sql)) {
        if (!(rv = conn_encode(self->conn, sql))) { goto exit; }
    }
    else if (0 != (iscomp = _curs_is_composible(sql))) {
        if (iscomp < 0) { goto exit; }
        if (!(comp = PyObject_CallMethod(sql, "as_string", "O", self->conn))) {
            goto exit;
        }

        if (Bytes_Check(comp)) {
            rv = comp;
            comp = NULL;
        }
        else if (PyUnicode_Check(comp)) {
            if (!(rv = conn_encode(self->conn, comp))) { goto exit; }
        }
        else {
            PyErr_Format(PyExc_TypeError,
                "as_string() should return a string: got %s instead",
                Py_TYPE(comp)->tp_name);
            goto exit;
        }
    }
    else {
        /* the  is not unicode or string, raise an error */
        PyErr_Format(PyExc_TypeError,
            "argument 1 must be a string or unicode object: got %s instead",
            Py_TYPE(sql)->tp_name);
        goto exit;
    }

exit:
    Py_XDECREF(comp);
    return rv;
}
Beispiel #3
0
static PyObject *
psyco_repl_curs_consume_stream(replicationCursorObject *self,
                               PyObject *args, PyObject *kwargs)
{
    cursorObject *curs = &self->cur;
    PyObject *consume = NULL, *res = NULL;
    double keepalive_interval = 10;
    static char *kwlist[] = {"consume", "keepalive_interval", NULL};

    if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|id", kwlist,
                                     &consume, &keepalive_interval)) {
        return NULL;
    }

    EXC_IF_CURS_CLOSED(curs);
    EXC_IF_CURS_ASYNC(curs, consume_stream);
    EXC_IF_GREEN(consume_stream);
    EXC_IF_TPC_PREPARED(self->cur.conn, consume_stream);

    Dprintf("psyco_repl_curs_consume_stream");

    if (keepalive_interval < 1.0) {
        psyco_set_error(ProgrammingError, curs, "keepalive_interval must be >= 1 (sec)");
        return NULL;
    }

    if (self->consuming) {
        PyErr_SetString(ProgrammingError,
                        "consume_stream cannot be used when already in the consume loop");
        return NULL;
    }

    if (curs->pgres == NULL || PQresultStatus(curs->pgres) != PGRES_COPY_BOTH) {
        PyErr_SetString(ProgrammingError,
                        "consume_stream: not replicating, call start_replication first");
        return NULL;
    }
    CLEARPGRES(curs->pgres);

    self->consuming = 1;

    if (pq_copy_both(self, consume, keepalive_interval) >= 0) {
        res = Py_None;
        Py_INCREF(res);
    }

    self->consuming = 0;

    return res;
}
Beispiel #4
0
PyObject *
microprotocols_adapt(PyObject *obj, PyObject *proto, PyObject *alt)
{
    PyObject *adapter, *adapted, *key, *meth;
    char buffer[256];

    /* we don't check for exact type conformance as specified in PEP 246
       because the ISQLQuote type is abstract and there is no way to get a
       quotable object to be its instance */

    Dprintf("microprotocols_adapt: trying to adapt %s",
        Py_TYPE(obj)->tp_name);

    /* look for an adapter in the registry */
    if (!(key = PyTuple_Pack(2, Py_TYPE(obj), proto))) { return NULL; }
    adapter = PyDict_GetItem(psyco_adapters, key);
    Py_DECREF(key);
    if (adapter) {
        adapted = PyObject_CallFunctionObjArgs(adapter, obj, NULL);
        return adapted;
    }

    /* try to have the protocol adapt this object*/
    if ((meth = PyObject_GetAttrString(proto, "__adapt__"))) {
        adapted = PyObject_CallFunctionObjArgs(meth, obj, NULL);
        Py_DECREF(meth);
        if (adapted && adapted != Py_None) return adapted;
        Py_XDECREF(adapted);
        if (PyErr_Occurred()) {
            if (PyErr_ExceptionMatches(PyExc_TypeError)) {
               PyErr_Clear();
            } else {
                return NULL;
            }
        }
    }
    else {
        /* proto.__adapt__ not found. */
        PyErr_Clear();
    }

    /* then try to have the object adapt itself */
    if ((meth = PyObject_GetAttrString(obj, "__conform__"))) {
        adapted = PyObject_CallFunctionObjArgs(meth, proto, NULL);
        Py_DECREF(meth);
        if (adapted && adapted != Py_None) return adapted;
        Py_XDECREF(adapted);
        if (PyErr_Occurred()) {
            if (PyErr_ExceptionMatches(PyExc_TypeError)) {
               PyErr_Clear();
            } else {
                return NULL;
            }
        }
    }
    else {
        /* obj.__conform__ not found. */
        PyErr_Clear();
    }

    /* Finally check if a superclass can be adapted and use the same adapter. */
    if (!(adapter = _get_superclass_adapter(obj, proto))) {
        return NULL;
    }
    if (Py_None != adapter) {
        adapted = PyObject_CallFunctionObjArgs(adapter, obj, NULL);
        return adapted;
    }

    /* else set the right exception and return NULL */
    PyOS_snprintf(buffer, 255, "can't adapt type '%s'",
        Py_TYPE(obj)->tp_name);
    psyco_set_error(ProgrammingError, NULL, buffer);
    return NULL;
}