Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Python/JavaScript bridge module, making use of Mozilla's spidermonkey JavaScript implementation.

branch: master

Make this build on 10.6

I had to do some hackery to get this building on 10.6 with Homebrew. I
mostly hacked together a fake nspr.pc and dropped it in [1] so that
the pkgconfig stuff in setup.py would work. There's a copy of this
file in the "extra" directory in this repo. I also added a patch
for the va_copy like MacPorts does.

If anyone would like to tear out the embedded copy of Spidermonkey
that this package ships with and update the build system to use
either what Homebrew installs or is found in the xulrunner package
on Ubuntu, that would be awesome.

[1] /usr/local/lib/pkgconfig/nspr.c
latest commit 6e3d647f7b
Paul J. Davis authored
Octocat-spinner-32 extra Make this build on 10.6 February 15, 2011
Octocat-spinner-32 spidermonkey Make this build on 10.6 February 15, 2011
Octocat-spinner-32 tests Make this build on 10.6 February 15, 2011
Octocat-spinner-32 .gitignore Ignore egg files. April 02, 2009
Octocat-spinner-32 LICENSE Yeah, bad commit policy. But hey, it works now. January 28, 2009
Octocat-spinner-32 MANIFEST.in Removed cruft in the source directory. July 18, 2009
Octocat-spinner-32 README.md Updated Gento package names. Thanks Scott T Silliman July 07, 2009
Octocat-spinner-32 THANKS Removed cruft in the source directory. July 18, 2009
Octocat-spinner-32 ez_setup.py Unicode enabled python-spidermonkey. January 30, 2009
Octocat-spinner-32 go Initial JS iteration of Python objects. May 09, 2009
Octocat-spinner-32 go.comm Add JS_[Begin|End]Request semantics. May 10, 2009
Octocat-spinner-32 setup.py Removed cruft in the source directory. July 18, 2009
Octocat-spinner-32 update-libjs.sh Builds on linux. Distracted by BSG. More tomorrow. February 27, 2009
README.md

Execute arbitrary JavaScript code from Python. Allows you to reference arbitrary Python objects and functions in the JavaScript VM

Having issues?

The project support site can be found at lighthouseapp.com.

Requirements

Pkg-Config

Mac OS X:

This should be installed by default. If not there is a port package:

$ sudo port install pkgconfig

Debian/Ubuntu:

This is also generally installed by default, but I have reports of it being otherwise.

$ sudo apt-get install pkg-config

Gentoo:

$ sudo emerge dev-util/pkgconfig

Python Development Files

Mac OS X:

If you installed Python via port then the headers should already be installed. I have not heard reports of problems from people using the bundled interpreters.

Debian/Ubuntu:

$ sudo apt-get install pythonX.X-dev

Where X.X is the version of Python you are using. I have not tested python-spidermonkey on Py3K so it may be horribly broken there.

Gentoo:

If you have python installed, then the headers should already be installed.

Netscape Portable Runtime (nspr)

The nspr library is required for building the Spidermonkey sources. You should be able to grab it from your package manager of choice with something like the following:

Mac OS X:

$ sudo port install nspr

Debian/Ubuntu:

$ sudo apt-get install libnspr4-dev

Gentoo:

$ sudo emerge dev-libs/nspr

Alternatively you can build from source. If you choose this route make sure that the nspr-config command is on your $PATH when running the install commands below.

If you choose this route make sure that the pkg-config command is on your $PATH when running the install commands below. Additionally, make sure that $PKG_CONFIG_PATH is properly set.

XULRunner (optional)

You can optionally build the extension linked to your system's spidermonkey library, which is installed with XULRunner. You should be able to grab it from your package manager of choice with something like the following:

Mac OS X:

$ sudo port install xulrunner

Debian/Ubuntu:

$ sudo apt-get install xulrunner-1.9-dev

Gentoo:

$ sudo emerge net-libs/xulrunner

As with nspr, you can also build xulrunner from source. And as with nspr you need to make sure hat $PATH and $PKG_CONFIG_PATH are properly set when building the module.

Installation

$ git clone git://github.com/davisp/python-spidermonkey.git
$ cd python-spidermonkey
$ python setup.py build
$ python setup.py test

$ sudo python setup.py install

*OR*

$ sudo python setup.py develop

If you want to build with the system spidermonkey library, replace the build command with the following:

$ python setup.py --system-library build

Examples

Basics

>>> import spidermonkey
>>> rt = spidermonkey.Runtime()
>>> cx = rt.new_context()
>>> cx.execute("var x = 3; x *= 4; x;")
12
>>> class Orange(object):
...   def is_ripe(self,arg):
...       return "ripe %s" % arg
...
>>> fruit = Orange()
>>> cx.add_global("apple", fruit)
>>> cx.execute('"Show me the " + apple.is_ripe("raisin");')
u'Show me the ripe raisin'

Playing with Classes

>>> import spidermonkey
>>> class Monkey(object):
...     def __init__(self):
...         self.baz = "blammo"
...     def wrench(self, arg):
...         return "%s now wrenched" % arg
...
>>> rt = spidermonkey.Runtime()
>>> cx = rt.new_context()
>>> cx.add_global("Monkey", Monkey)
>>> monkey = cx.execute('var x = new Monkey(); x.baz = "schmammo"; x;')
>>> monkey.baz
u'schmammo'
>>> monkey.__class__.__name__
'Monkey'

JavaScript Functions

>>> import spidermonkey
>>> rt = spidermonkey.Runtime()
>>> cx = rt.new_context()
>>> func = cx.execute('function(val) {return "whoosh: " + val;}')
>>> func("zipper!");
u'whoosh: zipper!'

Filtering access to Python

>>> import spidermonkey
>>> rt = spidermonkey.Runtime()
>>> def checker(obj, name):
...     return not name.startswith("_")
...
>>> cx = rt.new_context(access=checker)
>>> # Alternatively:
>>> cx.set_access()                                     #doctest: +ELLIPSIS
<function checker at ...>
>>> cx.set_access(checker)                              #doctest: +ELLIPSIS
<function checker at ...>
>>> cx.add_global("fish", {"gold": "gone", "_old_lady": "huzza"})
>>> cx.execute('fish["_old_lady"];')
Traceback (most recent call last):
        ...
JSError: Error executing JavaScript.

Previous Authors

  • John J. Lee
  • Atul Varma
Something went wrong with that request. Please try again.