From 1c0ecbf8c693673b189d8400e6f71e4a01c37897 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20Mart=C3=ADnez=20Portela?= Date: Thu, 26 Nov 2020 23:45:32 +0100 Subject: [PATCH] Improve dump checks debugging, and enable them by default. --- org_dom/org_dom.py | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/org_dom/org_dom.py b/org_dom/org_dom.py index 18e4bf9..eef8f63 100644 --- a/org_dom/org_dom.py +++ b/org_dom/org_dom.py @@ -1,6 +1,8 @@ import collections +import difflib import logging import re +import sys from enum import Enum from typing import List, Tuple @@ -862,15 +864,23 @@ class OrgDomReader: raise NotImplementedError("{}: ‘{}’".format(linenum, line)) -def loads(s, environment=BASE_ENVIRONMENT, extra_cautious=False): +def loads(s, environment=BASE_ENVIRONMENT, extra_cautious=True): doc = OrgDomReader() doc.read(s, environment) dom = doc.finalize() if extra_cautious: # Check that all options can be properly re-serialized - if dumps(dom) != s: - raise NotImplementedError( - "Error re-serializing, file uses something not implemented" + after_dump = dumps(dom) + if after_dump != s: + diff = list( + difflib.Differ().compare( + s.splitlines(keepends=True), after_dump.splitlines(keepends=True) + ) ) + + sys.stderr.writelines(diff) + # print("---\n" + after_dump + "\n---") + + raise Exception("Difference found between existing version and dumped one") return dom