[IronPython] Oddity with Setting Output Stream

Michael Foord fuzzyman at voidspace.org.uk
Sun Nov 2 13:36:06 CET 2008


Tomas Matousek wrote:
> You can also pass new UTF8Encoding(false) instead of Encoding.UTF8.
>   

OK, thanks.

Michael

> Tomas
>
> -----Original Message-----
> From: users-bounces at lists.ironpython.com [mailto:users-bounces at lists.ironpython.com] On Behalf Of Dino Viehland
> Sent: Saturday, November 01, 2008 3:18 PM
> To: Discussion of IronPython
> Subject: Re: [IronPython] Oddity with Setting Output Stream
>
> I pinged Tomas on this and he thinks this is by design and I'm tending to agree (even though it is just a side effect of us using a StreamWriter).  You can always pass a Stream and a TextWriter instead to get full control over this behavior.
>
> Good test though and I'll follow up w/ Bill to make sure this behavior gets spec'd.
>
> -----Original Message-----
> From: users-bounces at lists.ironpython.com [mailto:users-bounces at lists.ironpython.com] On Behalf Of Michael Foord
> Sent: Friday, October 31, 2008 7:35 AM
> To: Discussion of IronPython
> Subject: [IronPython] Oddity with Setting Output Stream
>
> Hello guys,
>
> Another oddity with the IronPython 2 hosting API. This one may be the
> correct behaviour, but it is different from IronPython 1.
>
> When we set a UTF8 output stream on a runtime we see a UTF8 BOM being
> written with the first output. I'm sure this didn't happen with
> IronPython 1 because I now have failing tests!
>
> The following code that traps standard out using a custom stream prints:
>
> out: u'\ufeff'
> out: 'foobar'
> out: '\r\n'
> out: 'foobar'
> out: '\r\n'
>
> import sys
> import clr
> clr.AddReference('IronPython')
> clr.AddReference('Microsoft.Scripting')
>
> from IronPython.Hosting import Python
> from Microsoft.Scripting import SourceCodeKind
> from System.Text import Encoding
> from System.IO import MemoryStream
>
> class CustomStream(MemoryStream):
>     def __new__(cls, prefix):
>         return MemoryStream.__new__(cls)
>
>     def __init__(self, prefix):
>         self._prefix = prefix
>
>     def Write(self, buffer, offset, count):
>         print self._prefix,
>         print repr(Encoding.UTF8.GetString(buffer, offset, count))
>
> engine = Python.CreateEngine()
> engine.Runtime.IO.SetOutput(CustomStream('out:'), Encoding.UTF8)
>
> source = engine.CreateScriptSourceFromString('print "foobar"\r\n',
> SourceCodeKind.Statements)
> scope = engine.CreateScope()
> code = source.Compile()
>
> code.Execute(scope)
> code.Execute(scope)
>
> All the best,
>
> Michael Foord
>
> --
> 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
>
> _______________________________________________
> Users mailing list
> Users at lists.ironpython.com
> http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
>   


-- 
http://www.ironpythoninaction.com/
http://www.voidspace.org.uk/blog





More information about the Ironpython-users mailing list