[IronPython] Embedding & Charsets

Michael Foord fuzzyman at voidspace.org.uk
Thu Mar 26 00:33:18 CET 2009


Igor Elyas wrote:
> Interesting info:
> 1. Calling unicode('russian symbols') throws "'ascii' codec can't 
> decode byte 0 in position 0: ordinal not in range"
In IronPython calling unicode is the same as calling str. Calling 
unicode on a bytestring with non-ascii characters in it would also blow 
up in CPython.

Michael

> 2.
> IronPython 2.0 (2.0.0.0) on .NET 2.0.50727.3082
> Type "help", "copyright", "credits" or "license" for more information.
>>>> str(u''russian symbols'.encode('cp1251'))
> Traceback (most recent call last):
>  File "<stdin>", line 1, in <module>
>  File "mscorlib", line unknown, in GetString
>  File "mscorlib", line unknown, in GetChars
>  File "mscorlib", line unknown, in Fallback
>  File "mscorlib", line unknown, in Throw
> UnicodeDecodeError: ('unknown', u'\xef', 0, 1, '')
>
> Python 2.5.4 (r254:67916, Dec 23 2008, 15:10:54) [MSC v.1310 32 bit 
> (Intel)] on win32
> Type "help", "copyright", "credits" or "license" for more information.
>>>> str(u''russian symbols'.encode('cp1251'))
> '\xef\xf0\xe8\xe2\xe5\xf2'
>
> --------------------------------------------------
> From: "Michael Foord" <fuzzyman at voidspace.org.uk>
> Sent: Wednesday, March 25, 2009 10:03 PM
> To: "Discussion of IronPython" <users at lists.ironpython.com>
> Subject: Re: [IronPython] Embedding & Charsets
>
>> Igor Elyas wrote:
>>> Ops
>>>  Deep in code on returned value  used Python str() function and 
>>> looks like:
>>>  str(self._reader.GetString(self._idx))
>>>
>>
>> You can't call str on a string if it contains non-ascii characters. :-)
>>
>> (and why would you need to...)
>>
>> Michael
>>
>>> That correct situation ?
>>>
>>> *From:* Igor Elyas <mailto:igor_elyas at hotmail.com>
>>> *Sent:* Wednesday, March 25, 2009 9:17 PM
>>> *To:* Users at lists.ironpython.com <mailto:Users at lists.ironpython.com>
>>> *Subject:* [IronPython] Embedding & Charsets
>>>
>>> Hello
>>>  I'm use IronPython 2.0.1 as embedded interpreter for read data from 
>>> database.
>>> Code for create Engine:
>>>  fEngine = Python.CreateEngine();
>>>             Ops = fEngine.CreateOperations();
>>>             // core libs
>>>             fEngine.Runtime.LoadAssembly(typeof(Decimal).Assembly);
>>>             fEngine.Runtime.LoadAssembly(typeof(IValue).Assembly); 
>>> // loading ADO.NET driver assemblies
>>>             LoadAssemblies(from v in 
>>> EtlBaseCfg.BaseCfg.Element("dlls").Elements("dll") select v.Value);
>>>  After that, I use generated Ipy class for read one value from 
>>> specific IDbDataReader:
>>>  from ETL.Common.pipes import IValuePipe
>>> class SelectExtractor(IValuePipe):
>>>     def __init__(self,adoreader,originId,storage):
>>>         self._name = originId
>>>         self._reader = adoreader
>>>         self._idx = adoreader.GetOrdinal(self._name)
>>>         self._transport = storage
>>>     def Do(self, pydict):
>>>         val = self._transport.Clone()
>>>         if not self._reader.IsDBNull(self._idx):
>>>             val.Data = self._reader.GetString(self._idx)
>>>         if pydict.ContainsKey(self._name):
>>>             pydict[self._name] = val
>>>         else:
>>>             pydict.Add(self._name,val)
>>>  This operation return string with Russian symbols and I get 
>>> Exception: "'ascii' codec can't decode byte 0 in position 0: ordinal 
>>> not in range"
>>> How I can enable unicode .NET strings ?
>>> Looks like I not initialize IronPython correctly, because this code 
>>> in Ipy console work fine with Russian charset (Russian Windows), but 
>>> not sure about other charsets. I need use .NET unicode strings.
>>>
>>> Best regards
>>>
>>> Igor Elyas
>>>
>>> ------------------------------------------------------------------------ 
>>>
>>> _______________________________________________
>>> Users mailing list
>>> Users at lists.ironpython.com
>>> http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
>>> ------------------------------------------------------------------------ 
>>>
>>>
>>> _______________________________________________
>>> Users mailing list
>>> Users at lists.ironpython.com
>>> http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
>>>
>>
>>
>> -- 
>> http://www.ironpythoninaction.com/
>>
>> _______________________________________________
>> Users mailing list
>> Users at lists.ironpython.com
>> http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
>>
> _______________________________________________
> Users mailing list
> Users at lists.ironpython.com
> http://lists.ironpython.com/listinfo.cgi/users-ironpython.com


-- 
http://www.ironpythoninaction.com/




More information about the Ironpython-users mailing list