source: trunk/py3c.h

Last change on this file was 780, checked in by cito, 3 years ago

Refactoring of the decoding of columns

Add a helper function cast_value() and use that to cast
values to Python objects in both getresult() and dictresult().

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