Skip to content

Commit 3a83cb4

Browse files
committed
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.
1 parent 0c3a700 commit 3a83cb4

File tree

2 files changed

+13
-7
lines changed

2 files changed

+13
-7
lines changed

src/oid.c

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,11 @@ Oid_richcompare(PyObject *o1, PyObject *o2, int op)
257257
return res;
258258
}
259259

260+
PyObject *
261+
Oid__str__(Oid *self)
262+
{
263+
return git_oid_to_py_str(&self->oid);
264+
}
260265

261266
PyDoc_STRVAR(Oid_raw__doc__, "Raw oid, a 20 bytes string.");
262267

@@ -267,12 +272,13 @@ Oid_raw__get__(Oid *self)
267272
}
268273

269274

270-
PyDoc_STRVAR(Oid_hex__doc__, "Hex oid, a 40 chars long string (type str).");
275+
PyDoc_STRVAR(Oid_hex__doc__, "Hex oid, a 40 chars long string (type str).\n"
276+
"This attribute is deprecated, please use the built-int str() or unicode()\n");
271277

272278
PyObject *
273279
Oid_hex__get__(Oid *self)
274280
{
275-
return git_oid_to_py_str(&self->oid);
281+
return Oid__str__(self);
276282
}
277283

278284
PyGetSetDef Oid_getseters[] = {
@@ -293,13 +299,13 @@ PyTypeObject OidType = {
293299
0, /* tp_getattr */
294300
0, /* tp_setattr */
295301
0, /* tp_compare */
296-
0, /* tp_repr */
302+
(reprfunc)Oid__str__, /* tp_repr */
297303
0, /* tp_as_number */
298304
0, /* tp_as_sequence */
299305
0, /* tp_as_mapping */
300306
(hashfunc)Oid_hash, /* tp_hash */
301307
0, /* tp_call */
302-
0, /* tp_str */
308+
(reprfunc)Oid__str__, /* tp_str */
303309
0, /* tp_getattro */
304310
0, /* tp_setattro */
305311
0, /* tp_as_buffer */

test/test_oid.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,19 +50,19 @@ class OidTest(utils.BareRepoTestCase):
5050
def test_raw(self):
5151
oid = Oid(raw=RAW)
5252
self.assertEqual(oid.raw, RAW)
53-
self.assertEqual(oid.hex, HEX)
53+
self.assertEqual(str(oid), HEX)
5454

5555
def test_hex(self):
5656
oid = Oid(hex=HEX)
5757
self.assertEqual(oid.raw, RAW)
58-
self.assertEqual(oid.hex, HEX)
58+
self.assertEqual(str(oid), HEX)
5959

6060
def test_hex_bytes(self):
6161
if version_info[0] == 2:
6262
hex = bytes(HEX)
6363
oid = Oid(hex=hex)
6464
self.assertEqual(oid.raw, RAW)
65-
self.assertEqual(oid.hex, HEX)
65+
self.assertEqual(str(oid), HEX)
6666
else:
6767
hex = bytes(HEX, "ascii")
6868
self.assertRaises(TypeError, Oid, hex=hex)

0 commit comments

Comments
 (0)