OK I found the solution. In the code I mentioned the error was that smith_form doesn't return matrices in $SL_2(\mathbb{Z})$. It's quite easy to correct by the following snippet that you have to add just after you use smith_form :
 D, U, V = A.smith_form()