winbind: Keep "force_reauth" in invalidate_cm_connection
[Samba.git] / python / py3compat.h
blobce317c65f8e2ddca8c8ddd6f2829e0b523e516c1
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 /* Ints */
87 #define PyInt_Type PyLong_Type
88 #define PyInt_Check PyLong_Check
89 #define PyInt_CheckExact PyLong_CheckExact
90 #define PyInt_FromString PyLong_FromString
91 #define PyInt_FromLong PyLong_FromLong
92 #define PyInt_FromSsize_t PyLong_FromSsize_t
93 #define PyInt_FromSize_t PyLong_FromSize_t
94 #define PyInt_AsLong PyLong_AsLong
95 #define PyInt_AS_LONG PyLong_AS_LONG
96 #define PyInt_AsUnsignedLongLongMask PyLong_AsUnsignedLongLongMask
97 #define PyInt_AsSsize_t PyLong_AsSsize_t
99 /* Module init */
101 #define MODULE_INIT_FUNC(name) \
102 PyMODINIT_FUNC PyInit_ ## name(void); \
103 PyMODINIT_FUNC PyInit_ ## name(void)
105 /* PyArg_ParseTuple/Py_BuildValue argument */
107 #define PYARG_BYTES_LEN "y#"
109 #else
111 /***** Python 2 *****/
113 #define IS_PY3 0
115 /* Strings */
117 #define PyStr_Type PyString_Type
118 #define PyStr_Check PyString_Check
119 #define PyStr_CheckExact PyString_CheckExact
120 #define PyStr_FromString PyString_FromString
121 #define PyStr_FromStringAndSize PyString_FromStringAndSize
122 #define PyStr_FromFormat PyString_FromFormat
123 #define PyStr_FromFormatV PyString_FromFormatV
124 #define PyStr_AsString PyString_AsString
125 #define PyStr_Format PyString_Format
126 #define PyStr_InternInPlace PyString_InternInPlace
127 #define PyStr_InternFromString PyString_InternFromString
128 #define PyStr_Decode PyString_Decode
130 #define PyStr_AsUTF8String(str) (Py_INCREF(str), (str))
131 #define PyStr_AsUTF8 PyString_AsString
132 #define PyStr_AsUTF8AndSize(pystr, sizeptr) \
133 ((*sizeptr=PyString_Size(pystr)), PyString_AsString(pystr))
135 #define PyBytes_Type PyString_Type
136 #define PyBytes_Check PyString_Check
137 #define PyBytes_CheckExact PyString_CheckExact
138 #define PyBytes_FromString PyString_FromString
139 #define PyBytes_FromStringAndSize PyString_FromStringAndSize
140 #define PyBytes_FromFormat PyString_FromFormat
141 #define PyBytes_FromFormatV PyString_FromFormatV
142 #define PyBytes_Size PyString_Size
143 #define PyBytes_GET_SIZE PyString_GET_SIZE
144 #define PyBytes_AsString PyString_AsString
145 #define PyBytes_AS_STRING PyString_AS_STRING
146 #define PyBytes_AsStringAndSize PyString_AsStringAndSize
147 #define PyBytes_Concat PyString_Concat
148 #define PyBytes_ConcatAndDel PyString_ConcatAndDel
149 #define _PyBytes_Resize _PyString_Resize
151 /* description of bytes and string objects */
152 #define PY_DESC_PY3_BYTES "string"
153 #define PY_DESC_PY3_STRING "unicode"
155 /* PyArg_ParseTuple/Py_BuildValue argument */
157 #define PYARG_BYTES_LEN "s#"
159 /* Module init */
161 #define PyModuleDef_HEAD_INIT 0
163 typedef struct PyModuleDef {
164 int m_base;
165 const char* m_name;
166 const char* m_doc;
167 Py_ssize_t m_size;
168 PyMethodDef *m_methods;
169 } PyModuleDef;
171 #define PyModule_Create(def) \
172 Py_InitModule3((def)->m_name, (def)->m_methods, (def)->m_doc)
174 #define MODULE_INIT_FUNC(name) \
175 static PyObject *PyInit_ ## name(void); \
176 void init ## name(void); \
177 void init ## name(void) { PyInit_ ## name(); } \
178 static PyObject *PyInit_ ## name(void)
181 #endif
183 #endif