1 /*-------------------------------------------------------------------------
3 * plpython.h - Python as a procedural language for PostgreSQL
5 * Portions Copyright (c) 1996-2012, PostgreSQL Global Development Group
6 * Portions Copyright (c) 1994, Regents of the University of California
8 * src/pl/plpython/plpython.h
10 *-------------------------------------------------------------------------
16 * Include order should be: postgres.h, other postgres headers, plpython.h,
17 * other plpython headers
20 #error postgres.h must be included before plpython.h
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.
28 #undef _POSIX_C_SOURCE
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.
38 #ifdef USE_REPL_SNPRINTF
43 #if defined(_MSC_VER) && defined(_DEBUG)
44 /* Python uses #pragma to bring in a non-default libpython on VC++ if
45 * _DEBUG is defined */
47 /* Also hide away errcode, since we load Python.h before postgres.h */
48 #define errcode __msvc_errcode
52 #elif defined (_MSC_VER)
53 #define errcode __msvc_errcode
61 * Py_ssize_t compat for Python <= 2.4
63 #if PY_VERSION_HEX < 0x02050000 && !defined(PY_SSIZE_T_MIN)
64 typedef int Py_ssize_t;
66 #define PY_SSIZE_T_MAX INT_MAX
67 #define PY_SSIZE_T_MIN INT_MIN
71 * PyBool_FromLong is supported from 2.3.
73 #if PY_VERSION_HEX < 0x02030000
74 #define PyBool_FromLong(x) PyInt_FromLong(x)
78 * Python 2/3 strings/unicode/bytes handling. Python 2 has strings
79 * and unicode, Python 3 has strings, which are unicode on the C
80 * level, and bytes. The porting convention, which is similarly used
81 * in Python 2.6, is that "Unicode" is always unicode, and "Bytes" are
82 * bytes in Python 3 and strings in Python 2. Since we keep
83 * supporting Python 2 and its usual strings, we provide a
84 * compatibility layer for Python 3 that when asked to convert a C
85 * string to a Python string it converts the C string from the
86 * PostgreSQL server encoding to a Python Unicode object.
89 #if PY_VERSION_HEX < 0x02060000
90 /* This is exactly the compatibility layer that Python 2.6 uses. */
91 #define PyBytes_AsString PyString_AsString
92 #define PyBytes_FromStringAndSize PyString_FromStringAndSize
93 #define PyBytes_Size PyString_Size
94 #define PyObject_Bytes PyObject_Str
97 #if PY_MAJOR_VERSION >= 3
98 #define PyString_Check(x) 0
99 #define PyString_AsString(x) PLyUnicode_AsString(x)
100 #define PyString_FromString(x) PLyUnicode_FromString(x)
104 * Python 3 only has long.
106 #if PY_MAJOR_VERSION >= 3
107 #define PyInt_FromLong(x) PyLong_FromLong(x)
108 #define PyInt_AsLong(x) PyLong_AsLong(x)
112 * PyVarObject_HEAD_INIT was added in Python 2.6. Its use is
113 * necessary to handle both Python 2 and 3. This replacement
114 * definition is for Python <=2.5
116 #ifndef PyVarObject_HEAD_INIT
117 #define PyVarObject_HEAD_INIT(type, size) \
118 PyObject_HEAD_INIT(type) size,
121 /* Python 3 removed the Py_TPFLAGS_HAVE_ITER flag */
122 #if PY_MAJOR_VERSION >= 3
123 #define Py_TPFLAGS_HAVE_ITER 0
126 /* define our text domain for translations */
128 #define TEXTDOMAIN PG_TEXTDOMAIN("plpython")
133 /* put back our snprintf and vsnprintf */
134 #ifdef USE_REPL_SNPRINTF
142 #define vsnprintf(...) pg_vsnprintf(__VA_ARGS__)
143 #define snprintf(...) pg_snprintf(__VA_ARGS__)
145 #define vsnprintf pg_vsnprintf
146 #define snprintf pg_snprintf
147 #endif /* __GNUC__ */
148 #endif /* USE_REPL_SNPRINTF */
151 * Used throughout, and also by the Python 2/3 porting layer, so it's easier to
152 * just include it everywhere.
154 #include "plpy_util.h"
156 #endif /* PLPYTHON_H */