[Python-checkins] CVS: python/dist/src/Lib imaplib.py,1.42,1.43

Piers Lauder pierslauder@users.sourceforge.net
Thu, 21 Feb 2002 17:15:20 -0800


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

Modified Files:
	imaplib.py 
Log Message:
moved command logging routines into IMAP4 class: thread safe/faster

Index: imaplib.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/imaplib.py,v
retrieving revision 1.42
retrieving revision 1.43
diff -C2 -d -r1.42 -r1.43
*** imaplib.py	16 Feb 2002 23:06:17 -0000	1.42
--- imaplib.py	22 Feb 2002 01:15:17 -0000	1.43
***************
*** 17,21 ****
  # GET/SETACL contributed by Anthony Baxter <anthony@interlink.com.au> April 2001.
  
! __version__ = "2.49"
  
  import binascii, re, socket, time, random, sys
--- 17,21 ----
  # GET/SETACL contributed by Anthony Baxter <anthony@interlink.com.au> April 2001.
  
! __version__ = "2.50"
  
  import binascii, re, socket, time, random, sys
***************
*** 53,57 ****
          'NAMESPACE':    ('AUTH', 'SELECTED'),
          'NOOP':         ('NONAUTH', 'AUTH', 'SELECTED', 'LOGOUT'),
!         'PARTIAL':      ('SELECTED',),
          'RENAME':       ('AUTH', 'SELECTED'),
          'SEARCH':       ('SELECTED',),
--- 53,57 ----
          'NAMESPACE':    ('AUTH', 'SELECTED'),
          'NOOP':         ('NONAUTH', 'AUTH', 'SELECTED', 'LOGOUT'),
!         'PARTIAL':      ('SELECTED',),                                  # NB: obsolete
          'RENAME':       ('AUTH', 'SELECTED'),
          'SEARCH':       ('SELECTED',),
***************
*** 159,165 ****
  
          if __debug__:
              if self.debug >= 1:
!                 _mesg('imaplib version %s' % __version__)
!                 _mesg('new IMAP4 connection, tag=%s' % self.tagpre)
  
          self.welcome = self._get_response()
--- 159,168 ----
  
          if __debug__:
+             self._cmd_log_len = 10
+             self._cmd_log_idx = 0
+             self._cmd_log = {}           # Last `_cmd_log_len' interactions
              if self.debug >= 1:
!                 self._mesg('imaplib version %s' % __version__)
!                 self._mesg('new IMAP4 connection, tag=%s' % self.tagpre)
  
          self.welcome = self._get_response()
***************
*** 179,183 ****
          if __debug__:
              if self.debug >= 3:
!                 _mesg('CAPABILITIES: %s' % `self.capabilities`)
  
          for version in AllowedVersions:
--- 182,186 ----
          if __debug__:
              if self.debug >= 3:
!                 self._mesg('CAPABILITIES: %s' % `self.capabilities`)
  
          for version in AllowedVersions:
***************
*** 225,228 ****
--- 228,232 ----
          self.sock.sendall(data)
  
+ 
      def shutdown(self):
          """Close I/O established in "open"."""
***************
*** 483,487 ****
          if __debug__:
              if self.debug >= 3:
!                 _dump_ur(self.untagged_responses)
          return self._simple_command('NOOP')
  
--- 487,491 ----
          if __debug__:
              if self.debug >= 3:
!                 self._dump_ur(self.untagged_responses)
          return self._simple_command('NOOP')
  
***************
*** 547,551 ****
              if __debug__:
                  if self.debug >= 1:
!                     _dump_ur(self.untagged_responses)
              raise self.readonly('%s is not writable' % mailbox)
          return typ, self.untagged_responses.get('EXISTS', [None])
--- 551,555 ----
              if __debug__:
                  if self.debug >= 1:
!                     self._dump_ur(self.untagged_responses)
              raise self.readonly('%s is not writable' % mailbox)
          return typ, self.untagged_responses.get('EXISTS', [None])
***************
*** 664,668 ****
          if __debug__:
              if self.debug >= 5:
!                 _mesg('untagged_responses[%s] %s += ["%s"]' %
                          (typ, len(ur.get(typ,'')), dat))
          if ur.has_key(typ):
--- 668,672 ----
          if __debug__:
              if self.debug >= 5:
!                 self._mesg('untagged_responses[%s] %s += ["%s"]' %
                          (typ, len(ur.get(typ,'')), dat))
          if ur.has_key(typ):
***************
*** 710,716 ****
          if __debug__:
              if self.debug >= 4:
!                 _mesg('> %s' % data)
              else:
!                 _log('> %s' % data)
  
          try:
--- 714,720 ----
          if __debug__:
              if self.debug >= 4:
!                 self._mesg('> %s' % data)
              else:
!                 self._log('> %s' % data)
  
          try:
***************
*** 736,740 ****
              if __debug__:
                  if self.debug >= 4:
!                     _mesg('write literal size %s' % len(literal))
  
              try:
--- 740,744 ----
              if __debug__:
                  if self.debug >= 4:
!                     self._mesg('write literal size %s' % len(literal))
  
              try:
***************
*** 815,819 ****
                  if __debug__:
                      if self.debug >= 4:
!                         _mesg('read literal size %s' % size)
                  data = self.read(size)
  
--- 819,823 ----
                  if __debug__:
                      if self.debug >= 4:
!                         self._mesg('read literal size %s' % size)
                  data = self.read(size)
  
***************
*** 835,839 ****
          if __debug__:
              if self.debug >= 1 and typ in ('NO', 'BAD', 'BYE'):
!                 _mesg('%s response: %s' % (typ, dat))
  
          return resp
--- 839,843 ----
          if __debug__:
              if self.debug >= 1 and typ in ('NO', 'BAD', 'BYE'):
!                 self._mesg('%s response: %s' % (typ, dat))
  
          return resp
***************
*** 858,862 ****
                  if __debug__:
                      if self.debug >= 1:
!                         print_log()
                  raise
  
--- 862,866 ----
                  if __debug__:
                      if self.debug >= 1:
!                         self.print_log()
                  raise
  
***************
*** 873,879 ****
          if __debug__:
              if self.debug >= 4:
!                 _mesg('< %s' % line)
              else:
!                 _log('< %s' % line)
          return line
  
--- 877,883 ----
          if __debug__:
              if self.debug >= 4:
!                 self._mesg('< %s' % line)
              else:
!                 self._log('< %s' % line)
          return line
  
***************
*** 887,891 ****
          if __debug__:
              if self.mo is not None and self.debug >= 5:
!                 _mesg("\tmatched r'%s' => %s" % (cre.pattern, `self.mo.groups()`))
          return self.mo is not None
  
--- 891,895 ----
          if __debug__:
              if self.mo is not None and self.debug >= 5:
!                 self._mesg("\tmatched r'%s' => %s" % (cre.pattern, `self.mo.groups()`))
          return self.mo is not None
  
***************
*** 935,943 ****
          if __debug__:
              if self.debug >= 5:
!                 _mesg('untagged_responses[%s] => %s' % (name, data))
          del self.untagged_responses[name]
          return typ, data
  
  
  
  class _Authenticator:
--- 939,985 ----
          if __debug__:
              if self.debug >= 5:
!                 self._mesg('untagged_responses[%s] => %s' % (name, data))
          del self.untagged_responses[name]
          return typ, data
  
  
+     if __debug__:
+ 
+         def _mesg(self, s, secs=None):
+             if secs is None:
+                 secs = time.time()
+             tm = time.strftime('%M:%S', time.localtime(secs))
+             sys.stderr.write('  %s.%02d %s\n' % (tm, (secs*100)%100, s))
+             sys.stderr.flush()
+ 
+         def _dump_ur(self, dict):
+             # Dump untagged responses (in `dict').
+             l = dict.items()
+             if not l: return
+             t = '\n\t\t'
+             l = map(lambda x:'%s: "%s"' % (x[0], x[1][0] and '" "'.join(x[1]) or ''), l)
+             self._mesg('untagged responses dump:%s%s' % (t, t.join(l)))
+ 
+         def _log(self, line):
+             # Keep log of last `_cmd_log_len' interactions for debugging.
+             self._cmd_log[self._cmd_log_idx] = (line, time.time())
+             self._cmd_log_idx += 1
+             if self._cmd_log_idx >= self._cmd_log_len:
+                 self._cmd_log_idx = 0
+ 
+         def print_log(self):
+             self._mesg('last %d IMAP4 interactions:' % len(self._cmd_log))
+             i, n = self._cmd_log_idx, self._cmd_log_len
+             while n:
+                 try:
+                     apply(self._mesg, self._cmd_log[i])
+                 except:
+                     pass
+                 i += 1
+                 if i >= self._cmd_log_len:
+                     i = 0
+                 n -= 1
+ 
+ 
  
  class _Authenticator:
***************
*** 1083,1119 ****
  
  
- if __debug__:
- 
-     def _mesg(s, secs=None):
-         if secs is None:
-             secs = time.time()
-         tm = time.strftime('%M:%S', time.localtime(secs))
-         sys.stderr.write('  %s.%02d %s\n' % (tm, (secs*100)%100, s))
-         sys.stderr.flush()
- 
-     def _dump_ur(dict):
-         # Dump untagged responses (in `dict').
-         l = dict.items()
-         if not l: return
-         t = '\n\t\t'
-         l = map(lambda x:'%s: "%s"' % (x[0], x[1][0] and '" "'.join(x[1]) or ''), l)
-         _mesg('untagged responses dump:%s%s' % (t, t.join(l)))
- 
-     _cmd_log = []           # Last `_cmd_log_len' interactions
-     _cmd_log_len = 10
- 
-     def _log(line):
-         # Keep log of last `_cmd_log_len' interactions for debugging.
-         if len(_cmd_log) == _cmd_log_len:
-             del _cmd_log[0]
-         _cmd_log.append((time.time(), line))
- 
-     def print_log():
-         _mesg('last %d IMAP4 interactions:' % len(_cmd_log))
-         for secs,line in _cmd_log:
-             _mesg(line, secs)
- 
- 
- 
  if __name__ == '__main__':
  
--- 1125,1128 ----
***************
*** 1146,1150 ****
      ('select', ('/tmp/yyz 2',)),
      ('search', (None, 'SUBJECT', 'test')),
!     ('partial', ('1', 'RFC822', 1, 1024)),
      ('store', ('1', 'FLAGS', '(\Deleted)')),
      ('namespace', ()),
--- 1155,1159 ----
      ('select', ('/tmp/yyz 2',)),
      ('search', (None, 'SUBJECT', 'test')),
!     ('fetch', ('1', '(FLAGS INTERNALDATE RFC822)')),
      ('store', ('1', 'FLAGS', '(\Deleted)')),
      ('namespace', ()),
***************
*** 1165,1177 ****
  
      def run(cmd, args):
!         _mesg('%s %s' % (cmd, args))
          typ, dat = apply(getattr(M, cmd), args)
!         _mesg('%s => %s %s' % (cmd, typ, dat))
          return dat
  
      try:
          M = IMAP4(host)
!         _mesg('PROTOCOL_VERSION = %s' % M.PROTOCOL_VERSION)
!         _mesg('CAPABILITIES = %s' % `M.capabilities`)
  
          for cmd,args in test_seq1:
--- 1174,1186 ----
  
      def run(cmd, args):
!         M._mesg('%s %s' % (cmd, args))
          typ, dat = apply(getattr(M, cmd), args)
!         M._mesg('%s => %s %s' % (cmd, typ, dat))
          return dat
  
      try:
          M = IMAP4(host)
!         M._mesg('PROTOCOL_VERSION = %s' % M.PROTOCOL_VERSION)
!         M._mesg('CAPABILITIES = %s' % `M.capabilities`)
  
          for cmd,args in test_seq1: