comparison mercurial/pathencode.c @ 30099:e60de7fcad29

pathencode: convert PyString* to PyBytes*
author Gregory Szorc <gregory.szorc@gmail.com>
date Sat, 08 Oct 2016 22:01:07 +0200
parents ae92c3eee88e
children a8c948ee3668
comparison
equal deleted inserted replaced
30098:301ef65e8ebb 30099:e60de7fcad29
154 char *path; 154 char *path;
155 155
156 if (!PyArg_ParseTuple(args, "O:encodedir", &pathobj)) 156 if (!PyArg_ParseTuple(args, "O:encodedir", &pathobj))
157 return NULL; 157 return NULL;
158 158
159 if (PyString_AsStringAndSize(pathobj, &path, &len) == -1) { 159 if (PyBytes_AsStringAndSize(pathobj, &path, &len) == -1) {
160 PyErr_SetString(PyExc_TypeError, "expected a string"); 160 PyErr_SetString(PyExc_TypeError, "expected a string");
161 return NULL; 161 return NULL;
162 } 162 }
163 163
164 newlen = len ? _encodedir(NULL, 0, path, len + 1) : 1; 164 newlen = len ? _encodedir(NULL, 0, path, len + 1) : 1;
166 if (newlen == len + 1) { 166 if (newlen == len + 1) {
167 Py_INCREF(pathobj); 167 Py_INCREF(pathobj);
168 return pathobj; 168 return pathobj;
169 } 169 }
170 170
171 newobj = PyString_FromStringAndSize(NULL, newlen); 171 newobj = PyBytes_FromStringAndSize(NULL, newlen);
172 172
173 if (newobj) { 173 if (newobj) {
174 PyString_GET_SIZE(newobj)--; 174 PyBytes_GET_SIZE(newobj)--;
175 _encodedir(PyString_AS_STRING(newobj), newlen, path, 175 _encodedir(PyBytes_AS_STRING(newobj), newlen, path,
176 len + 1); 176 len + 1);
177 } 177 }
178 178
179 return newobj; 179 return newobj;
180 } 180 }
513 513
514 if (!PyArg_ParseTuple(args, "s#:lowerencode", &path, &len)) 514 if (!PyArg_ParseTuple(args, "s#:lowerencode", &path, &len))
515 return NULL; 515 return NULL;
516 516
517 newlen = _lowerencode(NULL, 0, path, len); 517 newlen = _lowerencode(NULL, 0, path, len);
518 ret = PyString_FromStringAndSize(NULL, newlen); 518 ret = PyBytes_FromStringAndSize(NULL, newlen);
519 if (ret) 519 if (ret)
520 _lowerencode(PyString_AS_STRING(ret), newlen, path, len); 520 _lowerencode(PyBytes_AS_STRING(ret), newlen, path, len);
521 521
522 return ret; 522 return ret;
523 } 523 }
524 524
525 /* See store.py:_auxencode for a description. */ 525 /* See store.py:_auxencode for a description. */
566 the new string, so make room. */ 566 the new string, so make room. */
567 destsize = 120; 567 destsize = 120;
568 if (lastdot >= 0) 568 if (lastdot >= 0)
569 destsize += len - lastdot - 1; 569 destsize += len - lastdot - 1;
570 570
571 ret = PyString_FromStringAndSize(NULL, destsize); 571 ret = PyBytes_FromStringAndSize(NULL, destsize);
572 if (ret == NULL) 572 if (ret == NULL)
573 return NULL; 573 return NULL;
574 574
575 dest = PyString_AS_STRING(ret); 575 dest = PyBytes_AS_STRING(ret);
576 memcopy(dest, &destlen, destsize, "dh/", 3); 576 memcopy(dest, &destlen, destsize, "dh/", 3);
577 577
578 /* Copy up to dirprefixlen bytes of each path component, up to 578 /* Copy up to dirprefixlen bytes of each path component, up to
579 a limit of maxshortdirslen bytes. */ 579 a limit of maxshortdirslen bytes. */
580 for (i = d = p = 0; i < lastslash; i++, p++) { 580 for (i = d = p = 0; i < lastslash; i++, p++) {
636 636
637 if (lastdot >= 0) 637 if (lastdot >= 0)
638 memcopy(dest, &destlen, destsize, &src[lastdot], 638 memcopy(dest, &destlen, destsize, &src[lastdot],
639 len - lastdot - 1); 639 len - lastdot - 1);
640 640
641 PyString_GET_SIZE(ret) = destlen; 641 PyBytes_GET_SIZE(ret) = destlen;
642 642
643 return ret; 643 return ret;
644 } 644 }
645 645
646 /* 646 /*
651 { 651 {
652 static PyObject *shafunc; 652 static PyObject *shafunc;
653 PyObject *shaobj, *hashobj; 653 PyObject *shaobj, *hashobj;
654 654
655 if (shafunc == NULL) { 655 if (shafunc == NULL) {
656 PyObject *hashlib, *name = PyString_FromString("hashlib"); 656 PyObject *hashlib, *name = PyBytes_FromString("hashlib");
657 657
658 if (name == NULL) 658 if (name == NULL)
659 return -1; 659 return -1;
660 660
661 hashlib = PyImport_Import(name); 661 hashlib = PyImport_Import(name);
684 hashobj = PyObject_CallMethod(shaobj, "digest", ""); 684 hashobj = PyObject_CallMethod(shaobj, "digest", "");
685 Py_DECREF(shaobj); 685 Py_DECREF(shaobj);
686 if (hashobj == NULL) 686 if (hashobj == NULL)
687 return -1; 687 return -1;
688 688
689 if (!PyString_Check(hashobj) || PyString_GET_SIZE(hashobj) != 20) { 689 if (!PyBytes_Check(hashobj) || PyBytes_GET_SIZE(hashobj) != 20) {
690 PyErr_SetString(PyExc_TypeError, 690 PyErr_SetString(PyExc_TypeError,
691 "result of digest is not a 20-byte hash"); 691 "result of digest is not a 20-byte hash");
692 Py_DECREF(hashobj); 692 Py_DECREF(hashobj);
693 return -1; 693 return -1;
694 } 694 }
695 695
696 memcpy(hash, PyString_AS_STRING(hashobj), 20); 696 memcpy(hash, PyBytes_AS_STRING(hashobj), 20);
697 Py_DECREF(hashobj); 697 Py_DECREF(hashobj);
698 return 0; 698 return 0;
699 } 699 }
700 700
701 #define MAXENCODE 4096 * 4 701 #define MAXENCODE 4096 * 4
729 char *path; 729 char *path;
730 730
731 if (!PyArg_ParseTuple(args, "O:pathencode", &pathobj)) 731 if (!PyArg_ParseTuple(args, "O:pathencode", &pathobj))
732 return NULL; 732 return NULL;
733 733
734 if (PyString_AsStringAndSize(pathobj, &path, &len) == -1) { 734 if (PyBytes_AsStringAndSize(pathobj, &path, &len) == -1) {
735 PyErr_SetString(PyExc_TypeError, "expected a string"); 735 PyErr_SetString(PyExc_TypeError, "expected a string");
736 return NULL; 736 return NULL;
737 } 737 }
738 738
739 if (len > maxstorepathlen) 739 if (len > maxstorepathlen)
745 if (newlen == len + 1) { 745 if (newlen == len + 1) {
746 Py_INCREF(pathobj); 746 Py_INCREF(pathobj);
747 return pathobj; 747 return pathobj;
748 } 748 }
749 749
750 newobj = PyString_FromStringAndSize(NULL, newlen); 750 newobj = PyBytes_FromStringAndSize(NULL, newlen);
751 751
752 if (newobj) { 752 if (newobj) {
753 PyString_GET_SIZE(newobj)--; 753 PyBytes_GET_SIZE(newobj)--;
754 basicencode(PyString_AS_STRING(newobj), newlen, path, 754 basicencode(PyBytes_AS_STRING(newobj), newlen, path,
755 len + 1); 755 len + 1);
756 } 756 }
757 } 757 }
758 else 758 else
759 newobj = hashencode(path, len + 1); 759 newobj = hashencode(path, len + 1);