]> granicus.if.org Git - postgresql/blobdiff - src/pl/plpython/plpython.h
Python 2.2 is no longer supported
[postgresql] / src / pl / plpython / plpython.h
index c2f6bba6f45a22d662c447cbdae9c275abc50e56..15ec85e8057db70f2322b8a57125faa3b9347759 100644 (file)
-/* $Header: /cvsroot/pgsql/src/pl/plpython/Attic/plpython.h,v 1.6 2002/03/06 06:10:48 momjian Exp $ */
-
+/*-------------------------------------------------------------------------
+ *
+ * plpython.h - Python as a procedural language for PostgreSQL
+ *
+ * Portions Copyright (c) 1996-2012, PostgreSQL Global Development Group
+ * Portions Copyright (c) 1994, Regents of the University of California
+ *
+ * src/pl/plpython/plpython.h
+ *
+ *-------------------------------------------------------------------------
+ */
 #ifndef PLPYTHON_H
 #define PLPYTHON_H
 
-#define DEBUG_EXC 0
-#define DEBUG_LEVEL 0
+/*
+ * Include order should be: postgres.h, other postgres headers, plpython.h,
+ * other plpython headers
+ */
+#ifndef POSTGRES_H
+#error postgres.h must be included before plpython.h
+#endif
+
+/*
+ * Undefine some things that get (re)defined in the Python headers. They aren't
+ * used by the PL/Python code, and all PostgreSQL headers should be included
+ * earlier, so this should be pretty safe.
+ */
+#undef _POSIX_C_SOURCE
+#undef _XOPEN_SOURCE
+#undef HAVE_STRERROR
+#undef HAVE_TZNAME
 
-#define DECLARE_N_EXC(N) int rv_##N; sigjmp_buf buf_##N
-#define TRAP_N_EXC(N) ((rv_##N = sigsetjmp(Warn_restart, 1)) != 0)
+/*
+ * Sometimes python carefully scribbles on our *printf macros.
+ * So we undefine them here and redefine them after it's done its dirty deed.
+ */
 
-#if !DEBUG_EXC
-#define RESTORE_N_EXC(N) memcpy(&Warn_restart, &(buf_##N), sizeof(sigjmp_buf))
-#define SAVE_N_EXC(N) memcpy(&(buf_##N), &Warn_restart, sizeof(sigjmp_buf))
-#define RERAISE_N_EXC(N) siglongjmp(Warn_restart, rv_##N)
-#define RAISE_EXC(V) siglongjmp(Warn_restart, (V))
-#else
-#define RESTORE_N_EXC(N) do { \
-   elog(WARNING, "exception (%d,%d) restore at %s:%d",\
-               PLy_call_level, exc_save_calls, __FUNCTION__, (__LINE__));\
-   exc_save_calls -= 1; \
-   memcpy(&Warn_restart, &(buf_##N), sizeof(sigjmp_buf)); } while (0)
-#define SAVE_N_EXC(N) do { \
-   exc_save_calls += 1; \
-   elog(WARNING, "exception (%d,%d) save at %s:%d", \
-               PLy_call_level, exc_save_calls, __FUNCTION__, (__LINE__)); \
-   memcpy(&(buf_##N), &Warn_restart, sizeof(sigjmp_buf)); } while (0)
-#define RERAISE_N_EXC(N) do { \
-   elog(WARNING, "exception (%d,%d) reraise at %s:%d", \
-   PLy_call_level, exc_save_calls, __FUNCTION__, (__LINE__)); \
-   siglongjmp(Warn_restart, rv_##N); } while (0)
-#define RAISE_EXC(V) do { \
-   elog(WARNING, "exception (%d,%d) raise at %s:%d", \
-   PLy_call_level, exc_save_calls, __FUNCTION__, (__LINE__)); \
-   siglongjmp(Warn_restart, (V)); } while (0)
+#ifdef USE_REPL_SNPRINTF
+#undef snprintf
+#undef vsnprintf
 #endif
 
-#define DECLARE_EXC() DECLARE_N_EXC(save_restart)
-#define SAVE_EXC() SAVE_N_EXC(save_restart)
-#define RERAISE_EXC() RERAISE_N_EXC(save_restart)
-#define RESTORE_EXC() RESTORE_N_EXC(save_restart)
-#define TRAP_EXC() TRAP_N_EXC(save_restart)
-
-#if DEBUG_LEVEL
-#define CALL_LEVEL_INC() do { PLy_call_level += 1; \
-       elog(DEBUG3, "Level: %d", PLy_call_level); } while (0)
-#define CALL_LEVEL_DEC() do { elog(DEBUG3, "Level: %d", PLy_call_level); \
-       PLy_call_level -= 1; } while (0)
+#if defined(_MSC_VER) && defined(_DEBUG)
+/* Python uses #pragma to bring in a non-default libpython on VC++ if
+ * _DEBUG is defined */
+#undef _DEBUG
+/* Also hide away errcode, since we load Python.h before postgres.h */
+#define errcode __msvc_errcode
+#include <Python.h>
+#undef errcode
+#define _DEBUG
+#elif defined (_MSC_VER)
+#define errcode __msvc_errcode
+#include <Python.h>
+#undef errcode
 #else
-#define CALL_LEVEL_INC() do { PLy_call_level += 1; } while (0)
-#define CALL_LEVEL_DEC() do { PLy_call_level -= 1; } while (0)
+#include <Python.h>
 #endif
 
-/* temporary debugging macros
+/*
+ * Py_ssize_t compat for Python <= 2.4
  */
-#if DEBUG_LEVEL
-#define enter() elog(DEBUG3, "Enter(%d): %s", func_enter_calls++,__FUNCTION__)
-#define leave() elog(DEBUG3, "Leave(%d): %s", func_leave_calls++,__FUNCTION__)
-#define mark() elog(DEBUG3, "Mark: %s:%d", __FUNCTION__, __LINE__);
-#define refc(O) elog(DEBUG3, "Ref<%p>:<%d>:%s:%d", (O), (((O) == NULL) ? -1 : (O)->ob_refcnt), __FUNCTION__, __LINE__)
-#else
-#define enter()
-#define leave()
-#define mark()
-#define refc(O)
+#if PY_VERSION_HEX < 0x02050000 && !defined(PY_SSIZE_T_MIN)
+typedef int Py_ssize_t;
+
+#define PY_SSIZE_T_MAX INT_MAX
+#define PY_SSIZE_T_MIN INT_MIN
+#endif
+
+/*
+ * Python 2/3 strings/unicode/bytes handling.  Python 2 has strings
+ * and unicode, Python 3 has strings, which are unicode on the C
+ * level, and bytes.  The porting convention, which is similarly used
+ * in Python 2.6, is that "Unicode" is always unicode, and "Bytes" are
+ * bytes in Python 3 and strings in Python 2.  Since we keep
+ * supporting Python 2 and its usual strings, we provide a
+ * compatibility layer for Python 3 that when asked to convert a C
+ * string to a Python string it converts the C string from the
+ * PostgreSQL server encoding to a Python Unicode object.
+ */
+
+#if PY_VERSION_HEX < 0x02060000
+/* This is exactly the compatibility layer that Python 2.6 uses. */
+#define PyBytes_AsString PyString_AsString
+#define PyBytes_FromStringAndSize PyString_FromStringAndSize
+#define PyBytes_Size PyString_Size
+#define PyObject_Bytes PyObject_Str
+#endif
+
+#if PY_MAJOR_VERSION >= 3
+#define PyString_Check(x) 0
+#define PyString_AsString(x) PLyUnicode_AsString(x)
+#define PyString_FromString(x) PLyUnicode_FromString(x)
 #endif
 
+/*
+ * Python 3 only has long.
+ */
+#if PY_MAJOR_VERSION >= 3
+#define PyInt_FromLong(x) PyLong_FromLong(x)
+#define PyInt_AsLong(x) PyLong_AsLong(x)
+#endif
+
+/*
+ * PyVarObject_HEAD_INIT was added in Python 2.6.  Its use is
+ * necessary to handle both Python 2 and 3.  This replacement
+ * definition is for Python <=2.5
+ */
+#ifndef PyVarObject_HEAD_INIT
+#define PyVarObject_HEAD_INIT(type, size)              \
+               PyObject_HEAD_INIT(type) size,
+#endif
+
+/* Python 3 removed the Py_TPFLAGS_HAVE_ITER flag */
+#if PY_MAJOR_VERSION >= 3
+#define Py_TPFLAGS_HAVE_ITER 0
+#endif
+
+/* define our text domain for translations */
+#undef TEXTDOMAIN
+#define TEXTDOMAIN PG_TEXTDOMAIN("plpython")
+
+#include <compile.h>
+#include <eval.h>
+
+/* put back our snprintf and vsnprintf */
+#ifdef USE_REPL_SNPRINTF
+#ifdef snprintf
+#undef snprintf
+#endif
+#ifdef vsnprintf
+#undef vsnprintf
+#endif
+#ifdef __GNUC__
+#define vsnprintf(...)  pg_vsnprintf(__VA_ARGS__)
+#define snprintf(...)   pg_snprintf(__VA_ARGS__)
+#else
+#define vsnprintf               pg_vsnprintf
+#define snprintf                pg_snprintf
+#endif   /* __GNUC__ */
+#endif   /* USE_REPL_SNPRINTF */
+
+/*
+ * Used throughout, and also by the Python 2/3 porting layer, so it's easier to
+ * just include it everywhere.
+ */
+#include "plpy_util.h"
+
 #endif   /* PLPYTHON_H */