comparison mercurial/dirs.c @ 30105:b2f90d8878ac

dirs: convert PyString to PyBytes PyStringObject was renamed to PyBytes in Python 3 along with the corresponding PyString* functions and macros. PyString* doesn't exist in Python 3. But PyBytes* is an alias to PyString in Python 2. So rewrite PyString* to PyBytes* for Python 2/3 dual compatibility.
author Gregory Szorc <gregory.szorc@gmail.com>
date Sat, 08 Oct 2016 14:31:59 +0200
parents 63e1dca2d6a4
children cb3048746dae
comparison
equal deleted inserted replaced
30104:63e1dca2d6a4 30105:b2f90d8878ac
39 return pos; 39 return pos;
40 } 40 }
41 41
42 static int _addpath(PyObject *dirs, PyObject *path) 42 static int _addpath(PyObject *dirs, PyObject *path)
43 { 43 {
44 const char *cpath = PyString_AS_STRING(path); 44 const char *cpath = PyBytes_AS_STRING(path);
45 Py_ssize_t pos = PyString_GET_SIZE(path); 45 Py_ssize_t pos = PyBytes_GET_SIZE(path);
46 PyObject *key = NULL; 46 PyObject *key = NULL;
47 int ret = -1; 47 int ret = -1;
48 48
49 while ((pos = _finddir(cpath, pos - 1)) != -1) { 49 while ((pos = _finddir(cpath, pos - 1)) != -1) {
50 PyObject *val; 50 PyObject *val;
51 51
52 /* It's likely that every prefix already has an entry 52 /* It's likely that every prefix already has an entry
53 in our dict. Try to avoid allocating and 53 in our dict. Try to avoid allocating and
54 deallocating a string for each prefix we check. */ 54 deallocating a string for each prefix we check. */
55 if (key != NULL) 55 if (key != NULL)
56 ((PyStringObject *)key)->ob_shash = -1; 56 ((PyBytesObject *)key)->ob_shash = -1;
57 else { 57 else {
58 /* Force Python to not reuse a small shared string. */ 58 /* Force Python to not reuse a small shared string. */
59 key = PyString_FromStringAndSize(cpath, 59 key = PyBytes_FromStringAndSize(cpath,
60 pos < 2 ? 2 : pos); 60 pos < 2 ? 2 : pos);
61 if (key == NULL) 61 if (key == NULL)
62 goto bail; 62 goto bail;
63 } 63 }
64 Py_SIZE(key) = pos; 64 Py_SIZE(key) = pos;
65 ((PyStringObject *)key)->ob_sval[pos] = '\0'; 65 ((PyBytesObject *)key)->ob_sval[pos] = '\0';
66 66
67 val = PyDict_GetItem(dirs, key); 67 val = PyDict_GetItem(dirs, key);
68 if (val != NULL) { 68 if (val != NULL) {
69 PyInt_AS_LONG(val) += 1; 69 PyInt_AS_LONG(val) += 1;
70 break; 70 break;
91 return ret; 91 return ret;
92 } 92 }
93 93
94 static int _delpath(PyObject *dirs, PyObject *path) 94 static int _delpath(PyObject *dirs, PyObject *path)
95 { 95 {
96 char *cpath = PyString_AS_STRING(path); 96 char *cpath = PyBytes_AS_STRING(path);
97 Py_ssize_t pos = PyString_GET_SIZE(path); 97 Py_ssize_t pos = PyBytes_GET_SIZE(path);
98 PyObject *key = NULL; 98 PyObject *key = NULL;
99 int ret = -1; 99 int ret = -1;
100 100
101 while ((pos = _finddir(cpath, pos - 1)) != -1) { 101 while ((pos = _finddir(cpath, pos - 1)) != -1) {
102 PyObject *val; 102 PyObject *val;
103 103
104 key = PyString_FromStringAndSize(cpath, pos); 104 key = PyBytes_FromStringAndSize(cpath, pos);
105 105
106 if (key == NULL) 106 if (key == NULL)
107 goto bail; 107 goto bail;
108 108
109 val = PyDict_GetItem(dirs, key); 109 val = PyDict_GetItem(dirs, key);
132 { 132 {
133 PyObject *key, *value; 133 PyObject *key, *value;
134 Py_ssize_t pos = 0; 134 Py_ssize_t pos = 0;
135 135
136 while (PyDict_Next(source, &pos, &key, &value)) { 136 while (PyDict_Next(source, &pos, &key, &value)) {
137 if (!PyString_Check(key)) { 137 if (!PyBytes_Check(key)) {
138 PyErr_SetString(PyExc_TypeError, "expected string key"); 138 PyErr_SetString(PyExc_TypeError, "expected string key");
139 return -1; 139 return -1;
140 } 140 }
141 if (skipchar) { 141 if (skipchar) {
142 if (!dirstate_tuple_check(value)) { 142 if (!dirstate_tuple_check(value)) {
163 iter = PyObject_GetIter(source); 163 iter = PyObject_GetIter(source);
164 if (iter == NULL) 164 if (iter == NULL)
165 return -1; 165 return -1;
166 166
167 while ((item = PyIter_Next(iter)) != NULL) { 167 while ((item = PyIter_Next(iter)) != NULL) {
168 if (!PyString_Check(item)) { 168 if (!PyBytes_Check(item)) {
169 PyErr_SetString(PyExc_TypeError, "expected string"); 169 PyErr_SetString(PyExc_TypeError, "expected string");
170 break; 170 break;
171 } 171 }
172 172
173 if (_addpath(dirs, item) == -1) 173 if (_addpath(dirs, item) == -1)
222 222
223 PyObject *dirs_addpath(dirsObject *self, PyObject *args) 223 PyObject *dirs_addpath(dirsObject *self, PyObject *args)
224 { 224 {
225 PyObject *path; 225 PyObject *path;
226 226
227 if (!PyArg_ParseTuple(args, "O!:addpath", &PyString_Type, &path)) 227 if (!PyArg_ParseTuple(args, "O!:addpath", &PyBytes_Type, &path))
228 return NULL; 228 return NULL;
229 229
230 if (_addpath(self->dict, path) == -1) 230 if (_addpath(self->dict, path) == -1)
231 return NULL; 231 return NULL;
232 232
235 235
236 static PyObject *dirs_delpath(dirsObject *self, PyObject *args) 236 static PyObject *dirs_delpath(dirsObject *self, PyObject *args)
237 { 237 {
238 PyObject *path; 238 PyObject *path;
239 239
240 if (!PyArg_ParseTuple(args, "O!:delpath", &PyString_Type, &path)) 240 if (!PyArg_ParseTuple(args, "O!:delpath", &PyBytes_Type, &path))
241 return NULL; 241 return NULL;
242 242
243 if (_delpath(self->dict, path) == -1) 243 if (_delpath(self->dict, path) == -1)
244 return NULL; 244 return NULL;
245 245
246 Py_RETURN_NONE; 246 Py_RETURN_NONE;
247 } 247 }
248 248
249 static int dirs_contains(dirsObject *self, PyObject *value) 249 static int dirs_contains(dirsObject *self, PyObject *value)
250 { 250 {
251 return PyString_Check(value) ? PyDict_Contains(self->dict, value) : 0; 251 return PyBytes_Check(value) ? PyDict_Contains(self->dict, value) : 0;
252 } 252 }
253 253
254 static void dirs_dealloc(dirsObject *self) 254 static void dirs_dealloc(dirsObject *self)
255 { 255 {
256 Py_XDECREF(self->dict); 256 Py_XDECREF(self->dict);