fix possible use of deallocated python object

 __dealloc__ method might be called when the object is partially deleted, so
e.g. its methods must not be called. Fix this by copying the code of the
methods which require no python object internals.

Closes #458.