Repository: eyeseast/python-frontmatter

Issue:
Deal with no handler on post object
This bug: https://github.com/datadesk/django-bigbuild/issues/38

In cases where there's no `post.handler` (not just `None`) fall back to `YAMLHandler`.

Discussion:

Patch:
diff --git a/frontmatter/__init__.py b/frontmatter/__init__.py
index 0cb7e4d..92f7c97 100644
--- a/frontmatter/__init__.py
+++ b/frontmatter/__init__.py
@@ -180,7 +180,7 @@ def dumps(post, handler=None, **kwargs):
 
     """
     if handler is None:
-        handler = post.handler or YAMLHandler()
+        handler = getattr(post, 'handler', None) or YAMLHandler()
 
     start_delimiter = kwargs.pop('start_delimiter', handler.START_DELIMITER)
     end_delimiter = kwargs.pop('end_delimiter', handler.END_DELIMITER)


Test patch:
diff --git a/test.py b/test.py
index 92a1a38..21d3976 100644
--- a/test.py
+++ b/test.py
@@ -139,6 +139,15 @@ class HandlerTest(unittest.TestCase):
                 format = frontmatter.detect_format(f.read(), frontmatter.handlers)
                 self.assertIsInstance(format, Handler)
 
+    def test_no_handler(self):
+        "default to YAMLHandler when no handler is attached"
+        post = frontmatter.load('tests/hello-world.markdown')
+        del post.handler
+
+        text = frontmatter.dumps(post)
+        self.assertIsInstance(
+            frontmatter.detect_format(text, frontmatter.handlers), 
+            YAMLHandler)
 
     def test_custom_handler(self):
         "allow caller to specify a custom delimiter/handler"