blob: 07f478594d1fa200469ad90632aaed0cddc66736 [file] [log] [blame]
Victor Stinnerdfb866d2011-09-29 01:12:24 +02001#ifndef Py_PYMACRO_H
2#define Py_PYMACRO_H
3
4#define Py_MIN(x, y) (((x) > (y)) ? (y) : (x))
5#define Py_MAX(x, y) (((x) > (y)) ? (x) : (y))
6
7/* Argument must be a char or an int in [-128, 127] or [0, 255]. */
8#define Py_CHARMASK(c) ((unsigned char)((c) & 0xff))
9
10
11/* Assert a build-time dependency, as an expression.
12
13 Your compile will fail if the condition isn't true, or can't be evaluated
14 by the compiler. This can be used in an expression: its value is 0.
15
16 Example:
17
18 #define foo_to_char(foo) \
19 ((char *)(foo) \
20 + Py_BUILD_ASSERT(offsetof(struct foo, string) == 0))
21
Victor Stinner573696a2011-09-29 12:12:39 +020022 Written by Rusty Russell, public domain, http://ccodearchive.net/ */
Victor Stinnerdfb866d2011-09-29 01:12:24 +020023#define Py_BUILD_ASSERT(cond) \
24 (sizeof(char [1 - 2*!(cond)]) - 1)
25
Victor Stinnerdfb866d2011-09-29 01:12:24 +020026
27/* Get the number of elements in a visible array
28
29 This does not work on pointers, or arrays declared as [], or function
30 parameters. With correct compiler support, such usage will cause a build
31 error (see Py_BUILD_ASSERT).
32
Victor Stinner573696a2011-09-29 12:12:39 +020033 Written by Rusty Russell, public domain, http://ccodearchive.net/ */
34#if defined(__GNUC__)
35/* Two gcc extensions.
36 &a[0] degrades to a pointer: a different type from an array */
Victor Stinnerdfb866d2011-09-29 01:12:24 +020037#define Py_ARRAY_LENGTH(array) \
Victor Stinner573696a2011-09-29 12:12:39 +020038 (sizeof(array) / sizeof((array)[0]) \
39 + Py_BUILD_ASSERT(!__builtin_types_compatible_p(typeof(array), \
40 typeof(&(array)[0]))))
41#else
42#define Py_ARRAY_LENGTH(array) \
43 (sizeof(array) / sizeof((array)[0]))
44#endif
Victor Stinnerdfb866d2011-09-29 01:12:24 +020045
46
47/* Define macros for inline documentation. */
48#define PyDoc_VAR(name) static char name[]
49#define PyDoc_STRVAR(name,str) PyDoc_VAR(name) = PyDoc_STR(str)
50#ifdef WITH_DOC_STRINGS
51#define PyDoc_STR(str) str
52#else
53#define PyDoc_STR(str) ""
54#endif
55
56#endif /* Py_PYMACRO_H */