Exemple #1
0
static PyObject *
Reader_iternext(ReaderObj *self)
{
    PyObject *fields = NULL;
    Py_UCS4 c;
    Py_ssize_t pos, linelen;
    unsigned int kind;
    void *data;
    PyObject *lineobj;

    if (parse_reset(self) < 0)
        return NULL;
    do {
        lineobj = PyIter_Next(self->input_iter);
        if (lineobj == NULL) {
            /* End of input OR exception */
            if (!PyErr_Occurred() && self->field_len != 0)
                PyErr_Format(error_obj,
                             "newline inside string");
            return NULL;
        }
        if (!PyUnicode_Check(lineobj)) {
            PyErr_Format(error_obj,
                         "iterator should return strings, "
                         "not %.200s "
                         "(did you open the file in text mode?)",
                         lineobj->ob_type->tp_name
                );
            Py_DECREF(lineobj);
            return NULL;
        }
        ++self->line_num;
        kind = PyUnicode_KIND(lineobj);
        data = PyUnicode_DATA(lineobj);
        pos = 0;
        linelen = PyUnicode_GET_LENGTH(lineobj);
        while (linelen--) {
            c = PyUnicode_READ(kind, data, pos);
            if (c == '\0') {
                Py_DECREF(lineobj);
                PyErr_Format(error_obj,
                             "line contains NULL byte");
                goto err;
            }
            if (parse_process_char(self, c) < 0) {
                Py_DECREF(lineobj);
                goto err;
            }
            pos++;
        }
        Py_DECREF(lineobj);
        if (parse_process_char(self, 0) < 0)
            goto err;
    } while (self->state != START_RECORD);

    fields = self->fields;
    self->fields = NULL;
err:
    return fields;
}
static PyObject *
Reader_iternext(ReaderObj *self)
{
        PyObject *lineobj;
        PyObject *fields = NULL;
        Py_UNICODE *line, c;
        Py_ssize_t linelen;

	if (parse_reset(self) < 0)
		return NULL;
        do {
                lineobj = PyIter_Next(self->input_iter);
                if (lineobj == NULL) {
                        /* End of input OR exception */
                        if (!PyErr_Occurred() && self->field_len != 0)
                                PyErr_Format(error_obj,
					     "newline inside string");
                        return NULL;
                }
		if (!PyUnicode_Check(lineobj)) {
			PyErr_Format(error_obj,
				     "iterator should return strings, "
				     "not %.200s "
				     "(did you open the file in text mode?)",
				     lineobj->ob_type->tp_name
				);
			Py_DECREF(lineobj);
			return NULL;
		}
                ++self->line_num;
                line = PyUnicode_AsUnicode(lineobj);
                linelen = PyUnicode_GetSize(lineobj);
                if (line == NULL || linelen < 0) {
                        Py_DECREF(lineobj);
                        return NULL;
                }
                while (linelen--) {
			c = *line++;
			if (c == '\0') {
				Py_DECREF(lineobj);
				PyErr_Format(error_obj,
					     "line contains NULL byte");
				goto err;
			}
			if (parse_process_char(self, c) < 0) {
				Py_DECREF(lineobj);
				goto err;
			}
		}
                Py_DECREF(lineobj);
		if (parse_process_char(self, 0) < 0)
			goto err;
        } while (self->state != START_RECORD);

        fields = self->fields;
        self->fields = NULL;
err:
        return fields;
}
Exemple #3
0
static PyObject *
Reader_iternext(ReaderObj *self)
{
    PyObject *lineobj;
    PyObject *fields = NULL;
    char *line, c;
    int linelen;

    if (parse_reset(self) < 0)
        return NULL;
    do {
        lineobj = PyIter_Next(self->input_iter);
        if (lineobj == NULL) {
            /* End of input OR exception */
            if (!PyErr_Occurred() && (self->field_len != 0 ||
                                      self->state == IN_QUOTED_FIELD)) {
                if (self->dialect->strict)
                    PyErr_SetString(error_obj, "unexpected end of data");
                else if (parse_save_field(self) >= 0 )
                    break;
            }
            return NULL;
        }
        ++self->line_num;

        line = PyString_AsString(lineobj);
        linelen = PyString_Size(lineobj);

        if (line == NULL || linelen < 0) {
            Py_DECREF(lineobj);
            return NULL;
        }
        while (linelen--) {
            c = *line++;
            if (c == '\0') {
                Py_DECREF(lineobj);
                PyErr_Format(error_obj,
                             "line contains NULL byte");
                goto err;
            }
            if (parse_process_char(self, c) < 0) {
                Py_DECREF(lineobj);
                goto err;
            }
        }
        Py_DECREF(lineobj);
        if (parse_process_char(self, 0) < 0)
            goto err;
    } while (self->state != START_RECORD);

    fields = self->fields;
    self->fields = NULL;
err:
    return fields;
}
Exemple #4
0
static PyObject *
Reader_iternext(ReaderObj *self)
{
        PyObject *lineobj;
        PyObject *fields = NULL;
        char *line, c;
	int linelen;

	if (parse_reset(self) < 0)
		return NULL;
        do {
                lineobj = PyIter_Next(self->input_iter);
                if (lineobj == NULL) {
                        /* End of input OR exception */
                        if (!PyErr_Occurred() && self->field_len != 0)
                                PyErr_Format(error_obj,
					     "newline inside string");
                        return NULL;
                }
		++self->line_num;

                line = PyString_AsString(lineobj);
		linelen = PyString_Size(lineobj);

                if (line == NULL || linelen < 0) {
                        Py_DECREF(lineobj);
                        return NULL;
                }
                while (linelen--) {
			c = *line++;
			if (c == '\0') {
				Py_DECREF(lineobj);
				PyErr_Format(error_obj,
					     "line contains NULL byte");
				goto err;
			}
			if (parse_process_char(self, c) < 0) {
				Py_DECREF(lineobj);
				goto err;
			}
		}
                Py_DECREF(lineobj);
		if (parse_process_char(self, 0) < 0)
			goto err;
        } while (self->state != START_RECORD);

        fields = self->fields;
        self->fields = NULL;
err:
        return fields;
}
Exemple #5
0
static PyObject *
CSVParser_iternext(CSVParser *self)
{
    if(!self->is_file)
        return CSVParser_iternext_filelike(self);
    char buf[MAXBUFSIZE];
    char c;
    char *cp;
    PyObject *fields = NULL;
    long i;
    long linelen;
    Logq_Engine_reset(self->engine);
    while(!self->engine->is_success){
        if (parse_reset(self) < 0)
            return NULL;
        do {
            PyFile_IncUseCount((PyFileObject*)self->pyfile);
            Py_BEGIN_ALLOW_THREADS
            cp = fgets(buf, MAXBUFSIZE, self->file);
            Py_END_ALLOW_THREADS
            PyFile_DecUseCount((PyFileObject*)self->pyfile);
            if (cp == NULL) {
                /* End of input OR exception */
                if (!PyErr_Occurred() && (self->field_len != 0 ||
                                          self->state == IN_QUOTED_FIELD)) {
                    if (parse_save_field(self) >= 0 )
                        break;
                }
                return NULL;
            }
            ++self->line_num;
            linelen = strlen(buf);
            if (buf == NULL || linelen < 0) {
                return NULL;
            }
            for(i=0; i<linelen; ++i){
                c = buf[i];
                if (c == '\0') {
                    PyErr_Format(csv_error_obj,
                                 "line contains NULL byte");
                    goto err;
                }
                if (parse_process_char(self, c) < 0) {
                    goto err;
                }
                //query fail. go next line
                if (self->state == QUERY_FAIL){
                    if(buf[linelen-1]=='\n'){
                        self->state = START_RECORD;
                    }
                    break;
                }
            }
        } while (self->state != START_RECORD);
    }
    fields = self->fields;
    self->fields = NULL;
err:
    return fields;
}
Exemple #6
0
static PyObject *
CSVParser_iternext_filelike(CSVParser *self)
{
    char *buf;
    char c;
    PyObject *fields = NULL;
    PyObject *lineobj = NULL;
    long i;
    long linelen;
    Logq_Engine_reset(self->engine);
    while(!self->engine->is_success){
        if (parse_reset(self) < 0)
            return NULL;
        do {
            lineobj = PyIter_Next(self->pyfile);
            if (lineobj == NULL) {
                /* End of input OR exception */
                if (!PyErr_Occurred() && (self->field_len != 0 ||
                                          self->state == IN_QUOTED_FIELD)) {
                    if (parse_save_field(self) >= 0 )
                        break;
                }
                return NULL;
            }
            ++self->line_num;

            buf = PyString_AsString(lineobj);
            linelen = PyString_Size(lineobj);

            if (buf == NULL || linelen < 0) {
                return NULL;
            }
            for(i=0; i<linelen; ++i){
                c = buf[i];
                if (c == '\0') {
                    Py_DECREF(lineobj);
                    PyErr_Format(csv_error_obj,
                                 "line contains NULL byte");
                    goto err;
                }
                if (parse_process_char(self, c) < 0) {
                    Py_DECREF(lineobj);
                    goto err;
                }
                //query fail. go next line
                if (self->state == QUERY_FAIL){
                    if(buf[linelen-1]=='\n'){
                        self->state = START_RECORD;
                    }
                    break;
                }
            }
            Py_DECREF(lineobj);
        } while (self->state != START_RECORD);
    }
    fields = self->fields;
    self->fields = NULL;
err:
    return fields;
}
Exemple #7
0
static PyObject *
Reader_iternext(ReaderObj *self)
{
        PyObject *lineobj;
        PyObject *fields;
        char *line;

        do {
                lineobj = PyIter_Next(self->input_iter);
                if (lineobj == NULL) {
                        /* End of input OR exception */
                        if (!PyErr_Occurred() && self->field_len != 0)
                                return PyErr_Format(error_obj,
                                                    "newline inside string");
                        return NULL;
                }

                if (self->had_parse_error) {
                        if (self->fields) {
                                Py_XDECREF(self->fields);
                        }
                        self->fields = PyList_New(0);
                        self->field_len = 0;
                        self->state = START_RECORD;
                        self->had_parse_error = 0;
                }
                line = PyString_AsString(lineobj);

                if (line == NULL) {
                        Py_DECREF(lineobj);
                        return NULL;
                }
		if (strlen(line) < (size_t)PyString_GET_SIZE(lineobj)) {
			self->had_parse_error = 1;
			Py_DECREF(lineobj);
			return PyErr_Format(error_obj,
					    "string with NUL bytes");
		}

                /* Process line of text - send '\n' to processing code to
                represent end of line.  End of line which is not at end of
                string is an error. */
                while (*line) {
                        char c;

                        c = *line++;
                        if (c == '\r') {
                                c = *line++;
                                if (c == '\0')
                                        /* macintosh end of line */
                                        break;
                                if (c == '\n') {
                                        c = *line++;
                                        if (c == '\0')
                                                /* DOS end of line */
                                                break;
                                }
                                self->had_parse_error = 1;
                                Py_DECREF(lineobj);
                                return PyErr_Format(error_obj,
                                                    "newline inside string");
                        }
                        if (c == '\n') {
                                c = *line++;
                                if (c == '\0')
                                        /* unix end of line */
                                        break;
                                self->had_parse_error = 1;
                                Py_DECREF(lineobj);
                                return PyErr_Format(error_obj, 
                                                    "newline inside string");
                        }
                        parse_process_char(self, c);
                        if (PyErr_Occurred()) {
                                Py_DECREF(lineobj);
                                return NULL;
                        }
                }
                parse_process_char(self, '\n');
                Py_DECREF(lineobj);
        } while (self->state != START_RECORD);

        fields = self->fields;
        self->fields = PyList_New(0);
        return fields;
}