Header And Logo

PostgreSQL
| The world's most advanced open source database.

plpython.h

Go to the documentation of this file.
00001 /*-------------------------------------------------------------------------
00002  *
00003  * plpython.h - Python as a procedural language for PostgreSQL
00004  *
00005  * Portions Copyright (c) 1996-2013, PostgreSQL Global Development Group
00006  * Portions Copyright (c) 1994, Regents of the University of California
00007  *
00008  * src/pl/plpython/plpython.h
00009  *
00010  *-------------------------------------------------------------------------
00011  */
00012 #ifndef PLPYTHON_H
00013 #define PLPYTHON_H
00014 
00015 /*
00016  * Include order should be: postgres.h, other postgres headers, plpython.h,
00017  * other plpython headers
00018  */
00019 #ifndef POSTGRES_H
00020 #error postgres.h must be included before plpython.h
00021 #endif
00022 
00023 /*
00024  * Undefine some things that get (re)defined in the Python headers. They aren't
00025  * used by the PL/Python code, and all PostgreSQL headers should be included
00026  * earlier, so this should be pretty safe.
00027  */
00028 #undef _POSIX_C_SOURCE
00029 #undef _XOPEN_SOURCE
00030 #undef HAVE_STRERROR
00031 #undef HAVE_TZNAME
00032 
00033 /*
00034  * Sometimes python carefully scribbles on our *printf macros.
00035  * So we undefine them here and redefine them after it's done its dirty deed.
00036  */
00037 
00038 #ifdef USE_REPL_SNPRINTF
00039 #undef snprintf
00040 #undef vsnprintf
00041 #endif
00042 
00043 #if defined(_MSC_VER) && defined(_DEBUG)
00044 /* Python uses #pragma to bring in a non-default libpython on VC++ if
00045  * _DEBUG is defined */
00046 #undef _DEBUG
00047 /* Also hide away errcode, since we load Python.h before postgres.h */
00048 #define errcode __msvc_errcode
00049 #include <Python.h>
00050 #undef errcode
00051 #define _DEBUG
00052 #elif defined (_MSC_VER)
00053 #define errcode __msvc_errcode
00054 #include <Python.h>
00055 #undef errcode
00056 #else
00057 #include <Python.h>
00058 #endif
00059 
00060 /*
00061  * Py_ssize_t compat for Python <= 2.4
00062  */
00063 #if PY_VERSION_HEX < 0x02050000 && !defined(PY_SSIZE_T_MIN)
00064 typedef int Py_ssize_t;
00065 
00066 #define PY_SSIZE_T_MAX INT_MAX
00067 #define PY_SSIZE_T_MIN INT_MIN
00068 #endif
00069 
00070 /*
00071  * Python 2/3 strings/unicode/bytes handling.  Python 2 has strings
00072  * and unicode, Python 3 has strings, which are unicode on the C
00073  * level, and bytes.  The porting convention, which is similarly used
00074  * in Python 2.6, is that "Unicode" is always unicode, and "Bytes" are
00075  * bytes in Python 3 and strings in Python 2.  Since we keep
00076  * supporting Python 2 and its usual strings, we provide a
00077  * compatibility layer for Python 3 that when asked to convert a C
00078  * string to a Python string it converts the C string from the
00079  * PostgreSQL server encoding to a Python Unicode object.
00080  */
00081 
00082 #if PY_VERSION_HEX < 0x02060000
00083 /* This is exactly the compatibility layer that Python 2.6 uses. */
00084 #define PyBytes_AsString PyString_AsString
00085 #define PyBytes_FromStringAndSize PyString_FromStringAndSize
00086 #define PyBytes_Size PyString_Size
00087 #define PyObject_Bytes PyObject_Str
00088 #endif
00089 
00090 #if PY_MAJOR_VERSION >= 3
00091 #define PyString_Check(x) 0
00092 #define PyString_AsString(x) PLyUnicode_AsString(x)
00093 #define PyString_FromString(x) PLyUnicode_FromString(x)
00094 #endif
00095 
00096 /*
00097  * Python 3 only has long.
00098  */
00099 #if PY_MAJOR_VERSION >= 3
00100 #define PyInt_FromLong(x) PyLong_FromLong(x)
00101 #define PyInt_AsLong(x) PyLong_AsLong(x)
00102 #endif
00103 
00104 /*
00105  * PyVarObject_HEAD_INIT was added in Python 2.6.  Its use is
00106  * necessary to handle both Python 2 and 3.  This replacement
00107  * definition is for Python <=2.5
00108  */
00109 #ifndef PyVarObject_HEAD_INIT
00110 #define PyVarObject_HEAD_INIT(type, size)       \
00111         PyObject_HEAD_INIT(type) size,
00112 #endif
00113 
00114 /* Python 3 removed the Py_TPFLAGS_HAVE_ITER flag */
00115 #if PY_MAJOR_VERSION >= 3
00116 #define Py_TPFLAGS_HAVE_ITER 0
00117 #endif
00118 
00119 /* define our text domain for translations */
00120 #undef TEXTDOMAIN
00121 #define TEXTDOMAIN PG_TEXTDOMAIN("plpython")
00122 
00123 #include <compile.h>
00124 #include <eval.h>
00125 
00126 /* put back our snprintf and vsnprintf */
00127 #ifdef USE_REPL_SNPRINTF
00128 #ifdef snprintf
00129 #undef snprintf
00130 #endif
00131 #ifdef vsnprintf
00132 #undef vsnprintf
00133 #endif
00134 #ifdef __GNUC__
00135 #define vsnprintf(...)  pg_vsnprintf(__VA_ARGS__)
00136 #define snprintf(...)   pg_snprintf(__VA_ARGS__)
00137 #else
00138 #define vsnprintf               pg_vsnprintf
00139 #define snprintf                pg_snprintf
00140 #endif   /* __GNUC__ */
00141 #endif   /* USE_REPL_SNPRINTF */
00142 
00143 /*
00144  * Used throughout, and also by the Python 2/3 porting layer, so it's easier to
00145  * just include it everywhere.
00146  */
00147 #include "plpy_util.h"
00148 
00149 #endif   /* PLPYTHON_H */