From 72f136b94ee75c3d2d6b44d02df3c5a5938f08d1 Mon Sep 17 00:00:00 2001 From: Sergey Matveev Date: Thu, 23 Jan 2020 17:47:59 +0300 Subject: [PATCH] Escape control characters in pprint --- doc/news.rst | 2 ++ pyderasn.py | 12 +++++++++++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/doc/news.rst b/doc/news.rst index 3fbddd0..fe3a293 100644 --- a/doc/news.rst +++ b/doc/news.rst @@ -6,6 +6,8 @@ News 5.6 --- * Convenient ``.decod()`` method, that raises if tail is not empty +* Control characters (like newlines) of text fields in pprinted output + are escaped .. _release5.5: diff --git a/pyderasn.py b/pyderasn.py index 5f0c034..dee431d 100755 --- a/pyderasn.py +++ b/pyderasn.py @@ -655,6 +655,7 @@ from math import ceil from os import environ from string import ascii_letters from string import digits +from unicodedata import category as unicat from six import add_metaclass from six import binary_type @@ -3590,6 +3591,12 @@ class Enumerated(Integer): ) +def escape_control_unicode(c): + if unicat(c).startswith("C"): + c = repr(c).lstrip("u").strip("'") + return c + + class CommonString(OctetString): """Common class for all strings @@ -3708,7 +3715,10 @@ class CommonString(OctetString): def pps(self, decode_path=(), no_unicode=False): value = None if self.ready: - value = hexenc(bytes(self)) if no_unicode else self.__unicode__() + value = ( + hexenc(bytes(self)) if no_unicode else + "".join(escape_control_unicode(c) for c in self.__unicode__()) + ) yield _pp( obj=self, asn1_type_name=self.asn1_type_name, -- 2.44.0