You can simplify using:
'''python
start = sm.positionFromLine(sm.selectionStart)
end = sm.positionFromLine(sm.selectionEnd)
'''