[Python-checkins] python/nondist/sandbox/csv _csv.c,1.18,1.19
andrewmcnamara@users.sourceforge.net
andrewmcnamara@users.sourceforge.net
Thu, 06 Feb 2003 20:46:27 -0800
Update of /cvsroot/python/python/nondist/sandbox/csv
In directory sc8-pr-cvs1:/tmp/cvs-serv14308
Modified Files:
_csv.c
Log Message:
Commented out tests that were failing due to changed API, added tests for
dialect registry, fixed bugs in dialect registry... 8-)
Index: _csv.c
===================================================================
RCS file: /cvsroot/python/python/nondist/sandbox/csv/_csv.c,v
retrieving revision 1.18
retrieving revision 1.19
diff -C2 -d -r1.18 -r1.19
*** _csv.c 7 Feb 2003 02:36:25 -0000 1.18
--- _csv.c 7 Feb 2003 04:46:25 -0000 1.19
***************
*** 317,326 ****
}
static int
! parse_dialect_args(PyObject * self, PyObject * dia_inst, PyObject * kwargs)
{
PyObject * name_obj, * value_obj;
! Py_XINCREF(dia_inst);
if (kwargs != NULL) {
PyObject * key = PyString_FromString("dialect");
--- 317,339 ----
}
+ static PyObject *
+ get_dialect_from_registry(PyObject * name_obj)
+ {
+ PyObject *dialect_obj;
+
+ dialect_obj = PyDict_GetItem(dialects, name_obj);
+ if (dialect_obj == NULL)
+ return PyErr_Format(error_obj, "unknown dialect '%s'",
+ PyString_AsString(name_obj));
+ Py_INCREF(dialect_obj);
+ return dialect_obj;
+ }
+
static int
! parse_dialect_args(PyObject * self, PyObject * dialect, PyObject * kwargs)
{
PyObject * name_obj, * value_obj;
! Py_XINCREF(dialect);
if (kwargs != NULL) {
PyObject * key = PyString_FromString("dialect");
***************
*** 330,368 ****
if (d) {
Py_INCREF(d);
! Py_XDECREF(dia_inst);
PyDict_DelItem(kwargs, key);
! dia_inst = d;
}
Py_DECREF(key);
}
! if (dia_inst != NULL) {
int i;
PyObject * dir_list;
/* If dialect is a string, look it up in our registry */
! if (PyString_Check(dia_inst) || PyUnicode_Check(dia_inst)) {
PyObject * new_dia;
! new_dia = PyDict_GetItem(dialects, dia_inst);
! Py_DECREF(dia_inst);
if (new_dia == NULL)
return 0;
! Py_INCREF(new_dia);
! dia_inst = new_dia;
}
/* A class rather than an instance? Instanciate */
! if (PyObject_TypeCheck(dia_inst, &PyClass_Type)) {
PyObject * new_dia;
! new_dia = PyObject_CallFunction(dia_inst, "");
! Py_DECREF(dia_inst);
if (new_dia == NULL)
return 0;
! dia_inst = new_dia;
}
/* Make sure we finally have an instance */
! if (!PyInstance_Check(dia_inst) ||
! (dir_list = PyObject_Dir(dia_inst)) == NULL) {
PyErr_SetString(PyExc_TypeError,
"dialect must be an instance");
! Py_DECREF(dia_inst);
return 0;
}
--- 343,380 ----
if (d) {
Py_INCREF(d);
! Py_XDECREF(dialect);
PyDict_DelItem(kwargs, key);
! dialect = d;
}
Py_DECREF(key);
}
! if (dialect != NULL) {
int i;
PyObject * dir_list;
/* If dialect is a string, look it up in our registry */
! if (PyString_Check(dialect) || PyUnicode_Check(dialect)) {
PyObject * new_dia;
! new_dia = get_dialect_from_registry(dialect);
! Py_DECREF(dialect);
if (new_dia == NULL)
return 0;
! dialect = new_dia;
}
/* A class rather than an instance? Instanciate */
! if (PyObject_TypeCheck(dialect, &PyClass_Type)) {
PyObject * new_dia;
! new_dia = PyObject_CallFunction(dialect, "");
! Py_DECREF(dialect);
if (new_dia == NULL)
return 0;
! dialect = new_dia;
}
/* Make sure we finally have an instance */
! if (!PyInstance_Check(dialect) ||
! (dir_list = PyObject_Dir(dialect)) == NULL) {
PyErr_SetString(PyExc_TypeError,
"dialect must be an instance");
! Py_DECREF(dialect);
return 0;
}
***************
*** 372,376 ****
if (PyString_AsString(name_obj)[0] == '_')
continue;
! value_obj = PyObject_GetAttr(dia_inst, name_obj);
if (value_obj) {
if (PyObject_SetAttr(self, name_obj,
--- 384,388 ----
if (PyString_AsString(name_obj)[0] == '_')
continue;
! value_obj = PyObject_GetAttr(dialect, name_obj);
if (value_obj) {
if (PyObject_SetAttr(self, name_obj,
***************
*** 383,387 ****
}
Py_DECREF(dir_list);
! Py_DECREF(dia_inst);
}
if (kwargs != NULL) {
--- 395,399 ----
}
Py_DECREF(dir_list);
! Py_DECREF(dialect);
}
if (kwargs != NULL) {
***************
*** 1090,1094 ****
return NULL;
if (PyDict_DelItem(dialects, name_obj) < 0)
! return NULL;
Py_INCREF(Py_None);
return Py_None;
--- 1102,1107 ----
return NULL;
if (PyDict_DelItem(dialects, name_obj) < 0)
! return PyErr_Format(error_obj, "unknown dialect '%s'",
! PyString_AsString(name_obj));
Py_INCREF(Py_None);
return Py_None;
***************
*** 1098,1108 ****
csv_get_dialect(PyObject *module, PyObject *args)
{
! PyObject *name_obj, *dialect_obj;
if (!PyArg_ParseTuple(args, "O!", &PyBaseString_Type, &name_obj))
return NULL;
! dialect_obj = PyDict_GetItem(dialects, name_obj);
! Py_XINCREF(dialect_obj);
! return dialect_obj;
}
--- 1111,1119 ----
csv_get_dialect(PyObject *module, PyObject *args)
{
! PyObject *name_obj;
if (!PyArg_ParseTuple(args, "O!", &PyBaseString_Type, &name_obj))
return NULL;
! return get_dialect_from_registry(name_obj);
}