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_
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)
31 * "PyBytes_*" work like in Python 3; on Python 2 they are aliased to their
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)
44 * MODULE_INIT_FUNC(modulename)
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 *****/
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))
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
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#"
124 /***** Python 2 *****/
130 #define PyStr_Type PyString_Type
131 #define PyStr_Check PyString_Check
132 #define PyStr_CheckExact PyString_CheckExact
133 #define PyStr_FromString PyString_FromString
134 #define PyStr_FromStringAndSize PyString_FromStringAndSize
135 #define PyStr_FromFormat PyString_FromFormat
136 #define PyStr_FromFormatV PyString_FromFormatV
137 #define PyStr_AsString PyString_AsString
138 #define PyStr_Format PyString_Format
139 #define PyStr_InternInPlace PyString_InternInPlace
140 #define PyStr_InternFromString PyString_InternFromString
141 #define PyStr_Decode PyString_Decode
143 #define PyStr_AsUTF8String(str) (Py_INCREF(str), (str))
144 #define PyStr_AsUTF8 PyString_AsString
145 #define PyStr_AsUTF8AndSize(pystr, sizeptr) \
146 ((*sizeptr=PyString_Size(pystr)), PyString_AsString(pystr))
148 #define PyBytes_Type PyString_Type
149 #define PyBytes_Check PyString_Check
150 #define PyBytes_CheckExact PyString_CheckExact
151 #define PyBytes_FromString PyString_FromString
152 #define PyBytes_FromStringAndSize PyString_FromStringAndSize
153 #define PyBytes_FromFormat PyString_FromFormat
154 #define PyBytes_FromFormatV PyString_FromFormatV
155 #define PyBytes_Size PyString_Size
156 #define PyBytes_GET_SIZE PyString_GET_SIZE
157 #define PyBytes_AsString PyString_AsString
158 #define PyBytes_AS_STRING PyString_AS_STRING
159 #define PyBytes_AsStringAndSize PyString_AsStringAndSize
160 #define PyBytes_Concat PyString_Concat
161 #define PyBytes_ConcatAndDel PyString_ConcatAndDel
162 #define _PyBytes_Resize _PyString_Resize
164 /* description of bytes and string objects */
165 #define PY_DESC_PY3_BYTES "string"
166 #define PY_DESC_PY3_STRING "unicode"
168 /* Determine if object is really bytes, for code that runs
169 * in python2 & python3 (note: PyBytes_Check is replaced by
170 * PyString_Check in python2) so care needs to be taken when
171 * writing code that will check if incoming type is bytes that
172 * will work as expected in python2 & python3
175 #define IsPy3Bytes(pystr) false
177 #define IsPy3BytesOrString PyStr_Check
179 /* PyArg_ParseTuple/Py_BuildValue argument */
181 #define PYARG_BYTES_LEN "s#"
185 #define PyModuleDef_HEAD_INIT 0
187 typedef struct PyModuleDef
{
192 PyMethodDef
*m_methods
;
195 #define PyModule_Create(def) \
196 Py_InitModule3((def)->m_name, (def)->m_methods, (def)->m_doc)
198 #define MODULE_INIT_FUNC(name) \
199 static PyObject *PyInit_ ## name(void); \
200 void init ## name(void); \
201 void init ## name(void) { PyInit_ ## name(); } \
202 static PyObject *PyInit_ ## name(void)