Skip to content

Commit

Permalink
Convert list from python to elisp
Browse files Browse the repository at this point in the history
  • Loading branch information
813gan committed Aug 17, 2024
1 parent d1360f6 commit 31d7375
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
7 changes: 7 additions & 0 deletions emacspy.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,10 @@ cdef emacs_value unwrap(obj) except *:
obj = make_int(obj)
elif obj is None:
obj = nil
elif isinstance(obj, list):
obj = make_list(obj)
elif isinstance(obj, tuple):
obj = make_list(list(obj))

if isinstance(obj, EmacsValue):
return (<EmacsValue>obj).v
Expand Down Expand Up @@ -216,6 +220,9 @@ cpdef make_bool(bool b):
return EmacsValue.wrap(env.intern(env, "t".encode('utf8')))
return nil

cpdef make_list(list l):
return funcall(sym("list"), l)

cdef emacs_value string_ptr(str s):
cdef emacs_env* env = get_env()
s_utf8 = s.encode('utf8')
Expand Down
24 changes: 24 additions & 0 deletions tests/test.el
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
(require 'cl)

(ert-deftest ert-test-emacspy-py-import ()
(should-error (py-import "test" "NON_EXISTING_MOD")
:type 'python-exception) )
Expand Down Expand Up @@ -75,3 +77,25 @@
"test" (py-set-global "test" -1 "test_int"))))
(should (eq 0 (py-get-global-variable
"test" (py-set-global "test" 0 "test_int")))) )

(ert-deftest ert-test-emacspy-data-list ()
(let ((lst (py-run-string "test" "[1, True, 2, 'test']")))
(should (eq 4 (length lst)))
(should (eq 1 (nth 0 lst)))
(should (eq 't (nth 1 lst)))
(should (eq 2 (nth 2 lst)))
(should (string= "test" (nth 3 lst)))
)

(let ((lst (py-run-string "test" "(False,)")))
(should (eq 1 (length lst)))
(should (eq nil (nth 0 lst))) )

(let ((lst (py-run-string "test" "([1, 2, 3], 'test', False)")))
(should (eq 3 (length lst)))
(let ((nested-lst (nth 0 lst)))
(should (listp nested-lst))
(should (eq 3 (length nested-lst)))
(should (eq 3 (nth 2 nested-lst))) )
(should (string= "test" (nth 1 lst)))
(should (eq nil (nth 2 lst))) ) )

0 comments on commit 31d7375

Please sign in to comment.