I'll preface this by saying I am quite new to PyPy, though fairly experienced with Python.
I'm looking to run a web app where I run untrusted Python code. The PyPy sandboxing features look ideal for what I'm doing.
The PyPy docs on sandboxing indicate that you can call a PyPy sandbox from either Python or PyPy. This seems to imply that there's some separate program or executable that is the sandbox.
I'm wondering, is it possible to call a PyPy sandbox from a non-Python language? I'm looking at Haskell in particular, but it's also very possible that I could use C or C++ as an intermediate.
Yes, that's possible. The PyPy sandbox is a separate process communicating only via stdin/stdout. If you want to rewrite the "external" part, you can; it's not using anything that should be too heavily Python-related.
Note that the sandboxing feature of PyPy is not being maintained any more, see http://www.pypy.org/features.html#sandboxing
Related
Can Jython help here? Should I run Grails above Jython and if yes, how? Somehow I should be able to run Grails and Python script on same JVM. There are other possibilities like making REST service for Python script or some interprocess communication but lets not deal with those for now.
Jython is a JSR223 scripting language, so you should be able to follow the usual methods. (http://www.jython.org/archive/22/userguide.html#embedding-jython)
ScriptEngine engine = new ScriptEngineManager().getEngineByName("python")
engine.eval("x = 2 + 2")
It might be a non-trivial thing to get your Jython and any libraries you want to use organized on your server, but if all you need is the language and the standard libraries, you should be able to just add it as a dependency in your buildfile - it is on Maven Central (compile 'org.python:jython:2.7.1b3').
But keep in mind that many python libraries (i.e. ones that use compiled C-code) are not going to work with Jython.
So, you might need to instead use a native python installation and call it as a process (using ProcessBuilder, for example). Groovy has some nice sugar for doing this sort of thing with strings.
Process process = "python mypython.py".execute()
An internet search for things like "groovy execute shell command" will yield lots of examples. Depending on your deployment scenario, this could be tricky to set up and maintain.
I would like to embed a Python interpreter into my .NET application. I'm aware of IronPython, of course, but I'm specifically interested in PyPy, because of its stackless support and microthreads.
However, while PyPy can be built against the CLI, it looks like that just gives you a standalone Python interpreter a la python.exe. I haven't been able to find any documentation for building something that can actually be embedded inside a .NET host application.
Is there a way to use (stackless) PyPy to run Python scripts from a .NET app, and allow those scripts to interact with CLR objects provided by the host application?
PyPy's CLI backend is not as mature as C backend and also does not integrate as well with .NET libraries. While normal PyPy compiled to C is production ready, I wouldn't call the .NET version production ready. It's also missing the JIT (although some work has been done in this area) and microthreads. Unless someone steps in, IronPython seems to be the only viable option as of now.
No, there's not. CPython had the ability to access .NET libraries using the Python for .NET (see http://pythonnet.github.io/) maintained at github, but aside from IronPython there's never been a way to actually embed a Python interpreter into a .NET application. This was one of its main selling points.
On a related note, IronPython (by default) has a smaller stack size than CPython when it comes to recursion. That is, you must pass a "-X:FullFrames" command-line option to ipy.exe to enable CPython-esque stack frames. Know this isn't as good as PyPy...but it might help:)
I've built a fairly complicated application with PyQt4 and Python, but it is a pain to send to people (and once I do, they have no idea how to run it). Then there are dependencies to wrestle. Ugh.
Anyways, I just learned about Jython, and since virtually everybody has Java installed, it seems like a perfect solution to my problem of distribution of Python scripts. Has anybody actually developed a functional piece of software with Jython, and if it even exists, one with Jambi bindings?
I'm just asking so that I don't go digging for something which doesn't work.
Thanks!
If you did move this application to Jython, you would have to convert the GUI from QT to Jambi.
Jython is the Python language implemented in Java to run on the Java virtual machine. Because it runs on the JVM, Jython apps can use any Java libraries, such as SWING or Jambi.
It is possible that the differences between PyQT and Jambi are very small, but fundamentally, you would not be using QT directly. Instead you would be using Jambi. And if you use any non-standard Python modules you will still have to resolve packaging issues.
If your application uses other Python modules which are implemented in C, then you would also need to replace those with Java libraries. Jython is great at running a lot of pure Python code unchanged, but Jython runs in a Java environment and there are differences in the way some fundamental objects, such as strings, are implemented. Jython uses Java internals, Java's garbage collector, and so on.
There is more info available via this SO question: Migrating from CPython to Jython
I recently started learning Python. Not yet ventured into coding.
During one of my learning sessions, i came accross the term Jython.
I googled it & got some information.
I would like to know if anyone has implemented any real-world program using Jython.
Most of the time, Jython isn't used directly to write full read-world programs, but a lot of programs actually embed Jython to use it as a scripting language.
The official Jython website gives a list of projects, some written in Jython, others using Jython for scripting:
http://wiki.python.org/jython/JythonUsers
I am writing a full application in Jython at the moment, and would highly recommend it. Having all of the Java libraries at your disposal is very handy, and the Python syntax and language features actually make using some of them easier than it is in Java (I'm mostly talking about Swing here).
Check out the chapter on GUI Applications from the Jython book. It does a lot of comparisons like 'Look at all this Java code, and now look at it reduced to Python code of half the length!'.
The only caveats I've found are:
Jython development tends to run slightly behind Python, which can be annoying if you find a cool way of doing something in Python, only to discover it's not supported in the current Jython version.
Occasionally you might have hiccups with the interface between Python and Java (I have a couple of unsolved problems here and here, although there are always workarounds for this kind of thing).
Distribution is not as simple as it could be, although once you figure out how to do it, it's fairly painless. I recommend following the method here. It essentially consists of:
Exploding jython.jar and adding your own modules into it.
Writing and compiling a small Java class that creates a Python interpreter and loads up your Python modules.
Creating an executable .jar file consisting of the jython.jar modules, your own Python modules, and the Java class.
Jython really shines for dependency injection.
You know those pesky variables you have to give your program, like
file system paths
server names
ports
Jython provides a really nice way of injecting those variables by putting them in a script. It works equally well for injecting java dependencies, as well.
WebSphere and WebLogic use it as their default scripting engine for administrative purposes.
A lot of other Oracle products ship it as part of their "oracle_commons" module (Oracle Universal Installer, Oracle HTTP Server etc). It's mostly version 2.2 being deployed though, which is a bit old and clunky.
There is a list of application that uses jython at http://wiki.python.org/jython/JythonUsers
I have written a bunch of Perl libraries (actually Perl classes) and I want to use some of them in my Python application. Is there a natural way to do this without using SWIG or writing Perl API for Python. I am asking for a similar way of PHP's Perl interface. If there is no such kind of work for Perl in Python. What is the easiest way to use Perl classes in python?
Personally, I would expose the Perl libs as services via XML/RPC or some other such mechanism. That way you can call them from your Python application in a very natural manner.
I haven't tried it, but Inline::Python lets you call Python from Perl.
You should be able to use a thin bit of perl to load your python app and then use the perl python package that comes with I::P to access your Perl objects.
"What is the easiest way to use Perl classes in python?"
Easiest. Rewrite the Perl into Python and be done with it. Seriously. Just pick one languageāthat's easiest. Leaving Perl behind is no great loss. Rewriting classes into Python may give you an opportunity to improve them in small ways.
Not so easy. Run the Perl application using Python's subprocess module. That uses the Perl classes in the Perl application without problems. You can easily create pipelines so the Perl gets input from Python and produces output to Python
someApp.py | something.pl | finalStep.py
This has the advantage of breaking your application into three concurrent processes, using up lots of processor resources and running (sometimes) in 1/3 the time.
Everything else is much less easy.
You've just missed a chance for having Python running on the Parrot VM together with Perl. On April 1st, 2009 PEP 401 was published, and one of the Official Acts of the FLUFL read:
Recognized that C is a 20th century language with almost universal rejection by programmers under the age of 30, the CPython implementation will terminate with the release of Python 2.6.2 and 3.0.2. Thereafter, the reference implementation of Python will target the Parrot virtual machine. Alternative implementations of Python (e.g. Jython, IronPython, and PyPy ) are officially discouraged but tolerated.
Check out PyPerl.
WARNING: PyPerl is currently unmaintained, so don't use it if you require stability.