* ‘org-test-with-temp-text’ fails weirdly
@ 2020-05-07 2:22 Göktuğ Kayaalp
2020-05-14 14:06 ` Nicolas Goaziou
0 siblings, 1 reply; 2+ messages in thread
From: Göktuğ Kayaalp @ 2020-05-07 2:22 UTC (permalink / raw)
To: emacs-orgmode
[-- Attachment #1: Type: text/plain, Size: 1398 bytes --]
Hello,
I was trying to have spurious indentation removed from Python source
blocks before execution so that such blocks can be indented in Org mode
buffers. I managed to do so successfully, but some tests keep failing
and I believe it’s the test runner that’s the culprit. With the attached
patch applied, I’ve observed successful execution when manually trying
test cases similar to those in test-ob-python.el, indented or not. But
the following tests fail with ‘(args-out-of-range "return x" 42 43)’
errors:
FAILED test-ob-python/colnames-nil-header-argument
FAILED test-ob-python/colnames-no-header-argument
FAILED test-ob-python/colnames-yes-header-argument
(9 tests fail without this patch, the other 6 is irrelevant and do fail
when I test w/o the patch too, on commit b171ff02f.)
I think the cause is the modifications to the code blocks body (deletion
of spurious indentation from an indented src block), but I’m not sure
how exactly.
This is weird because the in-buffer text doesn’t change.
In any case I’m also proposing the attached patch as a new feature.
Could start a new thread for it if necessary.
P.S.: please keep me in the CC in your replies, I’m not subscribed to
the mailing list.
--
İ. Göktuğ Kayaalp / @cadadr / <https://www.gkayaalp.com/>
pgp: 024C 30DD 597D 142B 49AC 40EB 465C D949 B101 2427
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-lisp-ob-python.el-remove-spurious-indentation-before.patch --]
[-- Type: text/x-diff, Size: 1839 bytes --]
From 88ad28dce8e0111c10ca18db5f58d35924112441 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=C4=B0=2E=20G=C3=B6ktu=C4=9F=20Kayaalp?= <self@gkayaalp.com>
Date: Thu, 7 May 2020 03:11:50 +0300
Subject: [PATCH] lisp/ob-python.el: remove spurious indentation before
evaluation
---
lisp/ob-python.el | 21 ++++++++++++++++++++-
1 file changed, 20 insertions(+), 1 deletion(-)
diff --git a/lisp/ob-python.el b/lisp/ob-python.el
index dbcfac08d..42bb47f73 100644
--- a/lisp/ob-python.el
+++ b/lisp/ob-python.el
@@ -69,6 +69,24 @@ This will typically be either `python' or `python-mode'."
:package-version '(Org . "8.0")
:type 'symbol)
+(defun org-babel-python--clean-spurious-indentation (body)
+ (let* ((extra-indentation
+ (save-match-data
+ (string-match "\\`\\([ \t]+\\)" body)
+ (match-string 1 body)))
+ (xlen (length extra-indentation)))
+ (if (zerop xlen)
+ body
+ (mapconcat
+ (lambda (line) (if (<= (length line) xlen)
+ line
+ (if (string= extra-indentation
+ (substring line 0 xlen))
+ (substring line xlen)
+ line)))
+ (split-string body "\n")
+ "\n"))))
+
(defun org-babel-execute:python (body params)
"Execute a block of Python code with Babel.
This function is called by `org-babel-execute-src-block'."
@@ -84,7 +102,8 @@ This function is called by `org-babel-execute-src-block'."
(preamble (cdr (assq :preamble params)))
(full-body
(org-babel-expand-body:generic
- (concat body (if return-val (format "\nreturn %s" return-val) ""))
+ (concat (org-babel-python--clean-spurious-indentation body)
+ (if return-val (format "\nreturn %s" return-val) ""))
params (org-babel-variable-assignments:python params)))
(result (org-babel-python-evaluate
session full-body result-type result-params preamble)))
--
2.17.1
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: ‘org-test-with-temp-text’ fails weirdly
2020-05-07 2:22 ‘org-test-with-temp-text’ fails weirdly Göktuğ Kayaalp
@ 2020-05-14 14:06 ` Nicolas Goaziou
0 siblings, 0 replies; 2+ messages in thread
From: Nicolas Goaziou @ 2020-05-14 14:06 UTC (permalink / raw)
To: Göktuğ Kayaalp; +Cc: emacs-orgmode
Hello,
Göktuğ Kayaalp <self@gkayaalp.com> writes:
> I was trying to have spurious indentation removed from Python source
> blocks before execution so that such blocks can be indented in Org mode
> buffers.
Removing indentation from Python blocks sounds like a bad idea. Do you
have an example demonstrating what you need?
> I think the cause is the modifications to the code blocks body (deletion
> of spurious indentation from an indented src block), but I’m not sure
> how exactly.
>
> This is weird because the in-buffer text doesn’t change.
Do you have a reproducer for this? Ideally, using Emacs Lisp instead of
Python.
> In any case I’m also proposing the attached patch as a new feature.
> Could start a new thread for it if necessary.
> +(defun org-babel-python--clean-spurious-indentation (body)
> + (let* ((extra-indentation
> + (save-match-data
> + (string-match "\\`\\([ \t]+\\)" body)
> + (match-string 1 body)))
> + (xlen (length extra-indentation)))
> + (if (zerop xlen)
> + body
> + (mapconcat
> + (lambda (line) (if (<= (length line) xlen)
> + line
> + (if (string= extra-indentation
> + (substring line 0 xlen))
> + (substring line xlen)
> + line)))
> + (split-string body "\n")
> + "\n"))))
I think `org-remove-indentation' does about the same. It does not
systematically use first line as reference, though.
Regards,
--
Nicolas Goaziou
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2020-05-14 14:07 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2020-05-07 2:22 ‘org-test-with-temp-text’ fails weirdly Göktuğ Kayaalp
2020-05-14 14:06 ` Nicolas Goaziou
Code repositories for project(s) associated with this public inbox
https://git.savannah.gnu.org/cgit/emacs/org-mode.git
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).