source: trunk/py3c.h

Last change on this file was 1021, checked in by cito, 3 weeks ago

Fix deprecation warning issue with Python 3.8

File size: 4.0 KB
Line 
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#define PY_SSIZE_T_CLEAN
8#include <Python.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/* Other */
57
58#define Py_TPFLAGS_HAVE_ITER 0 // not needed in Python 3
59
60#else
61
62/***** Python 2 *****/
63
64#define IS_PY3 0
65
66/* Strings */
67
68#define PyStr_Type PyString_Type
69#define PyStr_Check PyString_Check
70#define PyStr_CheckExact PyString_CheckExact
71#define PyStr_FromString PyString_FromString
72#define PyStr_FromStringAndSize PyString_FromStringAndSize
73#define PyStr_FromFormat PyString_FromFormat
74#define PyStr_FromFormatV PyString_FromFormatV
75#define PyStr_AsString PyString_AsString
76#define PyStr_Format PyString_Format
77#define PyStr_InternInPlace PyString_InternInPlace
78#define PyStr_InternFromString PyString_InternFromString
79#define PyStr_Decode PyString_Decode
80
81static inline PyObject *PyStr_Concat(PyObject *left, PyObject *right) {
82    PyObject *str = left;
83    Py_INCREF(left);  // reference to old left will be stolen
84    PyString_Concat(&str, right);
85    if (str) {
86        return str;
87    } else {
88        return NULL;
89    }
90}
91
92#define PyStr_AsUTF8String(str) (Py_INCREF(str), (str))
93#define PyStr_AsUTF8 PyString_AsString
94#define PyStr_AsUTF8AndSize(pystr, sizeptr) \
95    ((*sizeptr=PyString_Size(pystr)), PyString_AsString(pystr))
96
97#define PyBytes_Type PyString_Type
98#define PyBytes_Check PyString_Check
99#define PyBytes_CheckExact PyString_CheckExact
100#define PyBytes_FromString PyString_FromString
101#define PyBytes_FromStringAndSize PyString_FromStringAndSize
102#define PyBytes_FromFormat PyString_FromFormat
103#define PyBytes_FromFormatV PyString_FromFormatV
104#define PyBytes_Size PyString_Size
105#define PyBytes_GET_SIZE PyString_GET_SIZE
106#define PyBytes_AsString PyString_AsString
107#define PyBytes_AS_STRING PyString_AS_STRING
108#define PyBytes_AsStringAndSize PyString_AsStringAndSize
109#define PyBytes_Concat PyString_Concat
110#define PyBytes_ConcatAndDel PyString_ConcatAndDel
111#define _PyBytes_Resize _PyString_Resize
112
113/* Floats */
114
115#define PyFloat_FromString(str) PyFloat_FromString(str, NULL)
116
117/* Module init */
118
119#define PyModuleDef_HEAD_INIT 0
120
121typedef struct PyModuleDef {
122    int m_base;
123    const char* m_name;
124    const char* m_doc;
125    Py_ssize_t m_size;
126    PyMethodDef *m_methods;
127} PyModuleDef;
128
129#define PyModule_Create(def) \
130    Py_InitModule3((def)->m_name, (def)->m_methods, (def)->m_doc)
131
132#define MODULE_INIT_FUNC(name) \
133    static PyObject *PyInit_ ## name(void); \
134    void init ## name(void); \
135    void init ## name(void) { PyInit_ ## name(); } \
136    static PyObject *PyInit_ ## name(void)
137
138
139#endif
140
141#endif
Note: See TracBrowser for help on using the repository browser.