Skip to content

Commit

Permalink
Add ValueErrorOnNone and RuntimeErrorOnNone to raise errors on nullpt…
Browse files Browse the repository at this point in the history
…r assignments

PiperOrigin-RevId: 622187569
  • Loading branch information
CLIF Team authored and Ralf W. Grosse-Kunstleve committed Apr 5, 2024
1 parent 129a4b3 commit f2903d6
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 4 deletions.
2 changes: 2 additions & 0 deletions clif/python/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,8 @@ The standard CLIF library comes with the following postprocessor functions:

* `ValueErrorOnFalse` takes first return value as bool, drops it from output
if True or raise a ValueError if it's False.
* `ValueErrorOnNone` raises a ValueError if any of the return values are None,
corresponding to `nullptr` assignments.
* `chr` is a Python built-in function useful to convert int/uint8 (from C++
char) to a Python 1-character string.

Expand Down
17 changes: 17 additions & 0 deletions clif/python/postproc.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,20 @@ def RuntimeErrorOnFalse(ok, *args):
def IgnoreTrueOrFalse(ok, *args):
"""Returns None / arg / (args,...) unconditionally, ignoring ok value."""
return _RaiseOnFalse('IgnoreTrueOrFalse', None, ok, *args)


def _RaiseOnNone(error_class, *args):
"""Raises error_class if args contain None, or returns args[0] / args."""
if None in args:
raise error_class('CLIF wrapped call returned None')
return args if len(args) > 1 else args[0]


def ValueErrorOnNone(obj, *args):
"""Raises ValueError if args contain None, or returns args[0] / args."""
return _RaiseOnNone(ValueError, obj, *args)


def RuntimeErrorOnNone(obj, *args):
"""Raises RuntimeError if args contain None, or returns args[0] / args."""
return _RaiseOnNone(RuntimeError, obj, *args)
28 changes: 24 additions & 4 deletions clif/python/postproc_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,30 @@ def testExceptionType(self, caller_name, error_type):
if error_type is not None:
for args in ((False,), (False, 1), (False, 1, 2)):
with self.assertRaises(error_type) as ctx:
postproc_function(False)
self.assertEqual(
str(ctx.exception),
'CLIF wrapped call returned False')
postproc_function(*args)
self.assertEqual(str(ctx.exception), 'CLIF wrapped call returned False')

@parameterized.parameterized.expand((
(postproc.ValueErrorOnNone, ValueError),
(postproc.RuntimeErrorOnNone, RuntimeError),
))
def testNoneExceptionType(self, postproc_function, error_type):
self.assertEqual(postproc_function(True), True)
self.assertEqual(postproc_function(False), False)
self.assertEqual(postproc_function(1), 1)
self.assertEqual(postproc_function(False, 2), (False, 2))
self.assertEqual(postproc_function(1, 2), (1, 2))
for args in (
(None,),
(None, 1),
(None, 1, 2),
(1, 2, None),
(1, None),
(None, None),
):
with self.assertRaises(error_type) as ctx:
postproc_function(*args)
self.assertEqual(str(ctx.exception), 'CLIF wrapped call returned None')


if __name__ == '__main__':
Expand Down

0 comments on commit f2903d6

Please sign in to comment.