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 #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 #undef vsnprintf
38 #undef snprintf
39 #undef vsprintf
40 #undef sprintf
41 #undef vfprintf
42 #undef fprintf
43 #undef vprintf
44 #undef printf
45 
46 #if defined(_MSC_VER) && defined(_DEBUG)
47 /* Python uses #pragma to bring in a non-default libpython on VC++ if
48  * _DEBUG is defined */
49 #undef _DEBUG
50 /* Also hide away errcode, since we load Python.h before postgres.h */
51 #define errcode __msvc_errcode
52 #include <Python.h>
53 #undef errcode
54 #define _DEBUG
55 #elif defined (_MSC_VER)
56 #define errcode __msvc_errcode
57 #include <Python.h>
58 #undef errcode
59 #else
60 #include <Python.h>
61 #endif
62 
63 /*
64  * Py_ssize_t compat for Python <= 2.4
65  */
66 #if PY_VERSION_HEX < 0x02050000 && !defined(PY_SSIZE_T_MIN)
67 typedef int Py_ssize_t;
68 
69 #define PY_SSIZE_T_MAX INT_MAX
70 #define PY_SSIZE_T_MIN INT_MIN
71 #endif
72 
73 /*
74  * Python 2/3 strings/unicode/bytes handling.  Python 2 has strings
75  * and unicode, Python 3 has strings, which are unicode on the C
76  * level, and bytes.  The porting convention, which is similarly used
77  * in Python 2.6, is that "Unicode" is always unicode, and "Bytes" are
78  * bytes in Python 3 and strings in Python 2.  Since we keep
79  * supporting Python 2 and its usual strings, we provide a
80  * compatibility layer for Python 3 that when asked to convert a C
81  * string to a Python string it converts the C string from the
82  * PostgreSQL server encoding to a Python Unicode object.
83  */
84 
85 #if PY_VERSION_HEX < 0x02060000
86 /* This is exactly the compatibility layer that Python 2.6 uses. */
87 #define PyBytes_AsString PyString_AsString
88 #define PyBytes_FromStringAndSize PyString_FromStringAndSize
89 #define PyBytes_Size PyString_Size
90 #define PyObject_Bytes PyObject_Str
91 #endif
92 
93 #if PY_MAJOR_VERSION >= 3
94 #define PyString_Check(x) 0
95 #define PyString_AsString(x) PLyUnicode_AsString(x)
96 #define PyString_FromString(x) PLyUnicode_FromString(x)
97 #define PyString_FromStringAndSize(x, size) PLyUnicode_FromStringAndSize(x, size)
98 #endif
99 
100 /*
101  * Python 3 only has long.
102  */
103 #if PY_MAJOR_VERSION >= 3
104 #define PyInt_FromLong(x) PyLong_FromLong(x)
105 #define PyInt_AsLong(x) PyLong_AsLong(x)
106 #endif
107 
108 /*
109  * PyVarObject_HEAD_INIT was added in Python 2.6.  Its use is
110  * necessary to handle both Python 2 and 3.  This replacement
111  * definition is for Python <=2.5
112  */
113 #ifndef PyVarObject_HEAD_INIT
114 #define PyVarObject_HEAD_INIT(type, size)		\
115 		PyObject_HEAD_INIT(type) size,
116 #endif
117 
118 /* Python 3 removed the Py_TPFLAGS_HAVE_ITER flag */
119 #if PY_MAJOR_VERSION >= 3
120 #define Py_TPFLAGS_HAVE_ITER 0
121 #endif
122 
123 /* define our text domain for translations */
124 #undef TEXTDOMAIN
125 #define TEXTDOMAIN PG_TEXTDOMAIN("plpython")
126 
127 #include <compile.h>
128 #include <eval.h>
129 
130 /* put back our *printf macros ... this must match src/include/port.h */
131 #ifdef vsnprintf
132 #undef vsnprintf
133 #endif
134 #ifdef snprintf
135 #undef snprintf
136 #endif
137 #ifdef vsprintf
138 #undef vsprintf
139 #endif
140 #ifdef sprintf
141 #undef sprintf
142 #endif
143 #ifdef vfprintf
144 #undef vfprintf
145 #endif
146 #ifdef fprintf
147 #undef fprintf
148 #endif
149 #ifdef vprintf
150 #undef vprintf
151 #endif
152 #ifdef printf
153 #undef printf
154 #endif
155 
156 #define vsnprintf		pg_vsnprintf
157 #define snprintf		pg_snprintf
158 #define vsprintf		pg_vsprintf
159 #define sprintf			pg_sprintf
160 #define vfprintf		pg_vfprintf
161 #define fprintf			pg_fprintf
162 #define vprintf			pg_vprintf
163 #define printf(...)		pg_printf(__VA_ARGS__)
164 
165 /*
166  * Used throughout, and also by the Python 2/3 porting layer, so it's easier to
167  * just include it everywhere.
168  */
169 #include "plpy_util.h"
170 
171 #endif							/* PLPYTHON_H */
172