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