From 3a83cb44b66a261a6234186b830e9d1a5586a452 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn=20Nieto?= Date: Fri, 24 Jan 2014 08:17:49 +0100 Subject: [PATCH] Oid: Deprecate 'hex' in favour of str() or unicode() Python has built-in functions to get a string representation of an object id, so let's use that instead of using a custom attribute. --- src/oid.c | 14 ++++++++++---- test/test_oid.py | 6 +++--- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/src/oid.c b/src/oid.c index 40a5d79..81749e5 100644 --- a/src/oid.c +++ b/src/oid.c @@ -257,6 +257,11 @@ Oid_richcompare(PyObject *o1, PyObject *o2, int op) return res; } +PyObject * +Oid__str__(Oid *self) +{ + return git_oid_to_py_str(&self->oid); +} PyDoc_STRVAR(Oid_raw__doc__, "Raw oid, a 20 bytes string."); @@ -267,12 +272,13 @@ Oid_raw__get__(Oid *self) } -PyDoc_STRVAR(Oid_hex__doc__, "Hex oid, a 40 chars long string (type str)."); +PyDoc_STRVAR(Oid_hex__doc__, "Hex oid, a 40 chars long string (type str).\n" + "This attribute is deprecated, please use the built-int str() or unicode()\n"); PyObject * Oid_hex__get__(Oid *self) { - return git_oid_to_py_str(&self->oid); + return Oid__str__(self); } PyGetSetDef Oid_getseters[] = { @@ -293,13 +299,13 @@ PyTypeObject OidType = { 0, /* tp_getattr */ 0, /* tp_setattr */ 0, /* tp_compare */ - 0, /* tp_repr */ + (reprfunc)Oid__str__, /* tp_repr */ 0, /* tp_as_number */ 0, /* tp_as_sequence */ 0, /* tp_as_mapping */ (hashfunc)Oid_hash, /* tp_hash */ 0, /* tp_call */ - 0, /* tp_str */ + (reprfunc)Oid__str__, /* tp_str */ 0, /* tp_getattro */ 0, /* tp_setattro */ 0, /* tp_as_buffer */ diff --git a/test/test_oid.py b/test/test_oid.py index e631399..d2d7a92 100644 --- a/test/test_oid.py +++ b/test/test_oid.py @@ -50,19 +50,19 @@ class OidTest(utils.BareRepoTestCase): def test_raw(self): oid = Oid(raw=RAW) self.assertEqual(oid.raw, RAW) - self.assertEqual(oid.hex, HEX) + self.assertEqual(str(oid), HEX) def test_hex(self): oid = Oid(hex=HEX) self.assertEqual(oid.raw, RAW) - self.assertEqual(oid.hex, HEX) + self.assertEqual(str(oid), HEX) def test_hex_bytes(self): if version_info[0] == 2: hex = bytes(HEX) oid = Oid(hex=hex) self.assertEqual(oid.raw, RAW) - self.assertEqual(oid.hex, HEX) + self.assertEqual(str(oid), HEX) else: hex = bytes(HEX, "ascii") self.assertRaises(TypeError, Oid, hex=hex)