docs-xml: deprecate "server schannel" and change the default to "yes"
[Samba.git] / python / py3compat.h
blobf54b3912f175f518fed3c7fb94a0521321736570
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 /* Ints */
83 #define PyInt_Type PyLong_Type
84 #define PyInt_Check PyLong_Check
85 #define PyInt_CheckExact PyLong_CheckExact
86 #define PyInt_FromString PyLong_FromString
87 #define PyInt_FromLong PyLong_FromLong
88 #define PyInt_FromSsize_t PyLong_FromSsize_t
89 #define PyInt_FromSize_t PyLong_FromSize_t
90 #define PyInt_AsLong PyLong_AsLong
91 #define PyInt_AS_LONG PyLong_AS_LONG
92 #define PyInt_AsUnsignedLongLongMask PyLong_AsUnsignedLongLongMask
93 #define PyInt_AsSsize_t PyLong_AsSsize_t
95 /* Module init */
97 #define MODULE_INIT_FUNC(name) \
98 PyMODINIT_FUNC PyInit_ ## name(void); \
99 PyMODINIT_FUNC PyInit_ ## name(void)
101 /* PyArg_ParseTuple/Py_BuildValue argument */
103 #define PYARG_BYTES_LEN "y#"
105 #else
107 /***** Python 2 *****/
109 #define IS_PY3 0
111 /* Strings */
113 #define PyStr_Type PyString_Type
114 #define PyStr_Check PyString_Check
115 #define PyStr_CheckExact PyString_CheckExact
116 #define PyStr_FromString PyString_FromString
117 #define PyStr_FromStringAndSize PyString_FromStringAndSize
118 #define PyStr_FromFormat PyString_FromFormat
119 #define PyStr_FromFormatV PyString_FromFormatV
120 #define PyStr_AsString PyString_AsString
121 #define PyStr_Format PyString_Format
122 #define PyStr_InternInPlace PyString_InternInPlace
123 #define PyStr_InternFromString PyString_InternFromString
124 #define PyStr_Decode PyString_Decode
126 #define PyStr_AsUTF8String(str) (Py_INCREF(str), (str))
127 #define PyStr_AsUTF8 PyString_AsString
128 #define PyStr_AsUTF8AndSize(pystr, sizeptr) \
129 ((*sizeptr=PyString_Size(pystr)), PyString_AsString(pystr))
131 #define PyBytes_Type PyString_Type
132 #define PyBytes_Check PyString_Check
133 #define PyBytes_CheckExact PyString_CheckExact
134 #define PyBytes_FromString PyString_FromString
135 #define PyBytes_FromStringAndSize PyString_FromStringAndSize
136 #define PyBytes_FromFormat PyString_FromFormat
137 #define PyBytes_FromFormatV PyString_FromFormatV
138 #define PyBytes_Size PyString_Size
139 #define PyBytes_GET_SIZE PyString_GET_SIZE
140 #define PyBytes_AsString PyString_AsString
141 #define PyBytes_AS_STRING PyString_AS_STRING
142 #define PyBytes_AsStringAndSize PyString_AsStringAndSize
143 #define PyBytes_Concat PyString_Concat
144 #define PyBytes_ConcatAndDel PyString_ConcatAndDel
145 #define _PyBytes_Resize _PyString_Resize
147 /* PyArg_ParseTuple/Py_BuildValue argument */
149 #define PYARG_BYTES_LEN "s#"
151 /* Module init */
153 #define PyModuleDef_HEAD_INIT 0
155 typedef struct PyModuleDef {
156 int m_base;
157 const char* m_name;
158 const char* m_doc;
159 Py_ssize_t m_size;
160 PyMethodDef *m_methods;
161 } PyModuleDef;
163 #define PyModule_Create(def) \
164 Py_InitModule3((def)->m_name, (def)->m_methods, (def)->m_doc)
166 #define MODULE_INIT_FUNC(name) \
167 static PyObject *PyInit_ ## name(void); \
168 void init ## name(void); \
169 void init ## name(void) { PyInit_ ## name(); } \
170 static PyObject *PyInit_ ## name(void)
173 #endif
175 #endif