[IronPython] Oddity with Setting Output Stream

Dino Viehland dinov at microsoft.com
Sat Nov 1 23:17:32 CET 2008

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

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',
scope = engine.CreateScope()
code = source.Compile()


All the best,

Michael Foord


Users mailing list
Users at lists.ironpython.com

More information about the Ironpython-users mailing list