Skip to content

Commit

Permalink
Add conversion of float
Browse files Browse the repository at this point in the history
  • Loading branch information
813gan committed Aug 17, 2024
1 parent bc1ea28 commit ccd047c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
13 changes: 13 additions & 0 deletions emacspy.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,11 @@ cdef class EmacsValue:
cdef intmax_t i = env.extract_integer(env, self.v)
return i

cpdef double float(self) except *:
cdef emacs_env* env = get_env()
cdef double i = env.extract_float(env, self.v)
return i

def sym_str(self):
return _F().symbol_name(self).str()

Expand Down Expand Up @@ -189,6 +194,8 @@ cdef class EmacsValue:
return self.str()
elif my_type == "integer":
return self.int()
elif my_type == "float":
return self.float()
elif my_type == "symbol":
as_str = self.sym_str()
if as_str == "nil":
Expand All @@ -209,6 +216,8 @@ cdef emacs_value unwrap(obj) except *:
obj = make_bool(obj)
elif isinstance(obj, int):
obj = make_int(obj)
elif isinstance(obj, float):
obj = make_float(obj)
elif obj is None:
obj = nil
elif isinstance(obj, list):
Expand Down Expand Up @@ -238,6 +247,10 @@ cpdef make_int(int i):
cdef emacs_env* env = get_env()
return EmacsValue.wrap(env.make_integer(env, i))

cpdef make_float(double f):
cdef emacs_env* env = get_env()
return EmacsValue.wrap(env.make_float(env, f))

cpdef make_bool(bool b):
cdef emacs_env* env = get_env()
if b:
Expand Down
8 changes: 8 additions & 0 deletions tests/test.el
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,14 @@
(should (eq 0 (py-get-global-variable
"test" (py-set-global "test" 0 "test_int")))) )

(ert-deftest ert-test-emacspy-data-float ()
(should (= 0.5 (py-get-global-variable
"test" (py-set-global "test" 0.5 "test_int"))))
(should (= -0.5 (py-get-global-variable
"test" (py-set-global "test" -0.5 "test_int"))))
(should (= 0.0 (py-get-global-variable
"test" (py-set-global "test" 0.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)))
Expand Down

0 comments on commit ccd047c

Please sign in to comment.