Fix %run -t -N<N> TypeError

Adds a test case and a fix for issue #2784.