I agree, it'd be a cosmetic change and the docstring is clear. I've
came across this function some time ago while looking for a handy
"convert to unicode" function to use it in templates or at least to
prepare some values for the template.

My problem is solved by doing a string conversion before calling
to_unicode (as you already suggested), but breaking validator code
would be a bad side-effect ;-)

Thanks for your reply,

Andi

On Thu, Dec 18, 2008 at 8:30 PM, Jorge Vargas <[email protected]> wrote:
>
> On Thu, Dec 18, 2008 at 3:36 AM, Andi Albrecht
> <[email protected]> wrote:
>>
>> Hi all,
>>
>> the docstring of util.to_unicode() says clearly that it converts an
>> "encoded string to unicode string". Unfortunately when the function is
>> called with anything else than an instance of basestring (str,
>> unicode) the input value is returned without any type conversion. It's
>> actually not what I'd expected. What I'd expected is either that the
>> function raises an exception if the input value isn't an instance of
>> basestring or that the function tries to convert the input value into
>> an unicode. For example, when calling "to_unicode(1L)" I would expect
>> that the return value is u"1" but not 1L.
>>
>> Are there any concerns to add some simple type conversion. IMO something like
>>
>> if not isinstance(value, basestring):
>>  value = unicode(value)
>> if isinstanve(value, str):
>>  [...conversion as usual...]
>> return value
>>
>> is better than returning value without even trying to convert it to an
>> unicode as the function's name suggests.
>>
>> Andi
>>
> hello I assume you are talking about the 1.x branch.
>
> I don't consider this a bug, as you pointed out the docstring clearly
> states it's a str-> unicode converter
>
> Now I don't think changing the behavior is good for two reasons
> 1- util is really a package of util functions for TG not for client code
> 2- this function is used here:
> widgets/base.py:        params["value"] =
> to_unicode(self.adjust_value(value, **params))
>
> therefore changing the function will mean that the widget will not
> have ints and floats, which I'm certain are used by the validators.
>
> if you want this behavior you can do:
>
>>>> util.to_unicode(str(1L))
> u'1'
>
> we could change the function name, after all it's used only at one
> stop in the tg1 codebase ( at least that's what grep says) but why
> break for a cosmetic change?
>
> >
>

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"TurboGears" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/turbogears?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to