From: Jelmer Vernooij Date: Fri, 24 Jul 2009 08:42:58 +0000 (+0200) Subject: Use binascii.{unhexlify,hexlify}. Thanks to Ronny for the hint. X-Git-Tag: dulwich-0.4.0~14 X-Git-Url: http://git.samba.org/samba.git/?p=jelmer%2Fdulwich-libgit2.git;a=commitdiff_plain;h=f4e4efc4a97b84d6e07d432f818f05a6534e7adb Use binascii.{unhexlify,hexlify}. Thanks to Ronny for the hint. --- diff --git a/NEWS b/NEWS index ff885e6..41058ed 100644 --- a/NEWS +++ b/NEWS @@ -1,5 +1,9 @@ 0.3.4 UNRELEASED + BUG FIXES + + * Use binascii.hexlify / binascii.unhexlify for better performance. + 0.3.3 2009-07-23 FEATURES diff --git a/dulwich/_objects.c b/dulwich/_objects.c index 996bb5e..cf6aaae 100644 --- a/dulwich/_objects.c +++ b/dulwich/_objects.c @@ -19,34 +19,8 @@ #include -#define hexbyte(x) (isdigit(x)?(x)-'0':(x)-'a'+0xa) #define bytehex(x) (((x)<0xa)?('0'+(x)):('a'-0xa+(x))) -static PyObject *py_hex_to_sha(PyObject *self, PyObject *py_hexsha) -{ - char *hexsha; - char sha[20]; - int i; - - if (!PyString_CheckExact(py_hexsha)) { - PyErr_SetString(PyExc_TypeError, "hex sha is not a string"); - return NULL; - } - - if (PyString_Size(py_hexsha) != 40) { - PyErr_SetString(PyExc_ValueError, "hex sha is not 40 bytes long"); - return NULL; - } - - hexsha = PyString_AsString(py_hexsha); - - for (i = 0; i < 20; i++) { - sha[i] = (hexbyte(hexsha[i*2]) << 4) + hexbyte(hexsha[i*2+1]); - } - - return PyString_FromStringAndSize(sha, 20); -} - static PyObject *sha_to_pyhex(const unsigned char *sha) { char hexsha[41]; @@ -59,21 +33,6 @@ static PyObject *sha_to_pyhex(const unsigned char *sha) return PyString_FromStringAndSize(hexsha, 40); } -static PyObject *py_sha_to_hex(PyObject *self, PyObject *py_sha) -{ - if (!PyString_CheckExact(py_sha)) { - PyErr_SetString(PyExc_TypeError, "sha is not a string"); - return NULL; - } - - if (PyString_Size(py_sha) != 20) { - PyErr_SetString(PyExc_ValueError, "sha is not 20 bytes long"); - return NULL; - } - - return sha_to_pyhex((unsigned char *)PyString_AsString(py_sha)); -} - static PyObject *py_parse_tree(PyObject *self, PyObject *args) { char *text, *end; @@ -131,8 +90,6 @@ static PyObject *py_parse_tree(PyObject *self, PyObject *args) } static PyMethodDef py_objects_methods[] = { - { "hex_to_sha", (PyCFunction)py_hex_to_sha, METH_O, NULL }, - { "sha_to_hex", (PyCFunction)py_sha_to_hex, METH_O, NULL }, { "parse_tree", (PyCFunction)py_parse_tree, METH_VARARGS, NULL }, { NULL, NULL, 0, NULL } }; diff --git a/dulwich/objects.py b/dulwich/objects.py index 01193d8..47fdc67 100644 --- a/dulwich/objects.py +++ b/dulwich/objects.py @@ -21,6 +21,7 @@ """Access to base git objects.""" +import binascii from cStringIO import ( StringIO, ) @@ -64,7 +65,7 @@ def _decompress(string): def sha_to_hex(sha): """Takes a string and returns the hex of the sha within""" - hexsha = "".join(["%02x" % ord(c) for c in sha]) + hexsha = binascii.hexlify(sha) assert len(hexsha) == 40, "Incorrect length of sha1 string: %d" % hexsha return hexsha @@ -72,7 +73,7 @@ def sha_to_hex(sha): def hex_to_sha(hex): """Takes a hex sha and returns a binary sha""" assert len(hex) == 40, "Incorrent length of hexsha: %s" % hex - return ''.join([chr(int(hex[i:i+2], 16)) for i in xrange(0, len(hex), 2)]) + return binascii.unhexlify(hex) def serializable_property(name, docstring=None): @@ -617,7 +618,7 @@ num_type_map = { try: # Try to import C versions - from dulwich._objects import hex_to_sha, sha_to_hex, parse_tree + from dulwich._objects import parse_tree except ImportError: pass