Skip to content

Commit 2340215

Browse files
committed
Add Oid.__bool__
Return False if the Oid is a null SHA-1 (all zeros).
1 parent 5454758 commit 2340215

File tree

4 files changed

+60
-2
lines changed

4 files changed

+60
-2
lines changed

docs/oid.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,9 @@ The Oid type supports:
6868
- rich comparisons, not just for equality, also: lesser-than, lesser-or-equal,
6969
etc.
7070

71-
- hashing, so Oid objects can be used as keys in a dictionary.
71+
- `hash(oid)`, so Oid objects can be used as keys in a dictionary.
72+
73+
- `bool(oid)`, returning False if the Oid is a null SHA-1 (all zeros).
7274

7375

7476
Constants

pygit2/_pygit2.pyi

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,7 @@ class Oid:
272272
def __le__(self, other) -> bool: ...
273273
def __lt__(self, other) -> bool: ...
274274
def __ne__(self, other) -> bool: ...
275+
def __bool__(self) -> bool: ...
275276

276277
class Patch:
277278
data: bytes

src/oid.c

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434

3535
PyTypeObject OidType;
3636

37+
static const git_oid oid_zero = GIT_OID_SHA1_ZERO;
38+
3739

3840
PyObject *
3941
git_oid_to_python(const git_oid *oid)
@@ -255,6 +257,13 @@ Oid__str__(Oid *self)
255257
return git_oid_to_py_str(&self->oid);
256258
}
257259

260+
int
261+
Oid__bool(PyObject *self)
262+
{
263+
git_oid *oid = &((Oid*)self)->oid;
264+
return !git_oid_equal(oid, &oid_zero);
265+
}
266+
258267
PyDoc_STRVAR(Oid_raw__doc__, "Raw oid, a 20 bytes string.");
259268

260269
PyObject *
@@ -269,6 +278,45 @@ PyGetSetDef Oid_getseters[] = {
269278
{NULL},
270279
};
271280

281+
PyNumberMethods Oid_as_number = {
282+
0, /* nb_add */
283+
0, /* nb_subtract */
284+
0, /* nb_multiply */
285+
0, /* nb_remainder */
286+
0, /* nb_divmod */
287+
0, /* nb_power */
288+
0, /* nb_negative */
289+
0, /* nb_positive */
290+
0, /* nb_absolute */
291+
Oid__bool, /* nb_bool */
292+
0, /* nb_invert */
293+
0, /* nb_lshift */
294+
0, /* nb_rshift */
295+
0, /* nb_and */
296+
0, /* nb_xor */
297+
0, /* nb_or */
298+
0, /* nb_int */
299+
0, /* nb_reserved */
300+
0, /* nb_float */
301+
0, /* nb_inplace_add */
302+
0, /* nb_inplace_subtract */
303+
0, /* nb_inplace_multiply */
304+
0, /* nb_inplace_remainder */
305+
0, /* nb_inplace_power */
306+
0, /* nb_inplace_lshift */
307+
0, /* nb_inplace_rshift */
308+
0, /* nb_inplace_and */
309+
0, /* nb_inplace_xor */
310+
0, /* nb_inplace_or */
311+
0, /* nb_floor_divide */
312+
0, /* nb_true_divide */
313+
0, /* nb_inplace_floor_divide */
314+
0, /* nb_inplace_true_divide */
315+
0, /* nb_index */
316+
0, /* nb_matrix_multiply */
317+
0, /* nb_inplace_matrix_multiply */
318+
};
319+
272320
PyDoc_STRVAR(Oid__doc__, "Object id.");
273321

274322
PyTypeObject OidType = {
@@ -282,7 +330,7 @@ PyTypeObject OidType = {
282330
0, /* tp_setattr */
283331
0, /* tp_compare */
284332
(reprfunc)Oid__str__, /* tp_repr */
285-
0, /* tp_as_number */
333+
&Oid_as_number, /* tp_as_number */
286334
0, /* tp_as_sequence */
287335
0, /* tp_as_mapping */
288336
(hashfunc)Oid_hash, /* tp_hash */

test/test_oid.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,3 +99,10 @@ def test_hash():
9999
s.add(Oid(hex='0000000000000000000000000000000000000000'))
100100
s.add(Oid(hex='0000000000000000000000000000000000000001'))
101101
assert len(s) == 3
102+
103+
104+
def test_bool():
105+
assert Oid(raw=RAW)
106+
assert Oid(hex=HEX)
107+
assert not Oid(raw=b'')
108+
assert not Oid(hex='0000000000000000000000000000000000000000')

0 commit comments

Comments
 (0)