From mboxrd@z Thu Jan 1 00:00:00 1970 From: Jack Kamm Subject: Re: ob-python newline & indentation behavior Date: Sun, 19 Nov 2017 07:41:26 +0000 Message-ID: References: <87h8trtyoa.fsf@kyleam.com> Mime-Version: 1.0 Content-Type: multipart/alternative; boundary="001a113dbea6f0fc53055e5118a0" Return-path: Received: from eggs.gnu.org ([2001:4830:134:3::10]:46847) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eGKEN-0004gh-IW for emacs-orgmode@gnu.org; Sun, 19 Nov 2017 02:41:33 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1eGKEK-0006Dw-GE for emacs-orgmode@gnu.org; Sun, 19 Nov 2017 02:41:31 -0500 Received: from mail-yw0-x233.google.com ([2607:f8b0:4002:c05::233]:39260) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1eGKEK-0006DO-7L for emacs-orgmode@gnu.org; Sun, 19 Nov 2017 02:41:28 -0500 Received: by mail-yw0-x233.google.com with SMTP id g204so3193430ywa.6 for ; Sat, 18 Nov 2017 23:41:28 -0800 (PST) In-Reply-To: List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org Sender: "Emacs-orgmode" To: Martin Alsinet Cc: emacs-orgmode@gnu.org --001a113dbea6f0fc53055e5118a0 Content-Type: text/plain; charset="UTF-8" Sorry, I should have mentioned my version info anyways. I have tested on emacs 25.3.1 and emacs 26.0.90, and org-mode versions 9.1.2 and 9.1.3 (current master). The same error occurs on all emacs and org-mode versions. However the error slightly differs between Python and IPython interpreters. IPython prints "11" (instead of the expected "20"), whereas Python raises an IndentationError then prints "10". I've cleaned up my patch to deal with a few edge cases. In particular: 1) Fixed a bug where I was removing blank lines (these may be needed if they are part of a multiline string object) 2) Send to tmpfile any multiline block even if not indented. This follows the python.el behavior and gives more consistent behavior for ":results output" 3) Allow ":results value" to work when the block ends in several newlines, using string-trim-right Here is the new version of the patch: >From f009da37d3b7e2730abb8cbb10f4d07b3d456dd8 Mon Sep 17 00:00:00 2001 From: Jack Kamm Date: Sun, 19 Nov 2017 07:13:56 +0000 Subject: [PATCH] Squashed commit of the following: commit d1fe88a9f61a8e7082f08b7c190a29737bb655d5 Author: Jack Kamm Date: Sun Nov 19 07:08:31 2017 +0000 fix block ending in blank lines; send multiline blocks to tmpfile commit fcc5a7795e882716775c9d925b0cd5b657da041b Author: Jack Kamm Date: Sat Nov 18 22:40:31 2017 +0000 fix newlines and blanklines when sending codeblock to tmpfile commit a5d553ece9f6ee35cd1e273e554a21a19e80ec3c Author: Jack Kamm Date: Sat Nov 18 21:47:09 2017 +0000 fix newline/indentation issues in ob-python :session --- lisp/ob-python.el | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/lisp/ob-python.el b/lisp/ob-python.el index 60ec5fa47..c3dba1565 100644 --- a/lisp/ob-python.el +++ b/lisp/ob-python.el @@ -303,6 +303,9 @@ last statement in BODY, as elisp." (mapc (lambda (line) (insert line) (funcall send-wait)) (split-string body "[\r\n]")) (funcall send-wait))) + (body (if (string-match-p ".\n+." body) ;; Multiline + (org-babel-python--replace-body-tmpfile body) + body)) (results (pcase result-type (`output @@ -340,6 +343,32 @@ last statement in BODY, as elisp." (substring string 1 -1) string)) + +(defun org-babel-python--replace-body-tmpfile (body) + "Place body in tmpfile, and return string to exec the tmpfile. +If last line of body is not indented, place it at end of exec string +instead of tmpfile, so shell can see the result" + (let* ((body (string-trim-right body)) + (tmp-file (org-babel-temp-file "python-")) + (lines (split-string body "[\r\n]")) + (lastline (car (last lines))) + (newbody (concat + (format "__pyfilename = '%s'; " tmp-file) + "__pyfile = open(__pyfilename); " + "exec(compile(" + "__pyfile.read(), __pyfilename, 'exec'" + ")); " + "__pyfile.close()"))) + (if (string-match-p "^[ \t]" lastline) + (progn + (with-temp-file tmp-file (insert body)) + newbody) + (with-temp-file tmp-file + (insert (mapconcat 'identity + (butlast lines) "\n"))) + (concat newbody "\n" lastline))) + ) + (provide 'ob-python) -- 2.15.0 On Sun, Nov 19, 2017 at 3:34 AM, Martin Alsinet wrote: > Sorry Jack, I overlooked the :session bit. > Disregard my email please > > > On Sat, Nov 18, 2017 at 10:27 PM Martin Alsinet > wrote: > >> Hello Jack: >> >> What versions of emacs and org-mode are you using? >> >> I tried your example on Emacs 25.3.1 and org-mode 9.1.2-22 and I got "20" >> as result >> It has happened to me in the past that some bug I was seeing goes away >> just by updating org-mode. >> >> Regards, >> >> >> Martin >> >> On Sat, Nov 18, 2017 at 5:16 PM Jack Kamm wrote: >> >>> Thanks Kyle, and sorry for missing that recent related thread. >>> >>> I adapted your old patch to the current master branch. I also extended >>> it to work for ":results value" (the original patch only worked for >>> ":results output"). I did this by not writing the last line of the code >>> block to the tmpfile, unless it is indented. >>> >>> I've never contributed before so would appreciate any comments on this >>> patch, and how to get it accepted. Cheers, Jack. >>> >>> From a5d553ece9f6ee35cd1e273e554a21a19e80ec3c Mon Sep 17 00:00:00 2001 >>> >>> From: Jack Kamm >>> Date: Sat, 18 Nov 2017 21:47:09 +0000 >>> Subject: [PATCH] fix newline/indentation issues in ob-python :session >>> >>> --- >>> lisp/ob-python.el | 33 +++++++++++++++++++++++++++++++++ >>> 1 file changed, 33 insertions(+) >>> >>> diff --git a/lisp/ob-python.el b/lisp/ob-python.el >>> index 60ec5fa47..6623ef5fc 100644 >>> --- a/lisp/ob-python.el >>> +++ b/lisp/ob-python.el >>> @@ -303,6 +303,10 @@ last statement in BODY, as elisp." >>> (mapc (lambda (line) (insert line) (funcall >>> send-wait)) >>> (split-string body "[\r\n]")) >>> (funcall send-wait))) >>> + (indented-p (org-babel-python--indented-p body)) >>> + (body (if indented-p >>> + (org-babel-python--replace-body-tmpfile body) >>> + body)) >>> (results >>> (pcase result-type >>> (`output >>> @@ -340,6 +344,35 @@ last statement in BODY, as elisp." >>> (substring string 1 -1) >>> string)) >>> >>> + >>> +(defun org-babel-python--indented-p (input) >>> + "Non-nil if any line in INPUT is indented." >>> + (string-match-p "^[ \t]" input)) >>> + >>> +(defun org-babel-python--replace-body-tmpfile (body) >>> + "Place body in tmpfile, and return string to exec the tmpfile. >>> +If last line of body is not indented, place it at end of exec string >>> +instead of tmpfile, so shell can see the result" >>> + (let* ((tmp-file (org-babel-temp-file "python-")) >>> + (lines (split-string body "\n" t)) >>> + (lastline (car (last lines))) >>> + (newbody (concat >>> + (format "__pyfilename = '%s'; " tmp-file) >>> + "__pyfile = open(__pyfilename); " >>> + "exec(compile(" >>> + "__pyfile.read(), __pyfilename, 'exec'" >>> + ")); " >>> + "__pyfile.close()"))) >>> + (if (string-match-p "^[ \t]" lastline) >>> + (progn >>> + (with-temp-file tmp-file (insert body)) >>> + newbody) >>> + (with-temp-file tmp-file >>> + (insert (mapconcat 'identity >>> + (butlast lines) "\n"))) >>> + (concat newbody "\n" lastline))) >>> + ) >>> + >>> (provide 'ob-python) >>> >>> >>> -- >>> 2.15.0 >>> >>> >>> On Sat, Nov 18, 2017 at 3:05 PM, Kyle Meyer wrote: >>> >>>> Hello, >>>> >>>> Jack Kamm writes: >>>> >>>> > ob-python newline & indentation behavior in :session is very ugly and >>>> > possibly broken. For example, consider the following code block: >>>> >>>> [...] >>>> >>>> > There is a 2 year old patch that fixes this behavior but has not yet >>>> > been incorporated: >>>> > https://lists.gnu.org/archive/html/emacs-orgmode/2015-03/ >>>> msg00505.html >>>> >>>> [...] >>>> >>>> > The patch follows the python.el behavior of using a temporary file and >>>> > executing that from the shell. >>>> > >>>> > Could this patch, or something similar, be incorporated into org-mode, >>>> > to fix this behavior? >>>> >>>> Here's what I said in a recent post about ob-python sessions: >>>> >>>> https://lists.gnu.org/archive/html/emacs-orgmode/2017-10/ >>>> msg00198.html >>>> >>>> I dropped my attempt to fix it because 1) I was still having trouble >>>> getting a complete understanding of what the issue was and 2) I >>>> didn't >>>> have the motivation to spend time digging deeper because I don't use >>>> ob-python (and in general am not a heavy Org-Babel user). Perhaps >>>> you >>>> or some other ob-python user could help make ob-python sessions more >>>> robust? >>>> >>>> Perhaps you are the "you"? >>>> >>>> -- >>>> Kyle >>>> >>> >>> --001a113dbea6f0fc53055e5118a0 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
Sorry, I should have mentioned my version info anyways. I = have tested on emacs 25.3.1 and emacs 26.0.90, and org-mode versions 9.1.2 = and 9.1.3 (current master).

The same error occurs on all= emacs and org-mode versions. However the error slightly differs between Py= thon and IPython interpreters. IPython prints "11" (instead of th= e expected "20"), whereas Python raises an IndentationError then = prints "10".

I've cleaned up my patc= h to deal with a few edge cases. In particular:
1) Fixed a bug wh= ere I was removing blank lines (these may be needed if they are part of a m= ultiline string object)
2) Send to tmpfile any multiline block ev= en if not indented. This follows the python.el behavior and gives more cons= istent behavior for ":results output"
3) Allow ":r= esults value" to work when the block ends in several newlines, using s= tring-trim-right

Here is the new version of the pa= tch:

From f009da37d3b7e2730abb8cbb10f4d07b3d4= 56dd8 Mon Sep 17 00:00:00 2001=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0=C2=A0
From: Jack Kamm <jackkamm@gmail.com>
Date: Sun, 19 Nov 2017 07:13:56 = +0000
Subject: [PATCH] Squashed commit of the following:

commit d1fe88a9f61a8e7082f08b7c190a29737bb655d5
= Author: Jack Kamm <jackkamm@gmail.= com>
Date:=C2=A0 =C2=A0Sun Nov 19 07:08:31 2017 +0000

=C2=A0 =C2=A0 fix block ending in blank lines; send mu= ltiline blocks to tmpfile

commit fcc5a7795e8827167= 75c9d925b0cd5b657da041b
Author: Jack Kamm <jackkamm@gmail.com>
Date:=C2=A0 =C2=A0S= at Nov 18 22:40:31 2017 +0000

=C2=A0 =C2=A0 fix ne= wlines and blanklines when sending codeblock to tmpfile

commit a5d553ece9f6ee35cd1e273e554a21a19e80ec3c
Author: Jac= k Kamm <jackkamm@gmail.com>=
Date:=C2=A0 =C2=A0Sat Nov 18 21:47:09 2017 +0000

<= /div>
=C2=A0 =C2=A0 fix newline/indentation issues in ob-python :sessio= n
---
=C2=A0lisp/ob-python.el | 29 ++++++++++++++++++++= +++++++++
=C2=A01 file changed, 29 insertions(+)

diff --git a/lisp/ob-python.el b/lisp/ob-python.el
index= 60ec5fa47..c3dba1565 100644
--- a/lisp/ob-python.el
++= + b/lisp/ob-python.el
@@ -303,6 +303,9 @@ last statement in BODY,= as elisp."
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0= =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0(mapc (lambda (line) (insert line) (func= all send-wait))
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0(split-string body &= quot;[\r\n]"))
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0(funcall send-wait)))
+=C2= =A0 =C2=A0 =C2=A0 =C2=A0 (body (if (string-match-p ".\n+." body) = ;; Multiline
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 (org-babel-python--replace-body-tmpfile body)
+=C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 body))
=C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 (results
=C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0(pcase result-type
=C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0(`output
@@ -340,6 +343,32 @@ last statement = in BODY, as elisp."
=C2=A0 =C2=A0 =C2=A0 =C2=A0(substring st= ring 1 -1)
=C2=A0 =C2=A0 =C2=A0string))
=C2=A0
+
+(defun org-babel-python--replace-body-tmpfile (body)
+=C2=A0 "Place body in tmpfile, and return string to exec the tmpfi= le.
+If last line of body is not indented, place it at end of exe= c string
+instead of tmpfile, so shell can see the result"
+=C2=A0 (let* ((body (string-trim-right body))
+=C2=A0 = =C2=A0 =C2=A0 =C2=A0 (tmp-file (org-babel-temp-file "python-"))
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 (lines (split-string body "[\r\n= ]"))
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 (lastline (car (last lines= )))
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 (newbody (concat
+=C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 (format "_= _pyfilename =3D '%s'; " tmp-file)
+=C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 "__pyfile =3D open(__pyf= ilename); "
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 "exec(compile("
+=C2=A0 =C2=A0 =C2=A0= =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 "__pyfile.read(), __pyfilen= ame, 'exec'"
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 ")); "
+=C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 "__pyfile.close()")))
+=C2=A0 =C2=A0 (if (string-match-p "^[ \t]" lastline)
+=C2=A0 =C2=A0 =C2=A0 =C2=A0(progn
+=C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0(with-temp-file tmp-file (insert body))
+=C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0newbody)
+=C2=A0 =C2=A0 =C2=A0 (with-temp= -file tmp-file
+=C2=A0 =C2=A0 =C2=A0 =C2=A0(insert (mapconcat = 9;identity
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 (butlast lines) "\n")))
+=C2=A0 =C2=A0 =C2=A0 (concat newbody "\n" lastline)))
+=C2=A0 )
+
=C2=A0(provide 'ob-python)
=C2=A0
=C2=A0
--=C2=A0
2.15.0




On Sun, Nov 19, 2017 at 3:34 AM, Martin= Alsinet <martin@alsinet.com.ar> wrote:
Sorry Jack, I overlooked the :session bi= t.
Disregard my email please


On = Sat, Nov 18, 2017 at 10:27 PM Martin Alsinet <martin@alsinet.com.ar> wrote:
Hello Jack:

What versions of emacs and org-mode are you using?

I tried your example on Emacs 25.3.1 and org-mode 9.1.2-22 and I g= ot "20" as result
It has happened to me in the past tha= t some bug I was seeing goes away just by updating org-mode.

=
Regards,


Martin

On Sat, Nov 18, 2017 at = 5:16 PM Jack Kamm <jackkamm@gmail.com> wrote:
Thanks Kyle, and sorry for missing that recent related t= hread.

I adapted your old patch to the current mast= er branch. I also extended it to work for ":results value" (the o= riginal patch only worked for ":results output"). I did this by n= ot writing the last line of the code block to the tmpfile, unless it is ind= ented.

I've never contributed before so would apprec= iate any comments on this patch, and how to get it accepted. Cheers, Jack.<= /div>

From a5d553ece9f6ee35cd1e273e554a21a19e8= 0ec3c Mon Sep 17 00:00:00 2001=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0=C2=A0
From: Jack Kamm <jackkamm@gmail.com>
Date: Sat, 18 = Nov 2017 21:47:09 +0000
Subject: [PATCH] fix newline/indentation = issues in ob-python :session

---
=C2=A0l= isp/ob-python.el | 33 +++++++++++++++++++++++++++++++++
=C2= =A01 file changed, 33 insertions(+)

diff --git a/l= isp/ob-python.el b/lisp/ob-python.el
index 60ec5fa47..6623ef5fc 1= 00644
--- a/lisp/ob-python.el
+++ b/lisp/ob-python.el
@@ -303,6 +303,10 @@ last statement in BODY, as elisp."
=
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0(mapc (lambda (line) (insert line) (funcall send-wait))
<= div>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0(split-string body "[\r\n]"))
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0(funcall send-wait)))
+=C2=A0 =C2=A0 =C2=A0 =C2= =A0 (indented-p (org-babel-python--indented-p body))
+=C2=A0 =C2= =A0 =C2=A0 =C2=A0 (body (if indented-p
+=C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 (org-babel-python--replace-body= -tmpfile body)
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 body))
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 (results
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0(pcase result-type
= =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0(`output
@@ -340,= 6 +344,35 @@ last statement in BODY, as elisp."
=C2=A0 =C2= =A0 =C2=A0 =C2=A0(substring string 1 -1)
=C2=A0 =C2=A0 =C2=A0stri= ng))

+
+(defun org-babel-python--indente= d-p (input)
+=C2=A0 =C2=A0 =C2=A0"Non-nil if any line in INP= UT is indented."
+=C2=A0 =C2=A0 =C2=A0(string-match-p "= ^[ \t]" input))
+
+(defun org-babel-python--replac= e-body-tmpfile (body)
+=C2=A0 "Place body in tmpfile, a= nd return string to exec the tmpfile.
+If last line of body is no= t indented, place it at end of exec string
+instead of tmpfile, s= o shell can see the result"
+=C2=A0 (let* ((tmp-file (org-ba= bel-temp-file "python-"))
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 = (lines (split-string body "\n" t))
+=C2=A0 =C2=A0 =C2= =A0 =C2=A0 (lastline (car (last lines)))
+=C2=A0 =C2=A0 =C2=A0 = =C2=A0 (newbody (concat
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 (format "__pyfilename =3D '%s'; "= ; tmp-file)
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 "__pyfile =3D open(__pyfilename); "
+=C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 "exec(compile(= "
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 "__pyfile.read(), __pyfilename, 'exec'"
= +=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 ")); &= quot;
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 "__pyfile.close()")))
+=C2=A0 =C2=A0 (if (string= -match-p "^[ \t]" lastline)
+=C2=A0 =C2=A0 =C2=A0 =C2= =A0(progn
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0(with-temp-file tmp-= file (insert body))
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0newbody)
+=C2=A0 =C2=A0 =C2=A0 (with-temp-file tmp-file
+=C2=A0 = =C2=A0 =C2=A0 =C2=A0(insert (mapconcat 'identity
+=C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 (butlast lines) "\n")))
+=C2=A0 =C2=A0 =C2=A0 (c= oncat newbody "\n" lastline)))
+=C2=A0 )
+
=C2=A0(provide 'ob-python)


<= div>--
2.15.0


On Sat, Nov 18, 2017 at = 3:05 PM, Kyle Meyer <kyle@kyleam.com> wrote:
Hello,

Jack Kamm <jackk= amm@gmail.com> writes:

> ob-python newline & indentation behavior in :session is very ugly = and
> possibly broken. For example, consider the following code block:

[...]

> There is a 2 year old patch that fixes this behavior but has not yet > been incorporated:
> https://lists.gnu.org/arc= hive/html/emacs-orgmode/2015-03/msg00505.html

[...]

> The patch follows the python.el behavior of using a temporary file and=
> executing that from the shell.
>
> Could this patch, or something similar, be incorporated into org-mode,=
> to fix this behavior?

Here's what I said in a recent post about ob-python sessions:

=C2=A0 =C2=A0 https://lists.gn= u.org/archive/html/emacs-orgmode/2017-10/msg00198.html

=C2=A0 =C2=A0 I dropped my attempt to fix it because 1) I was still having = trouble
=C2=A0 =C2=A0 getting a complete understanding of what the issue was and 2)= I didn't
=C2=A0 =C2=A0 have the motivation to spend time digging deeper because I do= n't use
=C2=A0 =C2=A0 ob-python (and in general am not a heavy Org-Babel user).=C2= =A0 Perhaps you
=C2=A0 =C2=A0 or some other ob-python user could help make ob-python sessio= ns more
=C2=A0 =C2=A0 robust?

Perhaps you are the "you"?

--
Kyle


--001a113dbea6f0fc53055e5118a0--