[GRASS-dev] start supporting python3 for GRASS

Glynn Clements glynn at gclements.plus.com
Thu May 5 11:59:41 PDT 2016

Pietro wrote:

> I committed some changes to start supporting python3 (r68348 - r68367)

Regarding r68357 ("fix in python3 sys.stdout.write accept only strings not bytes"):

             if not lines or lines[0] != b"@ARGS_PARSED@":
        -        sys.stdout.write(s)
        +        stdout = os.fdopen(sys.stdout.fileno(), 'wb')
        +        stdout.write(s)

You can get the underlying byte stream for a text stream using the
.buffer property (i.e. sys.stdout.buffer.write()).

OTOH, this probably doesn't matter much here, as sys.stdout will
almost certainly be a real file (i.e. .fileno() will be valid).

For the script module, it might be better to just replace the standard
streams with their underlying byte streams, i.e.

	sys.stdin = sys.stdin.detach()
	sys.stdout = sys.stdout.detach()
	sys.stderr = sys.stderr.detach()

[The .detach() method returns the underlying byte stream *after*
detaching it from the TextIOWrapper, to avoid any issues arising from
attempting to use both.]

Anything using the script module should be using byte strings
throughout (in spite of how awkward Python 3 tries to make this).

Glynn Clements <glynn at gclements.plus.com>

More information about the grass-dev mailing list