Assuming some python buffer like: #.... if 1: print 1 where the selected region is "print \"1\"" (mark before "p"), `python-shell-send-region' would fail with: File "test.py", line 3 print 1 ^ IndentationError: expected an indented block The reason lies within `python-shell-buffer-substring', which assumes that a region always starts with the correct indentation. The attached patch fixes the above behavior: when the region doesn't start with indentation, but the current block does (looking at (current-indentation)), we prefix the fillstr with the correct amount of indent.