comparison mercurial/parsers.c @ 30100:c5afe5531709

parsers: convert PyString* to PyBytes* With this change, we no longer have any occurrences of "PyString" in our C extensions.
author Gregory Szorc <gregory.szorc@gmail.com>
date Sat, 08 Oct 2016 22:02:29 +0200
parents 8abe9264c73a
children 74cd33c9be76
comparison
equal deleted inserted replaced
30099:e60de7fcad29 30100:c5afe5531709
608 } 608 }
609 609
610 /* Figure out how much we need to allocate. */ 610 /* Figure out how much we need to allocate. */
611 for (nbytes = 40, pos = 0; PyDict_Next(map, &pos, &k, &v);) { 611 for (nbytes = 40, pos = 0; PyDict_Next(map, &pos, &k, &v);) {
612 PyObject *c; 612 PyObject *c;
613 if (!PyString_Check(k)) { 613 if (!PyBytes_Check(k)) {
614 PyErr_SetString(PyExc_TypeError, "expected string key"); 614 PyErr_SetString(PyExc_TypeError, "expected string key");
615 goto bail; 615 goto bail;
616 } 616 }
617 nbytes += PyString_GET_SIZE(k) + 17; 617 nbytes += PyBytes_GET_SIZE(k) + 17;
618 c = PyDict_GetItem(copymap, k); 618 c = PyDict_GetItem(copymap, k);
619 if (c) { 619 if (c) {
620 if (!PyString_Check(c)) { 620 if (!PyBytes_Check(c)) {
621 PyErr_SetString(PyExc_TypeError, 621 PyErr_SetString(PyExc_TypeError,
622 "expected string key"); 622 "expected string key");
623 goto bail; 623 goto bail;
624 } 624 }
625 nbytes += PyString_GET_SIZE(c) + 1; 625 nbytes += PyBytes_GET_SIZE(c) + 1;
626 } 626 }
627 } 627 }
628 628
629 packobj = PyString_FromStringAndSize(NULL, nbytes); 629 packobj = PyBytes_FromStringAndSize(NULL, nbytes);
630 if (packobj == NULL) 630 if (packobj == NULL)
631 goto bail; 631 goto bail;
632 632
633 p = PyString_AS_STRING(packobj); 633 p = PyBytes_AS_STRING(packobj);
634 634
635 pn = PySequence_ITEM(pl, 0); 635 pn = PySequence_ITEM(pl, 0);
636 if (PyString_AsStringAndSize(pn, &s, &l) == -1 || l != 20) { 636 if (PyBytes_AsStringAndSize(pn, &s, &l) == -1 || l != 20) {
637 PyErr_SetString(PyExc_TypeError, "expected a 20-byte hash"); 637 PyErr_SetString(PyExc_TypeError, "expected a 20-byte hash");
638 goto bail; 638 goto bail;
639 } 639 }
640 memcpy(p, s, l); 640 memcpy(p, s, l);
641 p += 20; 641 p += 20;
642 pn = PySequence_ITEM(pl, 1); 642 pn = PySequence_ITEM(pl, 1);
643 if (PyString_AsStringAndSize(pn, &s, &l) == -1 || l != 20) { 643 if (PyBytes_AsStringAndSize(pn, &s, &l) == -1 || l != 20) {
644 PyErr_SetString(PyExc_TypeError, "expected a 20-byte hash"); 644 PyErr_SetString(PyExc_TypeError, "expected a 20-byte hash");
645 goto bail; 645 goto bail;
646 } 646 }
647 memcpy(p, s, l); 647 memcpy(p, s, l);
648 p += 20; 648 p += 20;
683 putbe32((uint32_t)mode, p); 683 putbe32((uint32_t)mode, p);
684 putbe32((uint32_t)size, p + 4); 684 putbe32((uint32_t)size, p + 4);
685 putbe32((uint32_t)mtime, p + 8); 685 putbe32((uint32_t)mtime, p + 8);
686 t = p + 12; 686 t = p + 12;
687 p += 16; 687 p += 16;
688 len = PyString_GET_SIZE(k); 688 len = PyBytes_GET_SIZE(k);
689 memcpy(p, PyString_AS_STRING(k), len); 689 memcpy(p, PyBytes_AS_STRING(k), len);
690 p += len; 690 p += len;
691 o = PyDict_GetItem(copymap, k); 691 o = PyDict_GetItem(copymap, k);
692 if (o) { 692 if (o) {
693 *p++ = '\0'; 693 *p++ = '\0';
694 l = PyString_GET_SIZE(o); 694 l = PyBytes_GET_SIZE(o);
695 memcpy(p, PyString_AS_STRING(o), l); 695 memcpy(p, PyBytes_AS_STRING(o), l);
696 p += l; 696 p += l;
697 len += l + 1; 697 len += l + 1;
698 } 698 }
699 putbe32((uint32_t)len, t); 699 putbe32((uint32_t)len, t);
700 } 700 }
701 701
702 pos = p - PyString_AS_STRING(packobj); 702 pos = p - PyBytes_AS_STRING(packobj);
703 if (pos != nbytes) { 703 if (pos != nbytes) {
704 PyErr_Format(PyExc_SystemError, "bad dirstate size: %ld != %ld", 704 PyErr_Format(PyExc_SystemError, "bad dirstate size: %ld != %ld",
705 (long)pos, (long)nbytes); 705 (long)pos, (long)nbytes);
706 goto bail; 706 goto bail;
707 } 707 }
794 inline_scan(self, self->offsets); 794 inline_scan(self, self->offsets);
795 } 795 }
796 return self->offsets[pos]; 796 return self->offsets[pos];
797 } 797 }
798 798
799 return PyString_AS_STRING(self->data) + pos * v1_hdrsize; 799 return PyBytes_AS_STRING(self->data) + pos * v1_hdrsize;
800 } 800 }
801 801
802 static inline int index_get_parents(indexObject *self, Py_ssize_t rev, 802 static inline int index_get_parents(indexObject *self, Py_ssize_t rev,
803 int *ps, int maxrev) 803 int *ps, int maxrev)
804 { 804 {
924 924
925 if (pos >= self->length - 1) { 925 if (pos >= self->length - 1) {
926 PyObject *tuple, *str; 926 PyObject *tuple, *str;
927 tuple = PyList_GET_ITEM(self->added, pos - self->length + 1); 927 tuple = PyList_GET_ITEM(self->added, pos - self->length + 1);
928 str = PyTuple_GetItem(tuple, 7); 928 str = PyTuple_GetItem(tuple, 7);
929 return str ? PyString_AS_STRING(str) : NULL; 929 return str ? PyBytes_AS_STRING(str) : NULL;
930 } 930 }
931 931
932 data = index_deref(self, pos); 932 data = index_deref(self, pos);
933 return data ? data + 32 : NULL; 933 return data ? data + 32 : NULL;
934 } 934 }
935 935
936 static int nt_insert(indexObject *self, const char *node, int rev); 936 static int nt_insert(indexObject *self, const char *node, int rev);
937 937
938 static int node_check(PyObject *obj, char **node, Py_ssize_t *nodelen) 938 static int node_check(PyObject *obj, char **node, Py_ssize_t *nodelen)
939 { 939 {
940 if (PyString_AsStringAndSize(obj, node, nodelen) == -1) 940 if (PyBytes_AsStringAndSize(obj, node, nodelen) == -1)
941 return -1; 941 return -1;
942 if (*nodelen == 20) 942 if (*nodelen == 20)
943 return 0; 943 return 0;
944 PyErr_SetString(PyExc_ValueError, "20-byte hash required"); 944 PyErr_SetString(PyExc_ValueError, "20-byte hash required");
945 return -1; 945 return -1;
1823 case -3: 1823 case -3:
1824 return NULL; 1824 return NULL;
1825 case -2: 1825 case -2:
1826 Py_RETURN_NONE; 1826 Py_RETURN_NONE;
1827 case -1: 1827 case -1:
1828 return PyString_FromStringAndSize(nullid, 20); 1828 return PyBytes_FromStringAndSize(nullid, 20);
1829 } 1829 }
1830 1830
1831 fullnode = index_node(self, rev); 1831 fullnode = index_node(self, rev);
1832 if (fullnode == NULL) { 1832 if (fullnode == NULL) {
1833 PyErr_Format(PyExc_IndexError, 1833 PyErr_Format(PyExc_IndexError,
1834 "could not access rev %d", rev); 1834 "could not access rev %d", rev);
1835 return NULL; 1835 return NULL;
1836 } 1836 }
1837 return PyString_FromStringAndSize(fullnode, 20); 1837 return PyBytes_FromStringAndSize(fullnode, 20);
1838 } 1838 }
1839 1839
1840 static PyObject *index_m_get(indexObject *self, PyObject *args) 1840 static PyObject *index_m_get(indexObject *self, PyObject *args)
1841 { 1841 {
1842 Py_ssize_t nodelen; 1842 Py_ssize_t nodelen;
2245 2245
2246 for (i = start; i < len; i++) { 2246 for (i = start; i < len; i++) {
2247 PyObject *tuple = PyList_GET_ITEM(self->added, i); 2247 PyObject *tuple = PyList_GET_ITEM(self->added, i);
2248 PyObject *node = PyTuple_GET_ITEM(tuple, 7); 2248 PyObject *node = PyTuple_GET_ITEM(tuple, 7);
2249 2249
2250 nt_insert(self, PyString_AS_STRING(node), -1); 2250 nt_insert(self, PyBytes_AS_STRING(node), -1);
2251 } 2251 }
2252 2252
2253 if (start == 0) 2253 if (start == 0)
2254 Py_CLEAR(self->added); 2254 Py_CLEAR(self->added);
2255 } 2255 }
2370 * Find all RevlogNG entries in an index that has inline data. Update 2370 * Find all RevlogNG entries in an index that has inline data. Update
2371 * the optional "offsets" table with those entries. 2371 * the optional "offsets" table with those entries.
2372 */ 2372 */
2373 static Py_ssize_t inline_scan(indexObject *self, const char **offsets) 2373 static Py_ssize_t inline_scan(indexObject *self, const char **offsets)
2374 { 2374 {
2375 const char *data = PyString_AS_STRING(self->data); 2375 const char *data = PyBytes_AS_STRING(self->data);
2376 Py_ssize_t pos = 0; 2376 Py_ssize_t pos = 0;
2377 Py_ssize_t end = PyString_GET_SIZE(self->data); 2377 Py_ssize_t end = PyBytes_GET_SIZE(self->data);
2378 long incr = v1_hdrsize; 2378 long incr = v1_hdrsize;
2379 Py_ssize_t len = 0; 2379 Py_ssize_t len = 0;
2380 2380
2381 while (pos + v1_hdrsize <= end && pos >= 0) { 2381 while (pos + v1_hdrsize <= end && pos >= 0) {
2382 uint32_t comp_len; 2382 uint32_t comp_len;
2414 self->nt = NULL; 2414 self->nt = NULL;
2415 self->offsets = NULL; 2415 self->offsets = NULL;
2416 2416
2417 if (!PyArg_ParseTuple(args, "OO", &data_obj, &inlined_obj)) 2417 if (!PyArg_ParseTuple(args, "OO", &data_obj, &inlined_obj))
2418 return -1; 2418 return -1;
2419 if (!PyString_Check(data_obj)) { 2419 if (!PyBytes_Check(data_obj)) {
2420 PyErr_SetString(PyExc_TypeError, "data is not a string"); 2420 PyErr_SetString(PyExc_TypeError, "data is not a string");
2421 return -1; 2421 return -1;
2422 } 2422 }
2423 size = PyString_GET_SIZE(data_obj); 2423 size = PyBytes_GET_SIZE(data_obj);
2424 2424
2425 self->inlined = inlined_obj && PyObject_IsTrue(inlined_obj); 2425 self->inlined = inlined_obj && PyObject_IsTrue(inlined_obj);
2426 self->data = data_obj; 2426 self->data = data_obj;
2427 2427
2428 self->ntlength = self->ntcapacity = 0; 2428 self->ntlength = self->ntcapacity = 0;
2611 PyObject *list = PyTuple_New(num); 2611 PyObject *list = PyTuple_New(num);
2612 if (list == NULL) { 2612 if (list == NULL) {
2613 return NULL; 2613 return NULL;
2614 } 2614 }
2615 for (i = 0; i < num; i++) { 2615 for (i = 0; i < num; i++) {
2616 PyObject *hash = PyString_FromStringAndSize(source, hashwidth); 2616 PyObject *hash = PyBytes_FromStringAndSize(source, hashwidth);
2617 if (hash == NULL) { 2617 if (hash == NULL) {
2618 Py_DECREF(list); 2618 Py_DECREF(list);
2619 return NULL; 2619 return NULL;
2620 } 2620 }
2621 PyTuple_SET_ITEM(list, i, hash); 2621 PyTuple_SET_ITEM(list, i, hash);
2667 dataend = databegin + *msize; /* narrow down to marker size */ 2667 dataend = databegin + *msize; /* narrow down to marker size */
2668 2668
2669 if (data + hashwidth > dataend) { 2669 if (data + hashwidth > dataend) {
2670 goto overflow; 2670 goto overflow;
2671 } 2671 }
2672 prec = PyString_FromStringAndSize(data, hashwidth); 2672 prec = PyBytes_FromStringAndSize(data, hashwidth);
2673 data += hashwidth; 2673 data += hashwidth;
2674 if (prec == NULL) { 2674 if (prec == NULL) {
2675 goto bail; 2675 goto bail;
2676 } 2676 }
2677 2677
2710 Py_ssize_t leftsize = (unsigned char)(*data++); 2710 Py_ssize_t leftsize = (unsigned char)(*data++);
2711 Py_ssize_t rightsize = (unsigned char)(*data++); 2711 Py_ssize_t rightsize = (unsigned char)(*data++);
2712 if (meta + leftsize + rightsize > dataend) { 2712 if (meta + leftsize + rightsize > dataend) {
2713 goto overflow; 2713 goto overflow;
2714 } 2714 }
2715 left = PyString_FromStringAndSize(meta, leftsize); 2715 left = PyBytes_FromStringAndSize(meta, leftsize);
2716 meta += leftsize; 2716 meta += leftsize;
2717 right = PyString_FromStringAndSize(meta, rightsize); 2717 right = PyBytes_FromStringAndSize(meta, rightsize);
2718 meta += rightsize; 2718 meta += rightsize;
2719 tmp = PyTuple_New(2); 2719 tmp = PyTuple_New(2);
2720 if (!left || !right || !tmp) { 2720 if (!left || !right || !tmp) {
2721 Py_XDECREF(left); 2721 Py_XDECREF(left);
2722 Py_XDECREF(right); 2722 Py_XDECREF(right);