From 3b249f3bac242e2eb65c9c8c53ae4f43da84550c Mon Sep 17 00:00:00 2001 From: Oliver Palmer Date: Sun, 12 Jun 2016 10:21:54 -0400 Subject: [PATCH 1/4] implementing the ability to add a custom message for InputError --- pywincffi/exceptions.py | 8 ++++++-- tests/test_exceptions.py | 4 ++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/pywincffi/exceptions.py b/pywincffi/exceptions.py index f932458..bfe329e 100644 --- a/pywincffi/exceptions.py +++ b/pywincffi/exceptions.py @@ -32,7 +32,8 @@ class InputError(PyWinCFFIError): we fail early and provide better error messages. """ def __init__( # pylint: disable=too-many-arguments - self, name, value, expected_types, allowed_values=None, ffi=None): + self, name, value, expected_types, allowed_values=None, ffi=None, + message=None): self.name = name self.value = value self.value_repr = value @@ -53,7 +54,10 @@ def __init__( # pylint: disable=too-many-arguments self.value_repr = "%s(kind=%r, cname=%r)" % ( value.__class__.__name__, typeof.kind, typeof.cname) - if self.allowed_values is None: + if message is not None: + self.message = message + + elif self.allowed_values is None: self.message = "Expected type(s) %r for %s. Got %s instead." % ( self.expected_types, self.name, self.value_repr ) diff --git a/tests/test_exceptions.py b/tests/test_exceptions.py index 841ef12..f74597f 100644 --- a/tests/test_exceptions.py +++ b/tests/test_exceptions.py @@ -41,6 +41,10 @@ def test_str(self): error = InputError("name", "value", (str, int)) self.assertEqual(str(error), error.message) + def test_custom_message(self): + error = InputError("name", "value", (str, int), message="Hello, world.") + self.assertEqual(str(error), "Hello, world.") + class TestWindowsAPIError(TestCase): """ From a7241b74f8d719b051565da688f2bbf2f84e2d1b Mon Sep 17 00:00:00 2001 From: Oliver Palmer Date: Sun, 12 Jun 2016 10:25:15 -0400 Subject: [PATCH 2/4] adding some missing documentation --- pywincffi/exceptions.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/pywincffi/exceptions.py b/pywincffi/exceptions.py index bfe329e..66e2a66 100644 --- a/pywincffi/exceptions.py +++ b/pywincffi/exceptions.py @@ -30,6 +30,30 @@ class InputError(PyWinCFFIError): is provided to a function. Because we're passing inputs to C we have to be sure that the input(s) being provided are what we're expecting so we fail early and provide better error messages. + + + :param str name: + The name of the parameter being checked + + :param value: + The value of the parameter being checked + + :param expected_types: + The expected type(s). This may be either a single value or a + tuple/list of types. + + :keyword allowed_values: + An explicit list of values which are allowed for ``value``. + + :keyword ffi: + If ``value`` is a C object then you may pass in an instance of the + FFI instance to help understand the underlying type of ``value`` + + :keyword str message: + A custom error message. This will override the default error messages + which :class:`InputError` would normally generate. This can be + helpful if there is a problem with a given input parameter to a function + but it's unrelated to the type of input. """ def __init__( # pylint: disable=too-many-arguments self, name, value, expected_types, allowed_values=None, ffi=None, From 8a6a0c9ce96ccb34b2af7cbc044f9a2e1b9a9401 Mon Sep 17 00:00:00 2001 From: Oliver Palmer Date: Sun, 12 Jun 2016 10:26:11 -0400 Subject: [PATCH 3/4] updating changelog for improvements to InputError --- docs/source/changelog.rst | 2 ++ 1 file changed, 2 insertions(+) diff --git a/docs/source/changelog.rst b/docs/source/changelog.rst index 6ce47ea..cbc6ebe 100644 --- a/docs/source/changelog.rst +++ b/docs/source/changelog.rst @@ -29,6 +29,8 @@ Notable enhancements and changes are: * All exposed APIs updated to use the new Windows equivalent Python types in :mod:`pywincffi.wintypes`. * All exposed APIs now explicitly require either text or binary data. + * Improved documentation for :class:`pywincffi.exceptions.InputError` and + added the ability to generate custom error messages. 0.2.0 ~~~~~ From 2ab2d480d5ceb549e54015306f0c715a7e02ec34 Mon Sep 17 00:00:00 2001 From: Oliver Palmer Date: Sun, 12 Jun 2016 10:27:52 -0400 Subject: [PATCH 4/4] lint fix --- pywincffi/exceptions.py | 4 ++-- tests/test_exceptions.py | 3 ++- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/pywincffi/exceptions.py b/pywincffi/exceptions.py index 66e2a66..bfd9e70 100644 --- a/pywincffi/exceptions.py +++ b/pywincffi/exceptions.py @@ -52,8 +52,8 @@ class InputError(PyWinCFFIError): :keyword str message: A custom error message. This will override the default error messages which :class:`InputError` would normally generate. This can be - helpful if there is a problem with a given input parameter to a function - but it's unrelated to the type of input. + helpful if there is a problem with a given input parameter to a + function but it's unrelated to the type of input. """ def __init__( # pylint: disable=too-many-arguments self, name, value, expected_types, allowed_values=None, ffi=None, diff --git a/tests/test_exceptions.py b/tests/test_exceptions.py index f74597f..4a77733 100644 --- a/tests/test_exceptions.py +++ b/tests/test_exceptions.py @@ -42,7 +42,8 @@ def test_str(self): self.assertEqual(str(error), error.message) def test_custom_message(self): - error = InputError("name", "value", (str, int), message="Hello, world.") + error = InputError( + "name", "value", (str, int), message="Hello, world.") self.assertEqual(str(error), "Hello, world.")