Re: eval() with unicode_literals?

Jeff Allen <[email protected]>
Newsgroups gmane.comp.lang.jython.user
Message-ID <[email protected]>
Glad you like it. The trick is to ensure your source code is treated as 
Unicode by eval(). But even then, the only place your non-ascii 
characters can appear is inside a Unicode literal, signalled by the 
u-prefix (or maybe in comments). The following worked for me (in 2.7.1, 
under Eclipse):

import org.python.core.Py;
import org.python.core.PyObject;
import org.python.util.PythonInterpreter;

public class UnicodeInterpret {
     public static void main(String[] args) {
         PythonInterpreter python = new PythonInterpreter();
         String source = "u'λαμβδα'";
         PyObject result = python.eval(Py.newUnicode(source));
         System.out.println(result);
     }
}

Jeff

On 19/10/2015 19:59, Paulo Costa wrote:
> Hello,
> First, thanks for this awesome library.
> I've just started used Jython and I'm loving it :)
>
>
> I'm using it inside my project to evalute some user-define expressions.
> I need these expressions evaluated using unicode_literals
>
> How can I do that?
>
> This is what I've tried so far:
>
>         PythonInterpreter python = new PythonInterpreter();
>
>
>         //Not using unicode_literals
>
>         System.out.println(python.eval("repr('é')"));
>
>
>         //Try to update the interpreter using exec - Doesn't work
>
>         python.exec("from __future__ import unicode_literals");
>
>         System.out.println(python.eval("repr('é')"));
>
>
>         //Import unicode_literals and print string inside exec() -
>         Works, but doesn't return a value
>
>         python.exec("from __future__ import unicode_literals\nprint
>         repr('é')");
>
>
> Thanks!
>

------------------------------------------------------------------------------

_______________________________________________
Jython-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/jython-users
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.