1 /* Copyright (c) 2015, Red Hat, Inc. and/or its affiliates
2 * Licensed under the MIT license; see py3c.h
3 */
4
5 #ifndef _PY3C_COMPAT_H_
6 #define _PY3C_COMPAT_H_
7 #include <Python.h>
8 #include <assert.h>
9
10 #if PY_MAJOR_VERSION >= 3
11
12 /***** Python 3 *****/
13
14 #define IS_PY3 1
15
16 /* Strings */
17
18 #define PyStr_Type PyUnicode_Type
19 #define PyStr_Check PyUnicode_Check
20 #define PyStr_CheckExact PyUnicode_CheckExact
21 #define PyStr_FromString PyUnicode_FromString
22 #define PyStr_FromStringAndSize PyUnicode_FromStringAndSize
23 #define PyStr_FromFormat PyUnicode_FromFormat
24 #define PyStr_FromFormatV PyUnicode_FromFormatV
25 #define PyStr_AsString PyUnicode_AsUTF8
26 #define PyStr_Concat PyUnicode_Concat
27 #define PyStr_Format PyUnicode_Format
28 #define PyStr_InternInPlace PyUnicode_InternInPlace
29 #define PyStr_InternFromString PyUnicode_InternFromString
30 #define PyStr_Decode PyUnicode_Decode
31
32 #define PyStr_AsUTF8String PyUnicode_AsUTF8String /* returns PyBytes */
33 #define PyStr_AsUTF8 PyUnicode_AsUTF8
34 #define PyStr_AsUTF8AndSize PyUnicode_AsUTF8AndSize
35
36 /* Ints */
37
38 #define PyInt_Type PyLong_Type
39 #define PyInt_Check PyLong_Check
40 #define PyInt_CheckExact PyLong_CheckExact
41 #define PyInt_FromString PyLong_FromString
42 #define PyInt_FromLong PyLong_FromLong
43 #define PyInt_FromSsize_t PyLong_FromSsize_t
44 #define PyInt_FromSize_t PyLong_FromSize_t
45 #define PyInt_AsLong PyLong_AsLong
46 #define PyInt_AS_LONG PyLong_AS_LONG
47 #define PyInt_AsUnsignedLongLongMask PyLong_AsUnsignedLongLongMask
48 #define PyInt_AsSsize_t PyLong_AsSsize_t
49
50 /* Module init */
51
52 #define MODULE_INIT_FUNC(name) \
53 PyMODINIT_FUNC PyInit_ ## name(void); \
54 PyMODINIT_FUNC PyInit_ ## name(void)
55
56 #else
57
58 /***** Python 2 *****/
59
60 #define IS_PY3 0
61
62 /* Strings */
63
64 #define PyStr_Type PyString_Type
65 #define PyStr_Check PyString_Check
66 #define PyStr_CheckExact PyString_CheckExact
67 #define PyStr_FromString PyString_FromString
68 #define PyStr_FromStringAndSize PyString_FromStringAndSize
69 #define PyStr_FromFormat PyString_FromFormat
70 #define PyStr_FromFormatV PyString_FromFormatV
71 #define PyStr_AsString PyString_AsString
72 #define PyStr_Format PyString_Format
73 #define PyStr_InternInPlace PyString_InternInPlace
74 #define PyStr_InternFromString PyString_InternFromString
75 #define PyStr_Decode PyString_Decode
76
77 #ifdef __GNUC__
78 static PyObject *PyStr_Concat(PyObject *left, PyObject *right) __attribute__ ((unused));
79 #endif
PyStr_Concat(PyObject * left,PyObject * right)80 static PyObject *PyStr_Concat(PyObject *left, PyObject *right) {
81 PyObject *str = left;
82 Py_INCREF(left); /* reference to old left will be stolen */
83 PyString_Concat(&str, right);
84 if (str) {
85 return str;
86 } else {
87 return NULL;
88 }
89 }
90
91 #define PyStr_AsUTF8String(str) (Py_INCREF(str), (str))
92 #define PyStr_AsUTF8 PyString_AsString
93 #define PyStr_AsUTF8AndSize(pystr, sizeptr) \
94 ((*sizeptr=PyString_Size(pystr)), PyString_AsString(pystr))
95
96 #define PyBytes_Type PyString_Type
97 #define PyBytes_Check PyString_Check
98 #define PyBytes_CheckExact PyString_CheckExact
99 #define PyBytes_FromString PyString_FromString
100 #define PyBytes_FromStringAndSize PyString_FromStringAndSize
101 #define PyBytes_FromFormat PyString_FromFormat
102 #define PyBytes_FromFormatV PyString_FromFormatV
103 #define PyBytes_Size PyString_Size
104 #define PyBytes_GET_SIZE PyString_GET_SIZE
105 #define PyBytes_AsString PyString_AsString
106 #define PyBytes_AS_STRING PyString_AS_STRING
107 #define PyBytes_AsStringAndSize PyString_AsStringAndSize
108 #define PyBytes_Concat PyString_Concat
109 #define PyBytes_ConcatAndDel PyString_ConcatAndDel
110 #define _PyBytes_Resize _PyString_Resize
111
112 /* Floats */
113
114 #define PyFloat_FromString(str) PyFloat_FromString(str, NULL)
115
116 /* Module init */
117
118 #define PyModuleDef_HEAD_INIT 0
119
120 typedef struct PyModuleDef {
121 int m_base;
122 const char* m_name;
123 const char* m_doc;
124 Py_ssize_t m_size;
125 PyMethodDef *m_methods;
126 void* m_slots;
127 void* m_traverse;
128 void* m_clear;
129 void* m_free;
130 } PyModuleDef;
131
PyModule_Create(PyModuleDef * def)132 static PyObject *PyModule_Create(PyModuleDef *def) {
133 assert(!def->m_slots);
134 assert(!def->m_traverse);
135 assert(!def->m_clear);
136 assert(!def->m_free);
137 return Py_InitModule3(def->m_name, def->m_methods, def->m_doc);
138 }
139
140 #define MODULE_INIT_FUNC(name) \
141 static PyObject *PyInit_ ## name(void); \
142 PyMODINIT_FUNC init ## name(void); \
143 PyMODINIT_FUNC init ## name(void) { PyInit_ ## name(); } \
144 static PyObject *PyInit_ ## name(void)
145
146
147 #endif
148
149 #endif
150