Skip to content

Commit

Permalink
Asserts replacer (#223)
Browse files Browse the repository at this point in the history
* Add a condition checker function

raising a configurable exception to replace bare asserts throughout
python code

* Replace assert with check_condition()

* Rename "check_condition" to "ensure"

* Test for a CustomError exception

* Correct typo.
  • Loading branch information
lmctv authored and reaperhulk committed Jan 5, 2017
1 parent 705f958 commit 718652a
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/nacl/bindings/crypto_shorthash.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

from __future__ import absolute_import, division, print_function

import nacl.exceptions as exc
from nacl import utils
from nacl._sodium import ffi, lib

BYTES = lib.crypto_shorthash_siphash24_bytes()
Expand All @@ -34,5 +36,5 @@ def crypto_shorthash_siphash24(data, key):
digest = ffi.new("unsigned char[]", BYTES)
rc = lib.crypto_shorthash_siphash24(digest, data, len(data), key)

assert rc == 0
utils.ensure(rc == 0, raising=exc.RuntimeError)
return ffi.buffer(digest, BYTES)[:]
8 changes: 8 additions & 0 deletions src/nacl/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,11 @@ class BadSignatureError(CryptoError):
"""
Raised when the signature was forged or otherwise corrupt.
"""


class RuntimeError(CryptoError, RuntimeError):
pass


class AssertionError(CryptoError, AssertionError):
pass
22 changes: 22 additions & 0 deletions src/nacl/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import six

import nacl.bindings
from nacl import exceptions as exc


class EncryptedMessage(six.binary_type):
Expand Down Expand Up @@ -58,3 +59,24 @@ def __str__(self):

def random(size=32):
return nacl.bindings.randombytes(size)


def ensure(cond, *args, **kwds):
"""
Return if a condition is true, otherwise raise a caller-configurable
:py:class:`Exception`
:param bool cond: the condition to be checked
:param sequence args: the arguments to be passed to the exception's
constructor
The only accepted named parameter is `raising` used to configure the
exception to be raised if `cond` is not `True`
"""
_CHK_UNEXP = 'check_condition() got an unexpected keyword argument {0}'

raising = kwds.pop('raising', exc.AssertionError)
if kwds:
raise TypeError(_CHK_UNEXP.format(repr(kwds.popitem[0])))

if cond is True:
return
raise raising(*args)
27 changes: 27 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,14 @@

from __future__ import absolute_import, division, print_function

import pytest

import nacl.utils
from nacl import exceptions as exc


class CustomError(exc.CryptoError):
pass


def test_random_bytes_produces():
Expand All @@ -23,3 +30,23 @@ def test_random_bytes_produces():

def test_random_bytes_produces_different_bytes():
assert nacl.utils.random(16) != nacl.utils.random(16)


def test_util_ensure_with_true_condition():
nacl.utils.ensure(1 == 1, 'one equals one')


def test_util_ensure_with_false_condition():
with pytest.raises(AssertionError):
nacl.utils.ensure(1 == 0, 'one is not zero',
raising=exc.AssertionError)


def test_util_ensure_with_unwanted_kwarg():
with pytest.raises(TypeError):
nacl.utils.ensure(1 == 1, unexpected='unexpected')


def test_util_ensure_custom_exception():
with pytest.raises(CustomError):
nacl.utils.ensure(1 == 0, 'Raising a CustomError', raising=CustomError)

0 comments on commit 718652a

Please sign in to comment.