]> granicus.if.org Git - postgresql/blob - src/pl/plpython/plpython.h
Add error-throwing wrappers for the printf family of functions.
[postgresql] / src / pl / plpython / plpython.h
1 /*-------------------------------------------------------------------------
2  *
3  * plpython.h - Python as a procedural language for PostgreSQL
4  *
5  * Portions Copyright (c) 1996-2015, PostgreSQL Global Development Group
6  * Portions Copyright (c) 1994, Regents of the University of California
7  *
8  * src/pl/plpython/plpython.h
9  *
10  *-------------------------------------------------------------------------
11  */
12 #ifndef PLPYTHON_H
13 #define PLPYTHON_H
14
15 /*
16  * Include order should be: postgres.h, other postgres headers, plpython.h,
17  * other plpython headers
18  */
19 #ifndef POSTGRES_H
20 #error postgres.h must be included before plpython.h
21 #endif
22
23 /*
24  * Undefine some things that get (re)defined in the Python headers. They aren't
25  * used by the PL/Python code, and all PostgreSQL headers should be included
26  * earlier, so this should be pretty safe.
27  */
28 #undef _POSIX_C_SOURCE
29 #undef _XOPEN_SOURCE
30 #undef HAVE_STRERROR
31 #undef HAVE_TZNAME
32
33 /*
34  * Sometimes python carefully scribbles on our *printf macros.
35  * So we undefine them here and redefine them after it's done its dirty deed.
36  */
37
38 #undef snprintf
39 #undef vsnprintf
40
41 #if defined(_MSC_VER) && defined(_DEBUG)
42 /* Python uses #pragma to bring in a non-default libpython on VC++ if
43  * _DEBUG is defined */
44 #undef _DEBUG
45 /* Also hide away errcode, since we load Python.h before postgres.h */
46 #define errcode __msvc_errcode
47 #include <Python.h>
48 #undef errcode
49 #define _DEBUG
50 #elif defined (_MSC_VER)
51 #define errcode __msvc_errcode
52 #include <Python.h>
53 #undef errcode
54 #else
55 #include <Python.h>
56 #endif
57
58 /*
59  * Py_ssize_t compat for Python <= 2.4
60  */
61 #if PY_VERSION_HEX < 0x02050000 && !defined(PY_SSIZE_T_MIN)
62 typedef int Py_ssize_t;
63
64 #define PY_SSIZE_T_MAX INT_MAX
65 #define PY_SSIZE_T_MIN INT_MIN
66 #endif
67
68 /*
69  * Python 2/3 strings/unicode/bytes handling.  Python 2 has strings
70  * and unicode, Python 3 has strings, which are unicode on the C
71  * level, and bytes.  The porting convention, which is similarly used
72  * in Python 2.6, is that "Unicode" is always unicode, and "Bytes" are
73  * bytes in Python 3 and strings in Python 2.  Since we keep
74  * supporting Python 2 and its usual strings, we provide a
75  * compatibility layer for Python 3 that when asked to convert a C
76  * string to a Python string it converts the C string from the
77  * PostgreSQL server encoding to a Python Unicode object.
78  */
79
80 #if PY_VERSION_HEX < 0x02060000
81 /* This is exactly the compatibility layer that Python 2.6 uses. */
82 #define PyBytes_AsString PyString_AsString
83 #define PyBytes_FromStringAndSize PyString_FromStringAndSize
84 #define PyBytes_Size PyString_Size
85 #define PyObject_Bytes PyObject_Str
86 #endif
87
88 #if PY_MAJOR_VERSION >= 3
89 #define PyString_Check(x) 0
90 #define PyString_AsString(x) PLyUnicode_AsString(x)
91 #define PyString_FromString(x) PLyUnicode_FromString(x)
92 #define PyString_FromStringAndSize(x, size) PLyUnicode_FromStringAndSize(x, size)
93 #endif
94
95 /*
96  * Python 3 only has long.
97  */
98 #if PY_MAJOR_VERSION >= 3
99 #define PyInt_FromLong(x) PyLong_FromLong(x)
100 #define PyInt_AsLong(x) PyLong_AsLong(x)
101 #endif
102
103 /*
104  * PyVarObject_HEAD_INIT was added in Python 2.6.  Its use is
105  * necessary to handle both Python 2 and 3.  This replacement
106  * definition is for Python <=2.5
107  */
108 #ifndef PyVarObject_HEAD_INIT
109 #define PyVarObject_HEAD_INIT(type, size)               \
110                 PyObject_HEAD_INIT(type) size,
111 #endif
112
113 /* Python 3 removed the Py_TPFLAGS_HAVE_ITER flag */
114 #if PY_MAJOR_VERSION >= 3
115 #define Py_TPFLAGS_HAVE_ITER 0
116 #endif
117
118 /* define our text domain for translations */
119 #undef TEXTDOMAIN
120 #define TEXTDOMAIN PG_TEXTDOMAIN("plpython")
121
122 #include <compile.h>
123 #include <eval.h>
124
125 /* put back our snprintf and vsnprintf */
126 #ifdef snprintf
127 #undef snprintf
128 #endif
129 #ifdef vsnprintf
130 #undef vsnprintf
131 #endif
132 #ifdef __GNUC__
133 #define vsnprintf(...)  vsnprintf_throw_on_fail(__VA_ARGS__)
134 #define snprintf(...)   snprintf_throw_on_fail(__VA_ARGS__)
135 #else
136 #define vsnprintf               vsnprintf_throw_on_fail
137 #define snprintf                snprintf_throw_on_fail
138 #endif   /* __GNUC__ */
139
140 /*
141  * Used throughout, and also by the Python 2/3 porting layer, so it's easier to
142  * just include it everywhere.
143  */
144 #include "plpy_util.h"
145
146 #endif   /* PLPYTHON_H */