Mercurial > hg
comparison mercurial/base85.c @ 11362:f42ef9493fa9
base85.c: Added support for py3k.
This patch adds support for py3k in base85.c. This is accomplished by including
a header file responsible for abstracting the API differences between python 2
and python 3.
author | Renato Cunha <renatoc@gmail.com> |
---|---|
date | Tue, 15 Jun 2010 19:49:56 -0300 |
parents | 08a0f04b56bd |
children | 613b8bd2284e |
comparison
equal
deleted
inserted
replaced
11361:3de3d670d2b6 | 11362:f42ef9493fa9 |
---|---|
8 | 8 |
9 Largely based on git's implementation | 9 Largely based on git's implementation |
10 */ | 10 */ |
11 | 11 |
12 #include <Python.h> | 12 #include <Python.h> |
13 | |
14 #include "util.h" | |
13 | 15 |
14 static const char b85chars[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" | 16 static const char b85chars[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
15 "abcdefghijklmnopqrstuvwxyz!#$%&()*+-;<=>?@^_`{|}~"; | 17 "abcdefghijklmnopqrstuvwxyz!#$%&()*+-;<=>?@^_`{|}~"; |
16 static char b85dec[256]; | 18 static char b85dec[256]; |
17 | 19 |
44 olen = len % 4; | 46 olen = len % 4; |
45 if (olen) | 47 if (olen) |
46 olen++; | 48 olen++; |
47 olen += len / 4 * 5; | 49 olen += len / 4 * 5; |
48 } | 50 } |
49 if (!(out = PyString_FromStringAndSize(NULL, olen + 3))) | 51 if (!(out = PyBytes_FromStringAndSize(NULL, olen + 3))) |
50 return NULL; | 52 return NULL; |
51 | 53 |
52 dst = PyString_AS_STRING(out); | 54 dst = PyBytes_AsString(out); |
53 | 55 |
54 while (len) { | 56 while (len) { |
55 acc = 0; | 57 acc = 0; |
56 for (i = 24; i >= 0; i -= 8) { | 58 for (i = 24; i >= 0; i -= 8) { |
57 ch = *text++; | 59 ch = *text++; |
66 } | 68 } |
67 dst += 5; | 69 dst += 5; |
68 } | 70 } |
69 | 71 |
70 if (!pad) | 72 if (!pad) |
71 _PyString_Resize(&out, olen); | 73 _PyBytes_Resize(&out, olen); |
72 | 74 |
73 return out; | 75 return out; |
74 } | 76 } |
75 | 77 |
76 static PyObject * | 78 static PyObject * |
87 | 89 |
88 olen = len / 5 * 4; | 90 olen = len / 5 * 4; |
89 i = len % 5; | 91 i = len % 5; |
90 if (i) | 92 if (i) |
91 olen += i - 1; | 93 olen += i - 1; |
92 if (!(out = PyString_FromStringAndSize(NULL, olen))) | 94 if (!(out = PyBytes_FromStringAndSize(NULL, olen))) |
93 return NULL; | 95 return NULL; |
94 | 96 |
95 dst = PyString_AS_STRING(out); | 97 dst = PyBytes_AsString(out); |
96 | 98 |
97 i = 0; | 99 i = 0; |
98 while (i < len) | 100 while (i < len) |
99 { | 101 { |
100 acc = 0; | 102 acc = 0; |
151 "five characters.\n"}, | 153 "five characters.\n"}, |
152 {"b85decode", b85decode, METH_VARARGS, "Decode base85 text.\n"}, | 154 {"b85decode", b85decode, METH_VARARGS, "Decode base85 text.\n"}, |
153 {NULL, NULL} | 155 {NULL, NULL} |
154 }; | 156 }; |
155 | 157 |
158 #ifdef IS_PY3K | |
159 static struct PyModuleDef base85_module = { | |
160 PyModuleDef_HEAD_INIT, | |
161 "base85", | |
162 base85_doc, | |
163 -1, | |
164 methods | |
165 }; | |
166 | |
167 PyMODINIT_FUNC PyInit_base85(void) | |
168 { | |
169 b85prep(); | |
170 | |
171 return PyModule_Create(&base85_module); | |
172 } | |
173 #else | |
156 PyMODINIT_FUNC initbase85(void) | 174 PyMODINIT_FUNC initbase85(void) |
157 { | 175 { |
158 Py_InitModule3("base85", methods, base85_doc); | 176 Py_InitModule3("base85", methods, base85_doc); |
159 | 177 |
160 b85prep(); | 178 b85prep(); |
161 } | 179 } |
180 #endif |