-
Notifications
You must be signed in to change notification settings - Fork 50
Tidy up the propagation of exceptions from UDUNITS2 #136
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
This PR now depends upon #135, as I added a few unicode tests on |
@pelson Rebase after recent merges? |
…we don't have multiple tracebacks in python 3. Also improved coverage of Unit.log, which was highlighted by this change. Bolster the unit tests to ensure that some of the exception handling is redundant.
OK, now rebased @bjlittle. Take it away. |
@@ -189,6 +190,18 @@ def test_format_unit_definition(self): | |||
u = Unit('watt') | |||
self.assertEqual(u.format(unit.UT_DEFINITION), 'm2.kg.s-3') | |||
|
|||
def test_format_multiple_options(self): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this was tested anywhere before!
u = Unit('watt') | ||
self.assertEqual( | ||
u.format([unit.UT_NAMES, unit.UT_DEFINITION, unit.UT_UTF8]), | ||
u'meter²·kilogram·second⁻³') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pretty cool eh?
|
||
def test_format_latin1(self): | ||
u = Unit('m2') | ||
self.assertEqual(u.format(unit.UT_LATIN1), u'm²') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
From a coverage perspective, LATIN1
== ISO8859-1
def test_negative(self): | ||
u = Unit('hPa') | ||
msg = re.escape("Failed to calculate logorithmic base of Unit('hPa')") | ||
with six.assertRaisesRegex(self, ValueError, msg): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No previous test of this.
self._propogate_error('Failed to format %r' % self, e) | ||
return str(result.decode(endocing_str)) | ||
encoding_str = _encoding_lookup[encoding] | ||
result = _ud.format(self.ut_unit, bitmask) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Discussed with @pelson, removed unnecessary defensive try...except
.
There were no test cases that raised this exception for _ud.format
.
except _ud.UdunitsError as e: | ||
self._propogate_error('Failed to invert %r' % self, e) | ||
calendar = None | ||
ut_unit = _ud.invert(self.ut_unit) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Discussed with @pelson, removed unnecessary defensive try...except
.
There were no test cases that raised this exception for _ud.invert
.
Lovely, thanks @pelson 👍 |
Modernises the exception raising when propagating from udunits2.
In python 3 we have a syntax to allow us to control whether to show multiple tracebacks (super useful for the case of exceptions being raised in multiple threads). This functionality is the default in python 3. Essentially, the following code:
The same is true for existing cf_units:
With this change we now get good exceptions in both python2 and python3: