haproxy/contrib/spoa_server/ps_python.h
Gilchrist Dadaglo 3e235d38ec MAJOR: contrib: porting spoa_server to support python3
Background:
    Python 2 is no longer supported since January, 1st 2020 as per
    https://www.python.org/doc/sunset-python-2/
    The purpose of this change is to make the spoa_server contrib library
    compatible with Python 3 to allow transition to Python 3.

Test Settings:
ps_python.py:
    ...
    spoa.set_var_null("null", spoa.scope_txn)
    spoa.set_var_boolean("boolean", spoa.scope_txn, True)
    spoa.set_var_int32("int32", spoa.scope_txn, 1234)
    spoa.set_var_uint32("uint32", spoa.scope_txn, 1234)
    spoa.set_var_int64("int64", spoa.scope_txn, 1234)
    spoa.set_var_uint64("uint64", spoa.scope_txn, 1234)
    spoa.set_var_ipv4("ipv4", spoa.scope_txn, ipaddress.IPv4Address(u"127.0.0.1"))
    spoa.set_var_ipv6("ipv6", spoa.scope_txn, ipaddress.IPv6Address(u"1::f"))
    spoa.set_var_str("str", spoa.scope_txn, "1::f")
    spoa.set_var_bin("bin", spoa.scope_txn, "1:\x01:\x42f\x63\x63")
    spoa.set_var_str("python_version", spoa.scope_sess, str(sys.version_info))
    ...
haproxy.cfg:
    ...
    http-request capture var(txn.verb.null),debug len 1
    http-request capture var(txn.verb.boolean),debug len 1
    http-request capture var(txn.verb.int32),debug len 4
    http-request capture var(txn.verb.uint32),debug len 4
    http-request capture var(txn.verb.int64),debug len 4
    http-request capture var(txn.verb.uint64),debug len 4
    http-request capture var(txn.verb.ipv4),debug len 16
    http-request capture var(txn.verb.ipv6),debug len 45
    http-request capture var(txn.verb.str),debug len 32
    http-request capture var(txn.verb.bin),debug len 32
    http-request capture var(sess.verb.python_version),debug len 100
    ...

Test result:
    Python 3.8:
        ft_public ft_public/<NOSRV> 0/-1/-1/-1/0 403 212 - - PR-- 1/1/0/0/0 0/0 {|1|1234|1234|1234|1234|127.0.0.1|1::f|1::f|1:#01:Bfcc|sys.version_info(major=3, minor=8, micro=1, releaselevel='final', serial=0)} "POST / HTTP/1.1"
    Python 3.7:
        ft_public ft_public/<NOSRV> 0/-1/-1/-1/0 403 212 - - PR-- 1/1/0/0/0 0/0 {|1|1234|1234|1234|1234|127.0.0.1|1::f|1::f|1:#01:Bfcc|sys.version_info(major=3, minor=7, micro=6, releaselevel='final', serial=0)} "POST / HTTP/1.1"
    Python 3.6:
        ft_public ft_public/<NOSRV> 0/-1/-1/-1/0 403 212 - - PR-- 1/1/0/0/0 0/0 {|1|1234|1234|1234|1234|127.0.0.1|1::f|1::f|1:#01:Bfcc|sys.version_info(major=3, minor=6, micro=10, releaselevel='final', serial=0)} "POST / HTTP/1.1"
    Python 2.7:
        ft_public ft_public/<NOSRV> 0/-1/-1/-1/0 403 212 - - PR-- 1/1/0/0/0 0/0 {|1|1234|1234|1234|1234|127.0.0.1|1::f|1::f|1:#01:Bfcc|sys.version_info(major=2, minor=7, micro=17, releaselevel='final', serial=0)} "POST / HTTP/1.1"

Not tested:
Python <2.7
2020-05-11 10:52:55 +02:00

53 lines
1.8 KiB
C

/* ps_python.h: SPOA Python processing includes
*
* Copyright (C) 2020 Gilchrist Dadaglo <gilchrist@dadaglo.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version
* 2 of the License, or (at your option) any later version.
*
* This program is provided in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
*/
#ifndef __PS_PYTHON_H__
#define __PS_PYTHON_H__
#include <Python.h>
#if PY_MAJOR_VERSION >= 3
#define IS_PYTHON_3K 1
#define IS_PYTHON_27 0
#elif PY_MAJOR_VERSION == 2 && PY_MINOR_VERSION == 7
#define IS_PYTHON_3K 0
#define IS_PYTHON_27 1
#else
#error "Unsupported Python Version - Please use Python 3"
#endif /* PY_MAJOR_VERSION */
#if IS_PYTHON_3K
#define PY_INIT_MODULE(instance, name, methods, moduledef) \
(instance = PyModule_Create(moduledef))
#define PY_STRING_FROM_STRING PyUnicode_FromString
#define PY_STRING_FROM_STRING_AND_SIZE PyUnicode_FromStringAndSize
#define PY_BYTES_FROM_STRING_AND_SIZE PyBytes_FromStringAndSize
#define PY_STRING_GET_SIZE PyBytes_Size
#define PY_STRING_AS_STRING PyBytes_AsString
#elif IS_PYTHON_27
#define PY_INIT_MODULE(instance, name, methods, moduledef) \
(instance = Py_InitModule(name, methods))
#define PY_STRING_FROM_STRING PyString_FromString
#define PY_STRING_FROM_STRING_AND_SIZE PyString_FromStringAndSize
#define PY_BYTES_FROM_STRING_AND_SIZE PyString_FromStringAndSize
#define PY_STRING_GET_SIZE PyString_GET_SIZE
#define PY_STRING_AS_STRING PyString_AS_STRING
#endif /* IS_PYTHON_3K */
#endif /* __PS_PYTHON_H__ */
/* EOF */