From 86625972a1e330e200a3ae34f414a6caf12c6502 Mon Sep 17 00:00:00 2001 From: Andrew McNamara Date: Tue, 11 Jan 2005 01:28:33 +0000 Subject: [PATCH] Allow dialect-describing keywords to be supplied to register_dialect, record objects of internal dialect type, rather than instances of python objects. --- Lib/test/test_csv.py | 11 ++++++++++- Modules/_csv.c | 38 +++++++++++--------------------------- 2 files changed, 21 insertions(+), 28 deletions(-) diff --git a/Lib/test/test_csv.py b/Lib/test/test_csv.py index 66b0ea9ae89..cdda17976cd 100644 --- a/Lib/test/test_csv.py +++ b/Lib/test/test_csv.py @@ -272,13 +272,22 @@ class TestDialectRegistry(unittest.TestCase): expected_dialects.sort() csv.register_dialect(name, myexceltsv) try: - self.failUnless(isinstance(csv.get_dialect(name), myexceltsv)) + self.failUnless(csv.get_dialect(name).delimiter, '\t') got_dialects = csv.list_dialects() got_dialects.sort() self.assertEqual(expected_dialects, got_dialects) finally: csv.unregister_dialect(name) + def test_register_kwargs(self): + name = 'fedcba' + csv.register_dialect(name, delimiter=';') + try: + self.failUnless(csv.get_dialect(name).delimiter, '\t') + self.failUnless(list(csv.reader('X;Y;Z', name)), ['X', 'Y', 'Z']) + finally: + csv.unregister_dialect(name) + def test_incomplete_dialect(self): class myexceltsv(csv.Dialect): delimiter = "\t" diff --git a/Modules/_csv.c b/Modules/_csv.c index 3876cd7e88e..405bc7112c3 100644 --- a/Modules/_csv.c +++ b/Modules/_csv.c @@ -1368,42 +1368,26 @@ csv_list_dialects(PyObject *module, PyObject *args) } static PyObject * -csv_register_dialect(PyObject *module, PyObject *args) +csv_register_dialect(PyObject *module, PyObject *args, PyObject *kwargs) { - PyObject *name_obj, *dialect_obj; + PyObject *name_obj, *dialect_obj = NULL; + PyObject *dialect; - if (!PyArg_UnpackTuple(args, "", 2, 2, &name_obj, &dialect_obj)) + if (!PyArg_UnpackTuple(args, "", 1, 2, &name_obj, &dialect_obj)) return NULL; if (!IS_BASESTRING(name_obj)) { PyErr_SetString(PyExc_TypeError, "dialect name must be a string or unicode"); return NULL; } - Py_INCREF(dialect_obj); - /* A class rather than an instance? Instantiate */ - if (PyObject_TypeCheck(dialect_obj, &PyClass_Type)) { - PyObject * new_dia; - new_dia = PyObject_CallFunction(dialect_obj, ""); - Py_DECREF(dialect_obj); - if (new_dia == NULL) - return NULL; - dialect_obj = new_dia; - } - /* Make sure we finally have an instance */ - if (!PyInstance_Check(dialect_obj)) { - PyErr_SetString(PyExc_TypeError, "dialect must be an instance"); - Py_DECREF(dialect_obj); + dialect = _call_dialect(dialect_obj, kwargs); + if (dialect == NULL) + return NULL; + if (PyDict_SetItem(dialects, name_obj, dialect) < 0) { + Py_DECREF(dialect); return NULL; } - if (PyObject_SetAttrString(dialect_obj, "_name", name_obj) < 0) { - Py_DECREF(dialect_obj); - return NULL; - } - if (PyDict_SetItem(dialects, name_obj, dialect_obj) < 0) { - Py_DECREF(dialect_obj); - return NULL; - } - Py_DECREF(dialect_obj); + Py_DECREF(dialect); Py_INCREF(Py_None); return Py_None; } @@ -1538,7 +1522,7 @@ static struct PyMethodDef csv_methods[] = { { "list_dialects", (PyCFunction)csv_list_dialects, METH_NOARGS, csv_list_dialects_doc}, { "register_dialect", (PyCFunction)csv_register_dialect, - METH_VARARGS, csv_register_dialect_doc}, + METH_VARARGS | METH_KEYWORDS, csv_register_dialect_doc}, { "unregister_dialect", (PyCFunction)csv_unregister_dialect, METH_O, csv_unregister_dialect_doc}, { "get_dialect", (PyCFunction)csv_get_dialect,