I have code that tests the arity of a function. I use it to determine whether optional arguments added in recent versions of a package are present. It calls subr-arity for built-in functions and parses the arglist of bytecode objects and lambdas.

(defun function-argspec (func)
  (if (symbolp func) (setq func (indirect-function func)))
   ((byte-code-function-p func)
    (aref func 0))
   ((and (consp func)
         (eq (car func) 'lambda)
         (consp (cdr func)))
    (car (cdr func)))

This has worked well up to Emacs 23. In Emacs 24.3 on Ubuntu 14.04, it's working well for some functions, but not for others.

(function-argspec 'revert-buffer)
(&optional ignore-auto noconfirm preserve-modes)
(require 'vc)
(function-argspec 'vc-print-log-internal)

Evidently the bytecode format has changed in a way that isn't reflected in the manual.

(symbol-function 'vc-print-log-internal)
#[1283 \301\211\302\301\211\203\211@\303!\203\304\262A\266\202\202\210\203'\305>\202*\306>??\262\2036\307\2027\310\262\311
$\317"\320\321%\312\322\323\315\316#\324"\325\326%\312\327\330\315\316!\331"\332\333%\312\334\335\315\316%\336"\325\337%&\262\207 [vc-log-short-style nil *vc-change-log* file-directory-p t directory file short long vc-log-internal-common make-byte-code 1028 \304\305\303\301\205\300\302&\207 vconcat vector [vc-call-backend print-log] 12 

(fn BK BUF TYPE-ARG FILES-ARG) 771 \303\300\301\302$\207 [vc-print-log-setup-buttons] 8 

(fn BK FILES-ARG RET) 257 \301\302\300#\207 [vc-call-backend show-log-entry] 5 

(fn BK) 514 \305\300\301\302\303\304%\207 [vc-print-log-internal] 



How can I reliably access the argument list of a bytecode object? Just knowing the arity would do, I don't care about the argument names. More precisely, I want to know how many arguments are mandatory and how many arguments are optional, or in other terms, I want the same information that I get from subr-arity. Of course my code must cope with both old-style and new-style bytecode, so I need to know not just where to dig but also when to dig where.

  • Tangential: maybe you just removed this part to trim down the example, but you might want to add support for closures in your function-argspec.
    – Malabarba
    Oct 9, 2014 at 18:46
  • Gilles, do you have a finalized version of your function-argspec function somewhere, including bytecode functions and closures? Aug 21, 2015 at 15:16
  • @JordonBiondo I added it as an answer here. Aug 21, 2015 at 20:06

2 Answers 2


Edit: Woo! I found a function that will take either the normal argument list, or the integer version and return somewhat of a signature: byte-compile-arglist-signature in bytecomp.el!

(byte-compile-arglist-signature 1283) ;; => (3 . 5)

Initial Answer:

I hope someone else can chime in on whether or not this is documented somewhere but this is what I learned by reading exec_byte_code inside bytecode.c in Emacs source.

The number you see is used to calculate the argspec when the the byte code is actually being run, I assume this for performance, it's actually quite clever.

I wrote this code to show you how to calculate the arity of a function given that number:

(defun arity-info (byte-code-int)
  (let* ((required  (logand byte-code-int 127))
         (total-named  (lsh byte-code-int -8))
         (optional (- total-named required))
         (allow-rest  (if (not (zerop (logand byte-code-int 128))) "yes" "no")))
     (cons 'required required)
     (cons 'total-named total-named)
     (cons 'optional optional)
     (cons 'allow-rest allow-rest))))

We can see here that if we run arity-info with 1283 we get the following:

((required . 3) (total-named . 5) (optional . 2) (allow-rest . "no"))

which you can see describes the arity of vc-print-log-internal perfectly, 5 total args, 3 required, 2 optional, does not allow &rest.

  • Good job. [filler chars]
    – Drew
    Oct 10, 2014 at 4:25

By request, here's my implementation of function-argspec and function-arity. I used Jordon Biondo's original solution for Emacs 24 bytecode.

 ;; XEmacs
 ((fboundp 'compiled-function-arglist)
  (defalias 'emacsen-compiled-function-arglist 'compiled-function-arglist))
 ;; GNU Emacs
  (defun emacsen-make-up-number-arglist (start end tail)
    (while (< start end)
      (setq end (1- end))
      (setq tail (cons (intern (format "a%d" end)) tail)))
  (defun emacsen-compiled-function-arglist (func)
    (let ((a (aref func 0)))
      (if (integerp a)
          ;; An integer encoding the arity. Encountered in Emacs 24.3.
          ;; https://emacs.stackexchange.com/questions/971/argspec-or-arity-of-a-bytecode-function-in-emacs-24/973#973
          (let ((arglist (if (zerop (logand a 128))
                           '(&rest rest)))
                (mandatory (logand a 127))
                (nonrest (lsh a -8)))
            (if (> nonrest mandatory)
                (setq arglist (cons '&optional (emacsen-make-up-number-arglist mandatory nonrest arglist))))
            (emacsen-make-up-number-arglist 0 mandatory arglist))
        ;; Otherwise: this is the arglist. The only format I've seen up to GNU 23.

(defun function-argspec (func)
  "Return a function's argument list.
For byte-compiled functions in Emacs >=24, some information may be lost as the
byte compiler sometimes erases argument names. In this case, fake argument names
are reconstructed."
  (if (symbolp func) (setq func (indirect-function func)))
   ((subrp func)
    (let ((docstring (documentation func)))
        (if (string-match "\n.*\\'" docstring)
            (let ((form (read (match-string 0 docstring))))
              (cdr form))
   ((byte-code-function-p func)
    (emacsen-compiled-function-arglist func))
   ((and (consp func)
         (eq (car func) 'lambda)
         (consp (cdr func)))
    (car (cdr func)))
   ((and (consp func)
         (eq (car func) 'closure)
         (consp (cdr func))
         (consp (cdr (cdr func))))
    (car (cdr (cdr func))))
   (t (signal 'wrong-type-argument
              (list 'functionp func)))))

(defun function-arity (func)
  "Return a function's arity as (MIN . MAX).
Return minimum and maximum number of args allowed for SUBR.
The returned value is a pair (MIN . MAX).  MIN is the minimum number
of args.  MAX is the maximum number or the symbol `many', for a
function with `&rest' args, or `unevalled' for a special form.

This function is like `subr-arity', but also works with user-defined
and byte-code functions. Symbols are dereferenced through
  ;; TODO: keyword support
  (if (symbolp func) (setq func (indirect-function func)))
   ((subrp func)
    (subr-arity func))
    (let ((mandatory 0) (optional 0) (rest nil)
          (where 'mandatory))
      (when (and (consp func) (eq 'macro (car func)))
        (setq func (cdr func))
        (setq rest 'unevalled))
      (let ((argspec (function-argspec func)))
        (dolist (arg argspec)
           ((eq arg '&optional) (setq where 'optional))
           ((eq arg '&rest) (unless rest (setq rest 'many)))
           (t (set where (+ (symbol-value where) 1)))))
        (cons mandatory (or rest (+ mandatory optional))))))))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.