emacs-orgmode
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

‘org-test-with-temp-text’ fails weirdly


From: Göktuğ Kayaalp
Subject: ‘org-test-with-temp-text’ fails weirdly
Date: Thu, 07 May 2020 05:22:24 +0300

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

>From 88ad28dce8e0111c10ca18db5f58d35924112441 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=C4=B0=2E=20G=C3=B6ktu=C4=9F=20Kayaalp?= <address@hidden>
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


reply via email to

[Prev in Thread] Current Thread [Next in Thread]