source: trunk/py3c.h

Last change on this file was 978, checked in by cito, 3 months ago

Make classic query work as iterator

Mostly following Justin's proposal on the mailing list.

File size: 4.0 KB
RevLine 
[564]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/* Strings */
16
17#define PyStr_Type PyUnicode_Type
18#define PyStr_Check PyUnicode_Check
19#define PyStr_CheckExact PyUnicode_CheckExact
20#define PyStr_FromString PyUnicode_FromString
21#define PyStr_FromStringAndSize PyUnicode_FromStringAndSize
22#define PyStr_FromFormat PyUnicode_FromFormat
23#define PyStr_FromFormatV PyUnicode_FromFormatV
24#define PyStr_AsString PyUnicode_AsUTF8
25#define PyStr_Concat PyUnicode_Concat
26#define PyStr_Format PyUnicode_Format
27#define PyStr_InternInPlace PyUnicode_InternInPlace
28#define PyStr_InternFromString PyUnicode_InternFromString
29#define PyStr_Decode PyUnicode_Decode
30
31#define PyStr_AsUTF8String PyUnicode_AsUTF8String // returns PyBytes
32#define PyStr_AsUTF8 PyUnicode_AsUTF8
33#define PyStr_AsUTF8AndSize PyUnicode_AsUTF8AndSize
34
35/* Ints */
36
37#define PyInt_Type PyLong_Type
38#define PyInt_Check PyLong_Check
39#define PyInt_CheckExact PyLong_CheckExact
40#define PyInt_FromString PyLong_FromString
41#define PyInt_FromLong PyLong_FromLong
42#define PyInt_FromSsize_t PyLong_FromSsize_t
43#define PyInt_FromSize_t PyLong_FromSize_t
44#define PyInt_AsLong PyLong_AsLong
45#define PyInt_AS_LONG PyLong_AS_LONG
46#define PyInt_AsUnsignedLongLongMask PyLong_AsUnsignedLongLongMask
47#define PyInt_AsSsize_t PyLong_AsSsize_t
48
49/* Module init */
50
51#define MODULE_INIT_FUNC(name) \
52    PyMODINIT_FUNC PyInit_ ## name(void); \
53    PyMODINIT_FUNC PyInit_ ## name(void)
54
[978]55/* Other */
56
57#define Py_TPFLAGS_HAVE_ITER 0 // not needed in Python 3
58
[564]59#else
60
61/***** Python 2 *****/
62
63#define IS_PY3 0
64
65/* Strings */
66
67#define PyStr_Type PyString_Type
68#define PyStr_Check PyString_Check
69#define PyStr_CheckExact PyString_CheckExact
70#define PyStr_FromString PyString_FromString
71#define PyStr_FromStringAndSize PyString_FromStringAndSize
72#define PyStr_FromFormat PyString_FromFormat
73#define PyStr_FromFormatV PyString_FromFormatV
74#define PyStr_AsString PyString_AsString
75#define PyStr_Format PyString_Format
76#define PyStr_InternInPlace PyString_InternInPlace
77#define PyStr_InternFromString PyString_InternFromString
78#define PyStr_Decode PyString_Decode
79
80static inline 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
[780]112/* Floats */
113
114#define PyFloat_FromString(str) PyFloat_FromString(str, NULL)
115
[564]116/* Module init */
117
118#define PyModuleDef_HEAD_INIT 0
119
120typedef 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} PyModuleDef;
127
128#define PyModule_Create(def) \
129    Py_InitModule3((def)->m_name, (def)->m_methods, (def)->m_doc)
130
131#define MODULE_INIT_FUNC(name) \
132    static PyObject *PyInit_ ## name(void); \
133    void init ## name(void); \
134    void init ## name(void) { PyInit_ ## name(); } \
135    static PyObject *PyInit_ ## name(void)
136
137
138#endif
139
140#endif
Note: See TracBrowser for help on using the repository browser.