Skip to content
Newer
Older
100644 131 lines (106 sloc) 3.49 KB
c278e74 @mkleehammer Single source base for Python 2 and 3
authored
1
2 #ifndef PYODBCCOMPAT_H
3 #define PYODBCCOMPAT_H
4
5 // Macros and functions to ease compatibility with Python 2 and Python 3.
6
7 #if PY_VERSION_HEX >= 0x03000000 && PY_VERSION_HEX < 0x03010000
8 #error Python 3.0 is not supported. Please use 3.1 and higher.
9 #endif
10
11 // Macros introduced in 2.6, backported for 2.4 and 2.5.
12 #ifndef PyVarObject_HEAD_INIT
13 #define PyVarObject_HEAD_INIT(type, size) PyObject_HEAD_INIT(type) size,
14 #endif
15 #ifndef Py_TYPE
16 #define Py_TYPE(ob) (((PyObject*)(ob))->ob_type)
17 #endif
18
19 // Used for items that are ANSI in Python 2 and Unicode in Python 3 or in int 2 and long in 3.
20
21 #if PY_MAJOR_VERSION >= 3
22 #define PyString_FromString PyUnicode_FromString
23 #define PyString_FromStringAndSize PyUnicode_FromStringAndSize
24 #define PyString_Check PyUnicode_Check
25 #define PyString_Type PyUnicode_Type
26 #define PyString_Size PyUnicode_Size
27 #define PyInt_FromLong PyLong_FromLong
28 #define PyInt_AsLong PyLong_AsLong
29 #define PyInt_AS_LONG PyLong_AS_LONG
30 #define PyInt_Type PyLong_Type
31 #define PyString_FromFormatV PyUnicode_FromFormatV
32 #define PyString_FromFormat PyUnicode_FromFormat
33 #define Py_TPFLAGS_HAVE_ITER 0
34
35 #define PyString_AsString PyUnicode_AsString
36
37 #define TEXT_T Py_UNICODE
38
39 #define PyString_Join PyUnicode_Join
40
41 inline void PyString_ConcatAndDel(PyObject** lhs, PyObject* rhs)
42 {
43 PyUnicode_Concat(*lhs, rhs);
44 Py_DECREF(rhs);
45 }
46
47 #else
48 #include <stringobject.h>
49 #include <intobject.h>
50 #include <bufferobject.h>
51
52 #define TEXT_T char
53
54 #define PyString_Join _PyString_Join
55
56 #endif
57
58 inline PyObject* Text_New(Py_ssize_t length)
59 {
60 // Returns a new, uninitialized String (Python 2) or Unicode object (Python 3) object.
61 #if PY_MAJOR_VERSION < 3
62 return PyString_FromStringAndSize(0, length);
63 #else
64 return PyUnicode_FromUnicode(0, length);
65 #endif
66 }
67
68 inline TEXT_T* Text_Buffer(PyObject* o)
69 {
70 #if PY_MAJOR_VERSION < 3
71 I(PyString_Check(o));
72 return PyString_AS_STRING(o);
73 #else
74 I(PyUnicode_Check(o));
75 return PyUnicode_AS_UNICODE(o);
76 #endif
77 }
78
79
80 inline bool Text_Check(PyObject* o)
81 {
82 // A compatibility function that determines if the object is a string, based on the version of Python.
83 // For Python 2, an ASCII or Unicode string is allowed. For Python 3, it must be a Unicode object.
84 #if PY_MAJOR_VERSION < 3
85 if (o && PyString_Check(o))
86 return true;
87 #endif
88 return o && PyUnicode_Check(o);
89 }
90
91 bool Text_EqualsI(PyObject* lhs, const char* rhs);
92 // Case-insensitive comparison for a Python string object (Unicode in Python 3, ASCII or Unicode in Python 2) against
93 // an ASCII string. If lhs is 0 or None, false is returned.
94
95
96 inline Py_ssize_t Text_Size(PyObject* o)
97 {
98 #if PY_MAJOR_VERSION < 3
99 if (o && PyString_Check(o))
100 return PyString_GET_SIZE(o);
101 #endif
102 return (o && PyUnicode_Check(o)) ? PyUnicode_GET_SIZE(o) : 0;
103 }
104
9a32afd @mkleehammer Python 2 crash when ASCII keyword values passed to connect.
authored
105 inline Py_ssize_t TextCopyToUnicode(Py_UNICODE* buffer, PyObject* o)
106 {
107 // Copies a String or Unicode object to a Unicode buffer and returns the number of characters copied.
108 // No NULL terminator is appended!
109
110 #if PY_MAJOR_VERSION < 3
111 if (PyBytes_Check(o))
112 {
113 const Py_ssize_t cch = PyBytes_GET_SIZE(o);
114 const char * pch = PyBytes_AS_STRING(o);
115 for (Py_ssize_t i = 0; i < cch; i++)
116 *buffer++ = (Py_UNICODE)*pch++;
117 return cch;
118 }
119 else
120 {
121 #endif
122 Py_ssize_t cch = PyUnicode_GET_SIZE(o);
123 memcpy(buffer, PyUnicode_AS_UNICODE(o), cch * sizeof(Py_UNICODE));
124 return cch;
125 #if PY_MAJOR_VERSION < 3
126 }
127 #endif
128 }
c278e74 @mkleehammer Single source base for Python 2 and 3
authored
129
130 #endif // PYODBCCOMPAT_H
Something went wrong with that request. Please try again.