Subject: bug#60142: 28.1; python.el Incorrect region when python-shell-send-region from indented code

From: pmercatoris <[email-masked]>
Date: Fri, 16 Dec 2022 23:54:23 +0100

Hello,

First of all, this is my first bug report, I'm not sure I am doing this
correctly, so forgive my ignorance.

I am unable to get correct behavious when sending a region from 
indented code
to the python shell. Consider this python code:

```python
if True:
    if True:
        a = "test"
```

If I select the first `True` and `python-shell-send-region`, it will
correctly send it to the consol and print `True`.

Now if I do the same thing with the second `True` it will throw an
error: `IndentationError: expected an indented block after 'if'
statement on line 1` Although it would also reffer to unexpected block
after `if` even it it were `for`, `try`, ...

If I select the `a` or `a = "test"` it will correctly send it to the
console, however it won't echo the evaluation of the statement.

If I select the string "test" and send, it will throw the same error as
the second True.

My python config is very minimal:

  (use-package python
    :ensure nil
    :mode
    ("\\.py\\'" . python-mode)

    :init
    (setq-default indent-tabs-mode nil)

    :config
    (setq python-indent-offset 4
          python-indent-guess-indent-offset-verbose nil
          )

    (setq python-shell-interpreter "jupyter"
          python-shell-interpreter-args "console --simple-prompt"
          python-shell-prompt-detect-failure-warning nil)

    (add-to-list 'python-shell-completion-native-disabled-interpreters
                 "jupyter"))

Kind regards,
Pierre

In GNU Emacs 28.1 (build 1, x86_64-redhat-linux-gnu, GTK+ Version 
3.24.34, cairo version 1.17.6)
 of 2022-07-21 built on buildvm-x86-04.iad2.fedoraproject.org
Windowing system distributor 'The X.Org Foundation', version 
11.0.12201005
System Description: Fedora Linux 37 (Workstation Edition)

Configured using:
 'configure --build=x86_64-redhat-linux-gnu
 --host=x86_64-redhat-linux-gnu --program-prefix=
 --disable-dependency-tracking --prefix=/usr --exec-prefix=/usr
 --bindir=/usr/bin --sbindir=/usr/sbin --sysconfdir=/etc
 --datadir=/usr/share --includedir=/usr/include --libdir=/usr/lib64
 --libexecdir=/usr/libexec --localstatedir=/var
 --sharedstatedir=/var/lib --mandir=/usr/share/man
 --infodir=/usr/share/info --with-dbus --with-gif --with-jpeg --with-png
 --with-rsvg --with-tiff --with-xpm --with-x-toolkit=gtk3 --with-gpm=no
 --with-xwidgets --with-modules --with-harfbuzz --with-cairo --with-json
 --with-native-compilation build_alias=x86_64-redhat-linux-gnu
 host_alias=x86_64-redhat-linux-gnu CC=gcc 'CFLAGS=-DMAIL_USE_LOCKF -O2
 -flto=auto -ffat-lto-objects -fexceptions -g -grecord-gcc-switches
 -pipe -Wall -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2
 -Wp,-D_GLIBCXX_ASSERTIONS
 -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -fstack-protector-strong
 -specs=/usr/lib/rpm/redhat/redhat-annobin-cc1 -m64 -mtune=generic
 -fasynchronous-unwind-tables -fstack-clash-protection -fcf-protection'
 LDFLAGS=-Wl,-z,relro
 PKG_CONFIG_PATH=:/usr/lib64/pkgconfig:/usr/share/pkgconfig'

Configured features:
ACL CAIRO DBUS FREETYPE GIF GLIB GMP GNUTLS GSETTINGS HARFBUZZ JPEG JSON
LIBOTF LIBSELINUX LIBSYSTEMD LIBXML2 M17N_FLT MODULES NATIVE_COMP NOTIFY
INOTIFY PDUMPER PNG RSVG SECCOMP SOUND THREADS TIFF TOOLKIT_SCROLL_BARS
X11 XDBE XIM XPM XWIDGETS GTK3 ZLIB

Important settings:
  value of $LC_MONETARY: en_GB.UTF-8
  value of $LC_NUMERIC: en_GB.UTF-8
  value of $LC_TIME: en_GB.UTF-8
  value of $LANG: en_US.UTF-8
  value of $XMODIFIERS: @im=ibus
  locale-coding-system: utf-8-unix

Major mode: Python

Minor modes in effect:
  which-key-mode: t
  display-line-numbers-mode: t
  rainbow-delimiters-mode: t
  envrc-global-mode: t
  envrc-mode: t
  tree-sitter-hl-mode: t
  global-tree-sitter-mode: t
  tree-sitter-mode: t
  eglot--managed-mode: t
  flymake-mode: t
  marginalia-mode: t
  global-corfu-mode: t
  corfu-mode: t
  vertico-mode: t
  doom-modeline-mode: t
  global-undo-tree-mode: t
  undo-tree-mode: t
  global-evil-surround-mode: t
  evil-surround-mode: t
  global-evil-collection-unimpaired-mode: t
  evil-collection-unimpaired-mode: t
  shell-dirtrack-mode: t
  evil-mode: t
  evil-local-mode: t
  windmove-mode: t
  recentf-mode: t
  savehist-mode: t
  save-place-mode: t
  global-hl-line-mode: t
  electric-pair-mode: t
  delete-selection-mode: t
  override-global-mode: t
  global-eldoc-mode: t
  eldoc-mode: t
  show-paren-mode: t
  electric-indent-mode: t
  mouse-wheel-mode: t
  file-name-shadow-mode: t
  global-font-lock-mode: t
  font-lock-mode: t
  auto-composition-mode: t
  auto-encryption-mode: t
  auto-compression-mode: t
  buffer-read-only: t
  column-number-mode: t
  line-number-mode: t
  transient-mark-mode: t

Load-path shadows:
/home/pmercatoris/.config/emacs/elpa/transient-20221202.1727/transient 
hides /usr/share/emacs/28.1/lisp/transient

Features:
(shadow sort mail-extr emacsbug sendmail cua-base misearch multi-isearch
avy evil-nerd-commenter evil-nerd-commenter-operator
evil-nerd-commenter-sdk sgml-mode facemenu mule-util
evil-collection-markdown-mode markdown-mode vc-mtn vc-hg vc-bzr vc-src
vc-sccs vc-svn vc-cvs vc-rcs vc evil-collection-which-key which-key
face-remap display-line-numbers rainbow-delimiters envrc inheritenv
tree-sitter-langs tree-sitter-langs-build evil-collection-tar-mode
tar-mode evil-collection-arc-mode arc-mode archive-mode tree-sitter-hl
tree-sitter tree-sitter-load tree-sitter-cli tsc tsc-dyn tsc-dyn-get
dired-aux tsc-obsolete ob-python evil-collection-python python tramp-sh
tramp tramp-loaddefs trampver tramp-integration files-x tramp-compat
ls-lisp hydra lv consult-eglot evil-collection-consult consult-vertico
consult compat-28 evil-collection-bookmark bookmark
evil-collection-eglot eglot array filenotify jsonrpc evil-collection-ert
ert pp ewoc evil-collection-debug debug backtrace evil-collection-xref
xref evil-collection-flymake flymake-proc flymake
evil-collection-compile compile pcase project evil-collection-imenu
imenu orderless marginalia evil-collection-corfu corfu
evil-collection-vertico vertico doom-modeline doom-modeline-segments
doom-modeline-env doom-modeline-core shrink-path f f-shortdoc
evil-collection-shortdoc shortdoc s all-the-icons all-the-icons-faces
data-material data-weathericons data-octicons data-fileicons
data-faicons data-alltheicons rainbow-mode xterm-color color
ef-duo-dark-theme ef-themes undo-tree diff queue evil-surround
evil-collection-unimpaired evil-collection-vc-git
evil-collection-tabulated-list evil-collection-tab-bar
evil-collection-speedbar evil-collection-simple evil-collection-replace
evil-collection-process-menu evil-collection-package-menu
evil-collection-outline evil-collection-org evil-collection-info
evil-collection-indent evil-collection-image evil-collection-help
evil-collection-gnus evil-collection-eww evil-collection-epa
evil-collection-elisp-mode evil-collection-eldoc
evil-collection-doc-view evil-collection-dired evil-collection-diff-mode
evil-collection-comint evil-collection-calendar calc-ext
evil-collection-calc evil-collection-buff-menu
evil-collection-auto-package-update evil-collection annalist evil
evil-integration evil-maps evil-commands reveal flyspell ispell
evil-jumps evil-command-window evil-search evil-ex shell evil-types
evil-macros evil-repeat evil-states evil-core comp comp-cstr warnings
evil-common windmove calc calc-loaddefs calc-macs rect evil-digraphs
evil-vars edmacro kmacro recentf tree-widget savehist saveplace hl-line
elec-pair delsel no-littering compat compat-macs diary-lib
diary-loaddefs auto-package-update dash use-package use-package-ensure
use-package-delight use-package-diminish use-package-bind-key bind-key
use-package-core finder-inf vc-git diff-mode vc-dispatcher org-element
avl-tree generator ol-eww eww xdg url-queue thingatpt mm-url ol-rmail
ol-mhe ol-irc ol-info ol-gnus nnselect gnus-search eieio-opt speedbar
ezimage dframe gnus-art mm-uu mml2015 mm-view mml-smime smime dig
gnus-sum shr kinsoku svg dom gnus-group gnus-undo gnus-start gnus-dbus
dbus xml gnus-cloud nnimap nnmail mail-source utf7 netrc nnoo parse-time
gnus-spec gnus-int gnus-range message rmc puny rfc822 mml mml-sec epa
derived epg rfc6068 epg-config mm-decode mm-bodies mm-encode mail-parse
rfc2231 mailabbrev gmm-utils mailheader gnus-win gnus nnheader gnus-util
rmail rmail-loaddefs rfc2047 rfc2045 ietf-drums text-property-search
mail-utils mm-util mail-prsvr wid-edit ol-docview doc-view jka-compr
image-mode exif dired dired-loaddefs ol-bibtex ol-bbdb ol-w3m ol-doi
org-link-doi cl-extra help-mode org ob ob-tangle ob-ref ob-lob ob-table
ob-exp org-macro org-footnote org-src ob-comint org-pcomplete pcomplete
comint ansi-color ring org-list org-faces org-entities noutline outline
easy-mmode org-version ob-emacs-lisp ob-core ob-eval org-table oc-basic
bibtex iso8601 time-date ol rx org-keys oc org-compat advice org-macs
org-loaddefs format-spec find-func cal-menu calendar cal-loaddefs info
package browse-url url url-proxy url-privacy url-expand url-methods
url-history url-cookie url-domsuf url-util mailcap url-handlers
url-parse auth-source cl-seq eieio eieio-core cl-macs eieio-loaddefs
password-cache json subr-x map url-vars seq byte-opt gv bytecomp
byte-compile cconv cl-loaddefs cl-lib iso-transl tooltip eldoc paren
electric uniquify ediff-hook vc-hooks lisp-float-type elisp-mode mwheel
term/x-win x-win term/common-win x-dnd tool-bar dnd fontset image
regexp-opt fringe tabulated-list replace newcomment text-mode lisp-mode
prog-mode register page tab-bar menu-bar rfn-eshadow isearch easymenu
timer select scroll-bar mouse jit-lock font-lock syntax font-core
term/tty-colors frame minibuffer cl-generic cham georgian utf-8-lang
misc-lang vietnamese tibetan thai tai-viet lao korean japanese eucjp-ms
cp51932 hebrew greek romanian slovak czech european ethiopic indian
cyrillic chinese composite emoji-zwj charscript charprop case-table
epa-hook jka-cmpr-hook help simple abbrev obarray cl-preloaded nadvice
button loaddefs faces cus-face macroexp files window text-properties
overlay sha1 md5 base64 format env code-pages mule custom widget
hashtable-print-readable backquote threads xwidget-internal dbusbind
inotify dynamic-setting system-font-setting font-render-setting cairo
move-toolbar gtk x-toolkit x multi-tty make-network-process
native-compile emacs)

Memory information:
((conses 16 773438 128306)
 (symbols 48 46851 49)
 (strings 32 207758 12187)
 (string-bytes 1 6926856)
 (vectors 16 89108)
 (vector-slots 8 2338965 214323)
 (floats 8 1053 928)
 (intervals 56 7474 3578)
 (buffers 992 27))