ANN: Shed Skin 0.2, an experimental (restricted) Python-to-C++ compiler

Nick Craig-Wood nick at craig-wood.com
Thu Jul 23 07:30:08 EDT 2009


srepmub <mark.dufour at gmail.com> wrote:
>  please send any program that doesn't work with shedskin (it still is
>  experimental after all) to me, or create an issue at
>  shedskin.googlecode.com, and I will have a look at the problem.

I divided and conquered the program as suggested and eventually I got
it to compile and run correctly :-)

I learnt that if you have lots of variables with indeterminate type
then shedskin takes a very long time indeed before blowing up!

I also learnt that shedskin doesn't support the idiom I'd been using
for creating shallow copies, namely the Board.__new__(Board) below.
shedskin compiles it ok, but the C++ won't compile complaning about
not being able to find __init__ methods

Producing these warnings

*WARNING* rush_hour_solver_cut_down.py:71: class 'Vehicle' has no method '__new__'
*WARNING* rush_hour_solver_cut_down.py:72: variable 'new' has no type
*WARNING* rush_hour_solver_cut_down.py:236: variable 'new_vehicle' has no type

And these compile errors

rush_hour_solver_cut_down.cpp:94: error: ‘__new__’ is not a member of ‘__rush_hour_solver_cut_down__::Vehicle’
rush_hour_solver_cut_down.cpp:95: error: expected type-specifier before ‘;’ token
rush_hour_solver_cut_down.cpp: In member function ‘void* __rush_hour_solver_cut_down__::Board::move(int, int)’:
rush_hour_solver_cut_down.cpp:276: error: ‘void*’ is not a pointer-to-object type
rush_hour_solver_cut_down.cpp:276: error: ‘void*’ is not a pointer-to-object type
rush_hour_solver_cut_down.cpp:279: error: ‘void*’ is not a pointer-to-object type
rush_hour_solver_cut_down.cpp:279: error: ‘void*’ is not a pointer-to-object type
rush_hour_solver_cut_down.cpp:281: error: invalid conversion from ‘void*’ to ‘__rush_hour_solver_cut_down__::Vehicle*’


    def copy(self):
        new = Board.__new__(Board)
        new.me_x = self.me_x
        new.me_y = self.me_y
        new.depth = self.depth
        new.parent = self
        new.best_child = None
        new.board = [self.board[i][:] for i in range(WIDTH)]
        new.rep = self.rep[:]
        new.vehicles = self.vehicles[:]
        return new

I changed to using copy.copy which did work, but I couldn't name my
copy methods "copy" otherwise I got this error from the C++ compile

rush_hour_solver_cut_down.cpp: In member function '__rush_hour_solver_cut_down__::Vehicle* __rush_hour_solver_cut_down__::Vehicle::copy()':
rush_hour_solver_cut_down.cpp:94: error: no matching function for call to '__rush_hour_solver_cut_down__::Vehicle::copy(__rush_hour_solver_cut_down__::Vehicle* const)'
rush_hour_solver_cut_down.cpp:89: note: candidates are: __rush_hour_solver_cut_down__::Vehicle* __rush_hour_solver_cut_down__::Vehicle::copy()
rush_hour_solver_cut_down.cpp: In member function '__rush_hour_solver_cut_down__::Board* __rush_hour_solver_cut_down__::Board::copy()':
rush_hour_solver_cut_down.cpp:135: error: no matching function for call to '__rush_hour_solver_cut_down__::Board::copy(__rush_hour_solver_cut_down__::Board* const)'
rush_hour_solver_cut_down.cpp:129: note: candidates are: __rush_hour_solver_cut_down__::Board* __rush_hour_solver_cut_down__::Board::copy()

So I renamed them to pycopy, and they ended up looking like

    def pycopy(self):
        new = copy(self)
        new.parent = self
        new.best_child = None
        new.board = [self.board[i][:] for i in range(WIDTH)]
        new.rep = self.rep[:]
        new.vehicles = self.vehicles[:]
        return new

After all that - some timing results!

Python:   9.3 seconds
Psyco:    5.8 seconds
ShedSkin: 1.0 seconds

Impressive!

I put the code http://www.craig-wood.com/nick/pub/rush_hour_solver_cut_down.py

I left in the commented out bits of code I had to change.

This is only part of the project (375 lines) - it solves Rush Hour
boards.  There is another part which I haven't attempted to compile
yet which finds the most difficult possible boards using a combination
of back tracking and a genetic algorithm.

-- 
Nick Craig-Wood <nick at craig-wood.com> -- http://www.craig-wood.com/nick



More information about the Python-list mailing list