]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
No longer attempt to instantiate python classes describing dialects. This
authorAndrew McNamara <andrewm@object-craft.com.au>
Tue, 11 Jan 2005 02:18:36 +0000 (02:18 +0000)
committerAndrew McNamara <andrewm@object-craft.com.au>
Tue, 11 Jan 2005 02:18:36 +0000 (02:18 +0000)
was done because we were previously performing validation of the dialect
from python, but this is now down within the C module. Also, the method
we were using to detect classes did not work with new-style classes.

Lib/test/test_csv.py
Modules/_csv.c

index cdda17976cda7489de2cf4dc74e7caf76ffb20c5..34177ccec390cb9584f75bf225a9704df1414082 100644 (file)
@@ -24,25 +24,12 @@ class Test_Csv(unittest.TestCase):
         self.assertRaises(TypeError, ctor, arg, delimiter = 0)
         self.assertRaises(TypeError, ctor, arg, delimiter = 'XX')
         self.assertRaises(csv.Error, ctor, arg, 'foo')
-        self.assertRaises(TypeError, ctor, arg, None)
         self.assertRaises(TypeError, ctor, arg, delimiter=None)
         self.assertRaises(TypeError, ctor, arg, delimiter=1)
         self.assertRaises(TypeError, ctor, arg, quotechar=1)
         self.assertRaises(TypeError, ctor, arg, lineterminator=None)
         self.assertRaises(TypeError, ctor, arg, lineterminator=1)
         self.assertRaises(TypeError, ctor, arg, quoting=None)
-#       We now allow this, only raising an exception if quoting is needed.
-#        self.assertRaises(TypeError, ctor, arg, quotechar=None)
-#        self.assertRaises(TypeError, ctor, arg,
-#                          quoting=csv.QUOTE_NONE, escapechar=None)
-#       No longer complains about dialects with invalid attributes [AM]
-#        class BadDialect:
-#            bad_attr = 0
-#        self.assertRaises(AttributeError, csv.reader, [], BadDialect)
-        class BadClass:
-            def __init__(self):
-                raise IOError
-        self.assertRaises(IOError, csv.reader, [], BadClass)
 
     def test_reader_arg_valid(self):
         self._test_arg_valid(csv.reader, [])
@@ -258,11 +245,6 @@ class TestDialectRegistry(unittest.TestCase):
         self.assertRaises(csv.Error, csv.unregister_dialect, "nonesuch")
         self.assertRaises(TypeError, csv.register_dialect, None)
         self.assertRaises(TypeError, csv.register_dialect, None, None)
-        self.assertRaises(TypeError, csv.register_dialect, "nonesuch", None)
-        class bogus:
-            def __init__(self):
-                raise KeyError
-        self.assertRaises(KeyError, csv.register_dialect, "nonesuch", bogus)
 
     def test_registry(self):
         class myexceltsv(csv.excel):
index 405bc7112c35106737f838d4779314a2730d7d06..34870fc08c16615ff2946845d87b5126045a257f 100644 (file)
@@ -288,40 +288,6 @@ Dialect_dealloc(DialectObj *self)
         self->ob_type->tp_free((PyObject *)self);
 }
 
-/*
- * Return a new reference to a dialect instance
- *
- * If given a string, looks up the name in our dialect registry
- * If given a class, instantiate (which runs python validity checks)
- * If given an instance, return a new reference to the instance
- */
-static PyObject *
-dialect_instantiate(PyObject *dialect)
-{
-       Py_INCREF(dialect);
-       /* If dialect is a string, look it up in our registry */
-       if (IS_BASESTRING(dialect)) {
-               PyObject * new_dia;
-               new_dia = get_dialect_from_registry(dialect);
-               Py_DECREF(dialect);
-               return new_dia;
-       }
-       /* A class rather than an instance? Instantiate */
-       if (PyObject_TypeCheck(dialect, &PyClass_Type)) {
-               PyObject * new_dia;
-               new_dia = PyObject_CallFunction(dialect, "");
-               Py_DECREF(dialect);
-               return new_dia;
-       }
-       /* Make sure we finally have an instance */
-       if (!PyInstance_Check(dialect)) {
-               PyErr_SetString(PyExc_TypeError, "dialect must be an instance");
-               Py_DECREF(dialect);
-               return NULL;
-       }
-       return dialect;
-}
-
 static char *dialect_kws[] = {
        "dialect",
        "delimiter",
@@ -371,9 +337,12 @@ dialect_init(DialectObj * self, PyObject * args, PyObject * kwargs)
        Py_XINCREF(skipinitialspace);
        Py_XINCREF(strict);
        if (dialect != NULL) {
-               dialect = dialect_instantiate(dialect);
-               if (dialect == NULL)
-                       goto err;
+               if (IS_BASESTRING(dialect)) {
+                       dialect = get_dialect_from_registry(dialect);
+                       if (dialect == NULL)
+                               goto err;
+               } else
+                       Py_INCREF(dialect);
 #define DIALECT_GETATTR(v, n) \
                if (v == NULL) \
                        v = PyObject_GetAttrString(dialect, n)