Issue2726

classification
Title: os.uname() throws IllegalArgumentException on Windows (Chinese localisation)
Type: crash Severity: normal
Components: Core Versions: Jython 2.7
Milestone: Jython 2.7.2
process
Status: pending Resolution: fixed
Dependencies: Superseder:
Assigned To: jeff.allen Nosy List: jeff.allen, k870611
Priority: normal Keywords:

Created on 2018-12-31.04:49:09 by k870611, last changed 2019-01-05.10:38:29 by jeff.allen.

Messages
msg12218 (view) Author: k870611 (k870611) Date: 2018-12-31.04:49:09
In Jython 2.7.1, I just simply use the code 

>>>import platform
>>>platform.system()

and it return Error

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\jython2.7.1\Lib\platform.py", line 1396, in system
    return uname()[0]
  File "C:\jython2.7.1\Lib\platform.py", line 1213, in uname
    machine = os.uname()[4]
        at org.python.core.PyString.<init>(PyString.java:57)
        at org.python.core.PyString.<init>(PyString.java:70)
        at org.python.core.PyString.<init>(PyString.java:74)
        at org.python.core.Py.newString(Py.java:647)
        at org.python.modules.posix.PosixModule.uname(PosixModule.java:1173)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
java.lang.IllegalArgumentException: java.lang.IllegalArgumentException: Cannot create PyString with non-byte value
msg12220 (view) Author: k870611 (k870611) Date: 2018-12-31.04:52:43
In Cpython it will the same code is return

>>> import platform
>>> platform.system()
'Windows'
msg12222 (view) Author: k870611 (k870611) Date: 2018-12-31.04:54:55
And the Same code, In jython 2.7.0 it will return

>>> import platform
>>> platform.system()
'Java'
>>>
msg12224 (view) Author: Jeff Allen (jeff.allen) Date: 2018-12-31.11:58:15
It works for me on all versions. However, I suspect a non-ascii host name may be behind this for you. I know you're not asking for the host name, but you can see from the dump that it uses os.uname(), and that's where it blows up.

Could you confirm? What does this give you:

>>> import java
>>> java.net.InetAddress.getLocalHost().getHostName().encode('utf-8')

If I guessed correctly, this is fixed already #2608 (essentially by adding the encode('utf-8')). (I'm marking this as duplpicate, pending contradiction of this theory.)

#2608 will be released in Jython 2.7.2, or you could download and build the current development version.
msg12230 (view) Author: k870611 (k870611) Date: 2018-12-31.15:34:58
The result I get of java.net.InetAddress.getLocalHost().getHostName().encode('utf-8')

C:\Users\Huang>jython
Jython 2.7.1 (default:0df7adb1b397, Jun 30 2017, 19:02:43)
[Java HotSpot(TM) 64-Bit Server VM (Oracle Corporation)] on java1.8.0_162
Type "help", "copyright", "credits" or "license" for more information.
>>> import java
>>> java.net.InetAddress.getLocalHost().getHostName().encode('utf-8')
'DESKTOP-VGULG8Q'
>>>

and I have same error when I run os.uname()


C:\Users\Huang>jython
Jython 2.7.1 (default:0df7adb1b397, Jun 30 2017, 19:02:43)
[Java HotSpot(TM) 64-Bit Server VM (Oracle Corporation)] on java1.8.0_162
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.uname()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
        at org.python.core.PyString.<init>(PyString.java:57)
        at org.python.core.PyString.<init>(PyString.java:70)
        at org.python.core.PyString.<init>(PyString.java:74)
        at org.python.core.Py.newString(Py.java:647)
        at org.python.modules.posix.PosixModule.uname(PosixModule.java:1173)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
java.lang.IllegalArgumentException: java.lang.IllegalArgumentException: Cannot create PyString with non-byte value
>>>


can you provide link for me to download 2.7.2 version ? I can find it on Google. Thx
msg12232 (view) Author: Jeff Allen (jeff.allen) Date: 2018-12-31.18:42:35
Thanks. That's interesting. By which I mean "I was wrong".

In 2.7.1, the exact line at org.python.modules.posix.PosixModule.uname(PosixModule.java:1173) is where we wrap a version string as a Python str/bytes object, and it must contain some character with a code > 255, where all we expect is digits and dots.

What does this (at a shell/command prompt) give you?

    cmd.exe /C ver

This is what we use to obtain the version string. But clearly we are not robust enough against possible variation. What does this give you at the Jython prompt?

>>> import sys
>>> sys.getSystemVersionString()

It should return the problem string. We can probably fix this by scanning for digits and dots, rather than looking for the word "version", as we are, but I'm curious what has actually happened.


Sorry, 2.7.2 is not out yet, even as a beta. On the plus side, it means we could fix this for 2.7.2. You could download and build from https://hg.python.org/jython or https://github.com/jythontools/jython (it builds with Apache Ant), but I don't expect it to fix the bug. It would be useful if you were able to do that with a version where I've treated the bug, as I can't reproduce the problem here.
msg12234 (view) Author: k870611 (k870611) Date: 2019-01-01.03:42:56
Yes, like what you say, it contain non-ascii word in cmd.exe /C ver and sys.getSystemVersionString()

C:\Users\Huang>cmd.exe /C ver

Microsoft Windows [版本 10.0.17134.472]

C:\Users\Huang>jython
Jython 2.7.1 (default:0df7adb1b397, Jun 30 2017, 19:02:43)
[Java HotSpot(TM) 64-Bit Server VM (Oracle Corporation)] on java1.8.0_162
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.getSystemVersionString()
u'Microsoft Windows [\u7248\u672c 10.0.17134.472]'
>>>


and it is where the problem cause (I guess). And I believe it will be solved soon.

BTW, I have the another problem when I build the java-2.7.2.a1 (by ant-1.9.13, GitHub \jython-master\maven), does I missing any file ?

BUILD FAILED
C:\Jython\jython-828c41688a52\maven\build.xml:63: The following error occurred while executing this line:
C:\Jython\jython-828c41688a52\maven\build.xml:129: The following error occurred while executing this line:
C:\Jython\jython-828c41688a52\maven\build.xml:166: Execute failed: java.io.IOException: Cannot run program "gpg" (in directory "C:\Jython\jython-828c41688a52"): CreateProcess error=2, 系統找不到指定的檔案。
        at java.lang.ProcessBuilder.start(ProcessBuilder.java:1048)
        at java.lang.Runtime.exec(Runtime.java:620)
        at org.apache.tools.ant.taskdefs.launcher.Java13CommandLauncher.exec(Java13CommandLauncher.java:58)
        at org.apache.tools.ant.taskdefs.Execute.launch(Execute.java:426)
        at org.apache.tools.ant.taskdefs.Execute.execute(Execute.java:440)
msg12236 (view) Author: Jeff Allen (jeff.allen) Date: 2019-01-01.10:43:30
That's very helpful, thanks. (And a happy new year.) I think os.uname() is easily fixed, and the rest will hopefully follow.

It would be difficult to create a test for this particular problem that is effective on machines we use for development.

The error you see from Ant is because you are in the wrong directory. Run ant in the root of the checkout. (The one you have found is something to do with creating the artefacts for maven, after the build. I don't think I've ever looked at it before!)
msg12238 (view) Author: k870611 (k870611) Date: 2019-01-01.13:36:25
Thanks for your suggestion, build is work!!!! But os.uname() and platform still not work now (just inform, i believe it will be solve in next formal release. Happy new year ~~~~~~~~)


C:\Jython\jython-master\dist\bin>jython
Jython 2.7.2a1+ (, ?@?? 1 2019, 21:29:26)
[Java HotSpot(TM) 64-Bit Server VM (Oracle Corporation)] on java1.8.0_162
Type "help", "copyright", "credits" or "license" for more information.
>>> import java
>>> java.net.InetAddress.getLocalHost().getHostName().encode('utf-8')
'DESKTOP-VGULG8Q'
>>>
>>> import sys
>>> sys.getSystemVersionString()
u'Microsoft Windows [\u7248\u672c 10.0.17134.472]'
>>>
>>> import os
>>> os.uname()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
        at org.python.core.PyString.<init>(PyString.java:57)
        at org.python.core.PyString.<init>(PyString.java:70)
        at org.python.core.PyString.<init>(PyString.java:74)
        at org.python.core.Py.newString(Py.java:646)
        at org.python.modules.posix.PosixModule.uname(PosixModule.java:1173)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
java.lang.IllegalArgumentException: java.lang.IllegalArgumentException: Cannot create PyString with non-byte value
>>> import platform
>>> platform.system()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Jython\jython-master\dist\Lib\platform.py", line 1396, in system
    return uname()[0]
  File "C:\Jython\jython-master\dist\Lib\platform.py", line 1213, in uname
    machine = os.uname()[4]
        at org.python.core.PyString.<init>(PyString.java:57)
        at org.python.core.PyString.<init>(PyString.java:70)
        at org.python.core.PyString.<init>(PyString.java:74)
        at org.python.core.Py.newString(Py.java:646)
        at org.python.modules.posix.PosixModule.uname(PosixModule.java:1173)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
java.lang.IllegalArgumentException: java.lang.IllegalArgumentException: Cannot create PyString with non-byte value
>>>
msg12240 (view) Author: Jeff Allen (jeff.allen) Date: 2019-01-01.17:02:03
Giving this a more accurate name, now we have diagnosed it.
msg12242 (view) Author: Jeff Allen (jeff.allen) Date: 2019-01-02.08:08:31
I claim to have fixed this in this change set:
https://hg.python.org/jython/rev/9f020857adce
Separately, I've pushed some refactoring that simplifies the code:
https://hg.python.org/jython/rev/4bbacf481833

As my environment doesn't create the problem, it would be useful to confirm that works where the problem arose. Thanks for identifying this fragility.
msg12274 (view) Author: k870611 (k870611) Date: 2019-01-05.10:01:12
BTW. May I ask you a question - how to get platform in 2.7.0, 
because in 2.7.0, platform.system() will return Java on Windows and Linux both. 

Thanks ~~~~~~~
msg12276 (view) Author: Jeff Allen (jeff.allen) Date: 2019-01-05.10:38:29
You can use this:

>>> import os
>>> os.name
'java'
>>> os._name
'nt'

It is reasonable to say that the "platform" is Java, and so that is what we return as os.name. But many programs actually want to know what the file system is like, so practicality suggests os.name really ought to tell you that. Unfortunately, practicality came too late to change things, but we added os._name to compensate. Portable code looks something like:

if os.name=='nt' or (os.name=='java' and os._name=='nt'):
    # Do windows stuff
History
Date User Action Args
2019-01-05 10:38:29jeff.allensetmessages: + msg12276
2019-01-05 10:01:12k870611setmessages: + msg12274
2019-01-02 08:08:31jeff.allensetstatus: open -> pending
resolution: accepted -> fixed
messages: + msg12242
2019-01-01 17:02:03jeff.allensettype: crash
title: platform.system() return java.lang.IllegalArgumentException -> os.uname() throws IllegalArgumentException on Windows (Chinese localisation)
superseder: Encoding problem in os.uname with non-ascii host name ->
messages: + msg12240
severity: urgent -> normal
2019-01-01 13:36:25k870611setmessages: + msg12238
2019-01-01 10:43:30jeff.allensetmessages: + msg12236
2019-01-01 03:42:56k870611setmessages: + msg12234
2018-12-31 18:42:35jeff.allensetstatus: pending -> open
assignee: jeff.allen
resolution: duplicate -> accepted
messages: + msg12232
2018-12-31 15:34:58k870611setmessages: + msg12230
2018-12-31 11:58:15jeff.allensetstatus: open -> pending
nosy: + jeff.allen
messages: + msg12224
priority: normal
superseder: Encoding problem in os.uname with non-ascii host name
milestone: Jython 2.7.1 -> Jython 2.7.2
resolution: duplicate
2018-12-31 04:54:55k870611setmessages: + msg12222
2018-12-31 04:52:43k870611setmessages: + msg12220
2018-12-31 04:49:09k870611create