Message11946

Author jeff.allen
Recipients amak, jeff.allen, otmarhumbel
Date 2018-05-05.08:24:26
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1525508667.78.0.682650639539.issue2659@psf.upfronthosting.co.za>
In-reply-to
Content
Here's a puzzle. In a Powershell window where I have:
PS jython-jvm9> chcp
Active code page: 936

On Java 7:
Jython 2.7.2a1+ (default:6b912cfd485a+, May 4 2018, 23:21:27)
[Java HotSpot(TM) 64-Bit Server VM (Oracle Corporation)] on java1.7.0_60
Type "help", "copyright", "credits" or "license" for more information.
>>> from java.io import InputStreamReader, BufferedReader
>>> from java.lang import ProcessBuilder
>>> pb = ProcessBuilder(["cmd", "/c", "chcp"])
>>> p = pb.start()
>>> r = BufferedReader( InputStreamReader( p.getInputStream() ) )
>>> r.readLine()
u'Active code page: 936'

Hurrah! On Java 8:
PS jython-jvm9> dist\bin\jython
Jython 2.7.2a1+ (default:6b912cfd485a+, May 4 2018, 23:21:27)
[Java HotSpot(TM) 64-Bit Server VM (Oracle Corporation)] on java1.8.0_151
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.stdout.encoding
'cp850'
>>> from java.io import InputStreamReader, BufferedReader
>>> from java.lang import ProcessBuilder
>>> pb = ProcessBuilder(["cmd", "/c", "chcp"])
>>> p = pb.start()
>>> r = BufferedReader( InputStreamReader( p.getInputStream() ) )
>>> r.readLine()
u'Active code page: 850'

:( And on Java 9 it's the same.

In a further twist, if I call pb.inheritIO() before I spawn the command, then it senses the console encoding correctly, but the message I want goes directly to the console (Java 9):
>>> p = pb.inheritIO().start()
>>> Active code page: 936

It looks like spawning off chcp doesn't do the trick on all version, but on exactly those where it fails (from Java 8 onwards) I have a registry entry:

>>> from java.lang import System
>>> System.getProperty("sun.stdout.encoding")
u'ms936'

Together these cover the observed cases, but it all feels a tad precarious. It depends on behaviours I don't think are guaranteed. OTOH if it fails I get cp850, which is not the end of the world and can be diagnosed by the user.
History
Date User Action Args
2018-05-05 08:24:27jeff.allensetmessageid: <1525508667.78.0.682650639539.issue2659@psf.upfronthosting.co.za>
2018-05-05 08:24:27jeff.allensetrecipients: + jeff.allen, otmarhumbel, amak
2018-05-05 08:24:27jeff.allenlinkissue2659 messages
2018-05-05 08:24:26jeff.allencreate