Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 66d4513

Browse files
committed
Piers Lauder:
This patch fixes the "search" command in imaplib. The problem was that a search can take multiple arguments, but as defined, would only accept one. I have also made changes to the test code at the end to be less verbose by default, but to accept a verbosity argument.
1 parent 8430624 commit 66d4513

1 file changed

Lines changed: 49 additions & 26 deletions

File tree

Lib/imaplib.py

Lines changed: 49 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
#
1616
# Authentication code contributed by Donn Cave <[email protected]> June 1998.
1717

18-
__version__ = "2.33"
18+
__version__ = "2.36"
1919

2020
import binascii, re, socket, string, time, random, sys
2121

@@ -446,17 +446,17 @@ def rename(self, oldmailbox, newmailbox):
446446
return self._simple_command('RENAME', oldmailbox, newmailbox)
447447

448448

449-
def search(self, charset, criteria):
449+
def search(self, charset, *criteria):
450450
"""Search mailbox for matching messages.
451451
452-
(typ, [data]) = <instance>.search(charset, criteria)
452+
(typ, [data]) = <instance>.search(charset, criterium, ...)
453453
454454
'data' is space separated list of matching message numbers.
455455
"""
456456
name = 'SEARCH'
457457
if charset:
458458
charset = 'CHARSET ' + charset
459-
typ, dat = self._simple_command(name, charset, criteria)
459+
typ, dat = apply(self._simple_command, (name, charset) + criteria)
460460
return self._untagged_response(typ, dat, name)
461461

462462

@@ -1022,10 +1022,20 @@ def print_log():
10221022

10231023
if __name__ == '__main__':
10241024

1025-
import getpass, sys
1025+
import getopt, getpass, sys
10261026

1027-
host = ''
1028-
if sys.argv[1:]: host = sys.argv[1]
1027+
try:
1028+
optlist, args = getopt.getopt(sys.argv[1:], 'd:')
1029+
except getopt.error, val:
1030+
pass
1031+
1032+
for opt,val in optlist:
1033+
if opt == '-d':
1034+
Debug = int(val)
1035+
1036+
if not args: args = ('',)
1037+
1038+
host = args[0]
10291039

10301040
USER = getpass.getuser()
10311041
PASSWD = getpass.getpass("IMAP password for %s on %s" % (USER, host or "localhost"))
@@ -1039,7 +1049,7 @@ def print_log():
10391049
('append', ('/tmp/yyz 2', None, None, test_mesg)),
10401050
('list', ('/tmp', 'yy*')),
10411051
('select', ('/tmp/yyz 2',)),
1042-
('search', (None, '(TO zork)')),
1052+
('search', (None, 'SUBJECT', 'test')),
10431053
('partial', ('1', 'RFC822', 1, 1024)),
10441054
('store', ('1', 'FLAGS', '(\Deleted)')),
10451055
('expunge', ()),
@@ -1063,26 +1073,39 @@ def run(cmd, args):
10631073
_mesg('%s => %s %s' % (cmd, typ, dat))
10641074
return dat
10651075

1066-
Debug = 5
1067-
M = IMAP4(host)
1068-
_mesg('PROTOCOL_VERSION = %s' % M.PROTOCOL_VERSION)
1076+
try:
1077+
M = IMAP4(host)
1078+
_mesg('PROTOCOL_VERSION = %s' % M.PROTOCOL_VERSION)
1079+
1080+
for cmd,args in test_seq1:
1081+
run(cmd, args)
1082+
1083+
for ml in run('list', ('/tmp/', 'yy%')):
1084+
mo = re.match(r'.*"([^"]+)"$', ml)
1085+
if mo: path = mo.group(1)
1086+
else: path = string.split(ml)[-1]
1087+
run('delete', (path,))
1088+
1089+
for cmd,args in test_seq2:
1090+
dat = run(cmd, args)
1091+
1092+
if (cmd,args) != ('uid', ('SEARCH', 'ALL')):
1093+
continue
10691094

1070-
for cmd,args in test_seq1:
1071-
run(cmd, args)
1095+
uid = string.split(dat[-1])
1096+
if not uid: continue
1097+
run('uid', ('FETCH', '%s' % uid[-1],
1098+
'(FLAGS INTERNALDATE RFC822.SIZE RFC822.HEADER RFC822.TEXT)'))
10721099

1073-
for ml in run('list', ('/tmp/', 'yy%')):
1074-
mo = re.match(r'.*"([^"]+)"$', ml)
1075-
if mo: path = mo.group(1)
1076-
else: path = string.split(ml)[-1]
1077-
run('delete', (path,))
1100+
print '\nAll tests OK.'
10781101

1079-
for cmd,args in test_seq2:
1080-
dat = run(cmd, args)
1102+
except:
1103+
print '\nTests failed.'
10811104

1082-
if (cmd,args) != ('uid', ('SEARCH', 'ALL')):
1083-
continue
1105+
if not Debug:
1106+
print '''
1107+
If you would like to see debugging output,
1108+
try: %s -d5
1109+
''' % sys.argv[0]
10841110

1085-
uid = string.split(dat[-1])
1086-
if not uid: continue
1087-
run('uid', ('FETCH', '%s' % uid[-1],
1088-
'(FLAGS INTERNALDATE RFC822.SIZE RFC822.HEADER RFC822.TEXT)'))
1111+
raise

0 commit comments

Comments
 (0)