Commonly-used names in the Python standard library

Marko Rauhamaa marko at pacujo.net
Fri Feb 21 05:26:48 EST 2014


Chris Angelico <rosuav at gmail.com>:

> How does C let you create new keywords?

With #define. Nowhere near as elegant (flexible, hygienic) as in Lisp,
but used to create new syntax:

  include/linux/list.h:
    #define list_for_each(pos, head) \
            for (pos = (head)->next; pos != (head); pos = pos->next)

  include/linux/wait.h:
    #define __wait_event_interruptible(wq, condition, ret)                  \
    do {                                                                    \
            DEFINE_WAIT(__wait);                                            \
                                                                            \
            for (;;) {                                                      \
                    prepare_to_wait(&wq, &__wait, TASK_INTERRUPTIBLE);      \
                    if (condition)                                          \
                            break;                                          \
                    if (!signal_pending(current)) {                         \
                            schedule();                                     \
                            continue;                                       \
                    }                                                       \
                    ret = -ERESTARTSYS;                                     \
                    break;                                                  \
            }                                                               \
            finish_wait(&wq, &__wait);                                      \
    } while (0)

In the latter example, note how "condition" is embedded in the middle of
the macro and evaluated repeatedly.


Marko



More information about the Python-list mailing list