Fix #7282

Hi, this is my first pull request. It is to close issue #7282.

It's a simple replacement of ip.set_hook with atexit.register. Note that the IPython session (`ip`), supplied as the second argument to `atexit.register` is the argument that is supplied to the lambda function.