5

I'm writing a package that provides some Python functionality. I want to use python-rx, which is part of Emacs' python.el.

(eval-when-compile (require 'python))

(defun python-foo-starts-defun-p ()
  "Return non-nil if the current statement opens a defun."
  (save-excursion
    (python-nav-beginning-of-statement)
    (looking-at (python-rx defun))))

(provide 'python-foo)

However, this gives me byte compilation errors:

the function `python-nav-beginning-of-statement' might not be defined at runtime.

the function `python-rx' is not known to be defined.

This error is correct: when I run my code in a fresh Emacs, I do get an error about python-rx. However, I can open python.el and run M-x eval-buffer and then my code works.

I believe this is because python-rx is wrapped in an eval-when-compile in python.el. How can I use it in my code?

  • Those are warnings, not errors, right? You might get away with switching that eval-when-compile to eval-and-compile. – PythonNut Aug 13 '15 at 14:46
  • I do get a runtime error when I try to call my function. I've clarified this in the question. The suggestion to use eval-and-compile is good, as it fixes the python-nav-... byte compilation issue, but not the python-rx issue. – Wilfred Hughes Aug 13 '15 at 15:08
  • 1
    There's no need to eval-and-compile a require statement. If you just want macros you use eval-when-compile. If you want functions and macros, you just use the plain require. – Malabarba Aug 13 '15 at 17:31
5

Firstly, that eval-when-compile method works fine for macros. It is a perfectly good way to use macros (not functions) from another package.

The problems you report are two different things.

the function python-nav-beginning-of-statement' might not be defined at runtime.

This is happening because python-nav-beginning-of-statement is a function, not a macro. Unlike macros (which are expanded at compile-time), functions have to be available at runtime. So that eval-when-compile doesn't work here. You need to use a plain (require 'python) to make sure that this function is available.

the function `python-rx' is not known to be defined.

This is a different warning. This is saying that python-rx wasn't found at all. The current stable Emacs (version 24.5) does not have this macro available.

Technically, the macro is there in the file, but it's defined in such a way that makes it unavailable for other files to use. This has been fixed on the development snapshots (futurely Emacs 25).

2

Regarding those warnings in general (Malabarba's answer covers your specific question nicely)...

When you know that a "Warning: the function X is not known to be defined." warning is unnecessary, you can use declare-function:

C-hig (elisp) Declaring Functions RET

C-hf declare-function RET

More generally, you want to read:

C-hig (elisp) Compiler Errors RET

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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