diff options
author | Sam Thursfield <sam.thursfield@codethink.co.uk> | 2013-02-08 10:48:20 +0000 |
---|---|---|
committer | Sam Thursfield <sam.thursfield@codethink.co.uk> | 2013-02-18 11:24:01 +0000 |
commit | 76826aa85806243c778a78611fdd8a9239496396 (patch) | |
tree | 3f4b7f565309f6eabefaf313a6456ea10f85e307 | |
parent | 0e80ed76854e88dc93ca9d5a59bd11b99932e83a (diff) | |
download | morph-76826aa85806243c778a78611fdd8a9239496396.tar.gz |
Detect and abort on empty morphologies
If given an empty string to parse yamlparse.load() will return None,
but this breaks code further down that expects to be dealing with a
dict. Raise an exception to avoid crashing.
Also, avoid catching 'Exception' when we only want to catch JSON
parse errors.
-rw-r--r-- | morphlib/morph2.py | 4 | ||||
-rw-r--r-- | morphlib/morph2_tests.py | 5 |
2 files changed, 9 insertions, 0 deletions
diff --git a/morphlib/morph2.py b/morphlib/morph2.py index 26813925..a32ecbb9 100644 --- a/morphlib/morph2.py +++ b/morphlib/morph2.py @@ -74,6 +74,10 @@ class Morphology(object): except ValueError as e: # pragma: no cover self._dict = morphlib.yamlparse.load(text) self._dumper = morphlib.yamlparse.dump + + if data is None: + raise morphlib.YAMLError("Morphology is empty") + self._set_defaults() self._validate_children() diff --git a/morphlib/morph2_tests.py b/morphlib/morph2_tests.py index 7a819556..a2c44753 100644 --- a/morphlib/morph2_tests.py +++ b/morphlib/morph2_tests.py @@ -164,6 +164,11 @@ class MorphologyTests(unittest.TestCase): ## Validation tests + def test_morphology_must_not_be_empty(self): + # Causes crashes if not handled, because code expecting a dict gets + # None instead. + self.assertRaises(morphlib.YAMLError, Morphology, '') + def test_makes_max_jobs_be_an_integer(self): m = Morphology(''' { |