예제 #1
0
static int
O_cwrite(PyObject *self, const char *c, Py_ssize_t  len) {
    Py_ssize_t newpos;
    Oobject *oself;
    char *newbuf;

    if (!IO__opencheck(IOOOBJECT(self))) return -1;
    oself = (Oobject *)self;

    if (len > INT_MAX) {
        PyErr_SetString(PyExc_OverflowError,
                        "length too large");
        return -1;
    }
    assert(len >= 0);
    if (oself->pos >= PY_SSIZE_T_MAX - len) {
        PyErr_SetString(PyExc_OverflowError,
                        "new position too large");
        return -1;
    }
    newpos = oself->pos + len;
    if (newpos >= oself->buf_size) {
        size_t newsize = oself->buf_size;
        newsize *= 2;
        if (newsize <= (size_t)newpos || newsize > PY_SSIZE_T_MAX) {
            assert(newpos < PY_SSIZE_T_MAX - 1);
            newsize = newpos + 1;
        }
        newbuf = (char*)realloc(oself->buf, newsize);
        if (!newbuf) {
            PyErr_SetString(PyExc_MemoryError,"out of memory");
            return -1;
        }
        oself->buf_size = (Py_ssize_t)newsize;
        oself->buf = newbuf;
    }

    if (oself->string_size < oself->pos) {
        /* In case of overseek, pad with null bytes the buffer region between
           the end of stream and the current position.

          0   lo      string_size                           hi
          |   |<---used--->|<----------available----------->|
          |   |            <--to pad-->|<---to write--->    |
          0   buf                   position
        */
        memset(oself->buf + oself->string_size, '\0',
               (oself->pos - oself->string_size) * sizeof(char));
    }

    memcpy(oself->buf + oself->pos, c, len);

    oself->pos = newpos;

    if (oself->string_size < oself->pos) {
        oself->string_size = oself->pos;
    }

    return (int)len;
}
예제 #2
0
static PyObject *
IO_cgetval(PyObject *self) {
    if (!IO__opencheck(IOOOBJECT(self))) return NULL;
    assert(IOOOBJECT(self)->pos >= 0);
    return PyString_FromStringAndSize(((IOobject*)self)->buf,
                                      ((IOobject*)self)->pos);
}
예제 #3
0
static int
IO_creadline(PyObject *self, char **output) {
    char *n, *start, *end;
    Py_ssize_t len;

    if (!IO__opencheck(IOOOBJECT(self))) return -1;

    n = start = ((IOobject*)self)->buf + ((IOobject*)self)->pos;
    end = ((IOobject*)self)->buf + ((IOobject*)self)->string_size;
    while (n < end && *n != '\n')
        n++;

    if (n < end) n++;

    len = n - start;
    if (len > INT_MAX)
        len = INT_MAX;

    *output=start;

    assert(IOOOBJECT(self)->pos <= PY_SSIZE_T_MAX - len);
    assert(IOOOBJECT(self)->pos >= 0);
    assert(IOOOBJECT(self)->string_size >= 0);

    ((IOobject*)self)->pos += len;
    return (int)len;
}
예제 #4
0
static PyObject *
O_seek(Oobject *self, PyObject *args) {
	Py_ssize_t position;
	int mode = 0;

        if (!IO__opencheck(IOOOBJECT(self))) return NULL;
        if (!PyArg_ParseTuple(args, "n|i:seek", &position, &mode)) 
                return NULL;

        if (mode == 2) {
                position += self->string_size;
        }
        else if (mode == 1) {
                position += self->pos;
        }

        if (position > self->buf_size) {
                  self->buf_size*=2;
                  if (self->buf_size <= position) self->buf_size=position+1;
		  self->buf = (char*) realloc(self->buf,self->buf_size);
                  if (!self->buf) {
                      self->buf_size=self->pos=0;
                      return PyErr_NoMemory();
                    }
          }
        else if (position < 0) position=0;

        self->pos=position;

        while (--position >= self->string_size) self->buf[position]=0;

        Py_INCREF(Py_None);
        return Py_None;
}
예제 #5
0
static int
O_cwrite(PyObject *self, const char *c, Py_ssize_t  l) {
        Py_ssize_t newl;
        Oobject *oself;

        if (!IO__opencheck(IOOOBJECT(self))) return -1;
        oself = (Oobject *)self;

        newl = oself->pos+l;
        if (newl >= oself->buf_size) {
            oself->buf_size *= 2;
            if (oself->buf_size <= newl) {
		    assert(newl + 1 < INT_MAX);
                    oself->buf_size = (int)(newl+1);
	    }
            oself->buf = (char*)realloc(oself->buf, oself->buf_size);
	    if (!oself->buf) {
                    PyErr_SetString(PyExc_MemoryError,"out of memory");
                    oself->buf_size = oself->pos = 0;
                    return -1;
              }
          }

        memcpy(oself->buf+oself->pos,c,l);

	assert(oself->pos + l < INT_MAX);
        oself->pos += (int)l;

        if (oself->string_size < oself->pos) {
            oself->string_size = oself->pos;
        }

        return (int)l;
}
예제 #6
0
static PyObject *
IO_tell(IOobject *self, PyObject *unused) {

        if (!IO__opencheck(self)) return NULL;

        return PyInt_FromSsize_t(self->pos);
}
예제 #7
0
static PyObject *
IO_flush(IOobject *self, PyObject *unused) {

    if (!IO__opencheck(self)) return NULL;

    Py_INCREF(Py_None);
    return Py_None;
}
예제 #8
0
static PyObject *
IO_reset(IOobject *self, PyObject *unused) {

    if (!IO__opencheck(self)) return NULL;

    self->pos = 0;

    Py_INCREF(Py_None);
    return Py_None;
}
예제 #9
0
static PyObject *
IO_truncate(IOobject *self, PyObject *args) {
        Py_ssize_t pos = -1;
	
        if (!IO__opencheck(self)) return NULL;
        if (!PyArg_ParseTuple(args, "|n:truncate", &pos)) return NULL;
        if (pos < 0) pos = self->pos;

        if (self->string_size > pos) self->string_size = pos;
        self->pos = self->string_size;

        Py_INCREF(Py_None);
        return Py_None;
}
예제 #10
0
static int
IO_cread(PyObject *self, char **output, Py_ssize_t  n) {
        Py_ssize_t l;

        if (!IO__opencheck(IOOOBJECT(self))) return -1;
        l = ((IOobject*)self)->string_size - ((IOobject*)self)->pos;  
        if (n < 0 || n > l) {
                n = l;
                if (n < 0) n=0;
        }

        *output=((IOobject*)self)->buf + ((IOobject*)self)->pos;
        ((IOobject*)self)->pos += n;
        return n;
}
예제 #11
0
static PyObject *
IO_getval(IOobject *self, PyObject *args) {
        PyObject *use_pos=Py_None;
        Py_ssize_t s;

        if (!IO__opencheck(self)) return NULL;
        if (!PyArg_UnpackTuple(args,"getval", 0, 1,&use_pos)) return NULL;

        if (PyObject_IsTrue(use_pos)) {
                  s=self->pos;
                  if (s > self->string_size) s=self->string_size;
        }
        else
                  s=self->string_size;
        return PyString_FromStringAndSize(self->buf, s);
}
예제 #12
0
static int
IO_creadline(PyObject *self, char **output) {
        char *n, *s;
        Py_ssize_t l;

        if (!IO__opencheck(IOOOBJECT(self))) return -1;

        for (n = ((IOobject*)self)->buf + ((IOobject*)self)->pos,
               s = ((IOobject*)self)->buf + ((IOobject*)self)->string_size; 
             n < s && *n != '\n'; n++);
        if (n < s) n++;

        *output=((IOobject*)self)->buf + ((IOobject*)self)->pos;
        l = n - ((IOobject*)self)->buf - ((IOobject*)self)->pos;
	assert(((IOobject*)self)->pos + l < INT_MAX);
        ((IOobject*)self)->pos += (int)l;
        return (int)l;
}
예제 #13
0
static PyObject *
I_seek(Iobject *self, PyObject *args) {
        Py_ssize_t position;
	int mode = 0;

        if (!IO__opencheck(IOOOBJECT(self))) return NULL;
        if (!PyArg_ParseTuple(args, "n|i:seek", &position, &mode)) 
                return NULL;

        if (mode == 2) position += self->string_size;
        else if (mode == 1) position += self->pos;

        if (position < 0) position=0;

        self->pos=position;

        Py_INCREF(Py_None);
        return Py_None;
}
예제 #14
0
static int
IO_cread(PyObject *self, char **output, Py_ssize_t  n) {
    Py_ssize_t l;

    if (!IO__opencheck(IOOOBJECT(self))) return -1;
    assert(IOOOBJECT(self)->pos >= 0);
    assert(IOOOBJECT(self)->string_size >= 0);
    l = ((IOobject*)self)->string_size - ((IOobject*)self)->pos;
    if (n < 0 || n > l) {
        n = l;
        if (n < 0) n=0;
    }
    if (n > INT_MAX) {
        PyErr_SetString(PyExc_OverflowError,
                        "length too large");
        return -1;
    }

    *output=((IOobject*)self)->buf + ((IOobject*)self)->pos;
    ((IOobject*)self)->pos += n;
    return (int)n;
}
예제 #15
0
static PyObject *
IO_truncate(IOobject *self, PyObject *args) {
    Py_ssize_t pos = -1;

    if (!IO__opencheck(self)) return NULL;
    if (!PyArg_ParseTuple(args, "|n:truncate", &pos)) return NULL;

    if (PyTuple_Size(args) == 0) {
        /* No argument passed, truncate to current position */
        pos = self->pos;
    }

    if (pos < 0) {
        errno = EINVAL;
        PyErr_SetFromErrno(PyExc_IOError);
        return NULL;
    }

    if (self->string_size > pos) self->string_size = pos;
    self->pos = self->string_size;

    Py_INCREF(Py_None);
    return Py_None;
}
예제 #16
0
static PyObject *
IO_isatty(IOobject *self, PyObject *unused) {
    if (!IO__opencheck(self)) return NULL;
    Py_INCREF(Py_False);
    return Py_False;
}