예제 #1
0
static PyObject *
psyco_lobj_read(lobjectObject *self, PyObject *args)
{
    PyObject *res;
    int where, end, size = -1;
    char *buffer;

    if (!PyArg_ParseTuple(args, "|i", &size)) return NULL;

    EXC_IF_LOBJ_CLOSED(self);
    EXC_IF_LOBJ_LEVEL0(self);
    EXC_IF_LOBJ_UNMARKED(self);

    if (size < 0) {
        if ((where = lobject_tell(self)) < 0) return NULL;
        if ((end = lobject_seek(self, 0, SEEK_END)) < 0) return NULL;
        if (lobject_seek(self, where, SEEK_SET) < 0) return NULL;
        size = end - where;
    }

    if ((buffer = PyMem_Malloc(size)) == NULL) {
        PyErr_NoMemory();
        return NULL;
    }
    if ((size = lobject_read(self, buffer, size)) < 0) {
        PyMem_Free(buffer);
        return NULL;
    }

    res = PyString_FromStringAndSize(buffer, size);
    PyMem_Free(buffer);
    
    return res;
}
예제 #2
0
static PyObject *
psyco_lobj_truncate(lobjectObject *self, PyObject *args)
{
    long len = 0;

    if (!PyArg_ParseTuple(args, "|l", &len))
        return NULL;

    EXC_IF_LOBJ_CLOSED(self);
    EXC_IF_LOBJ_LEVEL0(self);
    EXC_IF_LOBJ_UNMARKED(self);

#ifdef HAVE_LO64
    if (len > INT_MAX && self->conn->server_version < 90300) {
        PyErr_Format(NotSupportedError,
            "len out of range (%ld): server version %d "
            "does not support the lobject 64 API",
            len, self->conn->server_version);
        return NULL;
    }
#else
    if (len > INT_MAX) {
        PyErr_Format(InterfaceError,
            "len out of range (%ld): this psycopg version was not built "
            "with lobject 64 API support",
            len);
        return NULL;
    }
#endif

    if (lobject_truncate(self, len) < 0)
        return NULL;

    Py_RETURN_NONE;
}
예제 #3
0
static PyObject *
psyco_lobj_tell(lobjectObject *self, PyObject *args)
{
    int pos;

    EXC_IF_LOBJ_CLOSED(self);
    EXC_IF_LOBJ_LEVEL0(self);
    EXC_IF_LOBJ_UNMARKED(self);

    if ((pos = lobject_tell(self)) < 0)
    	return NULL;

    return PyInt_FromLong((long)pos);
}
예제 #4
0
static PyObject *
psyco_lobj_write(lobjectObject *self, PyObject *args)
{
    int len, res=0;
    const char *buffer;

    if (!PyArg_ParseTuple(args, "s#", &buffer, &len)) return NULL;

    EXC_IF_LOBJ_CLOSED(self);
    EXC_IF_LOBJ_LEVEL0(self);
    EXC_IF_LOBJ_UNMARKED(self);

    if ((res = lobject_write(self, buffer, len)) < 0) return NULL;

    return PyInt_FromLong((long)res);
}
예제 #5
0
static PyObject *
psyco_lobj_write(lobjectObject *self, PyObject *args)
{
    char *buffer;
    Py_ssize_t len;
    Py_ssize_t res;
    PyObject *obj;
    PyObject *data = NULL;
    PyObject *rv = NULL;

    if (!PyArg_ParseTuple(args, "O", &obj)) return NULL;

    EXC_IF_LOBJ_CLOSED(self);
    EXC_IF_LOBJ_LEVEL0(self);
    EXC_IF_LOBJ_UNMARKED(self);

    if (Bytes_Check(obj)) {
        Py_INCREF(obj);
        data = obj;
    }
    else if (PyUnicode_Check(obj)) {
        if (!(data = PyUnicode_AsEncodedString(obj, self->conn->codec, NULL))) {
            goto exit;
        }
    }
    else {
        PyErr_Format(PyExc_TypeError,
            "lobject.write requires a string; got %s instead",
            Py_TYPE(obj)->tp_name);
        goto exit;
    }

    if (-1 == Bytes_AsStringAndSize(data, &buffer, &len)) {
        goto exit;
    }

    if (0 > (res = lobject_write(self, buffer, (size_t)len))) {
        goto exit;
    }

    rv = PyInt_FromLong((long)res);

exit:
    Py_XDECREF(data);
    return rv;
}
예제 #6
0
static PyObject *
psyco_lobj_truncate(lobjectObject *self, PyObject *args)
{
    int len = 0;

    if (!PyArg_ParseTuple(args, "|i", &len))
        return NULL;

    EXC_IF_LOBJ_CLOSED(self);
    EXC_IF_LOBJ_LEVEL0(self);
    EXC_IF_LOBJ_UNMARKED(self);

    if (lobject_truncate(self, len) < 0)
        return NULL;

    Py_RETURN_NONE;
}
예제 #7
0
static PyObject *
psyco_lobj_seek(lobjectObject *self, PyObject *args)
{
    int offset, whence=0;
    int pos=0;

    if (!PyArg_ParseTuple(args, "i|i", &offset, &whence))
    	return NULL;

    EXC_IF_LOBJ_CLOSED(self);
    EXC_IF_LOBJ_LEVEL0(self);
    EXC_IF_LOBJ_UNMARKED(self);

    if ((pos = lobject_seek(self, offset, whence)) < 0)
    	return NULL;

    return PyInt_FromLong((long)pos);
}
예제 #8
0
static PyObject *
psyco_lobj_read(lobjectObject *self, PyObject *args)
{
    PyObject *res;
    int where, end;
    Py_ssize_t size = -1;
    char *buffer;

    if (!PyArg_ParseTuple(args, "|" CONV_CODE_PY_SSIZE_T,  &size)) return NULL;

    EXC_IF_LOBJ_CLOSED(self);
    EXC_IF_LOBJ_LEVEL0(self);
    EXC_IF_LOBJ_UNMARKED(self);

    if (size < 0) {
        if ((where = lobject_tell(self)) < 0) return NULL;
        if ((end = lobject_seek(self, 0, SEEK_END)) < 0) return NULL;
        if (lobject_seek(self, where, SEEK_SET) < 0) return NULL;
        size = end - where;
    }

    if ((buffer = PyMem_Malloc(size)) == NULL) {
        PyErr_NoMemory();
        return NULL;
    }
    if ((size = lobject_read(self, buffer, size)) < 0) {
        PyMem_Free(buffer);
        return NULL;
    }

    if (self->mode & LOBJECT_BINARY) {
        res = Bytes_FromStringAndSize(buffer, size);
    } else {
        res = PyUnicode_Decode(buffer, size, self->conn->codec, NULL);
    }
    PyMem_Free(buffer);

    return res;
}
예제 #9
0
static PyObject *
psyco_lobj_seek(lobjectObject *self, PyObject *args)
{
    long offset, pos=0;
    int whence=0;

    if (!PyArg_ParseTuple(args, "l|i", &offset, &whence))
        return NULL;

    EXC_IF_LOBJ_CLOSED(self);
    EXC_IF_LOBJ_LEVEL0(self);
    EXC_IF_LOBJ_UNMARKED(self);

#ifdef HAVE_LO64
    if ((offset < INT_MIN || offset > INT_MAX)
            && self->conn->server_version < 90300) {
        PyErr_Format(NotSupportedError,
            "offset out of range (%ld): server version %d "
            "does not support the lobject 64 API",
            offset, self->conn->server_version);
        return NULL;
    }
#else
    if (offset < INT_MIN || offset > INT_MAX) {
        PyErr_Format(InterfaceError,
            "offset out of range (%ld): this psycopg version was not built "
            "with lobject 64 API support",
            offset);
        return NULL;
    }
#endif

    if ((pos = lobject_seek(self, offset, whence)) < 0)
        return NULL;

    return PyLong_FromLong(pos);
}