Show More
@@ -1,172 +1,198 b'' | |||||
1 | /* |
|
1 | /* | |
2 | util.h - utility functions for interfacing with the various python APIs. |
|
2 | util.h - utility functions for interfacing with the various python APIs. | |
3 |
|
3 | |||
4 | This software may be used and distributed according to the terms of |
|
4 | This software may be used and distributed according to the terms of | |
5 | the GNU General Public License, incorporated herein by reference. |
|
5 | the GNU General Public License, incorporated herein by reference. | |
6 | */ |
|
6 | */ | |
7 |
|
7 | |||
8 | #ifndef _HG_UTIL_H_ |
|
8 | #ifndef _HG_UTIL_H_ | |
9 | #define _HG_UTIL_H_ |
|
9 | #define _HG_UTIL_H_ | |
10 |
|
10 | |||
11 | #if PY_MAJOR_VERSION >= 3 |
|
11 | #if PY_MAJOR_VERSION >= 3 | |
12 |
|
12 | |||
13 | #define IS_PY3K |
|
13 | #define IS_PY3K | |
14 | #define PyInt_FromLong PyLong_FromLong |
|
14 | #define PyInt_FromLong PyLong_FromLong | |
15 | #define PyInt_AsLong PyLong_AsLong |
|
15 | #define PyInt_AsLong PyLong_AsLong | |
16 |
|
16 | |||
17 | /* |
|
17 | /* | |
18 | Mapping of some of the python < 2.x PyString* functions to py3k's PyUnicode. |
|
18 | Mapping of some of the python < 2.x PyString* functions to py3k's PyUnicode. | |
19 |
|
19 | |||
20 | The commented names below represent those that are present in the PyBytes |
|
20 | The commented names below represent those that are present in the PyBytes | |
21 | definitions for python < 2.6 (below in this file) that don't have a direct |
|
21 | definitions for python < 2.6 (below in this file) that don't have a direct | |
22 | implementation. |
|
22 | implementation. | |
23 | */ |
|
23 | */ | |
24 |
|
24 | |||
25 | #define PyStringObject PyUnicodeObject |
|
25 | #define PyStringObject PyUnicodeObject | |
26 | #define PyString_Type PyUnicode_Type |
|
26 | #define PyString_Type PyUnicode_Type | |
27 |
|
27 | |||
28 | #define PyString_Check PyUnicode_Check |
|
28 | #define PyString_Check PyUnicode_Check | |
29 | #define PyString_CheckExact PyUnicode_CheckExact |
|
29 | #define PyString_CheckExact PyUnicode_CheckExact | |
30 | #define PyString_CHECK_INTERNED PyUnicode_CHECK_INTERNED |
|
30 | #define PyString_CHECK_INTERNED PyUnicode_CHECK_INTERNED | |
31 | #define PyString_AS_STRING PyUnicode_AsLatin1String |
|
31 | #define PyString_AS_STRING PyUnicode_AsLatin1String | |
32 | #define PyString_GET_SIZE PyUnicode_GET_SIZE |
|
32 | #define PyString_GET_SIZE PyUnicode_GET_SIZE | |
33 |
|
33 | |||
34 | #define PyString_FromStringAndSize PyUnicode_FromStringAndSize |
|
34 | #define PyString_FromStringAndSize PyUnicode_FromStringAndSize | |
35 | #define PyString_FromString PyUnicode_FromString |
|
35 | #define PyString_FromString PyUnicode_FromString | |
36 | #define PyString_FromFormatV PyUnicode_FromFormatV |
|
36 | #define PyString_FromFormatV PyUnicode_FromFormatV | |
37 | #define PyString_FromFormat PyUnicode_FromFormat |
|
37 | #define PyString_FromFormat PyUnicode_FromFormat | |
38 | /* #define PyString_Size PyUnicode_GET_SIZE */ |
|
38 | /* #define PyString_Size PyUnicode_GET_SIZE */ | |
39 | /* #define PyString_AsString */ |
|
39 | /* #define PyString_AsString */ | |
40 | /* #define PyString_Repr */ |
|
40 | /* #define PyString_Repr */ | |
41 | #define PyString_Concat PyUnicode_Concat |
|
41 | #define PyString_Concat PyUnicode_Concat | |
42 | #define PyString_ConcatAndDel PyUnicode_AppendAndDel |
|
42 | #define PyString_ConcatAndDel PyUnicode_AppendAndDel | |
43 | #define _PyString_Resize PyUnicode_Resize |
|
43 | #define _PyString_Resize PyUnicode_Resize | |
44 | /* #define _PyString_Eq */ |
|
44 | /* #define _PyString_Eq */ | |
45 | #define PyString_Format PyUnicode_Format |
|
45 | #define PyString_Format PyUnicode_Format | |
46 | /* #define _PyString_FormatLong */ |
|
46 | /* #define _PyString_FormatLong */ | |
47 | /* #define PyString_DecodeEscape */ |
|
47 | /* #define PyString_DecodeEscape */ | |
48 | #define _PyString_Join PyUnicode_Join |
|
48 | #define _PyString_Join PyUnicode_Join | |
49 | #define PyString_Decode PyUnicode_Decode |
|
49 | #define PyString_Decode PyUnicode_Decode | |
50 | #define PyString_Encode PyUnicode_Encode |
|
50 | #define PyString_Encode PyUnicode_Encode | |
51 | #define PyString_AsEncodedObject PyUnicode_AsEncodedObject |
|
51 | #define PyString_AsEncodedObject PyUnicode_AsEncodedObject | |
52 | #define PyString_AsEncodedString PyUnicode_AsEncodedString |
|
52 | #define PyString_AsEncodedString PyUnicode_AsEncodedString | |
53 | #define PyString_AsDecodedObject PyUnicode_AsDecodedObject |
|
53 | #define PyString_AsDecodedObject PyUnicode_AsDecodedObject | |
54 | #define PyString_AsDecodedString PyUnicode_AsDecodedUnicode |
|
54 | #define PyString_AsDecodedString PyUnicode_AsDecodedUnicode | |
55 | /* #define PyString_AsStringAndSize */ |
|
55 | /* #define PyString_AsStringAndSize */ | |
56 | #define _PyString_InsertThousandsGrouping _PyUnicode_InsertThousandsGrouping |
|
56 | #define _PyString_InsertThousandsGrouping _PyUnicode_InsertThousandsGrouping | |
57 |
|
57 | |||
58 | #endif /* PY_MAJOR_VERSION */ |
|
58 | #endif /* PY_MAJOR_VERSION */ | |
59 |
|
59 | |||
60 | /* Backports from 2.6 */ |
|
60 | /* Backports from 2.6 */ | |
61 | #if PY_VERSION_HEX < 0x02060000 |
|
61 | #if PY_VERSION_HEX < 0x02060000 | |
62 |
|
62 | |||
63 | #define Py_TYPE(ob) (ob)->ob_type |
|
63 | #define Py_TYPE(ob) (ob)->ob_type | |
64 | #define Py_SIZE(ob) (ob)->ob_size |
|
64 | #define Py_SIZE(ob) (ob)->ob_size | |
65 | #define PyVarObject_HEAD_INIT(type, size) PyObject_HEAD_INIT(type) size, |
|
65 | #define PyVarObject_HEAD_INIT(type, size) PyObject_HEAD_INIT(type) size, | |
66 |
|
66 | |||
67 | /* Shamelessly stolen from bytesobject.h */ |
|
67 | /* Shamelessly stolen from bytesobject.h */ | |
68 | #define PyBytesObject PyStringObject |
|
68 | #define PyBytesObject PyStringObject | |
69 | #define PyBytes_Type PyString_Type |
|
69 | #define PyBytes_Type PyString_Type | |
70 |
|
70 | |||
71 | #define PyBytes_Check PyString_Check |
|
71 | #define PyBytes_Check PyString_Check | |
72 | #define PyBytes_CheckExact PyString_CheckExact |
|
72 | #define PyBytes_CheckExact PyString_CheckExact | |
73 | #define PyBytes_CHECK_INTERNED PyString_CHECK_INTERNED |
|
73 | #define PyBytes_CHECK_INTERNED PyString_CHECK_INTERNED | |
74 | #define PyBytes_AS_STRING PyString_AS_STRING |
|
74 | #define PyBytes_AS_STRING PyString_AS_STRING | |
75 | #define PyBytes_GET_SIZE PyString_GET_SIZE |
|
75 | #define PyBytes_GET_SIZE PyString_GET_SIZE | |
76 | #define Py_TPFLAGS_BYTES_SUBCLASS Py_TPFLAGS_STRING_SUBCLASS |
|
76 | #define Py_TPFLAGS_BYTES_SUBCLASS Py_TPFLAGS_STRING_SUBCLASS | |
77 |
|
77 | |||
78 | #define PyBytes_FromStringAndSize PyString_FromStringAndSize |
|
78 | #define PyBytes_FromStringAndSize PyString_FromStringAndSize | |
79 | #define PyBytes_FromString PyString_FromString |
|
79 | #define PyBytes_FromString PyString_FromString | |
80 | #define PyBytes_FromFormatV PyString_FromFormatV |
|
80 | #define PyBytes_FromFormatV PyString_FromFormatV | |
81 | #define PyBytes_FromFormat PyString_FromFormat |
|
81 | #define PyBytes_FromFormat PyString_FromFormat | |
82 | #define PyBytes_Size PyString_Size |
|
82 | #define PyBytes_Size PyString_Size | |
83 | #define PyBytes_AsString PyString_AsString |
|
83 | #define PyBytes_AsString PyString_AsString | |
84 | #define PyBytes_Repr PyString_Repr |
|
84 | #define PyBytes_Repr PyString_Repr | |
85 | #define PyBytes_Concat PyString_Concat |
|
85 | #define PyBytes_Concat PyString_Concat | |
86 | #define PyBytes_ConcatAndDel PyString_ConcatAndDel |
|
86 | #define PyBytes_ConcatAndDel PyString_ConcatAndDel | |
87 | #define _PyBytes_Resize _PyString_Resize |
|
87 | #define _PyBytes_Resize _PyString_Resize | |
88 | #define _PyBytes_Eq _PyString_Eq |
|
88 | #define _PyBytes_Eq _PyString_Eq | |
89 | #define PyBytes_Format PyString_Format |
|
89 | #define PyBytes_Format PyString_Format | |
90 | #define _PyBytes_FormatLong _PyString_FormatLong |
|
90 | #define _PyBytes_FormatLong _PyString_FormatLong | |
91 | #define PyBytes_DecodeEscape PyString_DecodeEscape |
|
91 | #define PyBytes_DecodeEscape PyString_DecodeEscape | |
92 | #define _PyBytes_Join _PyString_Join |
|
92 | #define _PyBytes_Join _PyString_Join | |
93 | #define PyBytes_Decode PyString_Decode |
|
93 | #define PyBytes_Decode PyString_Decode | |
94 | #define PyBytes_Encode PyString_Encode |
|
94 | #define PyBytes_Encode PyString_Encode | |
95 | #define PyBytes_AsEncodedObject PyString_AsEncodedObject |
|
95 | #define PyBytes_AsEncodedObject PyString_AsEncodedObject | |
96 | #define PyBytes_AsEncodedString PyString_AsEncodedString |
|
96 | #define PyBytes_AsEncodedString PyString_AsEncodedString | |
97 | #define PyBytes_AsDecodedObject PyString_AsDecodedObject |
|
97 | #define PyBytes_AsDecodedObject PyString_AsDecodedObject | |
98 | #define PyBytes_AsDecodedString PyString_AsDecodedString |
|
98 | #define PyBytes_AsDecodedString PyString_AsDecodedString | |
99 | #define PyBytes_AsStringAndSize PyString_AsStringAndSize |
|
99 | #define PyBytes_AsStringAndSize PyString_AsStringAndSize | |
100 | #define _PyBytes_InsertThousandsGrouping _PyString_InsertThousandsGrouping |
|
100 | #define _PyBytes_InsertThousandsGrouping _PyString_InsertThousandsGrouping | |
101 |
|
101 | |||
102 | #endif /* PY_VERSION_HEX */ |
|
102 | #endif /* PY_VERSION_HEX */ | |
103 |
|
103 | |||
104 | #if (PY_VERSION_HEX < 0x02050000) |
|
104 | #if (PY_VERSION_HEX < 0x02050000) | |
105 | /* Definitions to get compatibility with python 2.4 and earlier which |
|
105 | /* Definitions to get compatibility with python 2.4 and earlier which | |
106 | does not have Py_ssize_t. See also PEP 353. |
|
106 | does not have Py_ssize_t. See also PEP 353. | |
107 | Note: msvc (8 or earlier) does not have ssize_t, so we use Py_ssize_t. |
|
107 | Note: msvc (8 or earlier) does not have ssize_t, so we use Py_ssize_t. | |
108 | */ |
|
108 | */ | |
109 | typedef int Py_ssize_t; |
|
109 | typedef int Py_ssize_t; | |
110 | typedef Py_ssize_t (*lenfunc)(PyObject *); |
|
110 | typedef Py_ssize_t (*lenfunc)(PyObject *); | |
111 | typedef PyObject *(*ssizeargfunc)(PyObject *, Py_ssize_t); |
|
111 | typedef PyObject *(*ssizeargfunc)(PyObject *, Py_ssize_t); | |
112 | #define PyInt_FromSsize_t PyInt_FromLong |
|
112 | #define PyInt_FromSsize_t PyInt_FromLong | |
113 |
|
113 | |||
114 | #if !defined(PY_SSIZE_T_MIN) |
|
114 | #if !defined(PY_SSIZE_T_MIN) | |
115 | #define PY_SSIZE_T_MAX INT_MAX |
|
115 | #define PY_SSIZE_T_MAX INT_MAX | |
116 | #define PY_SSIZE_T_MIN INT_MIN |
|
116 | #define PY_SSIZE_T_MIN INT_MIN | |
117 | #endif |
|
117 | #endif | |
118 | #endif |
|
118 | #endif | |
119 |
|
119 | |||
120 | #ifdef _WIN32 |
|
120 | #ifdef _WIN32 | |
121 | #ifdef _MSC_VER |
|
121 | #ifdef _MSC_VER | |
122 | /* msvc 6.0 has problems */ |
|
122 | /* msvc 6.0 has problems */ | |
123 | #define inline __inline |
|
123 | #define inline __inline | |
124 | typedef signed char int8_t; |
|
124 | typedef signed char int8_t; | |
125 | typedef short int16_t; |
|
125 | typedef short int16_t; | |
126 | typedef long int32_t; |
|
126 | typedef long int32_t; | |
127 | typedef __int64 int64_t; |
|
127 | typedef __int64 int64_t; | |
128 | typedef unsigned char uint8_t; |
|
128 | typedef unsigned char uint8_t; | |
129 | typedef unsigned short uint16_t; |
|
129 | typedef unsigned short uint16_t; | |
130 | typedef unsigned long uint32_t; |
|
130 | typedef unsigned long uint32_t; | |
131 | typedef unsigned __int64 uint64_t; |
|
131 | typedef unsigned __int64 uint64_t; | |
132 | #else |
|
132 | #else | |
133 | #include <stdint.h> |
|
133 | #include <stdint.h> | |
134 | #endif |
|
134 | #endif | |
135 | #else |
|
135 | #else | |
136 | /* not windows */ |
|
136 | /* not windows */ | |
137 | #include <sys/types.h> |
|
137 | #include <sys/types.h> | |
138 | #if defined __BEOS__ && !defined __HAIKU__ |
|
138 | #if defined __BEOS__ && !defined __HAIKU__ | |
139 | #include <ByteOrder.h> |
|
139 | #include <ByteOrder.h> | |
140 | #else |
|
140 | #else | |
141 | #include <arpa/inet.h> |
|
141 | #include <arpa/inet.h> | |
142 | #endif |
|
142 | #endif | |
143 | #include <inttypes.h> |
|
143 | #include <inttypes.h> | |
144 | #endif |
|
144 | #endif | |
145 |
|
145 | |||
146 | #if defined __hpux || defined __SUNPRO_C || defined _AIX |
|
146 | #if defined __hpux || defined __SUNPRO_C || defined _AIX | |
147 | #define inline |
|
147 | #define inline | |
148 | #endif |
|
148 | #endif | |
149 |
|
149 | |||
150 | #ifdef __linux |
|
150 | #ifdef __linux | |
151 | #define inline __inline |
|
151 | #define inline __inline | |
152 | #endif |
|
152 | #endif | |
153 |
|
153 | |||
|
154 | #if defined(_MSC_VER) && (_MSC_VER >= 1300) | |||
|
155 | static inline uint32_t getbe32(const char *c) | |||
|
156 | { | |||
|
157 | return _byteswap_ulong(*(uint32_t *)c); | |||
|
158 | } | |||
|
159 | #elif GCC_VERSION >= 403 | |||
|
160 | static inline uint32_t getbe32(const char *c) | |||
|
161 | { | |||
|
162 | return __builtin_bswap32(*(uint32_t *)c); | |||
|
163 | } | |||
|
164 | #else | |||
154 | static inline uint32_t getbe32(const char *c) |
|
165 | static inline uint32_t getbe32(const char *c) | |
155 | { |
|
166 | { | |
156 | const unsigned char *d = (const unsigned char *)c; |
|
167 | const unsigned char *d = (const unsigned char *)c; | |
157 |
|
168 | |||
158 | return ((d[0] << 24) | |
|
169 | return ((d[0] << 24) | | |
159 | (d[1] << 16) | |
|
170 | (d[1] << 16) | | |
160 | (d[2] << 8) | |
|
171 | (d[2] << 8) | | |
161 | (d[3])); |
|
172 | (d[3])); | |
162 | } |
|
173 | } | |
|
174 | #endif | |||
163 |
|
175 | |||
|
176 | #if defined(_MSC_VER) && (_MSC_VER >= 1300) | |||
|
177 | static inline void putbe32(uint32_t x, char *c) | |||
|
178 | { | |||
|
179 | x = _byteswap_ulong(x); | |||
|
180 | *(uint32_t *)c = x; | |||
|
181 | } | |||
|
182 | #elif GCC_VERSION >= 403 | |||
|
183 | static inline void putbe32(uint32_t x, char *c) | |||
|
184 | { | |||
|
185 | x = __builtin_bswap32(x); | |||
|
186 | *(uint32_t *)c = x; | |||
|
187 | } | |||
|
188 | #else | |||
164 | static inline void putbe32(uint32_t x, char *c) |
|
189 | static inline void putbe32(uint32_t x, char *c) | |
165 | { |
|
190 | { | |
166 | c[0] = (x >> 24) & 0xff; |
|
191 | c[0] = (x >> 24) & 0xff; | |
167 | c[1] = (x >> 16) & 0xff; |
|
192 | c[1] = (x >> 16) & 0xff; | |
168 | c[2] = (x >> 8) & 0xff; |
|
193 | c[2] = (x >> 8) & 0xff; | |
169 | c[3] = (x) & 0xff; |
|
194 | c[3] = (x) & 0xff; | |
170 | } |
|
195 | } | |
|
196 | #endif | |||
171 |
|
197 | |||
172 | #endif /* _HG_UTIL_H_ */ |
|
198 | #endif /* _HG_UTIL_H_ */ |
General Comments 0
You need to be logged in to leave comments.
Login now