Fixed shelving in the python version.

I ported the correct fix from the perl version over to python.
Merge shelve fix from  branch 'alpha'