Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

error when using \n in python script #554

Closed
githubbbbber opened this issue Sep 2, 2024 · 2 comments
Closed

error when using \n in python script #554

githubbbbber opened this issue Sep 2, 2024 · 2 comments

Comments

@githubbbbber
Copy link

githubbbbber commented Sep 2, 2024

Describe the bug
When I use \n in python code, it seems to get confused with python's own indent newlines, resulting in an error.

To Reproduce
execute the followding code in JEP java.

def mainFunction(args):
    test_str = "bar\nfoo"

I used interp.eval(codeStr) passing these code to jep, and used interp.invoke("mainFunction", new Object[] {inputs}); execute the python.
jep returned a exception:
jep.JepException:('unterminated string literal...)...

Environment (please complete the following information):

  • OS Platform, Distribution, and Version: linux
  • Python Distribution and Version:3.11
  • Java Distribution and Version:11
  • Jep Version: 4.2

Additional context
I tried to replace \n in java env before passing it to python env, such as new_line = "\n", then concatenate it with other python strings. But I still got errors in some scenarios such as regular expression. How can I use \n avoiding confusion.

@githubbbbber githubbbbber changed the title error when using error when using \n in python script Sep 2, 2024
@githubbbbber
Copy link
Author

githubbbbber commented Sep 3, 2024

I tried seperating test_str into like test_str = "bar" + "\n" + "foo"; and replace \n with \\n, so the final codeStr in interp.eval(codeStr) is like

def mainFunction(args):
    test_str = "bar\nfoo"

not like

def mainFunction(args):
    test_str = "bar
foo"

But in some scenarios such as test_str.split("\n") in python, it didn't work. The return is not like ["foo", "bar"] but ["foo\nbar"]

@bsteffensmeier
Copy link
Member

When you use \n in a Java string executed as a Python statement then \n is converted to the newline character by Java and Python sees 2 improperly formatted lines. It works for me when I use the \\n because then the \n is processed by python instead of java. In your example it looks like you reverted back to \n in split when it should be \\n. This is what works for me:

interpreter.exec("test_str = 'one\\ntwo'");
interpreter.exec("print(test_str.split('\\n'))");

I admit that is quite confusing, I would recommend avoiding passing complex strings directly to eval or exec and instead passing the data values using set like this:

interpreter.set("test_str", "one\ntwo");
interpreter.set("delim", '\n');
interpreter.exec("print(test_str.split(delim))");

It's also worth noting that this problem is not unique to jep, if you attempt to do the same thing using the python interpreter to run the python builtin exec function you will get the same errors as shown below:

>>> exec("test_str='one\ntwo'")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<string>", line 1
    test_str='one
             ^
SyntaxError: unterminated string literal (detected at line 1)
>>> exec("test_str='one\\ntwo'")
>>> exec("print(test_str.split('\n'))")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<string>", line 1
    print(test_str.split('
                         ^
SyntaxError: unterminated string literal (detected at line 1)
>>> exec("print(test_str.split('\\n'))")
['one', 'two']

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants