/* -------------------------------------------------------------------------
 *
 * 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/common/pl/plpython/plpython.h
 *
 * -------------------------------------------------------------------------
 */
#ifndef PLPYTHON_H
#define PLPYTHON_H

/*
 * 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 HAVE_STRERROR
#undef HAVE_TZNAME

/*
 * Sometimes python carefully scribbles on our *printf macros.
 * So we undefine them here and redefine them after it's done its dirty deed.
 */

#ifdef USE_REPL_SNPRINTF
#undef snprintf
#undef vsnprintf
#endif

#if defined(_MSC_VER) && defined(_DEBUG)
/* Python uses #pragma to bring in a non-default libpython on VC++ if
 * _DEBUG is defined */
/* Also hide away errcode, since we load Python.h before postgres.h */
#define errcode __msvc_errcode
#include <Python.h>
#undef errcode
#elif defined(_MSC_VER)
#define errcode __msvc_errcode
#include <Python.h>
#undef errcode
#else
#include <Python.h>
#endif

/*
 * Py_ssize_t compat for Python <= 2.4
 */
#if PY_VERSION_HEX < 0x02050000 && !defined(PY_SSIZE_T_MIN)

#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
 * openGauss server encoding to a Python Unicode object.
 */

#if PY_VERSION_HEX < 0x02060000
/* This is exactly the compatibility layer that Python 2.6 uses. */
#define PY_BYTES_AS_STRING PyString_AsString
#define PY_BYTES_FROM_STRING_AND_SIZE PyString_FromStringAndSize
#define PY_BYTES_SIZE PyString_Size
#define PY_OBJECT_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")

struct PLyExecutionContext;
typedef struct PlySessionCtx {
    /* 
     * OG has only one python interpreter, use this id to isolate sessions, 
     * avoid defining the same function in different sessions 
     */
    int ply_ctx_id;
    /* session level context */      
    MemoryContext session_mctx;
    /* temporary objects can use this context */
    MemoryContext session_tmp_mctx;
    /* GD variable, isolated between sessions according to pg's implementation */
    PyObject* PLy_session_gd;       
    /* PL/Python execution stack */
    struct PLyExecutionContext* PLy_execution_contexts;  
    /* session level function compilation cache */
    HTAB* PLy_procedure_cache;
    /* a list of nested explicit subtransactions */
    List* explicit_subtransactions;

    struct PlySessionCtx *next_free_context;
} PlySessionCtx;

typedef struct PlyGlobalsCtx {
    /* instance level MemoryContext */
    MemoryContext ply_mctx;
    /* plpython interpreter default global variables */
    PyObject* PLy_interp_globals;
    
    PyObject* PLy_exc_error;
    PyObject* PLy_exc_fatal;
    PyObject* PLy_exc_spi_error;
    /* spi exceptions */
    HTAB* PLy_spi_exceptions;

    int numberContexts;
    PlySessionCtx* sessionContexts;
    
    int numberFreeContexts;
    PlySessionCtx* free_session_head;
    PlySessionCtx* free_session_tail;
} PlyGlobalsCtx;

extern PlyGlobalsCtx* g_ply_ctx;     /* save instance level objects */
extern const bool ENABLE_PLPY;

#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"
#include "storage/plpython_init.h"

#endif /* PLPYTHON_H */