]> Cypherpunks.ru repositories - pyderasn.git/commitdiff
Escape control characters in pprint
authorSergey Matveev <stargrave@stargrave.org>
Thu, 23 Jan 2020 14:47:59 +0000 (17:47 +0300)
committerSergey Matveev <stargrave@stargrave.org>
Thu, 23 Jan 2020 16:03:15 +0000 (19:03 +0300)
doc/news.rst
pyderasn.py

index 3fbddd0325b56aa6c2c7fb8fcaa3f8d605fa6d6e..fe3a2938ddd6bfaf1a8d0749403969eea7820325 100644 (file)
@@ -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:
 
index 5f0c0343ccd0d4153d29e6328fde4a73882229ec..dee431d96c5b41049e35482ece9cc8c98eadc1e9 100755 (executable)
@@ -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,