[O] [PATCH] ob-core.el: Fix org-babel--string-to-number
Hello, I've write a patch (see attachment) to fix the `org-babel--string-to-number` function. In my opinion, if people write the data in the form `0001`, it means that he wants to treat it as a string: > #+name: TBL > | id | name | age | > |--||-| > | 0001 | Apollo | 16 | > | 0002 | Bmw| 16 | > > #+BEGIN_SRC emacs-lisp :results value pp :var tbl=TBL > (mapc 'print tbl) > #+END_SRC > > #+RESULTS: > : (("0001" "Apollo" 16) > : ("0002" "Bmw" 16)) Qijian 0001-ob-core.el-Fix-org-babel-string-to-number.patch Description: Binary data
Re: [O] [PATCH] ob-python: Insert necessary blank line when sending code to interpreter
I've added comment and test (New path file in attachment),the test results look fine. #+BEGIN_SRC sh $ make BTEST_RE="test-ob-python" test-dirty ... selected tests: test-ob-python Running 7 tests (2018-04-03 16:51:31+0800) executing Python code block... Code block evaluation complete. passed 1/7 test-ob-python/colnames-nil-header-argument executing Python code block... Code block evaluation complete. passed 2/7 test-ob-python/colnames-no-header-argument executing Python code block... Code block evaluation complete. passed 3/7 test-ob-python/colnames-no-header-argument-again executing Python code block... Code block evaluation complete. passed 4/7 test-ob-python/colnames-yes-header-argument executing Python code block... Code block evaluation complete. passed 5/7 test-ob-python/colnames-yes-header-argument-again executing Python code block... Code block evaluation complete. passed 6/7 test-ob-python/insert-necessary-blank-line-when-sending-code-to-interpreter executing Python code block... Code block evaluation complete. passed 7/7 test-ob-python/session-multiline Ran 7 tests, 7 results as expected (2018-04-03 16:51:33+0800) #+END_SRC Nicolas Goaziou <m...@nicolasgoaziou.fr>于2018年4月3日 周二上午2:56写道: > Hello, > > qijian gong <gongqij...@gmail.com> writes: > > > I've written this patch to fix the syntax error alarm caused by > evaluating > > the following code block: > > > > #+BEGIN_SRC python :session > > if True: > > 1 > > 2 > > #+END_SRC > > Thank you. > > Could you add a test in "test-ob-python.el"? Could you also add > a comment explaining what you are doing? > > > +(let ((curr-indent (string-match "[^\s]" line))) > > (string-match "\\S-" line) > > Regards, > > -- > Nicolas Goaziou > From 1ec522ab99daa17c2e7b4e06ace1dccea1a979a3 Mon Sep 17 00:00:00 2001 From: Gong Qijian <gongqijian@gmail.com> Date: Sun, 1 Apr 2018 16:34:32 +0800 Subject: [PATCH] ob-python: Insert necessary blank line when sending code to interpreter * lisp/ob-python.el (org-bable-python-evaluate-session): Syntax error occurs when evaulating the following code block: \#+begin_src python :session if True: 1 2 \#+end_src A blank line is required for top level module code to end an indented block, such as a for loop, try/except, or if statement. https://www.python.org/dev/peps/pep-0008/#blank-line TINYCHANGE --- lisp/ob-python.el | 14 +- testing/lisp/test-ob-python.el | 16 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/lisp/ob-python.el b/lisp/ob-python.el index 9f1234bac..13997c1a0 100644 --- a/lisp/ob-python.el +++ b/lisp/ob-python.el @@ -308,8 +308,20 @@ last statement in BODY, as elisp." (list (format "open('%s', 'w').write(str(_))" (org-babel-process-file-name tmp-file 'noquote))) + (last-indent 0) (input-body (lambda (body) - (mapc (lambda (line) (insert line) (funcall send-wait)) + (mapc (lambda (line) + ;; Insert a blank line to end an indent block. + (let ((curr-indent (string-match "[^\s]" line))) + (if curr-indent + (progn + (when (< curr-indent last-indent) + (insert "") + (funcall send-wait)) + (setq last-indent curr-indent)) + (setq last-indent 0))) + (insert line) + (funcall send-wait)) (split-string body "[\r\n]")) (funcall send-wait))) (results diff --git a/testing/lisp/test-ob-python.el b/testing/lisp/test-ob-python.el index 915b1bc77..8c5ca83be 100644 --- a/testing/lisp/test-ob-python.el +++ b/testing/lisp/test-ob-python.el @@ -118,6 +118,22 @@ return x (org-babel-next-src-block) (should (equal "20" (org-babel-execute-src-block) +(ert-deftest test-ob-python/insert-necessary-blank-line-when-sending-code-to-interpreter () + (org-test-with-temp-text "#+begin_src python :session :results value +if True: +1 +2 +#+end_src" +;; Previously, while adding `:session' to a normal code block, also need to add extra blank lines +;; to end indent block or indicate logical sections. Now, the `org-babel-python-evaluate-session' +;; can do it automatically: +;; >>> if True: +;; >>> 1 +;; >>> +;; >>> 2 +(org-babel-execute-maybe) +(should (equal 2 (org-babel-execute-src-block) + (provide 'test-ob-python) ;;; test-ob-python.el ends here -- 2.16.2
[O] [PATCH] ob-python: Insert necessary blank line when sending code to interpreter
Hello, I've written this patch to fix the syntax error alarm caused by evaluating the following code block: #+BEGIN_SRC python :session if True: 1 2 #+END_SRC In *Python* buffer: #+BEGIN_QUOTE if True: Python 3.6.1 (default, Apr 8 2017, 19:21:02) [GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.42.1)] on darwin Type "help", "copyright", "credits" or "license" for more information. 1 >>> ... ... File "", line 3 import codecs, os;__pyfile = codecs.open('''/var/folders/vq/k0y6n92j6r5dk_hx_jrl62jhgn/T/py6GOx5H''', encoding='''utf-8''');__code = __pyfile.read().encode('''utf-8''');__pyfile.close();os.remove('''/var/folders/vq/k0y6n92j6r5dk_hx_jrl62jhgn/T/py6GOx5H''');exec(compile(__code, '''/var/folders/vq/k0y6n92j6r5dk_hx_jrl62jhgn/T/py6GOx5H''', 'exec')); ^ SyntaxError: invalid syntax >>> 2 2 >>> open('/var/folders/vq/k0y6n92j6r5dk_hx_jrl62jhgn/T/babel-2PNS5b/python-oXjil1', 'w').write(str(_)) >>> 1 >>> 'org_babel_python_eoe' >>> 'org_babel_python_eoe' #+END_QUOTE To fix this, need to insert an extra blank line to end the if statement. More discussion[1][2][3]. Thanks, Qijian Footnotes: [1] https://emacs-china.org/t/topic/5436/ [2] https://stackoverflow.com/questions/8391633/blank-line-rule-at-interactive-prompt/41988906#41988906 [3] https://www.python.org/dev/peps/pep-0008/#blank-line -- >From 4481a5032e94a85153b708c66858d2965abf6d84 Mon Sep 17 00:00:00 2001 From: Gong QijianDate: Sun, 1 Apr 2018 16:34:32 +0800 Subject: [PATCH] ob-python: Insert necessary blank line when sending code to interpreter * lisp/ob-python.el (org-bable-python-evaluate-session): Syntax error occurs when evaulating the following code block: \#begin_src python :session if True: 1 2 \#end_src A blank line is required for top level module code to end an indented block, such as a for loop, try/except, or if statement. https://www.python.org/dev/peps/pep-0008/#blank-line TINYCHANGE --- lisp/ob-python.el | 13 - 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/lisp/ob-python.el b/lisp/ob-python.el index 9f1234bac..36d549697 100644 --- a/lisp/ob-python.el +++ b/lisp/ob-python.el @@ -308,8 +308,19 @@ last statement in BODY, as elisp." (list (format "open('%s', 'w').write(str(_))" (org-babel-process-file-name tmp-file 'noquote))) + (last-indent 0) (input-body (lambda (body) -(mapc (lambda (line) (insert line) (funcall send-wait)) +(mapc (lambda (line) +(let ((curr-indent (string-match "[^\s]" line))) + (if curr-indent + (progn +(when (< curr-indent last-indent) + (insert "") + (funcall send-wait)) +(setq last-indent curr-indent)) +(setq last-indent 0))) +(insert line) +(funcall send-wait)) (split-string body "[\r\n]")) (funcall send-wait))) (results -- 2.16.2