Commit 45e8e2f2 authored by Victor Stinner's avatar Victor Stinner

Issue #21490: Add new C macros: Py_ABS() and Py_STRINGIFY()

Keep _Py_STRINGIZE() in PC/pyconfig.h to not introduce a dependency between
pyconfig.h and pymacros.h.
parent 79b49ab5
#ifndef Py_PYMACRO_H #ifndef Py_PYMACRO_H
#define Py_PYMACRO_H #define Py_PYMACRO_H
/* Minimum value between x and y */
#define Py_MIN(x, y) (((x) > (y)) ? (y) : (x)) #define Py_MIN(x, y) (((x) > (y)) ? (y) : (x))
/* Maximum value between x and y */
#define Py_MAX(x, y) (((x) > (y)) ? (x) : (y)) #define Py_MAX(x, y) (((x) > (y)) ? (x) : (y))
/* Absolute value of the number x */
#define Py_ABS(x) ((x) < 0 ? -(x) : (x))
#define _Py_XSTRINGIFY(x) #x
/* Convert the argument to a string. For example, Py_STRINGIFY(123) is replaced
with "123" by the preprocessor. Defines are also replaced by their value.
For example Py_STRINGIFY(__LINE__) is replaced by the line number, not
by "__LINE__". */
#define Py_STRINGIFY(x) _Py_XSTRINGIFY(x)
/* Argument must be a char or an int in [-128, 127] or [0, 255]. */ /* Argument must be a char or an int in [-128, 127] or [0, 255]. */
#define Py_CHARMASK(c) ((unsigned char)((c) & 0xff)) #define Py_CHARMASK(c) ((unsigned char)((c) & 0xff))
/* Assert a build-time dependency, as an expression. /* Assert a build-time dependency, as an expression.
Your compile will fail if the condition isn't true, or can't be evaluated Your compile will fail if the condition isn't true, or can't be evaluated
......
...@@ -249,10 +249,8 @@ typedef enum { ...@@ -249,10 +249,8 @@ typedef enum {
} timeout_state; } timeout_state;
/* Wrap error strings with filename and line # */ /* Wrap error strings with filename and line # */
#define STRINGIFY1(x) #x
#define STRINGIFY2(x) STRINGIFY1(x)
#define ERRSTR1(x,y,z) (x ":" y ": " z) #define ERRSTR1(x,y,z) (x ":" y ": " z)
#define ERRSTR(x) ERRSTR1("_ssl.c", STRINGIFY2(__LINE__), x) #define ERRSTR(x) ERRSTR1("_ssl.c", Py_STRINGIFY(__LINE__), x)
/* /*
......
...@@ -85,8 +85,6 @@ typedef struct { char c; _Bool x; } s_bool; ...@@ -85,8 +85,6 @@ typedef struct { char c; _Bool x; } s_bool;
#define BOOL_ALIGN 0 #define BOOL_ALIGN 0
#endif #endif
#define STRINGIFY(x) #x
#ifdef __powerc #ifdef __powerc
#pragma options align=reset #pragma options align=reset
#endif #endif
...@@ -546,8 +544,8 @@ np_short(char *p, PyObject *v, const formatdef *f) ...@@ -546,8 +544,8 @@ np_short(char *p, PyObject *v, const formatdef *f)
return -1; return -1;
if (x < SHRT_MIN || x > SHRT_MAX){ if (x < SHRT_MIN || x > SHRT_MAX){
PyErr_SetString(StructError, PyErr_SetString(StructError,
"short format requires " STRINGIFY(SHRT_MIN) "short format requires " Py_STRINGIFY(SHRT_MIN)
" <= number <= " STRINGIFY(SHRT_MAX)); " <= number <= " Py_STRINGIFY(SHRT_MAX));
return -1; return -1;
} }
y = (short)x; y = (short)x;
...@@ -564,7 +562,8 @@ np_ushort(char *p, PyObject *v, const formatdef *f) ...@@ -564,7 +562,8 @@ np_ushort(char *p, PyObject *v, const formatdef *f)
return -1; return -1;
if (x < 0 || x > USHRT_MAX){ if (x < 0 || x > USHRT_MAX){
PyErr_SetString(StructError, PyErr_SetString(StructError,
"ushort format requires 0 <= number <= " STRINGIFY(USHRT_MAX)); "ushort format requires 0 <= number <= "
Py_STRINGIFY(USHRT_MAX));
return -1; return -1;
} }
y = (unsigned short)x; y = (unsigned short)x;
......
...@@ -16,9 +16,6 @@ static void raw_free(void *ptr); ...@@ -16,9 +16,6 @@ static void raw_free(void *ptr);
# define TRACE_DEBUG # define TRACE_DEBUG
#endif #endif
#define _STR(VAL) #VAL
#define STR(VAL) _STR(VAL)
/* Protected by the GIL */ /* Protected by the GIL */
static struct { static struct {
PyMemAllocator mem; PyMemAllocator mem;
......
This diff is collapsed.
...@@ -48,9 +48,6 @@ ...@@ -48,9 +48,6 @@
*/ */
#define XSTRINGIZE(v) #v
#define STRINGIZE(v) XSTRINGIZE(v)
#define CHECK_MBUF_RELEASED(mbuf) \ #define CHECK_MBUF_RELEASED(mbuf) \
if (((_PyManagedBufferObject *)mbuf)->flags&_Py_MANAGED_BUFFER_RELEASED) { \ if (((_PyManagedBufferObject *)mbuf)->flags&_Py_MANAGED_BUFFER_RELEASED) { \
PyErr_SetString(PyExc_ValueError, \ PyErr_SetString(PyExc_ValueError, \
...@@ -660,7 +657,7 @@ mbuf_add_view(_PyManagedBufferObject *mbuf, const Py_buffer *src) ...@@ -660,7 +657,7 @@ mbuf_add_view(_PyManagedBufferObject *mbuf, const Py_buffer *src)
if (src->ndim > PyBUF_MAX_NDIM) { if (src->ndim > PyBUF_MAX_NDIM) {
PyErr_SetString(PyExc_ValueError, PyErr_SetString(PyExc_ValueError,
"memoryview: number of dimensions must not exceed " "memoryview: number of dimensions must not exceed "
STRINGIZE(PyBUF_MAX_NDIM)); Py_STRINGIFY(PyBUF_MAX_NDIM));
return NULL; return NULL;
} }
...@@ -1341,7 +1338,7 @@ memory_cast(PyMemoryViewObject *self, PyObject *args, PyObject *kwds) ...@@ -1341,7 +1338,7 @@ memory_cast(PyMemoryViewObject *self, PyObject *args, PyObject *kwds)
if (ndim > PyBUF_MAX_NDIM) { if (ndim > PyBUF_MAX_NDIM) {
PyErr_SetString(PyExc_ValueError, PyErr_SetString(PyExc_ValueError,
"memoryview: number of dimensions must not exceed " "memoryview: number of dimensions must not exceed "
STRINGIZE(PyBUF_MAX_NDIM)); Py_STRINGIFY(PyBUF_MAX_NDIM));
return NULL; return NULL;
} }
if (self->view.ndim != 1 && ndim != 1) { if (self->view.ndim != 1 && ndim != 1) {
......
...@@ -13,8 +13,6 @@ ...@@ -13,8 +13,6 @@
#include "code.h" #include "code.h"
#include "marshal.h" #include "marshal.h"
#define ABS(x) ((x) < 0 ? -(x) : (x))
/* High water mark to determine when the marshalled object is dangerously deep /* High water mark to determine when the marshalled object is dangerously deep
* and risks coring the interpreter. When the object stack gets this deep, * and risks coring the interpreter. When the object stack gets this deep,
* raise an exception instead of continuing. * raise an exception instead of continuing.
...@@ -192,7 +190,7 @@ w_PyLong(const PyLongObject *ob, char flag, WFILE *p) ...@@ -192,7 +190,7 @@ w_PyLong(const PyLongObject *ob, char flag, WFILE *p)
} }
/* set l to number of base PyLong_MARSHAL_BASE digits */ /* set l to number of base PyLong_MARSHAL_BASE digits */
n = ABS(Py_SIZE(ob)); n = Py_ABS(Py_SIZE(ob));
l = (n-1) * PyLong_MARSHAL_RATIO; l = (n-1) * PyLong_MARSHAL_RATIO;
d = ob->ob_digit[n-1]; d = ob->ob_digit[n-1];
assert(d != 0); /* a PyLong is always normalized */ assert(d != 0); /* a PyLong is always normalized */
...@@ -727,8 +725,8 @@ r_PyLong(RFILE *p) ...@@ -727,8 +725,8 @@ r_PyLong(RFILE *p)
return NULL; return NULL;
} }
size = 1 + (ABS(n) - 1) / PyLong_MARSHAL_RATIO; size = 1 + (Py_ABS(n) - 1) / PyLong_MARSHAL_RATIO;
shorts_in_top_digit = 1 + (ABS(n) - 1) % PyLong_MARSHAL_RATIO; shorts_in_top_digit = 1 + (Py_ABS(n) - 1) % PyLong_MARSHAL_RATIO;
ob = _PyLong_New(size); ob = _PyLong_New(size);
if (ob == NULL) if (ob == NULL)
return NULL; return NULL;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment