Search code examples
elispert

Nested (funcall func) exceeds max-lisp-eval-depth, why?


I'm working on two general functions for executing independent unit-tests in elisp. One is about doing something and reset all custom-variables of my package, the other is about doing something in a temp-buffer and reset all custom-variables of my package.

Function (general):

(defun package-test-test (func)
    ""
    (unwind-protect
        (funcall func)
      (reset-all-custom-package-variables)))

Function (temp-buffer):

    (defun package-test-test-in-buffer (func)
        ""
        (package-test-test
            (lambda ()
                (with-temp-buffer (funcall func)))))

When i now call: (package-test-test-in-buffer (lambda () (insert "a"))) it exceeds max-lisp-eval-depth, why (there is no recursion)?


Solution

  • Your problem is the dynamic binding which is the default in Emacs Lisp: func arguments in your functions are the same variable. You need to rename one of them or use lexical binding.

    See also How to live with Emacs Lisp dynamic scoping?