tests: Work audit_log CLIENT_IP out from config instead of env var
[Samba.git] / python / py3compat.h
blob89b7552c791df7e359e20bc72565b4c5642fdddc
1 /*
2 Unix SMB/CIFS implementation.
3 Python 3 compatibility macros
4 Copyright (C) Petr Viktorin <pviktori@redhat.com> 2015
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #ifndef _SAMBA_PY3COMPAT_H_
21 #define _SAMBA_PY3COMPAT_H_
22 #include <Python.h>
24 /* Quick docs:
26 * The IS_PY3 preprocessor constant is 1 on Python 3, and 0 on Python 2.
28 * "PyStr_*" works like PyUnicode_* on Python 3, but uses bytestrings (str)
29 * under Python 2.
31 * "PyBytes_*" work like in Python 3; on Python 2 they are aliased to their
32 * PyString_* names.
34 * "PyInt_*" works like PyLong_*
36 * Syntax for module initialization is as in Python 3, except the entrypoint
37 * function definition and declaration:
38 * PyMODINIT_FUNC PyInit_modulename(void);
39 * PyMODINIT_FUNC PyInit_modulename(void)
40 * {
41 * ...
42 * }
43 * is replaced by:
44 * MODULE_INIT_FUNC(modulename)
45 * {
46 * ...
47 * }
49 * In the entrypoint, create a module using PyModule_Create and PyModuleDef,
50 * and return it. See Python 3 documentation for details.
51 * For Python 2 compatibility, always set PyModuleDef.m_size to -1.
55 #if PY_MAJOR_VERSION >= 3
57 /***** Python 3 *****/
59 #define IS_PY3 1
61 /* Strings */
63 #define PyStr_Type PyUnicode_Type
64 #define PyStr_Check PyUnicode_Check
65 #define PyStr_CheckExact PyUnicode_CheckExact
66 #define PyStr_FromString PyUnicode_FromString
67 #define PyStr_FromStringAndSize PyUnicode_FromStringAndSize
68 #define PyStr_FromFormat PyUnicode_FromFormat
69 #define PyStr_FromFormatV PyUnicode_FromFormatV
70 #define PyStr_AsString PyUnicode_AsUTF8
71 #define PyStr_Concat PyUnicode_Concat
72 #define PyStr_Format PyUnicode_Format
73 #define PyStr_InternInPlace PyUnicode_InternInPlace
74 #define PyStr_InternFromString PyUnicode_InternFromString
75 #define PyStr_Decode PyUnicode_Decode
77 #define PyStr_AsUTF8String PyUnicode_AsUTF8String // returns PyBytes
78 #define PyStr_AsUTF8 PyUnicode_AsUTF8
79 #define PyStr_AsUTF8AndSize PyUnicode_AsUTF8AndSize
81 /* description of bytes and string objects */
82 #define PY_DESC_PY3_BYTES "bytes"
83 #define PY_DESC_PY3_STRING "string"
85 /* Determine if object is really bytes, for code that runs
86 * in python2 & python3 (note: PyBytes_Check is replaced by
87 * PyString_Check in python2) so care needs to be taken when
88 * writing code that will check if incoming type is bytes that
89 * will work as expected in python2 & python3
92 #define IsPy3Bytes PyBytes_Check
94 #define IsPy3BytesOrString(pystr) \
95 (PyStr_Check(pystr) || PyBytes_Check(pystr))
98 /* Ints */
100 #define PyInt_Type PyLong_Type
101 #define PyInt_Check PyLong_Check
102 #define PyInt_CheckExact PyLong_CheckExact
103 #define PyInt_FromString PyLong_FromString
104 #define PyInt_FromLong PyLong_FromLong
105 #define PyInt_FromSsize_t PyLong_FromSsize_t
106 #define PyInt_FromSize_t PyLong_FromSize_t
107 #define PyInt_AsLong PyLong_AsLong
108 #define PyInt_AS_LONG PyLong_AS_LONG
109 #define PyInt_AsUnsignedLongLongMask PyLong_AsUnsignedLongLongMask
110 #define PyInt_AsSsize_t PyLong_AsSsize_t
112 /* Module init */
114 #define MODULE_INIT_FUNC(name) \
115 PyMODINIT_FUNC PyInit_ ## name(void); \
116 PyMODINIT_FUNC PyInit_ ## name(void)
118 /* PyArg_ParseTuple/Py_BuildValue argument */
120 #define PYARG_BYTES_LEN "y#"
121 #define PYARG_STR_UNI "es"
123 #else
125 /***** Python 2 *****/
127 #define IS_PY3 0
129 /* Strings */
131 #define PyStr_Type PyString_Type
132 #define PyStr_Check PyString_Check
133 #define PyStr_CheckExact PyString_CheckExact
134 #define PyStr_FromString PyString_FromString
135 #define PyStr_FromStringAndSize PyString_FromStringAndSize
136 #define PyStr_FromFormat PyString_FromFormat
137 #define PyStr_FromFormatV PyString_FromFormatV
138 #define PyStr_AsString PyString_AsString
139 #define PyStr_Format PyString_Format
140 #define PyStr_InternInPlace PyString_InternInPlace
141 #define PyStr_InternFromString PyString_InternFromString
142 #define PyStr_Decode PyString_Decode
144 #define PyStr_AsUTF8String(str) (Py_INCREF(str), (str))
145 #define PyStr_AsUTF8 PyString_AsString
146 #define PyStr_AsUTF8AndSize(pystr, sizeptr) \
147 ((*sizeptr=PyString_Size(pystr)), PyString_AsString(pystr))
149 #define PyBytes_Type PyString_Type
150 #define PyBytes_Check PyString_Check
151 #define PyBytes_CheckExact PyString_CheckExact
152 #define PyBytes_FromString PyString_FromString
153 #define PyBytes_FromStringAndSize PyString_FromStringAndSize
154 #define PyBytes_FromFormat PyString_FromFormat
155 #define PyBytes_FromFormatV PyString_FromFormatV
156 #define PyBytes_Size PyString_Size
157 #define PyBytes_GET_SIZE PyString_GET_SIZE
158 #define PyBytes_AsString PyString_AsString
159 #define PyBytes_AS_STRING PyString_AS_STRING
160 #define PyBytes_AsStringAndSize PyString_AsStringAndSize
161 #define PyBytes_Concat PyString_Concat
162 #define PyBytes_ConcatAndDel PyString_ConcatAndDel
163 #define _PyBytes_Resize _PyString_Resize
165 /* description of bytes and string objects */
166 #define PY_DESC_PY3_BYTES "string"
167 #define PY_DESC_PY3_STRING "unicode"
169 /* Determine if object is really bytes, for code that runs
170 * in python2 & python3 (note: PyBytes_Check is replaced by
171 * PyString_Check in python2) so care needs to be taken when
172 * writing code that will check if incoming type is bytes that
173 * will work as expected in python2 & python3
176 #define IsPy3Bytes(pystr) false
178 #define IsPy3BytesOrString PyStr_Check
180 /* PyArg_ParseTuple/Py_BuildValue argument */
182 #define PYARG_BYTES_LEN "s#"
184 * We want a format that will ensure unicode is encoded using the
185 * specified encoding 'utf8' (to obtain the char* array)
186 * In python3 we use "es" but in python2 the specifiying 'es' will
187 * result in the any incomming 'str' type being decoded first to ascii
188 * then encoded to the specified 'utf8' encoding. In order to avoid that
189 * we use format 'et' in python2 instead.
191 #define PYARG_STR_UNI "et"
193 /* Module init */
195 #define PyModuleDef_HEAD_INIT 0
197 typedef struct PyModuleDef {
198 int m_base;
199 const char* m_name;
200 const char* m_doc;
201 Py_ssize_t m_size;
202 PyMethodDef *m_methods;
203 } PyModuleDef;
205 #define PyModule_Create(def) \
206 Py_InitModule3((def)->m_name, (def)->m_methods, (def)->m_doc)
208 #define MODULE_INIT_FUNC(name) \
209 static PyObject *PyInit_ ## name(void); \
210 void init ## name(void); \
211 void init ## name(void) { PyInit_ ## name(); } \
212 static PyObject *PyInit_ ## name(void)
215 #endif
217 #endif