Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 91b9746

Browse files
committed
Factor out the code for making a dialect instance.
1 parent dbce261 commit 91b9746

1 file changed

Lines changed: 22 additions & 18 deletions

File tree

Modules/_csv.c

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -496,6 +496,24 @@ static PyTypeObject Dialect_Type = {
496496
0, /* tp_free */
497497
};
498498

499+
/*
500+
* Return an instance of the dialect type, given a Python instance or kwarg
501+
* description of the dialect
502+
*/
503+
static PyObject *
504+
_call_dialect(PyObject *dialect_inst, PyObject *kwargs)
505+
{
506+
PyObject *ctor_args;
507+
PyObject *dialect;
508+
509+
ctor_args = Py_BuildValue(dialect_inst ? "(O)" : "()", dialect_inst);
510+
if (ctor_args == NULL)
511+
return NULL;
512+
dialect = PyObject_Call((PyObject *)&Dialect_Type, ctor_args, kwargs);
513+
Py_DECREF(ctor_args);
514+
return dialect;
515+
}
516+
499517
static void
500518
parse_save_field(ReaderObj *self)
501519
{
@@ -862,7 +880,7 @@ static PyTypeObject Reader_Type = {
862880
static PyObject *
863881
csv_reader(PyObject *module, PyObject *args, PyObject *keyword_args)
864882
{
865-
PyObject * iterator, * dialect = NULL, *ctor_args;
883+
PyObject * iterator, * dialect = NULL;
866884
ReaderObj * self = PyObject_GC_New(ReaderObj, &Reader_Type);
867885

868886
if (!self)
@@ -890,14 +908,7 @@ csv_reader(PyObject *module, PyObject *args, PyObject *keyword_args)
890908
Py_DECREF(self);
891909
return NULL;
892910
}
893-
ctor_args = Py_BuildValue(dialect ? "(O)" : "()", dialect);
894-
if (ctor_args == NULL) {
895-
Py_DECREF(self);
896-
return NULL;
897-
}
898-
self->dialect = (DialectObj *)PyObject_Call((PyObject *)&Dialect_Type,
899-
ctor_args, keyword_args);
900-
Py_DECREF(ctor_args);
911+
self->dialect = (DialectObj *)_call_dialect(dialect, keyword_args);
901912
if (self->dialect == NULL) {
902913
Py_DECREF(self);
903914
return NULL;
@@ -1313,7 +1324,7 @@ static PyTypeObject Writer_Type = {
13131324
static PyObject *
13141325
csv_writer(PyObject *module, PyObject *args, PyObject *keyword_args)
13151326
{
1316-
PyObject * output_file, * dialect = NULL, *ctor_args;
1327+
PyObject * output_file, * dialect = NULL;
13171328
WriterObj * self = PyObject_GC_New(WriterObj, &Writer_Type);
13181329

13191330
if (!self)
@@ -1338,14 +1349,7 @@ csv_writer(PyObject *module, PyObject *args, PyObject *keyword_args)
13381349
Py_DECREF(self);
13391350
return NULL;
13401351
}
1341-
ctor_args = Py_BuildValue(dialect ? "(O)" : "()", dialect);
1342-
if (ctor_args == NULL) {
1343-
Py_DECREF(self);
1344-
return NULL;
1345-
}
1346-
self->dialect = (DialectObj *)PyObject_Call((PyObject *)&Dialect_Type,
1347-
ctor_args, keyword_args);
1348-
Py_DECREF(ctor_args);
1352+
self->dialect = (DialectObj *)_call_dialect(dialect, keyword_args);
13491353
if (self->dialect == NULL) {
13501354
Py_DECREF(self);
13511355
return NULL;

0 commit comments

Comments
 (0)