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
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
97 #define MODULE_INIT_FUNC(name) \
98 PyMODINIT_FUNC PyInit_ ## name(void); \
99 PyMODINIT_FUNC PyInit_ ## name(void)
103 /***** Python 2 *****/
109 #define PyStr_Type PyString_Type
110 #define PyStr_Check PyString_Check
111 #define PyStr_CheckExact PyString_CheckExact
112 #define PyStr_FromString PyString_FromString
113 #define PyStr_FromStringAndSize PyString_FromStringAndSize
114 #define PyStr_FromFormat PyString_FromFormat
115 #define PyStr_FromFormatV PyString_FromFormatV
116 #define PyStr_AsString PyString_AsString
117 #define PyStr_Format PyString_Format
118 #define PyStr_InternInPlace PyString_InternInPlace
119 #define PyStr_InternFromString PyString_InternFromString
120 #define PyStr_Decode PyString_Decode
122 #define PyStr_AsUTF8String(str) (Py_INCREF(str), (str))
123 #define PyStr_AsUTF8 PyString_AsString
124 #define PyStr_AsUTF8AndSize(pystr, sizeptr) \
125 ((*sizeptr=PyString_Size(pystr)), PyString_AsString(pystr))
127 #define PyBytes_Type PyString_Type
128 #define PyBytes_Check PyString_Check
129 #define PyBytes_CheckExact PyString_CheckExact
130 #define PyBytes_FromString PyString_FromString
131 #define PyBytes_FromStringAndSize PyString_FromStringAndSize
132 #define PyBytes_FromFormat PyString_FromFormat
133 #define PyBytes_FromFormatV PyString_FromFormatV
134 #define PyBytes_Size PyString_Size
135 #define PyBytes_GET_SIZE PyString_GET_SIZE
136 #define PyBytes_AsString PyString_AsString
137 #define PyBytes_AS_STRING PyString_AS_STRING
138 #define PyBytes_AsStringAndSize PyString_AsStringAndSize
139 #define PyBytes_Concat PyString_Concat
140 #define PyBytes_ConcatAndDel PyString_ConcatAndDel
141 #define _PyBytes_Resize _PyString_Resize
145 #define PyModuleDef_HEAD_INIT 0
147 typedef struct PyModuleDef
{
152 PyMethodDef
*m_methods
;
155 #define PyModule_Create(def) \
156 Py_InitModule3((def)->m_name, (def)->m_methods, (def)->m_doc)
158 #define MODULE_INIT_FUNC(name) \
159 static PyObject *PyInit_ ## name(void); \
160 void init ## name(void); \
161 void init ## name(void) { PyInit_ ## name(); } \
162 static PyObject *PyInit_ ## name(void)