Now that internal dialect type is immutable, and the dialect registry

only contains instances of the dialect type, we can refer directly to the
dialect instances rather than creating new ones. In other words, if the
dialect comes from the registry, and we apply no further modifications,
the reader/writer can use the dialect object directly.
This commit is contained in:
Andrew McNamara 2005-01-11 04:49:53 +00:00
parent a422c34b70
commit 29bf4e44f6
1 changed files with 40 additions and 29 deletions

View File

@ -301,10 +301,11 @@ static char *dialect_kws[] = {
NULL NULL
}; };
static int static PyObject *
dialect_init(DialectObj * self, PyObject * args, PyObject * kwargs) dialect_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
{ {
int ret = -1; DialectObj *self;
PyObject *ret = NULL;
PyObject *dialect = NULL; PyObject *dialect = NULL;
PyObject *delimiter = NULL; PyObject *delimiter = NULL;
PyObject *doublequote = NULL; PyObject *doublequote = NULL;
@ -326,7 +327,35 @@ dialect_init(DialectObj * self, PyObject * args, PyObject * kwargs)
&quoting, &quoting,
&skipinitialspace, &skipinitialspace,
&strict)) &strict))
return -1; return NULL;
if (dialect != NULL) {
if (IS_BASESTRING(dialect)) {
dialect = get_dialect_from_registry(dialect);
if (dialect == NULL)
return NULL;
}
else
Py_INCREF(dialect);
/* Can we reuse this instance? */
if (PyObject_TypeCheck(dialect, &Dialect_Type) &&
delimiter == 0 &&
doublequote == 0 &&
escapechar == 0 &&
lineterminator == 0 &&
quotechar == 0 &&
quoting == 0 &&
skipinitialspace == 0 &&
strict == 0)
return dialect;
}
self = (DialectObj *)type->tp_alloc(type, 0);
if (self == NULL) {
Py_XDECREF(dialect);
return NULL;
}
self->lineterminator = NULL;
Py_XINCREF(delimiter); Py_XINCREF(delimiter);
Py_XINCREF(doublequote); Py_XINCREF(doublequote);
@ -337,16 +366,9 @@ dialect_init(DialectObj * self, PyObject * args, PyObject * kwargs)
Py_XINCREF(skipinitialspace); Py_XINCREF(skipinitialspace);
Py_XINCREF(strict); Py_XINCREF(strict);
if (dialect != NULL) { if (dialect != NULL) {
if (IS_BASESTRING(dialect)) {
dialect = get_dialect_from_registry(dialect);
if (dialect == NULL)
goto err;
} else
Py_INCREF(dialect);
#define DIALECT_GETATTR(v, n) \ #define DIALECT_GETATTR(v, n) \
if (v == NULL) \ if (v == NULL) \
v = PyObject_GetAttrString(dialect, n) v = PyObject_GetAttrString(dialect, n)
DIALECT_GETATTR(delimiter, "delimiter"); DIALECT_GETATTR(delimiter, "delimiter");
DIALECT_GETATTR(doublequote, "doublequote"); DIALECT_GETATTR(doublequote, "doublequote");
DIALECT_GETATTR(escapechar, "escapechar"); DIALECT_GETATTR(escapechar, "escapechar");
@ -356,7 +378,6 @@ dialect_init(DialectObj * self, PyObject * args, PyObject * kwargs)
DIALECT_GETATTR(skipinitialspace, "skipinitialspace"); DIALECT_GETATTR(skipinitialspace, "skipinitialspace");
DIALECT_GETATTR(strict, "strict"); DIALECT_GETATTR(strict, "strict");
PyErr_Clear(); PyErr_Clear();
Py_DECREF(dialect);
} }
/* check types and convert to C values */ /* check types and convert to C values */
@ -391,8 +412,9 @@ dialect_init(DialectObj * self, PyObject * args, PyObject * kwargs)
goto err; goto err;
} }
ret = 0; ret = (PyObject *)self;
err: err:
Py_XDECREF(dialect);
Py_XDECREF(delimiter); Py_XDECREF(delimiter);
Py_XDECREF(doublequote); Py_XDECREF(doublequote);
Py_XDECREF(escapechar); Py_XDECREF(escapechar);
@ -404,17 +426,6 @@ err:
return ret; return ret;
} }
static PyObject *
dialect_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
{
DialectObj *self;
self = (DialectObj *)type->tp_alloc(type, 0);
if (self != NULL) {
self->lineterminator = NULL;
}
return (PyObject *)self;
}
PyDoc_STRVAR(Dialect_Type_doc, PyDoc_STRVAR(Dialect_Type_doc,
"CSV dialect\n" "CSV dialect\n"
@ -459,8 +470,8 @@ static PyTypeObject Dialect_Type = {
0, /* tp_descr_get */ 0, /* tp_descr_get */
0, /* tp_descr_set */ 0, /* tp_descr_set */
0, /* tp_dictoffset */ 0, /* tp_dictoffset */
(initproc)dialect_init, /* tp_init */ 0, /* tp_init */
PyType_GenericAlloc, /* tp_alloc */ 0, /* tp_alloc */
dialect_new, /* tp_new */ dialect_new, /* tp_new */
0, /* tp_free */ 0, /* tp_free */
}; };