Issue #17825: Cursor ^ is correctly positioned for SyntaxError and IndentationError.
diff --git a/Lib/traceback.py b/Lib/traceback.py
index 5ad2838..0a3dd11 100644
--- a/Lib/traceback.py
+++ b/Lib/traceback.py
@@ -189,11 +189,12 @@
         if badline is not None:
             lines.append('    %s\n' % badline.strip())
             if offset is not None:
-                caretspace = badline.rstrip('\n')[:offset].lstrip()
+                caretspace = badline.rstrip('\n')
+                offset = min(len(caretspace), offset) - 1
+                caretspace = caretspace[:offset].lstrip()
                 # non-space whitespace (likes tabs) must be kept for alignment
                 caretspace = ((c.isspace() and c or ' ') for c in caretspace)
-                # only three spaces to account for offset1 == pos 0
-                lines.append('   %s^\n' % ''.join(caretspace))
+                lines.append('    %s^\n' % ''.join(caretspace))
         value = msg
 
     lines.append(_format_final_exc_line(stype, value))