Why is sys.stdin.encoding under PyDev / Jython different from standalone Jython console? - jython

I am trying to get python-gnupg to work in Jython under PyDev under Eclipse. There is a problem which seems to be caused by the sys.stdin.encoding, which is tested by python-gnupg at it's initialisation.
I found that the following script
import sys
print sys.stdin.encoding
outptus cp850 when I run it from Python standalone in a console and from Jython standalone in a console. When I run it in Python under PyDev, it outputs Cp1252, and when I run it in Jython under PyDev, I get None.
Looking at python-gnupg, both encodings cp850 and Cp1252 seem to be ok, but if it's None, it raises an exception:
File "C:\Python27\lib\site-packages\gnupg.py", line 487, in __init__
self._collect_output(p, result, stdin=p.stdin)
File "C:\Python27\lib\site-packages\gnupg.py", line 561, in _collect_output
stderr = codecs.getreader(self.encoding)(process.stderr)
File "C:\jython2.5.2\Lib\codecs.py", line 920, in getreader
return lookup(encoding).streamreader
at org.python.core.codecs.normalizestring(codecs.java:101)
at org.python.core.codecs.lookup(codecs.java:75)
at org.python.modules._codecs.lookup(_codecs.java:33)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
java.lang.NullPointerException: java.lang.NullPointerException
Looks like there are differences between PyDev and standalone encodings, as well as no (?) encoding for PyDev/Jython? I'd really like to use the convenient PyDev development environment, but how can I get the sys.stdin.encoding to be something sensible when developing a Jython program?
Update 1
Maybe the problem is related to one of the following Jython bug reports:
http://bugs.jython.org/issue1839
http://bugs.jython.org/issue1807 (see msg6717)
But then again, these seem to be independent from PyDev/Eclipse..?

It's actually a Jython issue:
In PySystemState.java, when setting up the encodings, Jython will skip setting any encoding if it's not a tty console:
private void initEncoding() {
String encoding = registry.getProperty(PYTHON_CONSOLE_ENCODING);
if (encoding == null) {
return;
}
for (PyFile stdStream : new PyFile[] {(PyFile)this.stdin, (PyFile)this.stdout,
(PyFile)this.stderr}) {
if (stdStream.isatty()) {
stdStream.encoding = encoding;
}
}
}
So, a workaround could be creating a java module to set that (as encoding is a public field of PyFile, but does not have a setter from the Jython side -- or you could use java reflection to set it)...
Maybe you could ask the Jython guys why this is made in the first place (I think the encoding could be set even if it was not a tty device, but I'm not sure which implications could it have and why it's done that way).
EDIT to summarise the results of the many comments below, between the developers of Jython and PyDev as well as the author of the original question (Philip Jenvey, Fabio Zadrozny and Christian Gelinek, resp.):
During the progress, a couple of test scripts were discussed and developed:
A Python unit test written by Philip to test different encodings set by minimal custom created sitecusomize.py scripts. Fabio thinks that the reason for it not actually setting the encoding is because sys.stdin.encoding is inherited from the parent process instead configured by the unit test script.
A Java test program written by Christian which sets up custom I/O streams for creating a Python subprocess, which was found by Fabio to be more similar to how PyDev sets up the subprocess.
As this didn't work as expected by Fabio, he remembered that PyDev also uses the PYTHONIOENCODING environment variable.
As was pointed out by Philip, the current (2.5) version of Jython doesn't support the PYTHONIOENCODING environment variable, which is used by PyDev (in combination with sitecusomize.py) to set the encoding:
You can log a bug for it, although PYTHONIOENCODING was only added in CPython 2.6, so Jython probably wouldn't support it until 2.7 (Jython's skipping 2.6).

I believe you should be able to explicitly encoding on the launch properties.
Run configurations -> Python run -> Xxx -> Common tab -> Encoding.
PyDev inherits some settings from global Eclipse settings. On Linux the encoding is sane UTF-8 everywhere, but some cranky operating systems cause their own issues..
Also you can probably detect encoding in your .py main module and have PyDev specific hack to fix problems related to your configuration.

Related

Read from stdin with monkeyrunner

How do I read from stdin with monkeyrunner? I tried the following code.
import sys
print("type something")
something = sys.stdin.readline()
print(something)
I also tried the following:
print("type something")
something = raw_input()
print(something)
In both cases the program prints "type something" but it does not respond after I type something. It seems i am making some silly mistake?
This seems to be a bug with Jython 2.5.3, the version that is included with MonkeyRunner. The issue says Mac OS X but I am able to reproduce on Ubuntu.
To fix it, you can download the Jython 2.5.4rc1 standalone Jar from the Jython website (download link) and copy it into the $SDK/tools/lib directory. Note that the Jython 2.7.0 standalone Jar will not work properly. You don't need to rename the new Jar, but you do need to (re)move the old jython-standalone-2.5.3.jar from the directory.
Another option (if you only need user input) is to use the input function in the MonkeyRunner class.

Jython does not resolve python imports from bundled Lib files/folders on Weblogic 10.3.5

I am new to Jython and Python, trying to build a prototype that makes use of Python code to be called from within Java. The code I am developing works in Jetty and in standalone mode (running java -jar from the command line), but not when deployed to weblogic.
How can I make weblogic(10.3.5) server/Jython recognize the Lib folder within jython-standalone-2.5.4-rc1.jar?
My Java code uses the JythonObjectFactory to invoke python modules as outlined in the Jython book:
http://www.jython.org/jythonbook/en/1.0/JythonAndJavaIntegration.html
The Python modules are using external libraries like csv, logging etc. that are not packaged with jython.jar, hence I am using jython-standalone jar.
The java code includes an interface that would define the class type of the first invoked py module from within java. The interface and the input and output (to python modules) type classes are in a package structure as com.abc.xpackage. and the py modules exist at the root of this package. A controller layer calls the objectfactory and in turn executes the python code thus:
JythonObjectFactory calFactory = new JythonObjectFactory(CalcType.class, "Calculate", "Calculate");
CalcType engine = (CalcType)calFactory.createObject();
output = engine.execute(input);
The entire code is bundled as a jar file which would become part of a web application deployed on weblogic. The code was compiled with maven (with jython dependencies included in the repository) and runs fine on the included Jetty runtime within eclipse.
When deployed on weblogic, however, I get a "ImportError: no module named csv" error.
To analyze what is happening, I tried printing the Jython system state path on weblogic and the standalone environment/Jetty. What I found is,
on Jetty, the system path consists of the following:
C:\.m2\repo\org\python\jython\jython-standalone-2.5.3-rc1.jar\Lib, ____classpath__, ____pyclasspath__
on Weblogic, printing the system path by default shows the following:
____classpath__, ____pyclasspath__
I tried forcing the inclusion of the missing path using the code as follows:
public JythonObjectFactory(PySystemState state, Class interfaceType, String moduleName, String className) {
String pathToAppend = new File(state.getClass().getProtectionDomain().getCodeSource().getLocation().toURI().getPath()).getAbsolutePath()+"\\Lib";
state.path.insert(0, new PyString(pathToAppend));
state.path.append(new PyString(pathToAppend));
System.out.println("Jython sys path: "+state.path);
Please note, I prepended as well as appended the path in different trials. The sys path on weblogic now displays the following:
Jython sys path: ['C:\\wldomain\\wls135\\servers\\cgServer\\tmp\\app-1\\war\\WEB-INF\\lib\\jython-standalone-2.5.4-rc1.jar\\Lib', '__classpath__', '__pyclasspath__/', 'C:\\wldomain\\wls135\\servers\\cgServer\\tmp\\app-1\\war\\WEB-INF\\lib\\jython-standalone-2.5.4-rc1.jar\\Lib']
I am still getting ImportError despite this forcing of sys path. Please help why this works in a local environment, and not on weblogic, and if there is any configuration I am missing. Apologize for the rambling long post, I did not know how to explain the problem better. I will try and include any code/artifacts as needed.
Based on a comment(by Lassi) on the blog post below:
http://www.petervannes.nl/files/e1c3c56d15d25dcfd4adb5397a9ef71e-53.php
The jython issue was resolved after explicitly adding the Lib folder python.path to the weblogic startup script as a JAVA_OPTION.
In my case I added the exploded Lib folder to the domain server lib, but based on my test this works also from within the jython jar. Both the following JAVA_OPTIONS worked:
-Dpython.path=C:\wldomain\wls135\lib\Lib
-Dpython.path=C:\wldomain\wls135\lib\jython-standalone-2.5.4-rc1.jar\Lib
The programmatic way of sys.path.append worked for the local environment(jetty) but did not seem to work for weblogic.

JNI UnsatisfiedLinkError- how do I load libraries correctly?

I'm trying to work with the Java sample Database program from the CardScan SDK.
I am working with files located in Java/JNI and Java/Database. The program must be run with a 32 bit JRE. I was able to do so on a 64 bit machine by uninstalling Java and installing the 32 bit version, then re-adding the system path for Java. I can run the program and interface with a CardScan database file (.cdb) successfully by double clicking the SDKData.bat file, but when I open the source files for editing and edit the Java.library.path to include the required library (CRTK_JNI.dll), I get UnsatisfiedLinkErrors everywhere:
Exception in thread "main" java.lang.UnsatisfiedLinkError: sdkdata.CRTK.CRTK_Init([I)I
at sdkdata.CRTK.CRTK_Init(Native Method)
at sdkdata.CRTK.(CRTK.java:239)
at sdkdata.SDKData.(SDKData.java:97)
at sdkdata.SDKData.main(SDKData.java:643)
Java Result: 1
Presumably this is happening because the library is not loading properly.
What do I need to do to run and edit the program at full capacity (with all the native functions from CRTK_JNI in working order)?
Presumably this is happening because the library is not loading properly.
On the contrary. The library load is complete. You aren't getting that from a System.load()/loadLibrary() call, you are getting the error when calling your native method, the one that should have the signature:
package sdkdata;
public class CRTK
{
public native int CRTK_Init(int[]);
}
So it isn't there, or you have changed the signature without regenerating the .h and .c files, or you have manually mangled the declaration some other way.
Post your code.
To clarify, this Java sample program is officially unsupported by the CardScan API - it was a bad idea to try to use the API with an unsupported language relying solely on an experimental implementation. I ended up using one of the supported languages (Visual Basic) to work with the SDK; if anyone looking at this question happens to be struggling with using the CardScan API, here is my VB implementation on Github.

Reg Jython Python wrap for Java

I have been assigned a task to convert a java standalone application to python web application.Recoding the entire module in python language would take a lot of time and effort.Hence I was adviced do a quick wrap up of python and get the code working (jython.org) (Jython is Python wrap for JAVA )..Could any one guide me how to get started as I am new to Python as well as Jython?
To get you started:
If you're familiar with Java, then you should be able to get to the Jython prompt no problem. Just execute it like any other .jar. If you didn't download the standalone jython.jar, be sure to include the Jython libraries in your classpath.
Say your Java application's package is named com.stackoverflow.q10715162, and is compiled as a .jar in C:\jars\your_app.jar.
Then, you can get access to its classes in Jython. At the Jython prompt:
>>> import sys
>>> sys.path
['', 'C:\\jython\\Lib', 'C:\\jython\\jython.jar\\Lib', '__classpath__',
'__pyclasspath__']
Here, sys.path is, among other things, a list of directories where your Jython distribution is looking for compiled modules. By adding your compiled Java application to the list, it will become accessible (more in-depth info is available for this at http://www.jython.org/jythonbook/en/1.0/ModulesPackages.html):
>>> sys.path.append('C:\\jars\\your_app.jar')
>>> import com.stackoverflow.q10715162 as yourapp
*sys-package-mgr*: processing new jar, 'C:\\jars\\your_app.jar'
>>> dir(yourapp)
['Class1', 'Class2', 'Class3', ...]
By using dir(yourapp) you can see the classes you defined in your Java application. dir(yourapp.Class1) will list all methods, functions, etc. that are within the class.
You probably want to read through the first few pages at least of the Jython Book to familiarize yourself with the new syntax. I find it much simpler than Java's.
For making a Jython web app, I've heard cgi is by far the fastet way to get started with the least overhead:
#!/usr/bin/python
print("Content-Type: text/plain\n\n")
print("Hello, World!\n")
This tutorial seems helpful: http://www.cs.virginia.edu/~lab2q/lesson_1/. Although it is for Python, almost all of it should be applicable to Jython.
And of course, there are many other Python/Jython web service options if cgi doesn't suit you or your project. I've used web2py and really liked it.

Jython - javaos.getenv() gives "Failed to get environment, environ will be empty"

Whenever I run any jython program in Eclipse, I got the following error in the beginning of the output:
" Failed to
get environment, environ will be
empty: (0, 'Failed to execute command
([\'sh\', \'-c\', \'env\']):
java.io.IOException: Cannot run
program "sh": Crea teProcess error=2,
The system cannot find the file
specified')
First, my environment is:
Windows 2008
JDK 1.6.0u10
jython 2.2.1
I did some digging, and I realized that this message is produced in the function javaos.getenv().
Whenever I call the javaos.getenv() function, it throws the following error:
C:\jython2.2.1>java -jar jython.jar
import javaos
print javaos.getenv("user.name")
Failed to get environment, environ
will be empty: (0, 'Failed to execute
command ([\'sh\', \'-c\', \'env\']):
java.io.IOException: Cannot run
program "sh": Crea teProcess error=2,
The system cannot find the file
specified')
This is strange, because I'm currently using a Windows machine, not an Unix.
Try to uncomment and change the os setting in the 'registry' file
(it is in the same directory as your jython.jar / i hope)
# python.os determines operating-specific features, similar to and overriding the
# Java property "os.name".
# Some generic values are also supported: 'nt', 'ce' and 'posix'.
# Uncomment the following line for the most generic OS behavior available.
#python.os=None
python.os=nt
# try nt or dos
Interesting. Well, I seem to have found the relevant code here:
http://www.koders.com/python/fid4B7C33153C1427D2CE19CE361EA9519D1652F802.aspx?s=self
If you look towards the bottom, it seems when setting the environment command jython thinks your os is posix. You say you're using "Windows 2008". I'm not sure what that is. Do you mean Windows Server 2008? If so, it's quite new and if you look at the _getOsType function in the same module, it looks like it might be too new for that module. You may need to upgrade to the most recent version of jython or Eclipse. But it's quite possible they haven't yet released a version that supports this OS. If that's the case, you may need to just report the bug to them.
I'm running on Windows 7.
I'm running Jython as a script in the Websphere wsadmin tool.
I encountered this same error.
I cut-n-pasted these lines from javaos.py into my script:
os or sys.registry.getProperty( "python.os" ) or \ java.lang.System.getProperty( "os.name" )
and it returned "Windows Vista".
So I performed the same surgery as suggested above, ie., add Windows Vista to javaos.py and that solved my problem.
I ran into the same error, using Windows Vista, and Jython 2.5.1, under Eclipse/PyDev By editing javaos.py, to include "Windows Vista" in the OR statement in getOsType,;
I fixed the error. (I've filed a bug with the fix under the PyDev Tracker at SourceForge.)
Details:
I installed the full version of Jython, and that did not help.
I also tried editing the "registry" file in the Jython tree. That did not help either.
Then I looked at the files in:
C:\eclipse-platform-3.5-win32\eclipse\plugins\org.python.pydev.jython_1.4.8.2881\Lib
to find "javaos.py" and added a bit of code to read:
def _getOsType( os=None ):
os = os or sys.registry.getProperty( "python.os" ) or \
java.lang.System.getProperty( "os.name" )
_osTypeMap = (
( "nt", r"(nt)|(Windows NT)|(Windows NT 4.0)|(WindowsNT)|"
r"(Windows 2000)|(Windows XP)|(Windows CE)|(Windows Vista)" ),
( "dos", r"(dos)|(Windows 95)|(Windows 98)|(Windows ME)" ),
( "mac", r"(mac)|(MacOS.*)|(Darwin)" ),
( "None", r"(None)" ),
( "posix", r"(.*)" ), # default - posix seems to vary mast widely
)
for osType, pattern in _osTypeMap:
if re.match( pattern, os ):
break
return osType
I've used this hack from Dave Brands blog: http://dbrand666.wordpress.com/2010/04/08/fix1/
try:
import javaos
if javaos._osType == 'posix' and \
java.lang.System.getProperty('os.name').startswith('Windows'):
sys.registry.setProperty('python.os', 'nt')
reload(javaos)
except:
pass