[plugin] binary sys.stdout/err on win32

Alexander Belchenko bialix at ukr.net
Mon Jun 19 11:48:36 BST 2006


John Arbash Meinel пишет:
> I just created a very simple plugin which sets sys.stdout and stderr to
> binary for win32.
> 
> This is the sum total of the code:
> 
> import sys
> 
> if sys.platform in ('win32', 'cygwin'):
>     try:
>         import msvcrt, os
>     except ImportError:
>         pass
>     else:
>         msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)
>         msvcrt.setmode(sys.stderr.fileno(), os.O_BINARY)
> 
> I'm not sure if cygwin has msvcrt, or if sys.stdout is already in
> binary, but I thought I would cover it, just in case.

Actually, cygwin does not have msvcrt (it's win32-only library).

Furthermore, on all platform sys.stdout is a file-like object opened in
text mode. So on cygwin behaviour depends on cygwin installation,
because during installation user select what type of line-endings he
want to use (LF/CRLF).

> It isn't as difficult as I thought it would be. But now we need to
> decide if we always want binary mode or not. I think commands like diff
> and bundle definitely want binary mode set.

Command 'bundle' want binary mode. But I'm not sure about 'diff'.

> The windows command console will do the right thing when seeing a plain
> \r. The only thing I know of that doesn't handle it well is notepad, in
> the case that you redirected the output to a file.

Yes, and because notepad is pre-installed defualt file editor/viewer, it
create problems on windows.

> I would like to hear Alexander's position on this, as someone who works
> within windows all the time. For myself, I always use Vim on all
> platforms, and it copes without any problems.

I'm -1 on making this binary mode default. But I'm strongly positive to
merge it into mainline and make them optionally available. Or for some
commands (like 'bundle') or via some sort configuration. Per example, as
options in bazaar.conf, like this:

[DEFAULT]
binary=Yes/No

or via global bzr option '--binary'. But default behaviour should be
native CRLF on win32.

> I'm tempted to just put this code in bzr mainline, and just live with
> it. I'm not sure exactly where it should go in the stack, I'm guessing
> either in the 'bzr' front end, or in bzrlib.commands.main().
> 
> The other possibility would be to just use the encoding_type that I've
> already specified for other stuff. And if the encoding_type == 'exact'
> set sys.stdout to binary mode. Otherwise if we are translating
> filenames, it seems like we could translate line-endings at the same time.
> 
> Any thoughts?

--
Alexander






More information about the bazaar mailing list