[Python-checkins] CVS: python/dist/src/Lib quopri.py,1.15,1.16

Martin v. L?wis loewis@users.sourceforge.net
Sun, 30 Sep 2001 13:32:12 -0700


Update of /cvsroot/python/python/dist/src/Lib
In directory usw-pr-cvs1:/tmp/cvs-serv10858/Lib

Modified Files:
	quopri.py 
Log Message:
Patch #462190, patch #464070: Support quoted printable in the binascii module.
Decode and encode underscores for header style encoding. Fixes bug #463996.


Index: quopri.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/quopri.py,v
retrieving revision 1.15
retrieving revision 1.16
diff -C2 -d -r1.15 -r1.16
*** quopri.py	2001/09/04 19:14:14	1.15
--- quopri.py	2001/09/30 20:32:10	1.16
***************
*** 12,18 ****
  EMPTYSTRING = ''
  
  
  
! def needsquoting(c, quotetabs):
      """Decide whether a particular character needs to be quoted.
  
--- 12,23 ----
  EMPTYSTRING = ''
  
+ try:
+   from binascii import a2b_qp, b2a_qp
+ except:
+   a2b_qp = None
+   b2a_qp = None
  
  
! def needsquoting(c, quotetabs, header):
      """Decide whether a particular character needs to be quoted.
  
***************
*** 23,26 ****
--- 28,34 ----
      if c in ' \t':
          return quotetabs
+     # if header, we have to escape _ because _ is used to escape space
+     if c == '_': 
+         return header
      return c == ESCAPE or not (' ' <= c <= '~')
  
***************
*** 32,36 ****
  
  
! def encode(input, output, quotetabs):
      """Read 'input', apply quoted-printable encoding, and write to 'output'.
  
--- 40,44 ----
  
  
! def encode(input, output, quotetabs, header = 0):
      """Read 'input', apply quoted-printable encoding, and write to 'output'.
  
***************
*** 39,43 ****
--- 47,60 ----
      quoted.  Note that line-ending tabs and spaces are always encoded, as per
      RFC 1521.
+     The 'header' flag indicates whether we are encoding spaces as _ as per
+     RFC 1522.
      """
+ 
+     if b2a_qp is not None:
+         data = input.read()
+         odata = b2a_qp(data, quotetabs = quotetabs, header = header)
+         output.write(odata)
+         return
+       
      def write(s, output=output, lineEnd='\n'):
          # RFC 1521 requires that the line ending in a space or tab must have
***************
*** 61,67 ****
          # Calculate the un-length-limited encoded line
          for c in line:
!             if needsquoting(c, quotetabs):
                  c = quote(c)
!             outline.append(c)
          # First, write out the previous line
          if prevline is not None:
--- 78,87 ----
          # Calculate the un-length-limited encoded line
          for c in line:
!             if needsquoting(c, quotetabs, header):
                  c = quote(c)
!             if header and c == ' ':
!                 outline.append('_')
!             else:
!                 outline.append(c)
          # First, write out the previous line
          if prevline is not None:
***************
*** 81,97 ****
          write(prevline, lineEnd=stripped)
  
! def encodestring(s, quotetabs=0):
      from cStringIO import StringIO
      infp = StringIO(s)
      outfp = StringIO()
!     encode(infp, outfp, quotetabs)
      return outfp.getvalue()
  
  
  
! def decode(input, output):
      """Read 'input', apply quoted-printable decoding, and write to 'output'.
  
!     'input' and 'output' are files with readline() and write() methods."""
      new = ''
      while 1:
--- 101,126 ----
          write(prevline, lineEnd=stripped)
  
! def encodestring(s, quotetabs = 0, header = 0):
!     if b2a_qp is not None:
!         return b2a_qp(s, quotetabs = quotetabs, header = header)
      from cStringIO import StringIO
      infp = StringIO(s)
      outfp = StringIO()
!     encode(infp, outfp, quotetabs, header)
      return outfp.getvalue()
  
  
  
! def decode(input, output, header = 0):
      """Read 'input', apply quoted-printable decoding, and write to 'output'.
+     'input' and 'output' are files with readline() and write() methods.
+     If 'header' is true, decode underscore as space (per RFC 1522)."""
  
!     if a2b_qp is not None:
!         data = input.read()
!         odata = a2b_qp(data, header = header)
!         output.write(odata)
!         return
! 
      new = ''
      while 1:
***************
*** 108,112 ****
          while i < n:
              c = line[i]
!             if c != ESCAPE:
                  new = new + c; i = i+1
              elif i+1 == n and not partial:
--- 137,143 ----
          while i < n:
              c = line[i]
!             if c == '_' and header:
!                 new = new + ' '; i = i+1
!             elif c != ESCAPE:
                  new = new + c; i = i+1
              elif i+1 == n and not partial:
***************
*** 124,132 ****
          output.write(new)
  
! def decodestring(s):
      from cStringIO import StringIO
      infp = StringIO(s)
      outfp = StringIO()
!     decode(infp, outfp)
      return outfp.getvalue()
  
--- 155,165 ----
          output.write(new)
  
! def decodestring(s, header = 0):
!     if a2b_qp is not None:
!         return a2b_qp(s, header = header)
      from cStringIO import StringIO
      infp = StringIO(s)
      outfp = StringIO()
!     decode(infp, outfp, header = header)
      return outfp.getvalue()