'This function is called when a return trap is set here.'

def user_return(self, frame, return_value):

    if self._wait_for_mainpyfile:
        return
    else:
        frame.f_locals['__return__'] = return_value
        print >>self.stdout, '--Return--'
        self.interaction(frame, None)
        return
