py3: Remove unused PyInt_CheckExact macro from py3compat.h
[bbaumbach/samba-autobuild/.git] / python / py3compat.h
1 /*
2    Unix SMB/CIFS implementation.
3    Python 3 compatibility macros
4    Copyright (C) Petr Viktorin <pviktori@redhat.com> 2015
5
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.
10
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.
15
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/>.
18 */
19
20 #ifndef _SAMBA_PY3COMPAT_H_
21 #define _SAMBA_PY3COMPAT_H_
22 #include <Python.h>
23
24 /* Quick docs:
25  *
26  * "PyStr_*" works like PyUnicode_* on Python 3, but uses bytestrings (str)
27  * under Python 2.
28  *
29  * "PyBytes_*" work like in Python 3; on Python 2 they are aliased to their
30  * PyString_* names.
31  *
32  * "PyInt_*" works like PyLong_*
33  *
34  * Syntax for module initialization is as in Python 3, except the entrypoint
35  * function definition and declaration:
36  *     PyMODINIT_FUNC PyInit_modulename(void);
37  *     PyMODINIT_FUNC PyInit_modulename(void)
38  *     {
39  *         ...
40  *     }
41  * is replaced by:
42  *     MODULE_INIT_FUNC(modulename)
43  *     {
44  *         ...
45  *     }
46  *
47  * In the entrypoint, create a module using PyModule_Create and PyModuleDef,
48  * and return it. See Python 3 documentation for details.
49  * For Python 2 compatibility, always set PyModuleDef.m_size to -1.
50  *
51  */
52
53 /***** Python 3 *****/
54
55 /* Strings */
56
57 #define PyStr_Type PyUnicode_Type
58 #define PyStr_Check PyUnicode_Check
59 #define PyStr_FromString PyUnicode_FromString
60 #define PyStr_FromStringAndSize PyUnicode_FromStringAndSize
61 #define PyStr_FromFormat PyUnicode_FromFormat
62 #define PyStr_FromFormatV PyUnicode_FromFormatV
63 #define PyStr_AsString PyUnicode_AsUTF8
64
65 #define PyStr_AsUTF8 PyUnicode_AsUTF8
66 #define PyStr_AsUTF8AndSize PyUnicode_AsUTF8AndSize
67
68 /* description of bytes objects */
69 #define PY_DESC_PY3_BYTES "bytes"
70
71 /* Determine if object is really bytes, for code that runs
72  * in python2 & python3 (note: PyBytes_Check is replaced by
73  * PyString_Check in python2) so care needs to be taken when
74  * writing code that will check if incoming type is bytes that
75  * will work as expected in python2 & python3
76  */
77
78 #define IsPy3Bytes PyBytes_Check
79
80 #define IsPy3BytesOrString(pystr) \
81     (PyStr_Check(pystr) || PyBytes_Check(pystr))
82
83
84 /* Ints */
85
86 #define PyInt_Type PyLong_Type
87 #define PyInt_Check PyLong_Check
88 #define PyInt_FromString PyLong_FromString
89 #define PyInt_FromLong PyLong_FromLong
90 #define PyInt_FromSsize_t PyLong_FromSsize_t
91 #define PyInt_FromSize_t PyLong_FromSize_t
92 #define PyInt_AsLong PyLong_AsLong
93 #define PyInt_AS_LONG PyLong_AS_LONG
94 #define PyInt_AsUnsignedLongLongMask PyLong_AsUnsignedLongLongMask
95 #define PyInt_AsSsize_t PyLong_AsSsize_t
96
97 /* Module init */
98
99 #define MODULE_INIT_FUNC(name) \
100     PyMODINIT_FUNC PyInit_ ## name(void); \
101     PyMODINIT_FUNC PyInit_ ## name(void)
102
103 /* PyArg_ParseTuple/Py_BuildValue argument */
104
105 #define PYARG_BYTES_LEN "y#"
106 #define PYARG_STR_UNI "es"
107
108 #endif