Use binascii.{unhexlify,hexlify}. Thanks to Ronny for the hint.
authorJelmer Vernooij <jelmer@samba.org>
Fri, 24 Jul 2009 08:42:58 +0000 (10:42 +0200)
committerJelmer Vernooij <jelmer@samba.org>
Fri, 24 Jul 2009 08:42:58 +0000 (10:42 +0200)
NEWS
dulwich/_objects.c
dulwich/objects.py

diff --git a/NEWS b/NEWS
index ff885e6ecf169e3594550c43e8895ed82be78a73..41058ed34bb9c4215e1e398b02f983b655b9f5f6 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -1,5 +1,9 @@
 0.3.4  UNRELEASED
 
 0.3.4  UNRELEASED
 
+ BUG FIXES
+
+  * Use binascii.hexlify / binascii.unhexlify for better performance.
+
 0.3.3  2009-07-23
 
  FEATURES
 0.3.3  2009-07-23
 
  FEATURES
index 996bb5e5eaaeaacbc219a70e3aa25f62ac1d45e2..cf6aaaee40375a5d7d2349433fbb02fd6940f368 100644 (file)
 
 #include <Python.h>
 
 
 #include <Python.h>
 
-#define hexbyte(x) (isdigit(x)?(x)-'0':(x)-'a'+0xa)
 #define bytehex(x) (((x)<0xa)?('0'+(x)):('a'-0xa+(x)))
 
 #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];
 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);
 }
 
        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;
 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[] = {
 }
 
 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 }
 };
        { "parse_tree", (PyCFunction)py_parse_tree, METH_VARARGS, NULL },
        { NULL, NULL, 0, NULL }
 };
index 01193d8ce8bd5cb09495211662d03e9b7df7d15f..47fdc672cd422a830bee34365dc638e8195e85ca 100644 (file)
@@ -21,6 +21,7 @@
 """Access to base git objects."""
 
 
 """Access to base git objects."""
 
 
+import binascii
 from cStringIO import (
     StringIO,
     )
 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"""
 
 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
 
     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
 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):
 
 
 def serializable_property(name, docstring=None):
@@ -617,7 +618,7 @@ num_type_map = {
 
 try:
     # Try to import C versions
 
 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
 
 except ImportError:
     pass