mercurial/parsers.c
changeset 11361 3de3d670d2b6
parent 10449 7c8266c1d15a
child 13254 5ef5eb1f3515
equal deleted inserted replaced
11360:2ac98313b26c 11361:3de3d670d2b6
     8 */
     8 */
     9 
     9 
    10 #include <Python.h>
    10 #include <Python.h>
    11 #include <ctype.h>
    11 #include <ctype.h>
    12 #include <string.h>
    12 #include <string.h>
       
    13 
       
    14 #include "util.h"
    13 
    15 
    14 static int hexdigit(char c)
    16 static int hexdigit(char c)
    15 {
    17 {
    16 	if (c >= '0' && c <= '9')
    18 	if (c >= '0' && c <= '9')
    17 		return c - '0';
    19 		return c - '0';
    31 {
    33 {
    32 	PyObject *ret;
    34 	PyObject *ret;
    33 	const char *c;
    35 	const char *c;
    34 	char *d;
    36 	char *d;
    35 
    37 
    36 	ret = PyString_FromStringAndSize(NULL, len / 2);
    38 	ret = PyBytes_FromStringAndSize(NULL, len / 2);
       
    39 
    37 	if (!ret)
    40 	if (!ret)
    38 		return NULL;
    41 		return NULL;
    39 
    42 
    40 	d = PyString_AS_STRING(ret);
    43 	d = PyBytes_AsString(ret);
       
    44 
    41 	for (c = str; c < str + len;) {
    45 	for (c = str; c < str + len;) {
    42 		int hi = hexdigit(*c++);
    46 		int hi = hexdigit(*c++);
    43 		int lo = hexdigit(*c++);
    47 		int lo = hexdigit(*c++);
    44 		*d++ = (hi << 4) | lo;
    48 		*d++ = (hi << 4) | lo;
    45 	}
    49 	}
    79 			PyErr_SetString(PyExc_ValueError,
    83 			PyErr_SetString(PyExc_ValueError,
    80 					"manifest entry has no separator");
    84 					"manifest entry has no separator");
    81 			goto quit;
    85 			goto quit;
    82 		}
    86 		}
    83 
    87 
    84 		file = PyString_FromStringAndSize(start, zero - start);
    88 		file = PyBytes_FromStringAndSize(start, zero - start);
       
    89 
    85 		if (!file)
    90 		if (!file)
    86 			goto bail;
    91 			goto bail;
    87 
    92 
    88 		nlen = cur - zero - 1;
    93 		nlen = cur - zero - 1;
    89 
    94 
    90 		node = unhexlify(zero + 1, nlen > 40 ? 40 : nlen);
    95 		node = unhexlify(zero + 1, nlen > 40 ? 40 : nlen);
    91 		if (!node)
    96 		if (!node)
    92 			goto bail;
    97 			goto bail;
    93 
    98 
    94 		if (nlen > 40) {
    99 		if (nlen > 40) {
    95 			flags = PyString_FromStringAndSize(zero + 41,
   100 			flags = PyBytes_FromStringAndSize(zero + 41,
    96 							   nlen - 40);
   101 							   nlen - 40);
    97 			if (!flags)
   102 			if (!flags)
    98 				goto bail;
   103 				goto bail;
    99 
   104 
   100 			if (PyDict_SetItem(fdict, file, flags) == -1)
   105 			if (PyDict_SetItem(fdict, file, flags) == -1)
   204 			goto quit;
   209 			goto quit;
   205 		PyObject_GC_UnTrack(entry); /* don't waste time with this */
   210 		PyObject_GC_UnTrack(entry); /* don't waste time with this */
   206 
   211 
   207 		cpos = memchr(cur, 0, flen);
   212 		cpos = memchr(cur, 0, flen);
   208 		if (cpos) {
   213 		if (cpos) {
   209 			fname = PyString_FromStringAndSize(cur, cpos - cur);
   214 			fname = PyBytes_FromStringAndSize(cur, cpos - cur);
   210 			cname = PyString_FromStringAndSize(cpos + 1,
   215 			cname = PyBytes_FromStringAndSize(cpos + 1,
   211 							   flen - (cpos - cur) - 1);
   216 							   flen - (cpos - cur) - 1);
   212 			if (!fname || !cname ||
   217 			if (!fname || !cname ||
   213 			    PyDict_SetItem(cmap, fname, cname) == -1 ||
   218 			    PyDict_SetItem(cmap, fname, cname) == -1 ||
   214 			    PyDict_SetItem(dmap, fname, entry) == -1)
   219 			    PyDict_SetItem(dmap, fname, entry) == -1)
   215 				goto quit;
   220 				goto quit;
   216 			Py_DECREF(cname);
   221 			Py_DECREF(cname);
   217 		} else {
   222 		} else {
   218 			fname = PyString_FromStringAndSize(cur, flen);
   223 			fname = PyBytes_FromStringAndSize(cur, flen);
   219 			if (!fname ||
   224 			if (!fname ||
   220 			    PyDict_SetItem(dmap, fname, entry) == -1)
   225 			    PyDict_SetItem(dmap, fname, entry) == -1)
   221 				goto quit;
   226 				goto quit;
   222 		}
   227 		}
   223 		cur += flen;
   228 		cur += flen;
   246                                    const char *c_node_id)
   251                                    const char *c_node_id)
   247 {
   252 {
   248 	int err;
   253 	int err;
   249 	PyObject *entry, *node_id, *n_obj;
   254 	PyObject *entry, *node_id, *n_obj;
   250 
   255 
   251 	node_id = PyString_FromStringAndSize(c_node_id, 20);
   256 	node_id = PyBytes_FromStringAndSize(c_node_id, 20);
   252 	n_obj = PyInt_FromLong(n);
   257 	n_obj = PyInt_FromLong(n);
       
   258 
   253 	if (!node_id || !n_obj)
   259 	if (!node_id || !n_obj)
   254 		err = -1;
   260 		err = -1;
   255 	else
   261 	else
   256 		err = PyDict_SetItem(nodemap, node_id, n_obj);
   262 		err = PyDict_SetItem(nodemap, node_id, n_obj);
   257 
   263 
   425 	{"parse_dirstate", parse_dirstate, METH_VARARGS, "parse a dirstate\n"},
   431 	{"parse_dirstate", parse_dirstate, METH_VARARGS, "parse a dirstate\n"},
   426 	{"parse_index", parse_index, METH_VARARGS, "parse a revlog index\n"},
   432 	{"parse_index", parse_index, METH_VARARGS, "parse a revlog index\n"},
   427 	{NULL, NULL}
   433 	{NULL, NULL}
   428 };
   434 };
   429 
   435 
       
   436 #ifdef IS_PY3K
       
   437 static struct PyModuleDef parsers_module = {
       
   438 	PyModuleDef_HEAD_INIT,
       
   439 	"parsers",
       
   440 	parsers_doc,
       
   441 	-1,
       
   442 	methods
       
   443 };
       
   444 
       
   445 PyMODINIT_FUNC PyInit_parsers(void)
       
   446 {
       
   447 	return PyModule_Create(&parsers_module);
       
   448 }
       
   449 #else
   430 PyMODINIT_FUNC initparsers(void)
   450 PyMODINIT_FUNC initparsers(void)
   431 {
   451 {
   432 	Py_InitModule3("parsers", methods, parsers_doc);
   452 	Py_InitModule3("parsers", methods, parsers_doc);
   433 }
   453 }
       
   454 #endif
       
   455