From 59f408dcf437b0099edc0b54b73c006752c7dc7e Mon Sep 17 00:00:00 2001 From: Aldo Cortesi Date: Wed, 25 Jul 2012 10:44:21 +1200 Subject: [PATCH] Refine parse error message a bit. --- libpathod/rparse.py | 2 +- libpathod/utils.py | 16 +++++++++++++++- test/test_utils.py | 5 +++++ 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/libpathod/rparse.py b/libpathod/rparse.py index ab6579747..ea12185be 100644 --- a/libpathod/rparse.py +++ b/libpathod/rparse.py @@ -22,7 +22,7 @@ class ParseException(Exception): return "%s\n%s"%(self.s, " "*(self.col-1) + "^") def __str__(self): - return "%s at offset %s of %s"%(self.msg, self.col, repr(self.s)) + return "%s at char %s"%(self.msg, self.col) def actions_log(lst): diff --git a/libpathod/utils.py b/libpathod/utils.py index 1ee50b830..7a1e533f8 100644 --- a/libpathod/utils.py +++ b/libpathod/utils.py @@ -45,10 +45,24 @@ def xrepr(s): return repr(s)[1:-1] +def inner_repr(s): + """ + Returns the inner portion of a string or unicode repr (i.e. without the + quotes) + """ + if isinstance(s, unicode): + return repr(s)[2:-1] + else: + return repr(s)[1:-1] + + def escape_unprintables(s): + """ + Like inner_repr, but preserves line breaks. + """ s = s.replace("\r\n", "PATHOD_MARKER_RN") s = s.replace("\n", "PATHOD_MARKER_N") - s = repr(s)[1:-1] + s = inner_repr(s) s = s.replace("PATHOD_MARKER_RN", "\n") s = s.replace("PATHOD_MARKER_N", "\n") return s diff --git a/test/test_utils.py b/test/test_utils.py index a8f513f8f..0a48d87dc 100644 --- a/test/test_utils.py +++ b/test/test_utils.py @@ -17,6 +17,11 @@ def test_data_path(): tutils.raises(ValueError, utils.data.path, "nonexistent") +def test_inner_repr(): + assert utils.inner_repr("\x66") == "\x66" + assert utils.inner_repr(u"foo") == "foo" + + def test_escape_unprintables(): s = "".join([chr(i) for i in range(255)]) e = utils.escape_unprintables(s)