[Python-Dev] Problems with unicode_literals

Benjamin Peterson benjamin at python.org
Sat Jan 17 04:52:40 CET 2009


On Fri, Jan 16, 2009 at 9:45 PM, Barry Warsaw <barry at python.org> wrote:
>
> The optparse one could easily be fixed for 2.6, if we agree it should be
> fixed.  This untested patch should do it I think:
>
> Index: Lib/optparse.py
> ===================================================================
> - --- Lib/optparse.py   (revision 68465)
> +++ Lib/optparse.py     (working copy)
> @@ -994,7 +994,7 @@
>         """add_option(Option)
>            add_option(opt_str, ..., kwarg=val, ...)
>         """
> - -        if type(args[0]) is types.StringType:
> +        if type(args[0]) in types.StringTypes:
>             option = self.option_class(*args, **kwargs)
>         elif len(args) == 1 and not kwargs:
>             option = args[0]

It'd probably be better to replace that whole line with
isinstance(args[0], basestring).

>
> The fact that 'a' and 'b' are unicodes and not accepted as keyword arguments
> is probably the tougher problem.  I haven't yet looked at what it might take
> to fix.  Is it worth fixing in 2.6 or is this a wait-for-2.7 thing?

Actually, this looks like a one line fix, too:

--- Python/ceval.c      (revision 68625)
+++ Python/ceval.c      (working copy)
@@ -2932,7 +2932,8 @@
                        PyObject *keyword = kws[2*i];
                        PyObject *value = kws[2*i + 1];
                        int j;
-                       if (keyword == NULL || !PyString_Check(keyword)) {
+                       if (keyword == NULL || !(PyString_Check(keyword) ||
+                                                PyUnicode_Check(keyword))) {
                                PyErr_Format(PyExc_TypeError,
                                    "%.200s() keywords must be strings",
                                    PyString_AsString(co->co_name));

But I agree with Guido when he says this should be a 2.7 feature.

-- 
Regards,
Benjamin


More information about the Python-Dev mailing list