[IronPython] how to change default encoding of string and file at runtime?

Dino Viehland dinov at exchange.microsoft.com
Wed Dec 28 05:00:17 CET 2005


Cool, then obviously it's just a bug and we'll need to support this and I think there'll be no problems getting this in the next release.  Thanks for pointing that out.

________________________________________
From: users-bounces at lists.ironpython.com On Behalf Of Seo Sanghyeon
Sent: Tuesday, December 27, 2005 7:01 PM
To: Discussion of IronPython
Subject: Re: [IronPython] how to change default encoding of string and file     at runtime?

On Tue, Dec 27, 2005 at 04:57:32PM -0800, Dino Viehland wrote:
> This is an interesting issue...  If I take your same code and run it
> on CPython it raises an exception: (snip)

Actually, this works when you set "defaultencoding".

$ python
Python 2.3.5 (#2, Nov 20 2005, 16:40:39)
[GCC 4.0.3 20051111 (prerelease) (Debian 4.0.2-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> s = u'\u6211'
>>> import sys
>>> sys.getdefaultencoding()
'ascii'
>>> str(s)
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
UnicodeEncodeError: 'ascii' codec can't encode character u'\u6211' in position 0: ordinal not in range(128)
>>> reload(sys)
<module 'sys' (built-in)>
>>> sys.setdefaultencoding('gbk')
>>> sys.getdefaultencoding()
'gbk'
>>> str(s)
'\xce\xd2'
>>>

Hope this help,
Seo Sanghyeon
_______________________________________________
users mailing list
users at lists.ironpython.com
http://lists.ironpython.com/listinfo.cgi/users-ironpython.com


More information about the Ironpython-users mailing list