[unicode] inconvenient unicode conversion of non-string arguments

Holger Joukl Holger.Joukl at LBBW.de
Wed Dec 13 04:25:30 EST 2006


Hi there,

I consider the behaviour of unicode() inconvenient wrt to conversion of
non-string
arguments.
While you can do:

>>> unicode(17.3)
u'17.3'

you cannot do:

>>> unicode(17.3, 'ISO-8859-1', 'replace')
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: coercing to Unicode: need string or buffer, float found
>>>

This is somehow annoying when you want to convert a mixed-type argument
list
to unicode strings, e.g. for a logging system (that's where it bit me) and
want to make sure that possible raw string arguments are also converted to
unicode without errors (although by force).
Especially as this is a performance-critical part in my application so I
really
do not like to wrap unicode() into some custom tounicode() function that
handles
such cases by distinction of argument types.

Any reason why unicode() with a non-string argument should not allow the
encoding and errors arguments?
Or some good solution to work around my problem?

(Currently running on python 2.4.3)

Regards,
Holger

Der Inhalt dieser E-Mail ist vertraulich. Falls Sie nicht der angegebene
Empfänger sind oder falls diese E-Mail irrtümlich an Sie adressiert wurde,
verständigen Sie bitte den Absender sofort und löschen Sie die E-Mail
sodann. Das unerlaubte Kopieren sowie die unbefugte Übermittlung sind nicht
gestattet. Die Sicherheit von Übermittlungen per E-Mail kann nicht
garantiert werden. Falls Sie eine Bestätigung wünschen, fordern Sie bitte
den Inhalt der E-Mail als Hardcopy an.

The contents of this  e-mail are confidential. If you are not the named
addressee or if this transmission has been addressed to you in error,
please notify the sender immediately and then delete this e-mail.  Any
unauthorized copying and transmission is forbidden. E-Mail transmission
cannot be guaranteed to be secure. If verification is required, please
request a hard copy version.





More information about the Python-list mailing list