TWiki
>
Public Web
>
JythonUpgrade
(2012-10-19,
DavideRizzo
)
(raw view)
E
dit
A
ttach
Tags:
create new tag
view all tags
<!-- ANALYTICS CODE - DO NOT EDIT --> %INCLUDE{"%ATTACHURL%/GoogleAnalytics.txt" raw="on"}% <!-- END OF ANALYTICS CODE --> ---+ !!Upgrade to Jython 2.5 Up to HIPE 7, we have been using the *Jython 2.1* library, which _mostly_ uses the syntax of Python 2.1<br /> Starting from HIPE 8 we will upgrade to *Jython 2.5*, which uses the same syntax rules as Python 2.5. In most cases, your scripts will run fine; however, in certain cases you may need to adapt them. This page lists the known differences between Jython 2.1 and Jython 2.5 ---- %TOC% ---++ Wrong locations of global import statements <blockquote> You may have written code where global import statements are done _within_ a Jython function. As of Jython 2.5, this is _not allowed_ anymore (in fact, it was not allowed in Python 2.1 but Jython 2.1 inadvertently allowed for it). When executing the code against Jython 2.5, you will get messages such as: <pre> SyntaxWarning: import * only allowed at module level </pre> To fix your code, you have two options: * Move the global import statement out of the function and to the top of your script. * Make the import statement specific. See below for examples of fixes to incorrect code. ---+++ Current: wrong use of global import <pre> : def foo(): from herschel.ia.foo.banana import * %RED%<b># global import statement, to access SomeThing</b>%ENDCOLOR% x=SomeThing() return </pre> ---+++ Fix 1: global import statement at start of script <pre> %BLUE%from herschel.ia.foo.banana import *%ENDCOLOR% : def foo(): x=SomeThing() return </pre> ---+++ Fix 2: make import statement specific <pre> : def foo(): %BLUE%from herschel.ia.foo.banana import SomeThing%ENDCOLOR% x=SomeThing() return </pre> </blockquote> ---++ Known jython module changes <blockquote> The following modules changed their names: |*Jython 2.1*|*Jython 2.5*| |import javaos | import os | </blockquote> ---++ Known jython behavioral changes <blockquote> ---+++ <nop>__name__ <pre> $ from foo import Bar $ print Bar.__name__ Bar #<b>Jython 2.1:</b> foo.Bar $ print Bar.__module__ foo $ print Bar.__module__ + "." + Bar.__name__ foo.Bar $ print Bar.name # if Bar is a Java class foo.Bar $ import foo.Bar $ print foo.Bar.__name__ foo.Bar </pre> <em>If you always want the full name, use: </em><code>Bar.canonicalName</code> </blockquote> ---++ Numeric types <blockquote> In Jython 2.1 numeric types were Py objects like !PyInteger or !PyFloat and as such had methods like toString(). In Jython 2.5 numeric types are more like native types and do not have these basic Object methods available. The difference was discovered when passing an integer into a jython function and then printing this value using the toString() method. In Jython 2.5 this results in the following error:<verbatim> <type 'exceptions.AttributeError'>: 'int' object has no attribute 'toString' </verbatim> The solution is very simple and is also recommended to use in all Jython versions, i.e. replace the toString() with a format string, for example: <verbatim> HIPE> foo = 3.1415 HIPE> print "foo =", foo.toString() # DO NOT USE THIS !! HIPE> print "foo = %f" % foo foo = 3.141500 </verbatim> Some other Java methods can no longer be applied to numbers (which may require changing some client code). In general, when using Jython we should just use Jython functionality if available to avoid this type of problems. The following example shows what happens when you try to move from "long" to "double" using java.lang.Number methods (to make a floating point division for example). This works with Jython 2.1: <verbatim> HIPE> i = 3L HIPE> print i.__class__ org.python.core.PyLong HIPE> d = i.doubleValue() HIPE> print d 3.0 </verbatim> While it no longer works with Jython 2.5 (HIPE 8): <verbatim> HIPE> i = 3L HIPE> print i.__class__ <type 'long'> HIPE> d = i.doubleValue() <type 'exceptions.AttributeError'>: 'long' object has no attribute 'doubleValue' HIPE> print d <type 'exceptions.NameError'>: name 'd' is not defined </verbatim> In this particular case the following alternatives can be used to promote to floating point: * Add a float literal to the expression ( =+ 0.0= ) * Use an explicit cast to float (=float(i)=) </blockquote> <hr/> <!-- COMMENT BOX CODE - DO NOT EDIT --> <div id="disqus_thread"></div> <script type="text/javascript"> var disqus_shortname = 'herscheltwiki'; // required: replace example with your forum shortname (function() { var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true; dsq.src = 'http://' + disqus_shortname + '.disqus.com/embed.js'; (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq); })(); </script> <noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript> <a href="http://disqus.com" class="dsq-brlink">blog comments powered by <span class="logo-disqus">Disqus</span></a> <!-- END OF COMMENT BOX CODE --> <hr/> <!-- * Set ALLOWTOPICCHANGE = Main.RegisteredUsersGroup, Main.TWikiAdminGroup -->
E
dit
|
A
ttach
|
Watch
|
P
rint version
|
H
istory
: r10
<
r9
<
r8
<
r7
<
r6
|
B
acklinks
|
V
iew topic
|
Ra
w
edit
|
M
ore topic actions
Topic revision: r10 - 2012-10-19
-
DavideRizzo
Public
Log In
Public Web
Create New Topic
Index
Search
Changes
Notifications
Statistics
Preferences
Webs
Public
TWiki