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-2020, 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  * Python 2/3 strings/unicode/bytes handling. Python 2 has strings
64  * and unicode, Python 3 has strings, which are unicode on the C
65  * level, and bytes. The porting convention, which is similarly used
66  * in Python 2.6, is that "Unicode" is always unicode, and "Bytes" are
67  * bytes in Python 3 and strings in Python 2. Since we keep
68  * supporting Python 2 and its usual strings, we provide a
69  * compatibility layer for Python 3 that when asked to convert a C
70  * string to a Python string it converts the C string from the
71  * PostgreSQL server encoding to a Python Unicode object.
72  */
73 #if PY_MAJOR_VERSION >= 3
74 #define PyString_Check(x) 0
75 #define PyString_AsString(x) PLyUnicode_AsString(x)
76 #define PyString_FromString(x) PLyUnicode_FromString(x)
77 #define PyString_FromStringAndSize(x, size) PLyUnicode_FromStringAndSize(x, size)
78 #endif
79 
80 /*
81  * Python 3 only has long.
82  */
83 #if PY_MAJOR_VERSION >= 3
84 #define PyInt_FromLong(x) PyLong_FromLong(x)
85 #define PyInt_AsLong(x) PyLong_AsLong(x)
86 #endif
87 
88 /* Python 3 removed the Py_TPFLAGS_HAVE_ITER flag */
89 #if PY_MAJOR_VERSION >= 3
90 #define Py_TPFLAGS_HAVE_ITER 0
91 #endif
92 
93 /* define our text domain for translations */
94 #undef TEXTDOMAIN
95 #define TEXTDOMAIN PG_TEXTDOMAIN("plpython")
96 
97 #include <compile.h>
98 #include <eval.h>
99 
100 /* put back our *printf macros ... this must match src/include/port.h */
101 #ifdef vsnprintf
102 #undef vsnprintf
103 #endif
104 #ifdef snprintf
105 #undef snprintf
106 #endif
107 #ifdef vsprintf
108 #undef vsprintf
109 #endif
110 #ifdef sprintf
111 #undef sprintf
112 #endif
113 #ifdef vfprintf
114 #undef vfprintf
115 #endif
116 #ifdef fprintf
117 #undef fprintf
118 #endif
119 #ifdef vprintf
120 #undef vprintf
121 #endif
122 #ifdef printf
123 #undef printf
124 #endif
125 
126 #define vsnprintf pg_vsnprintf
127 #define snprintf pg_snprintf
128 #define vsprintf pg_vsprintf
129 #define sprintf pg_sprintf
130 #define vfprintf pg_vfprintf
131 #define fprintf pg_fprintf
132 #define vprintf pg_vprintf
133 #define printf(...) pg_printf(__VA_ARGS__)
134 
135 /*
136  * Used throughout, and also by the Python 2/3 porting layer, so it's easier to
137  * just include it everywhere.
138  */
139 #include "plpy_util.h"
140 
141 #endif /* PLPYTHON_H */