static PyObject * csv_writerow(WriterObj *self, PyObject *seq) { DialectObj *dialect = self->dialect; Py_ssize_t len, i; PyObject *line, *result; if (!PySequence_Check(seq)) return PyErr_Format(error_obj, "sequence expected"); len = PySequence_Length(seq); if (len < 0) return NULL; /* Join all fields in internal buffer. */ join_reset(self); for (i = 0; i < len; i++) { PyObject *field; int append_ok; int quoted; field = PySequence_GetItem(seq, i); if (field == NULL) return NULL; switch (dialect->quoting) { case QUOTE_NONNUMERIC: quoted = !PyNumber_Check(field); break; case QUOTE_ALL: quoted = 1; break; default: quoted = 0; break; } if (PyUnicode_Check(field)) { append_ok = join_append(self, field, "ed, len == 1); Py_DECREF(field); } else if (field == Py_None) { append_ok = join_append(self, NULL, "ed, len == 1); Py_DECREF(field); } else { PyObject *str; str = PyObject_Str(field); Py_DECREF(field); if (str == NULL) return NULL; append_ok = join_append(self, str, "ed, len == 1); Py_DECREF(str); } if (!append_ok) return NULL; } /* Add line terminator. */ if (!join_append_lineterminator(self)) return 0; line = PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND, (void *) self->rec, self->rec_len); if (line == NULL) return NULL; result = PyObject_CallFunctionObjArgs(self->writeline, line, NULL); Py_DECREF(line); return result; }
static PyObject * csv_writerow(WriterObj *self, PyObject *seq) { DialectObj *dialect = self->dialect; int len, i; if (!PySequence_Check(seq)) return PyErr_Format(error_obj, "sequence expected"); len = PySequence_Length(seq); if (len < 0) return NULL; /* Join all fields in internal buffer. */ join_reset(self); for (i = 0; i < len; i++) { PyObject *field; int append_ok; int quoted; field = PySequence_GetItem(seq, i); if (field == NULL) return NULL; switch (dialect->quoting) { case QUOTE_NONNUMERIC: quoted = !PyNumber_Check(field); break; case QUOTE_ALL: quoted = 1; break; default: quoted = 0; break; } if (PyString_Check(field)) { append_ok = join_append(self, PyString_AS_STRING(field), "ed, len == 1); Py_DECREF(field); } else if (field == Py_None) { append_ok = join_append(self, "", "ed, len == 1); Py_DECREF(field); } else { PyObject *str; if (PyFloat_Check(field)) { str = PyObject_Repr(field); } else { str = PyObject_Str(field); } Py_DECREF(field); if (str == NULL) return NULL; append_ok = join_append(self, PyString_AS_STRING(str), "ed, len == 1); Py_DECREF(str); } if (!append_ok) return NULL; } /* Add line terminator. */ if (!join_append_lineterminator(self)) return 0; return PyObject_CallFunction(self->writeline, "(s#)", self->rec, self->rec_len); }
static PyObject * csv_writerow(WriterObj *self, PyObject *seq) { DialectObj *dialect = self->dialect; PyObject *iter, *field, *line, *result; iter = PyObject_GetIter(seq); if (iter == NULL) return PyErr_Format(_csvstate_global->error_obj, "iterable expected, not %.200s", seq->ob_type->tp_name); /* Join all fields in internal buffer. */ join_reset(self); while ((field = PyIter_Next(iter))) { int append_ok; int quoted; switch (dialect->quoting) { case QUOTE_NONNUMERIC: quoted = !PyNumber_Check(field); break; case QUOTE_ALL: quoted = 1; break; default: quoted = 0; break; } if (PyUnicode_Check(field)) { append_ok = join_append(self, field, quoted); Py_DECREF(field); } else if (field == Py_None) { append_ok = join_append(self, NULL, quoted); Py_DECREF(field); } else { PyObject *str; str = PyObject_Str(field); Py_DECREF(field); if (str == NULL) { Py_DECREF(iter); return NULL; } append_ok = join_append(self, str, quoted); Py_DECREF(str); } if (!append_ok) { Py_DECREF(iter); return NULL; } } Py_DECREF(iter); if (PyErr_Occurred()) return NULL; if (self->num_fields > 0 && self->rec_len == 0) { if (dialect->quoting == QUOTE_NONE) { PyErr_Format(_csvstate_global->error_obj, "single empty field record must be quoted"); return NULL; } self->num_fields--; if (!join_append(self, NULL, 1)) return NULL; } /* Add line terminator. */ if (!join_append_lineterminator(self)) return NULL; line = PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND, (void *) self->rec, self->rec_len); if (line == NULL) return NULL; result = PyObject_CallFunctionObjArgs(self->writeline, line, NULL); Py_DECREF(line); return result; }