Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Translator

bittner edited this page · 11 revisions

pyjs Python-to-JavaScript Translator

The pyjs Python-to-JavaScript compiler is actually a language translator. It has two main modes: -O and --strict. The --strict mode aims for full Python interoperability, even at the expense of performance, whilst the -O mode is, like gcc's -O option, "optimised" for speed, even at the expense of missing out certain Python language features and relying on JavaScript instead. The Migration Guide best describes the differences.

Supported Syntax

The majority of Python 2.5, and parts of Python 2.6 syntax are supported, including yield despite only one web browser engine having support for ECMAScript "yield" (which is not used). The yield support is implemented as a state machine, rewriting the code so that it is re-entrant, and can resume on each yield. The features not supported are pretty small and obscure, and include for example, at the time of writing, assignment to variables of list items in for-loop syntax is not (for [a, b] in enumerate(l)), whereas assignment to tuples is (for (a, b) in enumerate(l)). Overall it is best to read the Migration Guide and go from there.

Multiple inheritance is also supported, as is the creation of metaclasses using three arguments to the builtin "type" function (but a call to "type" with only one argument is not supported, and you should be using isinstance for that, anyway). Object introspection is supported, as are decorators, static methods, exceptions and so on. __new__ however, at the time of writing, is a little iffy. unicode is not supported, as it would be horrendous to implement in JavaScript, but the long type is supported, emulated correctly as JavaScript.

One particular feature worth noting: the pyjs Python-to-JavaScript compiler uses the internal features of Python (its compile module) to turn Python into an Abstract Syntax Tree. This had the disadvantage that the compiler was tied to one specific version of Python (2.5). As of pyjs 0.6, it has become possible to use anything from Python 2.4 and upwards to compile code with Python 2.5 syntax, using the --internal-ast option. A port of lib2to3 was used as the basis for an AST which is identical to that created by the C code in the CPython compiler. The plan is to use this at some point to implement "exec" and "eval" builtins, but it has the nice side-effect of allowing people who can only use specific versions of Python to reliably compile pyjs Python into JavaScript.

Overall, the general rule is that whoever wants or needs something, they either work around the limitations of JavaScript and the pyjs implementation, or they contribute code to improve the compiler/translator.

Supported Modules

Additionally, the Python-to-JavaScript compiler has available to it some implementations of standard Python modules, either in pure Python or in hybrid JavaScript and using web browser built-ins. Here is a list of the current Python modules that are partially supported. They have all been contributed by pyjs developers, and anyone wishing to have particular features that are not yet included should simply write them and make a Pull Request:

base64.py
binascii.py
csv.py
datetime.py
math.py
md5.py
os.py
_random.py
random.py
re.py
sets.py
struct.py
sys.py
time.py
urllib.py

Supported Built-ins

Here, also, is a list of supported built-in functions, classes and types. Again: these have been contributed by various pyjs developers, and again, if there is a particular function or feature missing, it can be submitted as a contribution on GitHub.

Functions:

__import__
abs
all
any
bool
callable
chr
cmp
delattr
dir
divmod
enumerate
filter
float
getattr
hasattr
hash
hex
isinstance
issubclass
iter
len
map
max
min
oct
open
ord
pow
property
range
reduce
repr
reversed
round
setattr
sorted
staticmethod
str
sum
super
type
xrange
zip

Classes:

ArithmeticError
AttributeError
BaseException
Exception
GeneratorExit
ImportError
IndexError
KeyError
LookupError
NameError
NotImplemented
NotImplementedError
NotImplementedType
RuntimeError
StandardError
StopIteration
TypeError
ValueError
ZeroDivisionError

Types:

dict
frozenset
int
list
long
object
set
tuple
Something went wrong with that request. Please try again.