Jason R. Coombs committed 454aa98

Fixed issue with low bytes not being escaped consistently

  • Participants
  • Parent commits 7dede23

Comments (0)

Files changed (2)

File keyring/tests/

     def test_low_byte(self):
-        The current encoding allows low bytes (less than hex 16) to encode
-        as two bytes. For example '\n' (hex A) will encode as '_A', which
+        Ensure that encoding low bytes (ordinal less than hex F) encode as
+        as three bytes to avoid ambiguity. For example '\n' (hex A) should
+        encode as '_0A' and not '_A', the latter of which
         isn't matched by the inverse operation.
-    def test_ambiguous_string(self):
-        """
-        The current encoding encodes each non-alphanumeric byte to _XX where
-        XX is the hex code for that byte. However, it doesn't encode dual-
-        digits, so '\x00' encodes to '_0'. Thus, if one tries to escape the
-        string '\x000' (the null byte followed by the number 0), it will be
-        encoded to '_00', which decodes to '\x00'.
-        """
 def test_suite():

File keyring/util/

     or getattr(string, 'ascii_letters') # Python 3
 ) + string.digits
+ESCAPE_FMT = "_%02X"
 def _escape_char(c):
     "Single char escape. Return the char, escaped if not already legal"
     re_esc = re.compile(
         # the pattern must be bytes to operate on bytes
-        ESCAPE_FMT.replace('%X', '(?P<code>[0-9A-F]{2})').encode('ascii')
+        ESCAPE_FMT.replace('%02X', '(?P<code>[0-9A-F]{2})').encode('ascii')
     return re_esc.sub(_unescape_code, value.encode('ascii')).decode('utf-8')