A.3 Unsupported Python constructs

Psyco only compiles functions. It will not accelerate any code that runs outside any function, like:

You can always work around the above limitations by creating functions and calling them instead of directly executing a time-consuming source. For example, instead of writing a short test script like

some_big_list = ...
for x in some_big_list:
    do_something()
write instead
def process_list(lst):
    for x in lst:
        do_something()
process_list(...)

As another example, a function like

def f(some_expr):
    for x in range(100):
        print eval(some_expr)   # where some_expr can depend on x
should instead be written
def f(some_expr):
    my_func = eval("lambda x: " + some_expr)   # -> a function object
    for x in range(100):
        print my_func(x)

In addition, inside a function, some syntactic constructs are not supported by Psyco. It does not mean that a function using them will fail; it merely means that the whole function will not be accelerated. The following table lists the unsupported constructs, along with the corresponding bytecode instruction name and number. Log files only report the bytecode instruction number, which you need to look up here.

Bytecode Instruction name Appears in
82 LOAD_LOCALS (1) class definitions
84 IMPORT_STAR (5) from xx import *
85 EXEC_STMT (2) exec xx
86 YIELD_VALUE (3) generators
90 STORE_NAME (5) outside functions
91 DELETE_NAME (5) outside functions
101 LOAD_NAME (5) outside functions
134 MAKE_CLOSURE (4) nested scopes
135 LOAD_CLOSURE (4) nested scopes
136 LOAD_DEREF (4) nested scopes
137 STORE_DEREF (4) nested scopes

Notes:

(1)
Psyco cannot accelerate class definitions, i.e. the execution of the body of the class statement - i.e. the creation of the class object itself. This does not prevent it from accelerating methods in the class.
(2)
Functions using this construct cannot be accelerated.
(3)
Generators (i.e. any function using the yield keyword) cannot be accelerated currently. If there is enough interest I can consider implementing them. This includes generator expressions (Python 2.4). Warning! The function containing a generator expression will be compiled by Psyco, but not the generator expression itself. If the latter calls other functions compiled by Psyco, then performance will be very bad: calling from Psyco to Python to Psyco comes with a significant overhead.
(4)
Using nested scopes (i.e. variables shared by a function and an inner sub-function) will prevent both the outer and the inner function to be accelerated. This too could be worked around if there is enough interest, at least for accelerating the unrelated parts of the functions - the accesses to the shared variables themselves might be difficult to optimize.
(5)
These constructs can appear in class definitions (see (1)) or at the module top-level. It is possible to enable support for module top-level code, but not recommended; instead, try to put all the code you want accelerated in function bodies.



Footnotes

... codeA.1
Support for top-level module code is possible but disabled by default in recent versions of Psyco; contact me for more information.