PostgreSQL Source Code  git master
plpython.h
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * plpython.h - Python as a procedural language for PostgreSQL
4  *
5  * Portions Copyright (c) 1996-2019, 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. (In practice, other plpython headers will also
18  * include this file, so that they can compile standalone.)
19  */
20 #ifndef POSTGRES_H
21 #error postgres.h must be included before plpython.h
22 #endif
23 
24 /*
25  * Undefine some things that get (re)defined in the Python headers. They aren't
26  * used by the PL/Python code, and all PostgreSQL headers should be included
27  * earlier, so this should be pretty safe.
28  */
29 #undef _POSIX_C_SOURCE
30 #undef _XOPEN_SOURCE
31 
32 /*
33  * Sometimes python carefully scribbles on our *printf macros.
34  * So we undefine them here and redefine them after it's done its dirty deed.
35  */
36 #undef vsnprintf
37 #undef snprintf
38 #undef vsprintf
39 #undef sprintf
40 #undef vfprintf
41 #undef fprintf
42 #undef vprintf
43 #undef printf
44 
45 #if defined(_MSC_VER) && defined(_DEBUG)
46 /* Python uses #pragma to bring in a non-default libpython on VC++ if
47  * _DEBUG is defined */
48 #undef _DEBUG
49 /* Also hide away errcode, since we load Python.h before postgres.h */
50 #define errcode __msvc_errcode
51 #include <Python.h>
52 #undef errcode
53 #define _DEBUG
54 #elif defined (_MSC_VER)
55 #define errcode __msvc_errcode
56 #include <Python.h>
57 #undef errcode
58 #else
59 #include <Python.h>
60 #endif
61 
62 /*
63  * Py_ssize_t compat for Python <= 2.4
64  */
65 #if PY_VERSION_HEX < 0x02050000 && !defined(PY_SSIZE_T_MIN)
66 typedef int Py_ssize_t;
67 
68 #define PY_SSIZE_T_MAX INT_MAX
69 #define PY_SSIZE_T_MIN INT_MIN
70 #endif
71 
72 /*
73  * Python 2/3 strings/unicode/bytes handling. Python 2 has strings
74  * and unicode, Python 3 has strings, which are unicode on the C
75  * level, and bytes. The porting convention, which is similarly used
76  * in Python 2.6, is that "Unicode" is always unicode, and "Bytes" are
77  * bytes in Python 3 and strings in Python 2. Since we keep
78  * supporting Python 2 and its usual strings, we provide a
79  * compatibility layer for Python 3 that when asked to convert a C
80  * string to a Python string it converts the C string from the
81  * PostgreSQL server encoding to a Python Unicode object.
82  */
83 
84 #if PY_VERSION_HEX < 0x02060000
85 /* This is exactly the compatibility layer that Python 2.6 uses. */
86 #define PyBytes_AsString PyString_AsString
87 #define PyBytes_FromStringAndSize PyString_FromStringAndSize
88 #define PyBytes_Size PyString_Size
89 #define PyObject_Bytes PyObject_Str
90 #endif
91 
92 #if PY_MAJOR_VERSION >= 3
93 #define PyString_Check(x) 0
94 #define PyString_AsString(x) PLyUnicode_AsString(x)
95 #define PyString_FromString(x) PLyUnicode_FromString(x)
96 #define PyString_FromStringAndSize(x, size) PLyUnicode_FromStringAndSize(x, size)
97 #endif
98 
99 /*
100  * Python 3 only has long.
101  */
102 #if PY_MAJOR_VERSION >= 3
103 #define PyInt_FromLong(x) PyLong_FromLong(x)
104 #define PyInt_AsLong(x) PyLong_AsLong(x)
105 #endif
106 
107 /*
108  * PyVarObject_HEAD_INIT was added in Python 2.6. Its use is
109  * necessary to handle both Python 2 and 3. This replacement
110  * definition is for Python <=2.5
111  */
112 #ifndef PyVarObject_HEAD_INIT
113 #define PyVarObject_HEAD_INIT(type, size) \
114  PyObject_HEAD_INIT(type) size,
115 #endif
116 
117 /* Python 3 removed the Py_TPFLAGS_HAVE_ITER flag */
118 #if PY_MAJOR_VERSION >= 3
119 #define Py_TPFLAGS_HAVE_ITER 0
120 #endif
121 
122 /* define our text domain for translations */
123 #undef TEXTDOMAIN
124 #define TEXTDOMAIN PG_TEXTDOMAIN("plpython")
125 
126 #include <compile.h>
127 #include <eval.h>
128 
129 /* put back our *printf macros ... this must match src/include/port.h */
130 #ifdef vsnprintf
131 #undef vsnprintf
132 #endif
133 #ifdef snprintf
134 #undef snprintf
135 #endif
136 #ifdef vsprintf
137 #undef vsprintf
138 #endif
139 #ifdef sprintf
140 #undef sprintf
141 #endif
142 #ifdef vfprintf
143 #undef vfprintf
144 #endif
145 #ifdef fprintf
146 #undef fprintf
147 #endif
148 #ifdef vprintf
149 #undef vprintf
150 #endif
151 #ifdef printf
152 #undef printf
153 #endif
154 
155 #define vsnprintf pg_vsnprintf
156 #define snprintf pg_snprintf
157 #define vsprintf pg_vsprintf
158 #define sprintf pg_sprintf
159 #define vfprintf pg_vfprintf
160 #define fprintf pg_fprintf
161 #define vprintf pg_vprintf
162 #define printf(...) pg_printf(__VA_ARGS__)
163 
164 /*
165  * Used throughout, and also by the Python 2/3 porting layer, so it's easier to
166  * just include it everywhere.
167  */
168 #include "plpy_util.h"
169 
170 #endif /* PLPYTHON_H */
int Py_ssize_t
Definition: plpython.h:66