]> granicus.if.org Git - python/commitdiff
Patch #1185447: binascii.b2a_qp() now correctly quotes binary characters
authorGeorg Brandl <georg@python.org>
Tue, 13 Mar 2007 22:49:43 +0000 (22:49 +0000)
committerGeorg Brandl <georg@python.org>
Tue, 13 Mar 2007 22:49:43 +0000 (22:49 +0000)
with ASCII value less than 32. Also, it correctly quotes dots only if
they occur on a single line, as opposed to the previous behavior of
quoting dots if they are the second character of any line.

Lib/test/test_binascii.py
Misc/NEWS
Modules/binascii.c

index 8272ad91aa818b75c0b3bd00c0545d69417b4e19..ea8be31c072da58d82cd7456f8efa91057ecd4c2 100755 (executable)
@@ -148,6 +148,15 @@ class BinASCIITest(unittest.TestCase):
             "0"*75+"=\r\n=FF\r\n=FF\r\n=FF"
         )
 
+        self.assertEqual(binascii.b2a_qp('\0\n'), '=00\n')
+        self.assertEqual(binascii.b2a_qp('\0\n', quotetabs=True), '=00\n')
+        self.assertEqual(binascii.b2a_qp('foo\tbar\t\n'), 'foo\tbar=09\n')
+        self.assertEqual(binascii.b2a_qp('foo\tbar\t\n', quotetabs=True), 'foo=09bar=09\n')
+
+        self.assertEqual(binascii.b2a_qp('.'), '=2E')
+        self.assertEqual(binascii.b2a_qp('.\n'), '=2E\n')
+        self.assertEqual(binascii.b2a_qp('a.\n'), 'a.\n')
+
     def test_empty_string(self):
         # A test for SF bug #1022953.  Make sure SystemError is not raised.
         for n in ['b2a_qp', 'a2b_hex', 'b2a_base64', 'a2b_uu', 'a2b_qp',
index d9bf14f9fea7d181712327f9b50ad2063c9f3324..355a827aa494460fd57596d75198048578017280 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -525,6 +525,11 @@ Library
 Extension Modules
 -----------------
 
+- Patch #1185447: binascii.b2a_qp() now correctly quotes binary characters
+  with ASCII value less than 32. Also, it correctly quotes dots only if
+  they occur on a single line, as opposed to the previous behavior of
+  quoting dots if they are the second character of any line.
+
 - Bug #1622896: fix a rare corner case where the bz2 module raised an
   error in spite of a succesful compression.
 
index 4dee45198ef19c81d73c47e6a8c8d9741b98e6ac..91309f673bb4e417a8bf04948ff4bb4b4d390b8c 100644 (file)
@@ -1160,12 +1160,14 @@ binascii_b2a_qp (PyObject *self, PyObject *args, PyObject *kwargs)
                if ((data[in] > 126) ||
                    (data[in] == '=') ||
                    (header && data[in] == '_') ||
-                   ((data[in] == '.') && (linelen == 1)) ||
+                   ((data[in] == '.') && (linelen == 0) &&
+                    (data[in+1] == '\n' || data[in+1] == '\r' || data[in+1] == 0)) ||
                    (!istext && ((data[in] == '\r') || (data[in] == '\n'))) ||
                    ((data[in] == '\t' || data[in] == ' ') && (in + 1 == datalen)) ||
                    ((data[in] < 33) &&
                     (data[in] != '\r') && (data[in] != '\n') &&
-                    (quotetabs && ((data[in] != '\t') || (data[in] != ' ')))))
+                    (quotetabs ||
+                       (!quotetabs && ((data[in] != '\t') && (data[in] != ' '))))))
                {
                        if ((linelen + 3) >= MAXLINESIZE) {
                                linelen = 0;
@@ -1230,12 +1232,14 @@ binascii_b2a_qp (PyObject *self, PyObject *args, PyObject *kwargs)
                if ((data[in] > 126) ||
                    (data[in] == '=') ||
                    (header && data[in] == '_') ||
-                   ((data[in] == '.') && (linelen == 1)) ||
+                   ((data[in] == '.') && (linelen == 0) &&
+                    (data[in+1] == '\n' || data[in+1] == '\r' || data[in+1] == 0)) ||
                    (!istext && ((data[in] == '\r') || (data[in] == '\n'))) ||
                    ((data[in] == '\t' || data[in] == ' ') && (in + 1 == datalen)) ||
                    ((data[in] < 33) &&
                     (data[in] != '\r') && (data[in] != '\n') &&
-                    (quotetabs && ((data[in] != '\t') || (data[in] != ' ')))))
+                    (quotetabs ||
+                       (!quotetabs && ((data[in] != '\t') && (data[in] != ' '))))))
                {
                        if ((linelen + 3 )>= MAXLINESIZE) {
                                odata[out++] = '=';