Sub-interpreter running in restricted mode?

155 views
Skip to first unread message

erikj

unread,
Jan 20, 2017, 4:17:42 PM1/20/17
to Jep Project

Hello. I am using Jep to execute python scripts from within a Java application. Even with the numpy work around using shared modules I still get funny behavior. See the test case below.


import jep.Jep; import jep.JepConfig; public class JepTest { private static String script = "import numpy as np\n" + "from skimage.io import imread\n" + "testImagePath = '<path to test image>'\n" + "testImage = imread(testImagePath)\n"; private static JepConfig jepConfig = null; private static final String[] sharedModules = {"numpy", "scipy", "skimage", "skimage.transform", "skimage.io"}; public static void main(String[] args) { try { jepConfig = new JepConfig(); jepConfig.addSharedModules(sharedModules); Jep jep = new Jep(jepConfig); //jep.set("_pythonScript", script); //jep.eval("exec _pythonScript"); //jep.eval("del _pythonScript"); jep.eval("exec \"\"\"" + script + "\"\"\""); System.out.println("Finished with script..."); jep.close(); } catch (Exception e) { System.out.println("Caught Jep Exception:"); e.printStackTrace(); } } }

This results in the following error:


Caught Jep Exception: jep.JepException: <type 'exceptions.IOError'>: file() constructor not accessible in restricted mode at /usr/lib/python2.7/dist-packages/PIL/Image.open(Image.py:1996) at /usr/lib/python2.7/dist-packages/skimage/io/_plugins/pil_plugin.imread(pil_plugin.py:21) at /usr/lib/python2.7/dist-packages/skimage/io/_plugins/plugin.call(plugin.py:105) at /usr/lib/python2.7/dist-packages/skimage/io/_io.imread(_io.py:150)
 

Evidently the jep sub-interpreter is running in restricted execution mode. I get the same behavior by saving the script as a file and using Jep.runScript(). Is there a way to fix this? Any suggestions are appreciated.


Thanks,


Erik

Nathan Jensen

unread,
Jan 20, 2017, 5:40:23 PM1/20/17
to Jep Project
Hi, there is some discussion about restricted mode over here:  https://github.com/mrj0/jep/issues/62

Restricted mode is deprecated in Python 2 and doesn't exist in Python 3.  I'm not sure why/how some applications are running in restricted mode.  You might investigate updating to Python 3 or figuring out how to get Python 2 out of restricted mode.  You could also try adding PIL to your shared modules and see if that makes a difference.



--
You received this message because you are subscribed to the Google Groups "Jep Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jep-project+unsubscribe@googlegroups.com.
To post to this group, send email to jep-p...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jep-project/579d996e-12ae-413b-8342-6f391cee76a6%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Ben Steffensmeier

unread,
Jan 20, 2017, 6:11:45 PM1/20/17
to Jep Project
It looks like you may have stumbled across a limitation of shared modules that we were not aware of. When you use shared modules it brings objects into your interpreter from a different interpreter. The goal is to bring across the modules that cannot safely exist in multiple interpreters but you also end up bringing alot of other objects along. In this case it looks like the skimage brought along the PIL module, which brought along the builtin open() method. When you use the open() method on an interpreter that is not the one that created it then it thinks it is in restricted mode and you get this error.

I created a small test case to play with that is just a module that opens a file and verified I see the same problem as you in python 2.7. The good news is that python 3 eliminated restricted mode so my test did not have any problems on python 3. This is the same result we saw on issue #62, which also avoided this error by switching to python 3.

I also noticed that PIL allows you to pass in an already open file instead of a filename. If it is possible to pass an open file into skimage then you may be able to work around the issue by opening the file yourself.

I will try to investigate further to see if their is anything else we can do in jep to make this work better but its a complex issue so it will not be quick to solve.

Ben


Ben Steffensmeier

unread,
Jan 24, 2017, 5:09:58 PM1/24/17
to Jep Project
I have created a github issue specifically for restricted mode problems: https://github.com/mrj0/jep/issues/68. Anyone interested in this problem should sign up for notifications of the issue and if anyone has more information it can be added there.

erikj

unread,
Jan 24, 2017, 6:09:37 PM1/24/17
to Jep Project
Thanks Ben and Nathan for the inputs. I migrated to Python 3 this weekend and that indeed fixed the restricted mode issue. I also found that I only need to use numpy and PIL as the shared objects and everything else works fine importing from the script. Now I am working on implementing a singleton version where I reuse the same sub-interpreter so that we can use matplotlib from the script as well.

Thanks again for your help!
Erik
To unsubscribe from this group and stop receiving emails from it, send an email to jep-project...@googlegroups.com.

To post to this group, send email to jep-p...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jep-project/579d996e-12ae-413b-8342-6f391cee76a6%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Jep Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jep-project...@googlegroups.com.

To post to this group, send email to jep-p...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages